genericopenlibs/openenvcore/libc/src/net/if_nametoindex.c
author William Roberts <williamr@symbian.org>
Thu, 22 Jul 2010 16:48:56 +0100
branchGCC_SURGE
changeset 45 4b03adbd26ca
parent 0 e4d67989cc36
permissions -rw-r--r--
Catchup to latest Symbian^4

/*
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/



#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <stdapis/net/if.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

/*
 *    If the specified interface name does not exist, the return value is
 *    0, and errno is set to ENXIO.  If there was a system error (such as
 *    running out of memory), the return value is 0 and errno is set to the
 *    proper value (e.g., ENOMEM).
 */

EXPORT_C unsigned int if_nametoindex(const char *ifname)
	{
	struct if_nameindex *ifi = NULL;
	struct if_nameindex *ifa;
	unsigned int ni;
	
	ifi = if_nameindex();
	if (ifi == NULL)
		{
		errno = ENXIO;
		return 0;
		}
	ni = 0;
	for (ifa = ifi; ifa->if_index != 0; ifa++ )
		{
		if ( strcmp(ifa->if_name, ifname ) == 0)
			{
			ni = ifa->if_index;
			break;
			}
		}
	
	if_freenameindex(ifi);
	if (!ni)
		{
		errno = ENXIO;
		}
	return(ni);
	}