annotate net-traffic/udp-test-sink.c @ 10:e686bc92c7d8

revamp for new subdir structure and configure script
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 15 May 2024 01:44:46 +0000
parents udp-test-sink.c@c00510e1ae8b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program is a simple sink for UDP: it binds to a UDP port and sinks
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * (reads and discards) all packets that arrive at it. Upon receiving a
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * burst or stream of packets followed by a prolonged pause, it prints
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * the number of packets that were received.
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/types.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <sys/socket.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <sys/time.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <sys/errno.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <netinet/in.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <arpa/inet.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <stdio.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <stdlib.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include <string.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 #include <strings.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 #include <unistd.h>
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 static void
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 parse_ip_port(arg, sin)
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 char *arg;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 struct sockaddr_in *sin;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 char *cp;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 int rc;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 cp = index(arg, ':');
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (!cp) {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 fprintf(stderr, "error: missing ':' in IP:port argument\n");
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 exit(1);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 }
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 *cp++ = '\0';
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 sin->sin_family = AF_INET;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 rc = inet_aton(arg, &sin->sin_addr);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (!rc) {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 fprintf(stderr, "error: invalid IP address argument\n");
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 exit(1);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 sin->sin_port = htons(atoi(cp));
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 main(argc, argv)
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 char **argv;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 struct sockaddr_in bindsin;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 int udp_fd, rc;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 unsigned idle_sec, rx_count;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 fd_set fds;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 struct timeval tv;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 u_char dummybuf[256];
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 if (argc < 2 || argc > 3) {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 fprintf(stderr, "usage: bind-IP:port [idle-sec]\n", argv[0]);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 exit(1);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 }
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 parse_ip_port(argv[1], &bindsin);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if (argc >= 3)
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 idle_sec = atoi(argv[2]);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 else
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 idle_sec = 0;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (udp_fd < 0) {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 perror("socket(AF_INET, SOCK_DGRAM, 0)");
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 exit(1);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 rc = bind(udp_fd, (struct sockaddr *) &bindsin, sizeof bindsin);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (rc < 0) {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 perror("bind");
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 exit(1);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 for (rx_count = 0; ; ) {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 FD_ZERO(&fds);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 FD_SET(udp_fd, &fds);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (rx_count && idle_sec) {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 tv.tv_sec = idle_sec;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 tv.tv_usec = 0;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 rc = select(udp_fd+1, &fds, 0, 0, &tv);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 } else
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 rc = select(udp_fd+1, &fds, 0, 0, 0);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (rc < 0) {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (errno == EINTR)
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 continue;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 perror("select");
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 exit(1);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (FD_ISSET(udp_fd, &fds)) {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 recv(udp_fd, dummybuf, sizeof dummybuf, 0);
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 rx_count++;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 } else {
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 printf("Received %u packet%s\n", rx_count,
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 rx_count != 1 ? "s" : "");
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 rx_count = 0;
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 }
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 }
c00510e1ae8b new program udp-test-sink
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 }