You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
2.8 KiB

8 years ago
​
​
​
​
8 years ago
8 years ago
​
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
8 years ago
8 years ago
8 years ago
8 years ago
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
8 years ago
8 years ago
​
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
​
8 years ago
8 years ago
​
8 years ago
8 years ago
8 years ago
​
​
8 years ago
​
8 years ago
8 years ago
8 years ago
​
8 years ago
​
​
​
​
​
​
​
​
​
8 years ago
8 years ago
​
​
​
​
​
​
8 years ago
​
8 years ago
​
​
  1. /* $Id: listifaces.c,v 1.7 2015/02/08 08:51:54 nanard Exp $ */
  2. /* (c) 2006-2015 Thomas BERNARD
  3. * http://miniupnp.free.fr/ http://miniupnp.tuxfamily.org/
  4. */
  5. #include "upnputils.h"
  6. #include <arpa/inet.h>
  7. #include <net/if.h>
  8. #include <netinet/in.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/socket.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. /* hexdump */
  16. void printhex(const unsigned char * p, int n)
  17. {
  18. int i;
  19. while (n > 0)
  20. {
  21. for (i = 0; i < 16; i++)
  22. {
  23. printf("%02x ", p[i]);
  24. }
  25. printf("| ");
  26. for (i = 0; i < 16; i++)
  27. {
  28. putchar((p[i] >= 32 && p[i] < 127) ? p[i] : '.');
  29. }
  30. printf("\n");
  31. p += 16;
  32. n -= 16;
  33. }
  34. }
  35. /* List network interfaces */
  36. void listifaces(void)
  37. {
  38. struct ifconf ifc;
  39. char * buf = NULL;
  40. int buflen;
  41. int s, i;
  42. int j;
  43. char saddr[256 /*INET_ADDRSTRLEN*/];
  44. #ifdef __linux__
  45. buflen = sizeof(struct ifreq) * 10;
  46. #else
  47. buflen = 0;
  48. #endif
  49. /*s = socket(PF_INET, SOCK_DGRAM, 0);*/
  50. s = socket(AF_INET, SOCK_DGRAM, 0);
  51. do
  52. {
  53. char * tmp;
  54. #ifdef __linux__
  55. buflen += buflen;
  56. #endif
  57. if (buflen > 0)
  58. {
  59. tmp = realloc(buf, buflen);
  60. if (! tmp)
  61. {
  62. fprintf(stderr, "error allocating %d bytes.\n", buflen);
  63. close(f);
  64. free(buf);
  65. return;
  66. }
  67. buf = tmp;
  68. }
  69. ifc.ifc_len = buflen;
  70. ifc.ifc_buf = (caddr_t) buf;
  71. if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
  72. {
  73. perror("ioctl");
  74. close(s);
  75. free(buf);
  76. return;
  77. }
  78. printf("buffer length=%d - buffer used=%d - sizeof(struct ifreq)=%d\n", buflen, ifc.ifc_len, (int) sizeof(struct ifreq));
  79. printf("IFNAMSIZ=%d ", IFNAMSIZ);
  80. printf("sizeof(struct sockaddr)=%d sizeof(struct sockaddr_in)=%d\n", (int) sizeof(struct sockaddr), (int) sizeof(struct sockaddr_in));
  81. #ifndef __linux__
  82. if (buflen == 0)
  83. {
  84. buflen = ifc.ifc_len;
  85. }
  86. else
  87. {
  88. break;
  89. }
  90. }
  91. while (1);
  92. #else
  93. }
  94. while (buflen <= ifc.ifc_len);
  95. #endif
  96. printhex((const unsigned char *) ifc.ifc_buf, ifc.ifc_len);
  97. printf("off index fam name address\n");
  98. for (i = 0, j = 0; i < ifc.ifc_len; j++)
  99. {
  100. /*const struct ifreq * ifrp = &(ifc.ifc_req[j]);*/
  101. const struct ifreq * ifrp = (const struct ifreq *) (buf + i);
  102. /*inet_ntop(AF_INET, &(((struct sockaddr_in *)&(ifrp->ifr_addr))->sin_addr), saddr, sizeof(saddr));*/
  103. saddr[0] = '\0';
  104. /* inet_ntop(ifrp->ifr_addr.sa_family, &(ifrp->ifr_addr.sa_data[2]), saddr, sizeof(saddr)); */
  105. sockaddr_to_string(&ifrp->ifr_addr, saddr, sizeof(saddr));
  106. printf("0x%03x %2d %2d %-16s %s\n", i, j, ifrp->ifr_addr.sa_family, ifrp->ifr_name, saddr);
  107. /*ifrp->ifr_addr.sa_len is only available on BSD */
  108. #ifdef __linux__
  109. i += sizeof(struct ifreq);
  110. #else
  111. if (ifrp->ifr_addr.sa_len == 0)
  112. {
  113. break;
  114. }
  115. i += IFNAMSIZ + ifrp->ifr_addr.sa_len;
  116. #endif
  117. }
  118. free(buf);
  119. close(s);
  120. }
  121. int main(int argc, char ** argv)
  122. {
  123. (void) argc;
  124. (void) argv;
  125. listifaces();
  126. return 0;
  127. }