tcptop.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <pcap.h>
  36. #include <time.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. void usage();
  55. void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);
  56. void main(int argc, char **argv)
  57. {
  58. pcap_t *fp;
  59. char errbuf[PCAP_ERRBUF_SIZE];
  60. struct timeval st_ts;
  61. u_int netmask;
  62. struct bpf_program fcode;
  63. /* Load Npcap and its functions. */
  64. if (!LoadNpcapDlls())
  65. {
  66. fprintf(stderr, "Couldn't load Npcap\n");
  67. exit(1);
  68. }
  69. /* Check the validity of the command line */
  70. if (argc != 2)
  71. {
  72. usage();
  73. return;
  74. }
  75. /* Open the output adapter */
  76. if ( (fp= pcap_open(argv[1], 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
  77. {
  78. fprintf(stderr,"\nUnable to open adapter %s.\n", errbuf);
  79. return;
  80. }
  81. /* Don't care about netmask, it won't be used for this filter */
  82. netmask=0xffffff;
  83. //compile the filter
  84. if (pcap_compile(fp, &fcode, "tcp", 1, netmask) <0 )
  85. {
  86. fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
  87. /* Free the device list */
  88. return;
  89. }
  90. //set the filter
  91. if (pcap_setfilter(fp, &fcode)<0)
  92. {
  93. fprintf(stderr,"\nError setting the filter.\n");
  94. pcap_close(fp);
  95. /* Free the device list */
  96. return;
  97. }
  98. /* Put the interface in statstics mode */
  99. if (pcap_setmode(fp, MODE_STAT)<0)
  100. {
  101. fprintf(stderr,"\nError setting the mode.\n");
  102. pcap_close(fp);
  103. /* Free the device list */
  104. return;
  105. }
  106. printf("TCP traffic summary:\n");
  107. /* Start the main loop */
  108. pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts);
  109. pcap_close(fp);
  110. return;
  111. }
  112. void dispatcher_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data)
  113. {
  114. struct timeval *old_ts = (struct timeval *)state;
  115. u_int delay;
  116. LARGE_INTEGER Bps,Pps;
  117. struct tm ltime;
  118. char timestr[16];
  119. time_t local_tv_sec;
  120. /* Calculate the delay in microseconds from the last sample. */
  121. /* This value is obtained from the timestamp that the associated with the sample. */
  122. delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec;
  123. /* Get the number of Bits per second */
  124. Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay));
  125. /* ^ ^
  126. | |
  127. | |
  128. | |
  129. converts bytes in bits -- |
  130. |
  131. delay is expressed in microseconds --
  132. */
  133. /* Get the number of Packets per second */
  134. Pps.QuadPart=(((*(LONGLONG*)(pkt_data)) * 1000000) / (delay));
  135. /* Convert the timestamp to readable format */
  136. local_tv_sec = header->ts.tv_sec;
  137. localtime_s(&ltime, &local_tv_sec);
  138. strftime( timestr, sizeof timestr, "%H:%M:%S", &ltime);
  139. /* Print timestamp*/
  140. printf("%s ", timestr);
  141. /* Print the samples */
  142. printf("BPS=%I64u ", Bps.QuadPart);
  143. printf("PPS=%I64u\n", Pps.QuadPart);
  144. //store current timestamp
  145. old_ts->tv_sec=header->ts.tv_sec;
  146. old_ts->tv_usec=header->ts.tv_usec;
  147. }
  148. void usage()
  149. {
  150. printf("\nShows the TCP traffic load, in bits per second and packets per second.\nCopyright (C) 2002 Loris Degioanni.\n");
  151. printf("\nUsage:\n");
  152. printf("\t tcptop adapter\n");
  153. printf("\t You can use \"WinDump -D\" if you don't know the name of your adapters.\n");
  154. exit(0);
  155. }