summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/lib/if_nametoindex.c
blob: c9dd1b0513a2f7a4d8dd86f205375efe27c0dd38 (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
#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/socket.h>

#ifndef SOCK_DGRAM
#define SOCK_DGRAM 2
#endif

unsigned int if_nametoindex(const char* blub) {
  struct ifreq ifr;
  int fd;
  char *tmp;
  int len=sizeof(ifr.ifr_name);
  fd=socket(AF_INET6,SOCK_DGRAM,0);
  if (fd<0) 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;
}