pktdump_ex.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <stdlib.h>
  41. #include <stdio.h>
  42. #include <pcap.h>
  43. #define LINE_LEN 16
  44. #ifdef _WIN32
  45. #include <tchar.h>
  46. BOOL LoadNpcapDlls()
  47. {
  48. _TCHAR npcap_dir[512];
  49. UINT len;
  50. len = GetSystemDirectory(npcap_dir, 480);
  51. if (!len) {
  52. fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
  53. return FALSE;
  54. }
  55. _tcscat_s(npcap_dir, 512, _T("\\Npcap"));
  56. if (SetDllDirectory(npcap_dir) == 0) {
  57. fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
  58. return FALSE;
  59. }
  60. return TRUE;
  61. }
  62. #endif
  63. int main(int argc, char **argv)
  64. {
  65. pcap_if_t *alldevs, *d;
  66. pcap_t *fp;
  67. u_int inum, i=0;
  68. char errbuf[PCAP_ERRBUF_SIZE];
  69. int res;
  70. struct pcap_pkthdr *header;
  71. const u_char *pkt_data;
  72. #ifdef _WIN32
  73. /* Load Npcap and its functions. */
  74. if (!LoadNpcapDlls())
  75. {
  76. fprintf(stderr, "Couldn't load Npcap\n");
  77. exit(1);
  78. }
  79. #endif
  80. printf("pktdump_ex: prints the packets of the network using Npcap.\n");
  81. printf(" Usage: pktdump_ex [-s source]\n\n"
  82. " Examples:\n"
  83. " pktdump_ex -s file.acp\n"
  84. " pktdump_ex -s \\Device\\NPF_{C8736017-F3C3-4373-94AC-9A34B7DAD998}\n\n");
  85. if(argc < 3)
  86. {
  87. printf("\nNo adapter selected: printing the device list:\n");
  88. /* The user didn't provide a packet source: Retrieve the local device list */
  89. if(pcap_findalldevs(&alldevs, errbuf) == -1)
  90. {
  91. fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
  92. exit(1);
  93. }
  94. /* Print the list */
  95. for(d=alldevs; d; d=d->next)
  96. {
  97. printf("%d. %s\n ", ++i, d->name);
  98. if (d->description)
  99. printf(" (%s)\n", d->description);
  100. else
  101. printf(" (No description available)\n");
  102. }
  103. if (i==0)
  104. {
  105. printf("\nNo interfaces found! Make sure Npcap is installed.\n");
  106. return -1;
  107. }
  108. printf("Enter the interface number (1-%d):",i);
  109. scanf("%d", &inum);
  110. if (inum < 1 || inum > i)
  111. {
  112. printf("\nInterface number out of range.\n");
  113. /* Free the device list */
  114. pcap_freealldevs(alldevs);
  115. return -1;
  116. }
  117. /* Jump to the selected adapter */
  118. for (d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
  119. /* Open the adapter */
  120. if ((fp = pcap_open_live(d->name, // name of the device
  121. 65536, // portion of the packet to capture.
  122. // 65536 grants that the whole packet will be captured on all the MACs.
  123. 1, // promiscuous mode (nonzero means promiscuous)
  124. 1000, // read timeout
  125. errbuf // error buffer
  126. )) == NULL)
  127. {
  128. fprintf(stderr,"\nError opening adapter\n");
  129. return -1;
  130. }
  131. }
  132. else
  133. {
  134. /* Do not check for the switch type ('-s') */
  135. if ((fp = pcap_open_live(argv[2], // name of the device
  136. 65536, // portion of the packet to capture.
  137. // 65536 grants that the whole packet will be captured on all the MACs.
  138. 1, // promiscuous mode (nonzero means promiscuous)
  139. 1000, // read timeout
  140. errbuf // error buffer
  141. )) == NULL)
  142. {
  143. fprintf(stderr,"\nError opening adapter\n");
  144. return -1;
  145. }
  146. }
  147. /* Read the packets */
  148. while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0)
  149. {
  150. if(res == 0)
  151. /* Timeout elapsed */
  152. continue;
  153. /* print pkt timestamp and pkt len */
  154. printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
  155. /* Print the packet */
  156. for (i=1; (i < header->caplen + 1 ) ; i++)
  157. {
  158. printf("%.2x ", pkt_data[i-1]);
  159. if ( (i % LINE_LEN) == 0) printf("\n");
  160. }
  161. printf("\n\n");
  162. }
  163. if(res == -1)
  164. {
  165. printf("Error reading the packets: %s\n", pcap_geterr(fp));
  166. return -1;
  167. }
  168. pcap_close(fp);
  169. return 0;
  170. }