get_ifi_info.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "unpifi.h"
  2. #include "unproute.h"
  3. /* include get_ifi_info1 */
  4. struct ifi_info *
  5. get_ifi_info(int family, int doaliases)
  6. {
  7. int flags;
  8. char *buf, *next, *lim;
  9. size_t len;
  10. struct if_msghdr *ifm;
  11. struct ifa_msghdr *ifam;
  12. struct sockaddr *sa, *rti_info[RTAX_MAX];
  13. struct sockaddr_dl *sdl;
  14. struct ifi_info *ifi, *ifisave, *ifihead, **ifipnext;
  15. buf = Net_rt_iflist(family, 0, &len);
  16. ifihead = NULL;
  17. ifipnext = &ifihead;
  18. lim = buf + len;
  19. for (next = buf; next < lim; next += ifm->ifm_msglen) {
  20. ifm = (struct if_msghdr *) next;
  21. if (ifm->ifm_type == RTM_IFINFO) {
  22. if ( ((flags = ifm->ifm_flags) & IFF_UP) == 0)
  23. continue; /* ignore if interface not up */
  24. sa = (struct sockaddr *) (ifm + 1);
  25. get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
  26. if ( (sa = rti_info[RTAX_IFP]) != NULL) {
  27. ifi = Calloc(1, sizeof(struct ifi_info));
  28. *ifipnext = ifi; /* prev points to this new one */
  29. ifipnext = &ifi->ifi_next; /* ptr to next one goes here */
  30. ifi->ifi_flags = flags;
  31. if (sa->sa_family == AF_LINK) {
  32. sdl = (struct sockaddr_dl *) sa;
  33. ifi->ifi_index = sdl->sdl_index;
  34. if (sdl->sdl_nlen > 0)
  35. snprintf(ifi->ifi_name, IFI_NAME, "%*s",
  36. sdl->sdl_nlen, &sdl->sdl_data[0]);
  37. else
  38. snprintf(ifi->ifi_name, IFI_NAME, "index %d",
  39. sdl->sdl_index);
  40. if ( (ifi->ifi_hlen = sdl->sdl_alen) > 0)
  41. memcpy(ifi->ifi_haddr, LLADDR(sdl),
  42. min(IFI_HADDR, sdl->sdl_alen));
  43. }
  44. }
  45. /* end get_ifi_info1 */
  46. /* include get_ifi_info3 */
  47. } else if (ifm->ifm_type == RTM_NEWADDR) {
  48. if (ifi->ifi_addr) { /* already have an IP addr for i/f */
  49. if (doaliases == 0)
  50. continue;
  51. /* 4we have a new IP addr for existing interface */
  52. ifisave = ifi;
  53. ifi = Calloc(1, sizeof(struct ifi_info));
  54. *ifipnext = ifi; /* prev points to this new one */
  55. ifipnext = &ifi->ifi_next; /* ptr to next one goes here */
  56. ifi->ifi_flags = ifisave->ifi_flags;
  57. ifi->ifi_index = ifisave->ifi_index;
  58. ifi->ifi_hlen = ifisave->ifi_hlen;
  59. memcpy(ifi->ifi_name, ifisave->ifi_name, IFI_NAME);
  60. memcpy(ifi->ifi_haddr, ifisave->ifi_haddr, IFI_HADDR);
  61. }
  62. ifam = (struct ifa_msghdr *) next;
  63. sa = (struct sockaddr *) (ifam + 1);
  64. get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
  65. if ( (sa = rti_info[RTAX_IFA]) != NULL) {
  66. ifi->ifi_addr = Calloc(1, sa->sa_len);
  67. memcpy(ifi->ifi_addr, sa, sa->sa_len);
  68. }
  69. if ((flags & IFF_BROADCAST) &&
  70. (sa = rti_info[RTAX_BRD]) != NULL) {
  71. ifi->ifi_brdaddr = Calloc(1, sa->sa_len);
  72. memcpy(ifi->ifi_brdaddr, sa, sa->sa_len);
  73. }
  74. if ((flags & IFF_POINTOPOINT) &&
  75. (sa = rti_info[RTAX_BRD]) != NULL) {
  76. ifi->ifi_dstaddr = Calloc(1, sa->sa_len);
  77. memcpy(ifi->ifi_dstaddr, sa, sa->sa_len);
  78. }
  79. } else
  80. err_quit("unexpected message type %d", ifm->ifm_type);
  81. }
  82. /* "ifihead" points to the first structure in the linked list */
  83. return(ifihead); /* ptr to first structure in linked list */
  84. }
  85. /* end get_ifi_info3 */
  86. void
  87. free_ifi_info(struct ifi_info *ifihead)
  88. {
  89. struct ifi_info *ifi, *ifinext;
  90. for (ifi = ifihead; ifi != NULL; ifi = ifinext) {
  91. if (ifi->ifi_addr != NULL)
  92. free(ifi->ifi_addr);
  93. if (ifi->ifi_brdaddr != NULL)
  94. free(ifi->ifi_brdaddr);
  95. if (ifi->ifi_dstaddr != NULL)
  96. free(ifi->ifi_dstaddr);
  97. ifinext = ifi->ifi_next; /* can't fetch ifi_next after free() */
  98. free(ifi); /* the ifi_info{} itself */
  99. }
  100. }
  101. struct ifi_info *
  102. Get_ifi_info(int family, int doaliases)
  103. {
  104. struct ifi_info *ifi;
  105. if ( (ifi = get_ifi_info(family, doaliases)) == NULL)
  106. err_quit("get_ifi_info error");
  107. return(ifi);
  108. }