iflist.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include <stdio.h>
  34. #include "pcap.h"
  35. #ifndef _WIN32
  36. #include <sys/socket.h>
  37. #include <netinet/in.h>
  38. #else
  39. #include <winsock.h>
  40. #include <tchar.h>
  41. BOOL LoadNpcapDlls()
  42. {
  43. TCHAR npcap_dir[512];
  44. UINT len;
  45. len = GetSystemDirectory(npcap_dir, 480);
  46. if (!len) {
  47. fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
  48. return FALSE;
  49. }
  50. _tcscat_s(npcap_dir, 512, TEXT("\\Npcap"));
  51. if (SetDllDirectory(npcap_dir) == 0) {
  52. fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
  53. return FALSE;
  54. }
  55. return TRUE;
  56. }
  57. #endif
  58. // Function prototypes
  59. void ifprint(pcap_if_t *d);
  60. char *iptos(u_long in);
  61. char* ip6tos(struct sockaddr *sockaddr, char *address, int addrlen);
  62. int main()
  63. {
  64. pcap_if_t *alldevs;
  65. pcap_if_t *d;
  66. char errbuf[PCAP_ERRBUF_SIZE+1];
  67. char source[PCAP_ERRBUF_SIZE+1];
  68. #ifdef _WIN32
  69. /* Load Npcap and its functions. */
  70. if (!LoadNpcapDlls())
  71. {
  72. fprintf(stderr, "Couldn't load Npcap\n");
  73. exit(1);
  74. }
  75. #endif
  76. printf("Enter the device you want to list:\n"
  77. "rpcap:// ==> lists interfaces in the local machine\n"
  78. "rpcap://hostname:port ==> lists interfaces in a remote machine\n"
  79. " (rpcapd daemon must be up and running\n"
  80. " and it must accept 'null' authentication)\n"
  81. "file://foldername ==> lists all pcap files in the give folder\n\n"
  82. "Enter your choice: ");
  83. fgets(source, PCAP_ERRBUF_SIZE, stdin);
  84. source[PCAP_ERRBUF_SIZE] = '\0';
  85. /* Retrieve the interfaces list */
  86. if (pcap_findalldevs_ex(source, NULL, &alldevs, errbuf) == -1)
  87. {
  88. fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
  89. exit(1);
  90. }
  91. /* Scan the list printing every entry */
  92. for(d=alldevs;d;d=d->next)
  93. {
  94. ifprint(d);
  95. }
  96. pcap_freealldevs(alldevs);
  97. return 1;
  98. }
  99. /* Print all the available information on the given interface */
  100. void ifprint(pcap_if_t *d)
  101. {
  102. pcap_addr_t *a;
  103. char ip6str[128];
  104. /* Name */
  105. printf("%s\n",d->name);
  106. /* Description */
  107. if (d->description)
  108. printf("\tDescription: %s\n",d->description);
  109. /* Loopback Address*/
  110. printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");
  111. /* IP addresses */
  112. for(a=d->addresses;a;a=a->next) {
  113. printf("\tAddress Family: #%d\n",a->addr->sa_family);
  114. switch(a->addr->sa_family)
  115. {
  116. case AF_INET:
  117. printf("\tAddress Family Name: AF_INET\n");
  118. if (a->addr)
  119. printf("\tAddress: %s\n",iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr));
  120. if (a->netmask)
  121. printf("\tNetmask: %s\n",iptos(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr));
  122. if (a->broadaddr)
  123. printf("\tBroadcast Address: %s\n",iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr));
  124. if (a->dstaddr)
  125. printf("\tDestination Address: %s\n",iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr));
  126. break;
  127. case AF_INET6:
  128. printf("\tAddress Family Name: AF_INET6\n");
  129. if (a->addr)
  130. printf("\tAddress: %s\n", ip6tos(a->addr, ip6str, sizeof(ip6str)));
  131. break;
  132. default:
  133. printf("\tAddress Family Name: Unknown\n");
  134. break;
  135. }
  136. }
  137. printf("\n");
  138. }
  139. /* From tcptraceroute, convert a numeric IP address to a string */
  140. #define IPTOSBUFFERS 12
  141. char *iptos(u_long in)
  142. {
  143. static char output[IPTOSBUFFERS][3*4+3+1];
  144. static short which;
  145. u_char *p;
  146. p = (u_char *)&in;
  147. which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
  148. _snprintf_s(output[which], sizeof(output[which]), sizeof(output[which]),"%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
  149. return output[which];
  150. }
  151. char* ip6tos(struct sockaddr *sockaddr, char *address, int addrlen)
  152. {
  153. socklen_t sockaddrlen;
  154. #ifdef _WIN32
  155. sockaddrlen = sizeof(struct sockaddr_in6);
  156. #else
  157. sockaddrlen = sizeof(struct sockaddr_storage);
  158. #endif
  159. if(getnameinfo(sockaddr,
  160. sockaddrlen,
  161. address,
  162. addrlen,
  163. NULL,
  164. 0,
  165. NI_NUMERICHOST) != 0) address = NULL;
  166. return address;
  167. }