iflist.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
  3. * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the Politecnico di Torino, CACE Technologies
  16. * nor the names of its contributors may be used to endorse or promote
  17. * products derived from this software without specific prior written
  18. * permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #ifdef _MSC_VER
  34. /*
  35. * we do not want the warnings about the old deprecated and unsecure CRT functions
  36. * since these examples can be compiled under *nix as well
  37. */
  38. #define _CRT_SECURE_NO_WARNINGS
  39. #endif
  40. #include <pcap.h>
  41. #include <stdio.h>
  42. #ifndef _WIN32
  43. #include <sys/socket.h>
  44. #include <netinet/in.h>
  45. #else
  46. #include <winsock2.h>
  47. #endif
  48. #ifdef _WIN32
  49. #include <tchar.h>
  50. BOOL LoadNpcapDlls()
  51. {
  52. _TCHAR npcap_dir[512];
  53. UINT len;
  54. len = GetSystemDirectory(npcap_dir, 480);
  55. if (!len) {
  56. fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
  57. return FALSE;
  58. }
  59. _tcscat_s(npcap_dir, 512, _T("\\Npcap"));
  60. if (SetDllDirectory(npcap_dir) == 0) {
  61. fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
  62. return FALSE;
  63. }
  64. return TRUE;
  65. }
  66. #endif
  67. // Function prototypes
  68. void ifprint(pcap_if_t *d);
  69. const char* iptos(struct sockaddr *sockaddr);
  70. int main()
  71. {
  72. pcap_if_t *alldevs;
  73. pcap_if_t *d;
  74. char errbuf[PCAP_ERRBUF_SIZE+1];
  75. #ifdef _WIN32
  76. WSADATA wsadata;
  77. int err = WSAStartup(MAKEWORD(2,2), &wsadata);
  78. if (err != 0) {
  79. fprintf(stderr, "WSAStartup failed: %d\n", err);
  80. exit(1);
  81. }
  82. /* Load Npcap and its functions. */
  83. if (!LoadNpcapDlls())
  84. {
  85. fprintf(stderr, "Couldn't load Npcap\n");
  86. WSACleanup();
  87. exit(1);
  88. }
  89. #endif
  90. /* Retrieve the device list */
  91. if(pcap_findalldevs(&alldevs, errbuf) == -1)
  92. {
  93. fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
  94. WSACleanup();
  95. exit(1);
  96. }
  97. /* Scan the list printing every entry */
  98. for(d=alldevs;d;d=d->next)
  99. {
  100. ifprint(d);
  101. }
  102. /* Free the device list */
  103. pcap_freealldevs(alldevs);
  104. WSACleanup();
  105. return 0;
  106. }
  107. /* Print all the available information on the given interface */
  108. void ifprint(pcap_if_t *d)
  109. {
  110. pcap_addr_t *a;
  111. /* Name */
  112. printf("%s\n",d->name);
  113. /* Description */
  114. if (d->description)
  115. printf("\tDescription: %s\n",d->description);
  116. /* Loopback Address*/
  117. printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");
  118. /* IP addresses */
  119. for(a=d->addresses;a;a=a->next) {
  120. printf("\tAddress Family: #%d\n",a->addr->sa_family);
  121. switch(a->addr->sa_family)
  122. {
  123. case AF_INET:
  124. printf("\tAddress Family Name: AF_INET\n");
  125. break;
  126. case AF_INET6:
  127. printf("\tAddress Family Name: AF_INET6\n");
  128. break;
  129. default:
  130. printf("\tAddress Family Name: Unknown\n");
  131. break;
  132. }
  133. if (a->addr && a->addr->sa_family > 0)
  134. printf("\tAddress: %s\n",iptos(a->addr));
  135. if (a->netmask && a->netmask->sa_family > 0)
  136. printf("\tNetmask: %s\n",iptos(a->netmask));
  137. if (a->broadaddr && a->broadaddr->sa_family > 0)
  138. printf("\tBroadcast Address: %s\n",iptos(a->broadaddr));
  139. if (a->dstaddr && a->dstaddr->sa_family > 0)
  140. printf("\tDestination Address: %s\n",iptos(a->dstaddr));
  141. }
  142. printf("\n");
  143. }
  144. #define ADDR_STR_MAX 128
  145. const char* iptos(struct sockaddr *sockaddr)
  146. {
  147. static char address[ADDR_STR_MAX] = {0};
  148. int gni_error = 0;
  149. gni_error = getnameinfo(sockaddr,
  150. sizeof(struct sockaddr_storage),
  151. address,
  152. ADDR_STR_MAX,
  153. NULL,
  154. 0,
  155. NI_NUMERICHOST);
  156. if (gni_error != 0)
  157. {
  158. fprintf(stderr, "getnameinfo: %s\n", gai_strerror(gni_error));
  159. return "ERROR!";
  160. }
  161. return address;
  162. }