we can leave out subscribe/unsubscribe, since this has no relevance in pratice yet...
[ana-net.git] / opt / pflana-pps.c
blob04c0ce84f7f24de6d6f68d58824ba49cae8d2e24
1 /* Compile with -lrt */
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 #include <signal.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <assert.h>
11 #include <sys/socket.h>
12 #include <sys/time.h>
13 #include <time.h>
15 #define AF_LANA 27
17 static sig_atomic_t sigint = 0;
19 static inline double timespec_to_double(const struct timespec *time)
21 return time->tv_nsec * 1E-9 + (double) time->tv_sec;
24 static void sig_handler(int sig)
26 if (sig == SIGINT)
27 sigint = 1;
30 int main(void)
32 int sock, ret;
33 char buff[1600];
34 struct timespec before, after;
35 unsigned long long pkts = 0, byte = 0;
36 double x1, x2, elapsed;
38 if (geteuid() != 0) {
39 fprintf(stderr, "Not root?!\n");
40 exit(EXIT_FAILURE);
43 signal(SIGINT, sig_handler);
45 sock = socket(AF_LANA, SOCK_RAW, 0);
46 if (sock < 0) {
47 perror("socket");
48 return 0;
51 printf("Hit key to start!\n");
52 getchar();
53 printf("Abort with ^C\n");
55 memset(&before, 0, sizeof(before));
56 memset(&after, 0, sizeof(after));
58 clock_gettime(CLOCK_REALTIME, &before);
59 while (!sigint) {
60 ret = recv(sock, buff, sizeof(buff), 0);
61 if (ret < 0) {
62 perror("recvmsg");
63 continue;
64 } else {
65 pkts++;
66 byte += ret;
69 clock_gettime(CLOCK_REALTIME, &after);
71 x1 = timespec_to_double(&after);
72 x2 = timespec_to_double(&before);
73 elapsed = x1 - x2;
75 printf("\n\n");
76 fflush(stdout);
77 printf("time: %lf, pkts/s: %.2lf, bytes/s: %.2lf\n",
78 elapsed, 1.0 * pkts / elapsed, 1.0 * byte / elapsed);
80 close(sock);
81 return 0;