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.

120 lines
2.7 KiB

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