# HG changeset patch # User Mychaela Falconia # Date 1710037657 0 # Node ID c00510e1ae8bf495122dcd4cfc44008a9ae2faf2 # Parent d180987db615e85f3f4b209087f8051c1be14ab7 new program udp-test-sink diff -r d180987db615 -r c00510e1ae8b .hgignore --- a/.hgignore Thu Mar 07 22:39:41 2024 +0000 +++ b/.hgignore Sun Mar 10 02:27:37 2024 +0000 @@ -10,3 +10,4 @@ ^rtp-stream-dump$ ^rtp-stream-gen$ ^rtp-tfo-trace$ +^udp-test-sink$ diff -r d180987db615 -r c00510e1ae8b Makefile --- a/Makefile Thu Mar 07 22:39:41 2024 +0000 +++ b/Makefile Sun Mar 10 02:27:37 2024 +0000 @@ -1,7 +1,8 @@ CC= gcc CFLAGS= -O2 PROGS= rtp-cont-check rtp-g711-extr rtp-gsmfr-dump rtp-gsmfr-extr \ - rtp-jitter-view rtp-stream-dump rtp-stream-gen rtp-tfo-trace + rtp-jitter-view rtp-stream-dump rtp-stream-gen rtp-tfo-trace \ + udp-test-sink INSTBIN=/opt/freecalypso/bin all: ${PROGS} @@ -30,6 +31,9 @@ rtp-tfo-trace: rtp-tfo-trace.c ${CC} ${CFLAGS} -o $@ $@.c -lpcap +udp-test-sink: udp-test-sink.c + ${CC} ${CFLAGS} -o $@ $@.c + install: mkdir -p ${INSTBIN} install -c ${PROGS} ${INSTBIN} diff -r d180987db615 -r c00510e1ae8b udp-test-sink.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/udp-test-sink.c Sun Mar 10 02:27:37 2024 +0000 @@ -0,0 +1,96 @@ +/* + * This program is a simple sink for UDP: it binds to a UDP port and sinks + * (reads and discards) all packets that arrive at it. Upon receiving a + * burst or stream of packets followed by a prolonged pause, it prints + * the number of packets that were received. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void +parse_ip_port(arg, sin) + char *arg; + struct sockaddr_in *sin; +{ + char *cp; + int rc; + + cp = index(arg, ':'); + if (!cp) { + fprintf(stderr, "error: missing ':' in IP:port argument\n"); + exit(1); + } + *cp++ = '\0'; + sin->sin_family = AF_INET; + rc = inet_aton(arg, &sin->sin_addr); + if (!rc) { + fprintf(stderr, "error: invalid IP address argument\n"); + exit(1); + } + sin->sin_port = htons(atoi(cp)); +} + +main(argc, argv) + char **argv; +{ + struct sockaddr_in bindsin; + int udp_fd, rc; + unsigned idle_sec, rx_count; + fd_set fds; + struct timeval tv; + u_char dummybuf[256]; + + if (argc < 2 || argc > 3) { + fprintf(stderr, "usage: bind-IP:port [idle-sec]\n", argv[0]); + exit(1); + } + parse_ip_port(argv[1], &bindsin); + if (argc >= 3) + idle_sec = atoi(argv[2]); + else + idle_sec = 0; + udp_fd = socket(AF_INET, SOCK_DGRAM, 0); + if (udp_fd < 0) { + perror("socket(AF_INET, SOCK_DGRAM, 0)"); + exit(1); + } + rc = bind(udp_fd, (struct sockaddr *) &bindsin, sizeof bindsin); + if (rc < 0) { + perror("bind"); + exit(1); + } + for (rx_count = 0; ; ) { + FD_ZERO(&fds); + FD_SET(udp_fd, &fds); + if (rx_count && idle_sec) { + tv.tv_sec = idle_sec; + tv.tv_usec = 0; + rc = select(udp_fd+1, &fds, 0, 0, &tv); + } else + rc = select(udp_fd+1, &fds, 0, 0, 0); + if (rc < 0) { + if (errno == EINTR) + continue; + perror("select"); + exit(1); + } + if (FD_ISSET(udp_fd, &fds)) { + recv(udp_fd, dummybuf, sizeof dummybuf, 0); + rx_count++; + } else { + printf("Received %u packet%s\n", rx_count, + rx_count != 1 ? "s" : ""); + rx_count = 0; + } + } +}