if_nametoindex.c 948 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. *
  3. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
  4. * The Silver Hammer Group, Ltd.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #define __FORCE_GLIBC
  13. #include <features.h>
  14. #include <net/if.h>
  15. #include <sys/ioctl.h>
  16. #include <unistd.h>
  17. unsigned int if_nametoindex(const char* blub) {
  18. struct ifreq ifr;
  19. int fd;
  20. char *tmp;
  21. int len=sizeof(ifr.ifr_name);
  22. #ifdef __UCLIBC_HAS_IPV6__
  23. fd=socket(AF_INET6,SOCK_DGRAM,0);
  24. if (fd<0)
  25. #endif /* __UCLIBC_HAS_IPV6__ */
  26. fd=socket(AF_INET,SOCK_DGRAM,0);
  27. for (tmp=ifr.ifr_name; len>0; --len) {
  28. if ((*tmp++ = *blub++)==0) break;
  29. }
  30. if (ioctl(fd,SIOCGIFINDEX,&ifr)==0) {
  31. close(fd);
  32. return ifr.ifr_ifindex;
  33. }
  34. close(fd);
  35. return 0;
  36. }