if_indextoname.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* include if_indextoname */
  2. #include "unpifi.h"
  3. #include "unproute.h"
  4. char *
  5. if_indextoname(unsigned int idx, char *name)
  6. {
  7. char *buf, *next, *lim;
  8. size_t len;
  9. struct if_msghdr *ifm;
  10. struct sockaddr *sa, *rti_info[RTAX_MAX];
  11. struct sockaddr_dl *sdl;
  12. if ( (buf = net_rt_iflist(0, idx, &len)) == NULL)
  13. return(NULL);
  14. lim = buf + len;
  15. for (next = buf; next < lim; next += ifm->ifm_msglen) {
  16. ifm = (struct if_msghdr *) next;
  17. if (ifm->ifm_type == RTM_IFINFO) {
  18. sa = (struct sockaddr *) (ifm + 1);
  19. get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
  20. if ( (sa = rti_info[RTAX_IFP]) != NULL) {
  21. if (sa->sa_family == AF_LINK) {
  22. sdl = (struct sockaddr_dl *) sa;
  23. if (sdl->sdl_index == idx) {
  24. int slen = min(IFNAMSIZ - 1, sdl->sdl_nlen);
  25. strncpy(name, sdl->sdl_data, slen);
  26. name[slen] = 0; /* null terminate */
  27. free(buf);
  28. return(name);
  29. }
  30. }
  31. }
  32. }
  33. }
  34. free(buf);
  35. return(NULL); /* no match for index */
  36. }
  37. /* end if_indextoname */
  38. char *
  39. If_indextoname(unsigned int idx, char *name)
  40. {
  41. char *ptr;
  42. if ( (ptr = if_indextoname(idx, name)) == NULL)
  43. err_quit("if_indextoname error for %d", idx);
  44. return(ptr);
  45. }