• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #ifdef _WIN32
9   #include <winsock2.h>
10   #include <ws2tcpip.h>
11   #include <windows.h>
12 #else
13   #include <unistd.h>
14   #include <sys/resource.h>
15 #endif
16 
17 #include <pcap.h>
18 
19 #include "varattrs.h"
20 #include "pcap/funcattrs.h"
21 
22 #ifdef _WIN32
23 #include "portability.h"
24 #endif
25 
main(int argc _U_,char ** argv _U_)26 int main(int argc _U_, char **argv _U_)
27 {
28   pcap_if_t *alldevs;
29   int exit_status = 0;
30   char errbuf[PCAP_ERRBUF_SIZE+1];
31 #ifdef _WIN32
32   FILETIME start_ktime, start_utime, end_ktime, end_utime;
33   FILETIME dummy1, dummy2;
34   ULARGE_INTEGER start_kticks, end_kticks, start_uticks, end_uticks;
35   ULONGLONG ktime, utime, tottime;
36 #else
37   struct rusage start_rusage, end_rusage;
38   struct timeval ktime, utime, tottime;
39 #endif
40 
41 #ifdef _WIN32
42   if (!GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2,
43                        &start_ktime, &start_utime))
44   {
45     fprintf(stderr, "GetProcessTimes() fails at start\n");
46     exit(1);
47   }
48   start_kticks.LowPart = start_ktime.dwLowDateTime;
49   start_kticks.HighPart = start_ktime.dwHighDateTime;
50   start_uticks.LowPart = start_utime.dwLowDateTime;
51   start_uticks.HighPart = start_utime.dwHighDateTime;
52 #else
53   if (getrusage(RUSAGE_SELF, &start_rusage) == -1) {
54     fprintf(stderr, "getrusage() fails at start\n");
55     exit(1);
56   }
57 #endif
58   for (int i = 0; i < 500; i++)
59   {
60     if (pcap_findalldevs(&alldevs, errbuf) == -1)
61     {
62       fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
63       exit(1);
64     }
65     pcap_freealldevs(alldevs);
66   }
67 
68 #ifdef _WIN32
69   if (!GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2,
70                        &end_ktime, &end_utime))
71   {
72     fprintf(stderr, "GetProcessTimes() fails at end\n");
73     exit(1);
74   }
75   end_kticks.LowPart = end_ktime.dwLowDateTime;
76   end_kticks.HighPart = end_ktime.dwHighDateTime;
77   end_uticks.LowPart = end_utime.dwLowDateTime;
78   end_uticks.HighPart = end_utime.dwHighDateTime;
79   ktime = end_kticks.QuadPart - start_kticks.QuadPart;
80   utime = end_uticks.QuadPart - start_uticks.QuadPart;
81   tottime = ktime + utime;
82   printf("Total CPU secs: kernel %g, user %g, total %g\n",
83       ((double)ktime) / 10000000.0,
84       ((double)utime) / 10000000.0,
85       ((double)tottime) / 10000000.0);
86 #else
87   if (getrusage(RUSAGE_SELF, &end_rusage) == -1) {
88     fprintf(stderr, "getrusage() fails at end\n");
89     exit(1);
90   }
91   timersub(&end_rusage.ru_stime, &start_rusage.ru_stime, &ktime);
92   timersub(&end_rusage.ru_utime, &start_rusage.ru_utime, &utime);
93   timeradd(&ktime, &utime, &tottime);
94   printf("Total CPU secs: kernel %g, user %g, total %g\n",
95       (double)ktime.tv_sec + ((double)ktime.tv_usec / 1000000.0),
96       (double)utime.tv_sec + ((double)utime.tv_usec / 1000000.0),
97       (double)tottime.tv_sec + ((double)tottime.tv_usec / 1000000.0));
98 #endif
99   exit(exit_status);
100 }
101