pktdump_ex.c 5.2 KB

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