summaryrefslogtreecommitdiff
path: root/libc/inet/if_nametoindex.c
blob: 40818aad9cb03e9120c06ca5e5c201711bed508c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 *
 * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>,
 *                     The Silver Hammer Group, Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 */
#define __FORCE_GLIBC
#include <features.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>

unsigned int if_nametoindex(const char* blub) {
	struct ifreq ifr;
	int fd;
	char *tmp;
	int len=sizeof(ifr.ifr_name);

#ifdef __UCLIBC_HAS_IPV6__
	fd=socket(AF_INET6,SOCK_DGRAM,0);
	if (fd<0)
#endif /* __UCLIBC_HAS_IPV6__ */
		fd=socket(AF_INET,SOCK_DGRAM,0);

	for (tmp=ifr.ifr_name; len>0; --len) {
		if ((*tmp++ = *blub++)==0) break;
	}

	if (ioctl(fd,SIOCGIFINDEX,&ifr)==0) {
		close(fd);
		return ifr.ifr_ifindex;
	}
	close(fd);
	return 0;
}