convert manual.html to docbook
[l2tpns.git] / test / bounce.c
1 #include <arpa/inet.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <linux/if.h>
5 #include <netdb.h>
6 #include <netinet/in.h>
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <linux/ip.h>
14 #include <linux/udp.h>
15 #include <pthread.h>
16 #include <unistd.h>
17 #include <signal.h>
18 #include <getopt.h>
19
20 #define PORT 39000
21
22 void sigalarm(int junk);
23 unsigned long long recv_count = 0;
24 unsigned long pps = 0;
25 unsigned long bytes = 0;
26 unsigned long dropped = 0, seq = 0;
27 unsigned port = PORT;
28
29 int main(int argc, char *argv[])
30 {
31 int on = 1;
32 struct sockaddr_in addr;
33 int s;
34 char *packet;
35
36 while ((s = getopt(argc, argv, "?p:")) > 0)
37 {
38 switch (s)
39 {
40 case 'p' :
41 port = atoi(optarg);
42 break;
43 case '?' :
44 printf("Options:\n");
45 printf("\t-p port to listen on\n");
46 return(0);
47 break;
48 }
49 }
50
51 memset(&addr, 0, sizeof(addr));
52 addr.sin_family = AF_INET;
53 addr.sin_port = htons(port);
54
55 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
56 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
57 if (bind(s, (void *) &addr, sizeof(addr)) < 0)
58 {
59 perror("bind");
60 return -1;
61 }
62
63 signal(SIGALRM, sigalarm);
64 alarm(1);
65
66 printf("Waiting on port %d\n", port);
67 packet = (char *)malloc(65535);
68 while (1)
69 {
70 struct sockaddr_in addr;
71 int alen = sizeof(addr), l;
72 unsigned int iseq;
73
74 l = recvfrom(s, packet, 65535, 0, (void *) &addr, &alen);
75 if (l < 0) continue;
76 recv_count++;
77 pps++;
78 bytes += l;
79 iseq = *((unsigned int *) packet);
80 if (seq != iseq)
81 dropped += (iseq - seq);
82 seq = iseq + 1;
83
84 sendto(s, packet, l, 0, (struct sockaddr *)&addr, alen);
85 }
86
87 free(packet);
88 }
89
90 void sigalarm(int junk)
91 {
92 printf("Recv: %10llu %0.1fMbits/s (%lu pps) (%5ld dropped)\n", recv_count, (bytes / 1024.0 / 1024.0 * 8), pps, dropped);
93 pps = bytes = 0;
94 alarm(1);
95 }
96