basic_dump.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include <stdio.h>
  10. #include <time.h>
  11. #ifdef _WIN32
  12. #include <tchar.h>
  13. BOOL LoadNpcapDlls()
  14. {
  15. _TCHAR npcap_dir[512];
  16. UINT len;
  17. len = GetSystemDirectory(npcap_dir, 480);
  18. if (!len) {
  19. fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
  20. return FALSE;
  21. }
  22. _tcscat_s(npcap_dir, 512, _T("\\Npcap"));
  23. if (SetDllDirectory(npcap_dir) == 0) {
  24. fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
  25. return FALSE;
  26. }
  27. return TRUE;
  28. }
  29. #endif
  30. /* prototype of the packet handler */
  31. void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
  32. int main()
  33. {
  34. pcap_if_t *alldevs;
  35. pcap_if_t *d;
  36. int inum;
  37. int i=0;
  38. pcap_t *adhandle;
  39. char errbuf[PCAP_ERRBUF_SIZE];
  40. #ifdef _WIN32
  41. /* Load Npcap and its functions. */
  42. if (!LoadNpcapDlls())
  43. {
  44. fprintf(stderr, "Couldn't load Npcap\n");
  45. exit(1);
  46. }
  47. #endif
  48. /* Retrieve the device list */
  49. if(pcap_findalldevs(&alldevs, errbuf) == -1)
  50. {
  51. fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
  52. exit(1);
  53. }
  54. /* Print the list */
  55. for(d=alldevs; d; d=d->next)
  56. {
  57. printf("%d. %s", ++i, d->name);
  58. if (d->description)
  59. printf(" (%s)\n", d->description);
  60. else
  61. printf(" (No description available)\n");
  62. }
  63. if(i==0)
  64. {
  65. printf("\nNo interfaces found! Make sure Npcap is installed.\n");
  66. return -1;
  67. }
  68. printf("Enter the interface number (1-%d):",i);
  69. scanf("%d", &inum);
  70. if(inum < 1 || inum > i)
  71. {
  72. printf("\nInterface number out of range.\n");
  73. /* Free the device list */
  74. pcap_freealldevs(alldevs);
  75. return -1;
  76. }
  77. /* Jump to the selected adapter */
  78. for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
  79. /* Open the device */
  80. /* Open the adapter */
  81. if ((adhandle= pcap_open_live(d->name, // name of the device
  82. 65536, // portion of the packet to capture.
  83. // 65536 grants that the whole packet will be captured on all the MACs.
  84. 1, // promiscuous mode (nonzero means promiscuous)
  85. 1000, // read timeout
  86. errbuf // error buffer
  87. )) == NULL)
  88. {
  89. fprintf(stderr,"\nUnable to open the adapter. %s is not supported by Npcap\n", d->name);
  90. /* Free the device list */
  91. pcap_freealldevs(alldevs);
  92. return -1;
  93. }
  94. printf("\nlistening on %s...\n", d->description);
  95. /* At this point, we don't need any more the device list. Free it */
  96. pcap_freealldevs(alldevs);
  97. /* start the capture */
  98. pcap_loop(adhandle, 0, packet_handler, NULL);
  99. pcap_close(adhandle);
  100. return 0;
  101. }
  102. /* Callback function invoked by libpcap for every incoming packet */
  103. void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
  104. {
  105. struct tm *ltime;
  106. char timestr[16];
  107. time_t local_tv_sec;
  108. /*
  109. * unused parameters
  110. */
  111. (VOID)(param);
  112. (VOID)(pkt_data);
  113. /* convert the timestamp to readable format */
  114. local_tv_sec = header->ts.tv_sec;
  115. ltime=localtime(&local_tv_sec);
  116. strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
  117. printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
  118. }