smp_1.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
  3. * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the Politecnico di Torino, CACE Technologies
  16. * nor the names of its contributors may be used to endorse or promote
  17. * products derived from this software without specific prior written
  18. * permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <conio.h>
  36. #include <pcap.h>
  37. #include <tchar.h>
  38. BOOL LoadNpcapDlls()
  39. {
  40. _TCHAR npcap_dir[512];
  41. UINT len;
  42. len = GetSystemDirectory(npcap_dir, 480);
  43. if (!len) {
  44. fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
  45. return FALSE;
  46. }
  47. _tcscat_s(npcap_dir, 512, _T("\\Npcap"));
  48. if (SetDllDirectory(npcap_dir) == 0) {
  49. fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
  50. return FALSE;
  51. }
  52. return TRUE;
  53. }
  54. int main()
  55. {
  56. pcap_if_t *alldevs, *d;
  57. pcap_t *fp;
  58. u_int inum, i=0;
  59. char errbuf[PCAP_ERRBUF_SIZE];
  60. int res;
  61. struct pcap_pkthdr *header;
  62. const u_char *pkt_data;
  63. struct pcap_pkthdr old;
  64. /* Load Npcap and its functions. */
  65. if (!LoadNpcapDlls())
  66. {
  67. fprintf(stderr, "Couldn't load Npcap\n");
  68. exit(1);
  69. }
  70. printf("SMP_1\n");
  71. printf("\nThis program tests the Npcap kernel driver on SMP machines.\n");
  72. printf("The program tests that timestamps on the captured packets are consistent,\n");
  73. printf("and that the caplen is equal to the packet length.\n");
  74. printf("If there is an error, it will print out a message saying \"Inconsistent XXX\"\n");
  75. if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
  76. {
  77. fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
  78. exit(1);
  79. }
  80. /* Print the list */
  81. for(d=alldevs; d; d=d->next)
  82. {
  83. printf("%d. %s", ++i, d->name);
  84. if (d->description)
  85. printf(" (%s)\n", d->description);
  86. else
  87. printf(" (No description available)\n");
  88. }
  89. if(i==0)
  90. {
  91. printf("\nNo interfaces found! Make sure Npcap is installed.\n");
  92. return -1;
  93. }
  94. printf("Enter the interface number (1-%d):",i);
  95. scanf_s("%d", &inum);
  96. if(inum < 1 || inum > i)
  97. {
  98. printf("\nInterface number out of range.\n");
  99. /* Free the device list */
  100. pcap_freealldevs(alldevs);
  101. return -1;
  102. }
  103. /* Jump to the selected adapter */
  104. for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
  105. /* Open the device */
  106. if ( (fp= pcap_open(d->name, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
  107. {
  108. fprintf(stderr,"\nUnable to open the adapter. %s is not supported by Npcap\n", d->name);
  109. /* Free the device list */
  110. pcap_freealldevs(alldevs);
  111. return -1;
  112. }
  113. old.ts.tv_sec=0;
  114. old.ts.tv_usec=0;
  115. /* Read the packets */
  116. while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0){
  117. if(res == 0)
  118. continue;
  119. //check that caplen is equal to packet length
  120. if (header->caplen!=header->len)
  121. printf("Inconsistent header: CapLen %d\t Len %d\n",header->caplen,header->len);
  122. //check that timestamps always grow
  123. if ( old.ts.tv_sec > header->ts.tv_sec || (old.ts.tv_sec == header->ts.tv_sec && old.ts.tv_usec > header->ts.tv_usec))
  124. printf("Inconsistent Timestamps! Old was %d.%.06d - New is %d.%.06d\n",old.ts.tv_sec,old.ts.tv_usec, header->ts.tv_sec,header->ts.tv_usec);
  125. old=*header;
  126. }
  127. if(res == -1){
  128. printf("Error reading the packets: %s\n", pcap_geterr(fp));
  129. return -1;
  130. }
  131. _getch();
  132. return 0;
  133. }