savedump.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <pcap.h>
  2. #include "misc.h"
  3. /* prototype of the packet handler */
  4. void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
  5. int main(int argc, char **argv)
  6. {
  7. pcap_if_t *alldevs;
  8. pcap_if_t *d;
  9. int inum;
  10. int i=0;
  11. pcap_t *adhandle;
  12. char errbuf[PCAP_ERRBUF_SIZE];
  13. pcap_dumper_t *dumpfile;
  14. /* Load Npcap and its functions. */
  15. if (!LoadNpcapDlls())
  16. {
  17. fprintf(stderr, "Couldn't load Npcap\n");
  18. exit(1);
  19. }
  20. /* Check command line */
  21. if(argc != 2)
  22. {
  23. printf("usage: %s filename", argv[0]);
  24. return -1;
  25. }
  26. /* Retrieve the device list on the local machine */
  27. if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
  28. {
  29. fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
  30. exit(1);
  31. }
  32. /* Print the list */
  33. for(d=alldevs; d; d=d->next)
  34. {
  35. printf("%d. %s", ++i, d->name);
  36. if (d->description)
  37. printf(" (%s)\n", d->description);
  38. else
  39. printf(" (No description available)\n");
  40. }
  41. if(i==0)
  42. {
  43. printf("\nNo interfaces found! Make sure Npcap is installed.\n");
  44. return -1;
  45. }
  46. printf("Enter the interface number (1-%d):",i);
  47. scanf_s("%d", &inum);
  48. if(inum < 1 || inum > i)
  49. {
  50. printf("\nInterface number out of range.\n");
  51. /* Free the device list */
  52. pcap_freealldevs(alldevs);
  53. return -1;
  54. }
  55. /* Jump to the selected adapter */
  56. for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
  57. /* Open the device */
  58. if ( (adhandle= pcap_open(d->name, // name of the device
  59. 65536, // portion of the packet to capture
  60. // 65536 guarantees that the whole packet will be captured on all the link layers
  61. PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
  62. 1000, // read timeout
  63. NULL, // authentication on the remote machine
  64. errbuf // error buffer
  65. ) ) == NULL)
  66. {
  67. fprintf(stderr,"\nUnable to open the adapter. %s is not supported by Npcap\n", d->name);
  68. /* Free the device list */
  69. pcap_freealldevs(alldevs);
  70. return -1;
  71. }
  72. /* Open the dump file */
  73. dumpfile = pcap_dump_open(adhandle, argv[1]);
  74. if(dumpfile==NULL)
  75. {
  76. fprintf(stderr,"\nError opening output file\n");
  77. return -1;
  78. }
  79. printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);
  80. /* At this point, we no longer need the device list. Free it */
  81. pcap_freealldevs(alldevs);
  82. /* start the capture */
  83. pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);
  84. return 0;
  85. }
  86. /* Callback function invoked by libpcap for every incoming packet */
  87. void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
  88. {
  89. /* save the packet on the dump file */
  90. pcap_dump(dumpfile, header, pkt_data);
  91. }