savedump.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifdef _MSC_VER
  2. /*
  3. * we do not want the warnings about the old deprecated and unsecure CRT functions
  4. * since these examples can be compiled under *nix as well
  5. */
  6. #define _CRT_SECURE_NO_WARNINGS
  7. #endif
  8. #include <pcap.h>
  9. #ifdef _WIN32
  10. #include <tchar.h>
  11. BOOL LoadNpcapDlls()
  12. {
  13. _TCHAR npcap_dir[512];
  14. UINT len;
  15. len = GetSystemDirectory(npcap_dir, 480);
  16. if (!len) {
  17. fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
  18. return FALSE;
  19. }
  20. _tcscat_s(npcap_dir, 512, _T("\\Npcap"));
  21. if (SetDllDirectory(npcap_dir) == 0) {
  22. fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
  23. return FALSE;
  24. }
  25. return TRUE;
  26. }
  27. #endif
  28. /* prototype of the packet handler */
  29. void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
  30. int main(int argc, char **argv)
  31. {
  32. pcap_if_t *alldevs;
  33. pcap_if_t *d;
  34. int inum;
  35. int i=0;
  36. pcap_t *adhandle;
  37. char errbuf[PCAP_ERRBUF_SIZE];
  38. pcap_dumper_t *dumpfile;
  39. #ifdef _WIN32
  40. /* Load Npcap and its functions. */
  41. if (!LoadNpcapDlls())
  42. {
  43. fprintf(stderr, "Couldn't load Npcap\n");
  44. exit(1);
  45. }
  46. #endif
  47. /* Check command line */
  48. if(argc != 2)
  49. {
  50. printf("usage: %s filename", argv[0]);
  51. return -1;
  52. }
  53. /* Retrieve the device list on the local machine */
  54. if (pcap_findalldevs(&alldevs, errbuf) == -1)
  55. {
  56. fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
  57. exit(1);
  58. }
  59. /* Print the list */
  60. for(d=alldevs; d; d=d->next)
  61. {
  62. printf("%d. %s", ++i, d->name);
  63. if (d->description)
  64. printf(" (%s)\n", d->description);
  65. else
  66. printf(" (No description available)\n");
  67. }
  68. if(i==0)
  69. {
  70. printf("\nNo interfaces found! Make sure Npcap is installed.\n");
  71. return -1;
  72. }
  73. printf("Enter the interface number (1-%d):",i);
  74. scanf("%d", &inum);
  75. if(inum < 1 || inum > i)
  76. {
  77. printf("\nInterface number out of range.\n");
  78. /* Free the device list */
  79. pcap_freealldevs(alldevs);
  80. return -1;
  81. }
  82. /* Jump to the selected adapter */
  83. for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
  84. /* Open the adapter */
  85. if ((adhandle= pcap_open_live(d->name, // name of the device
  86. 65536, // portion of the packet to capture.
  87. // 65536 grants that the whole packet will be captured on all the MACs.
  88. 1, // promiscuous mode (nonzero means promiscuous)
  89. 1000, // read timeout
  90. errbuf // error buffer
  91. )) == NULL)
  92. {
  93. fprintf(stderr,"\nUnable to open the adapter. %s is not supported by Npcap\n", d->name);
  94. /* Free the device list */
  95. pcap_freealldevs(alldevs);
  96. return -1;
  97. }
  98. /* Open the dump file */
  99. dumpfile = pcap_dump_open(adhandle, argv[1]);
  100. if(dumpfile==NULL)
  101. {
  102. fprintf(stderr,"\nError opening output file\n");
  103. return -1;
  104. }
  105. printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);
  106. /* At this point, we no longer need the device list. Free it */
  107. pcap_freealldevs(alldevs);
  108. /* start the capture */
  109. pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);
  110. pcap_close(adhandle);
  111. return 0;
  112. }
  113. /* Callback function invoked by libpcap for every incoming packet */
  114. void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
  115. {
  116. /* save the packet on the dump file */
  117. pcap_dump(dumpfile, header, pkt_data);
  118. }