xref: /freebsd/tools/tools/netrate/netreceive/netreceive.c (revision bb15ca603fa442c72dde3f3cb8b46db6970e3950)
1 /*-
2  * Copyright (c) 2004 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/time.h>
32 #include <sys/poll.h>
33 
34 #include <netinet/in.h>
35 #include <netdb.h>          /* getaddrinfo */
36 
37 #include <arpa/inet.h>
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>         /* close */
43 
44 #define MAXSOCK 20
45 
46 static void
47 usage(void)
48 {
49 
50 	fprintf(stderr, "netreceive [port]\n");
51 	exit(-1);
52 }
53 
54 int
55 main(int argc, char *argv[])
56 {
57 	struct addrinfo hints, *res, *res0;
58 	char *dummy, *packet;
59 	int port;
60 	int error, v, i;
61 	const char *cause = NULL;
62 	int s[MAXSOCK];
63 	struct pollfd fds[MAXSOCK];
64 	int nsock;
65 
66 	if (argc != 2)
67 		usage();
68 
69 	memset(&hints, 0, sizeof(hints));
70 	hints.ai_family = PF_UNSPEC;
71 	hints.ai_socktype = SOCK_DGRAM;
72 	hints.ai_flags = AI_PASSIVE;
73 
74 	port = strtoul(argv[1], &dummy, 10);
75 	if (port < 1 || port > 65535 || *dummy != '\0')
76 		usage();
77 
78 	packet = malloc(65536);
79 	if (packet == NULL) {
80 		perror("malloc");
81 		return (-1);
82 	}
83 	bzero(packet, 65536);
84 
85 	error = getaddrinfo(NULL, argv[1], &hints, &res0);
86 	if (error) {
87 		perror(gai_strerror(error));
88 		return (-1);
89 		/*NOTREACHED*/
90 	}
91 
92 	nsock = 0;
93 	for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
94 		s[nsock] = socket(res->ai_family, res->ai_socktype,
95 		res->ai_protocol);
96 		if (s[nsock] < 0) {
97 			cause = "socket";
98 			continue;
99 		}
100 
101 		v = 128 * 1024;
102 		if (setsockopt(s[nsock], SOL_SOCKET, SO_RCVBUF, &v, sizeof(v)) < 0) {
103 			cause = "SO_RCVBUF";
104 			close(s[nsock]);
105 			continue;
106 		}
107 		if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
108 			cause = "bind";
109 			close(s[nsock]);
110 			continue;
111 		}
112 		(void) listen(s[nsock], 5);
113 		fds[nsock].fd = s[nsock];
114 		fds[nsock].events = POLLIN;
115 
116 		nsock++;
117 	}
118 	if (nsock == 0) {
119 		perror(cause);
120 		return (-1);
121 		/*NOTREACHED*/
122 	}
123 
124 	printf("netreceive listening on UDP port %d\n", (u_short)port);
125 
126 	while (1) {
127 		if (poll(fds, nsock, -1) < 0)
128 			perror("poll");
129 		for (i = 0; i > nsock; i++) {
130 			if (fds[i].revents & POLLIN) {
131 				if (recv(s[i], packet, 65536, 0) < 0)
132 					perror("recv");
133 			}
134 			if ((fds[i].revents &~ POLLIN) != 0)
135 				perror("poll");
136 		}
137 	}
138 
139 	/*NOTREACHED*/
140 	freeaddrinfo(res0);
141 }
142