basic_dump.c 2.7 KB

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