lib/libc/net/minix/getifaddrs.c

http://www.minix3.org/ · C · 70 lines · 54 code · 13 blank · 3 comment · 5 complexity · 7b46250452343eef7d40548f839541c3 MD5 · raw file

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <net/if.h>
  6. #include <ifaddrs.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/socket.h>
  9. #include <sys/types.h>
  10. #include <netinet/in.h>
  11. #include <net/gen/in.h>
  12. #include <net/gen/ip_io.h>
  13. #include <net/gen/tcp.h>
  14. #include <net/gen/udp.h>
  15. int
  16. getifaddrs(struct ifaddrs **ifap)
  17. {
  18. static int fd = -1;
  19. nwio_ipconf_t ipconf;
  20. int flags;
  21. static struct ifaddrs ifa;
  22. static struct sockaddr_in addr, netmask;
  23. memset(&ifa, 0, sizeof(ifa));
  24. memset(&addr, 0, sizeof(addr));
  25. memset(&netmask, 0, sizeof(netmask));
  26. ifa.ifa_next = NULL;
  27. ifa.ifa_name = __UNCONST("ip");
  28. addr.sin_family = netmask.sin_family = AF_INET;
  29. ifa.ifa_addr = (struct sockaddr *) &addr;
  30. ifa.ifa_netmask = (struct sockaddr *) &netmask;
  31. addr.sin_addr.s_addr = 0;
  32. netmask.sin_addr.s_addr = 0;
  33. if(fd < 0) {
  34. char *ipd;
  35. if(!(ipd = getenv("IP_DEVICE")))
  36. ipd = __UNCONST("/dev/ip");
  37. if((fd = open(ipd, O_RDWR)) < 0)
  38. return -1;
  39. }
  40. /* Code taken from commands/simple/ifconfig.c. */
  41. if((flags = fcntl(fd, F_GETFL)) < 0 ||
  42. fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0 ||
  43. ioctl(fd, NWIOGIPCONF, &ipconf))
  44. return 0; /* Report interface as down. */
  45. addr.sin_addr.s_addr = ipconf.nwic_ipaddr;
  46. netmask.sin_addr.s_addr = ipconf.nwic_netmask;
  47. if(addr.sin_addr.s_addr) ifa.ifa_flags = IFF_UP;
  48. /* Just report on this interface. */
  49. *ifap = &ifa;
  50. return 0;
  51. }
  52. void
  53. freeifaddrs(struct ifaddrs *ifp)
  54. {
  55. /* getifaddrs points to static data, so no need to free. */
  56. ;
  57. }