basic_dump_ex.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. int main()
  31. {
  32. pcap_if_t *alldevs;
  33. pcap_if_t *d;
  34. int inum;
  35. int i=0;
  36. pcap_t *adhandle;
  37. int res;
  38. char errbuf[PCAP_ERRBUF_SIZE];
  39. struct tm *ltime;
  40. char timestr[16];
  41. struct pcap_pkthdr *header;
  42. const u_char *pkt_data;
  43. time_t local_tv_sec;
  44. #ifdef _WIN32
  45. /* Load Npcap and its functions. */
  46. if (!LoadNpcapDlls())
  47. {
  48. fprintf(stderr, "Couldn't load Npcap\n");
  49. exit(1);
  50. }
  51. #endif
  52. /* Retrieve the device list */
  53. if(pcap_findalldevs(&alldevs, errbuf) == -1)
  54. {
  55. fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
  56. return -1;
  57. }
  58. /* Print the list */
  59. for(d=alldevs; d; d=d->next)
  60. {
  61. printf("%d. %s", ++i, d->name);
  62. if (d->description)
  63. printf(" (%s)\n", d->description);
  64. else
  65. printf(" (No description available)\n");
  66. }
  67. if(i==0)
  68. {
  69. printf("\nNo interfaces found! Make sure Npcap is installed.\n");
  70. return -1;
  71. }
  72. printf("Enter the interface number (1-%d):",i);
  73. scanf("%d", &inum);
  74. if(inum < 1 || inum > i)
  75. {
  76. printf("\nInterface number out of range.\n");
  77. /* Free the device list */
  78. pcap_freealldevs(alldevs);
  79. return -1;
  80. }
  81. /* Jump to the selected adapter */
  82. for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
  83. /* Open the adapter */
  84. if ((adhandle= pcap_open_live(d->name, // name of the device
  85. 65536, // portion of the packet to capture.
  86. // 65536 grants that the whole packet will be captured on all the MACs.
  87. 1, // promiscuous mode (nonzero means promiscuous)
  88. 1000, // read timeout
  89. errbuf // error buffer
  90. )) == NULL)
  91. {
  92. fprintf(stderr,"\nUnable to open the adapter. %s is not supported by Npcap\n", d->name);
  93. /* Free the device list */
  94. pcap_freealldevs(alldevs);
  95. return -1;
  96. }
  97. printf("\nlistening on %s...\n", d->description);
  98. /* At this point, we don't need any more the device list. Free it */
  99. pcap_freealldevs(alldevs);
  100. /* Retrieve the packets */
  101. while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){
  102. if(res == 0)
  103. /* Timeout elapsed */
  104. continue;
  105. /* convert the timestamp to readable format */
  106. local_tv_sec = header->ts.tv_sec;
  107. ltime=localtime(&local_tv_sec);
  108. strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
  109. printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
  110. }
  111. if(res == -1){
  112. printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
  113. return -1;
  114. }
  115. pcap_close(adhandle);
  116. return 0;
  117. }