xref: /freebsd/contrib/blocklist/test/srvtest.c (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1*5f4c09ddSEd Maste /*	$NetBSD: srvtest.c,v 1.10 2015/05/30 22:40:38 christos Exp $	*/
2*5f4c09ddSEd Maste 
3*5f4c09ddSEd Maste /*-
4*5f4c09ddSEd Maste  * Copyright (c) 2015 The NetBSD Foundation, Inc.
5*5f4c09ddSEd Maste  * All rights reserved.
6*5f4c09ddSEd Maste  *
7*5f4c09ddSEd Maste  * This code is derived from software contributed to The NetBSD Foundation
8*5f4c09ddSEd Maste  * by Christos Zoulas.
9*5f4c09ddSEd Maste  *
10*5f4c09ddSEd Maste  * Redistribution and use in source and binary forms, with or without
11*5f4c09ddSEd Maste  * modification, are permitted provided that the following conditions
12*5f4c09ddSEd Maste  * are met:
13*5f4c09ddSEd Maste  * 1. Redistributions of source code must retain the above copyright
14*5f4c09ddSEd Maste  *    notice, this list of conditions and the following disclaimer.
15*5f4c09ddSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
16*5f4c09ddSEd Maste  *    notice, this list of conditions and the following disclaimer in the
17*5f4c09ddSEd Maste  *    documentation and/or other materials provided with the distribution.
18*5f4c09ddSEd Maste  *
19*5f4c09ddSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*5f4c09ddSEd Maste  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*5f4c09ddSEd Maste  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*5f4c09ddSEd Maste  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*5f4c09ddSEd Maste  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*5f4c09ddSEd Maste  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*5f4c09ddSEd Maste  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*5f4c09ddSEd Maste  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*5f4c09ddSEd Maste  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*5f4c09ddSEd Maste  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*5f4c09ddSEd Maste  * POSSIBILITY OF SUCH DAMAGE.
30*5f4c09ddSEd Maste  */
31*5f4c09ddSEd Maste #ifdef HAVE_CONFIG_H
32*5f4c09ddSEd Maste #include "config.h"
33*5f4c09ddSEd Maste #endif
34*5f4c09ddSEd Maste 
35*5f4c09ddSEd Maste #include <sys/cdefs.h>
36*5f4c09ddSEd Maste __RCSID("$NetBSD: srvtest.c,v 1.10 2015/05/30 22:40:38 christos Exp $");
37*5f4c09ddSEd Maste 
38*5f4c09ddSEd Maste #include <sys/types.h>
39*5f4c09ddSEd Maste #include <sys/socket.h>
40*5f4c09ddSEd Maste #include <netinet/in.h>
41*5f4c09ddSEd Maste 
42*5f4c09ddSEd Maste #include <stdio.h>
43*5f4c09ddSEd Maste #include <signal.h>
44*5f4c09ddSEd Maste #include <string.h>
45*5f4c09ddSEd Maste #include <syslog.h>
46*5f4c09ddSEd Maste #include <unistd.h>
47*5f4c09ddSEd Maste #include <stdlib.h>
48*5f4c09ddSEd Maste #include <poll.h>
49*5f4c09ddSEd Maste #include <err.h>
50*5f4c09ddSEd Maste 
51*5f4c09ddSEd Maste #include "blacklist.h"
52*5f4c09ddSEd Maste #ifdef BLDEBUG
53*5f4c09ddSEd Maste #include "bl.h"
54*5f4c09ddSEd Maste static void *b;
55*5f4c09ddSEd Maste #endif
56*5f4c09ddSEd Maste 
57*5f4c09ddSEd Maste #ifndef INFTIM
58*5f4c09ddSEd Maste #define INFTIM -1
59*5f4c09ddSEd Maste #endif
60*5f4c09ddSEd Maste 
61*5f4c09ddSEd Maste static void
process_tcp(int afd)62*5f4c09ddSEd Maste process_tcp(int afd)
63*5f4c09ddSEd Maste {
64*5f4c09ddSEd Maste 	ssize_t n;
65*5f4c09ddSEd Maste 	char buffer[256];
66*5f4c09ddSEd Maste 
67*5f4c09ddSEd Maste 	memset(buffer, 0, sizeof(buffer));
68*5f4c09ddSEd Maste 
69*5f4c09ddSEd Maste 	if ((n = read(afd, buffer, sizeof(buffer))) == -1)
70*5f4c09ddSEd Maste 		err(1, "read");
71*5f4c09ddSEd Maste 	buffer[sizeof(buffer) - 1] = '\0';
72*5f4c09ddSEd Maste 	printf("%s: sending %d %s\n", getprogname(), afd, buffer);
73*5f4c09ddSEd Maste #ifdef BLDEBUG
74*5f4c09ddSEd Maste 	blacklist_r(b, 1, afd, buffer);
75*5f4c09ddSEd Maste #else
76*5f4c09ddSEd Maste 	blacklist(1, afd, buffer);
77*5f4c09ddSEd Maste #endif
78*5f4c09ddSEd Maste 	exit(0);
79*5f4c09ddSEd Maste }
80*5f4c09ddSEd Maste 
81*5f4c09ddSEd Maste static void
process_udp(int afd)82*5f4c09ddSEd Maste process_udp(int afd)
83*5f4c09ddSEd Maste {
84*5f4c09ddSEd Maste 	ssize_t n;
85*5f4c09ddSEd Maste 	char buffer[256];
86*5f4c09ddSEd Maste 	struct sockaddr_storage ss;
87*5f4c09ddSEd Maste 	socklen_t slen;
88*5f4c09ddSEd Maste 
89*5f4c09ddSEd Maste 	memset(buffer, 0, sizeof(buffer));
90*5f4c09ddSEd Maste 
91*5f4c09ddSEd Maste 	slen = (socklen_t)sizeof(ss);
92*5f4c09ddSEd Maste 	memset(&ss, 0, sizeof(ss));
93*5f4c09ddSEd Maste 	if ((n = recvfrom(afd, buffer, sizeof(buffer), 0, (void *)&ss,
94*5f4c09ddSEd Maste 		&slen)) == -1)
95*5f4c09ddSEd Maste 		err(1, "recvfrom");
96*5f4c09ddSEd Maste 	buffer[sizeof(buffer) - 1] = '\0';
97*5f4c09ddSEd Maste 	printf("%s: sending %d %s\n", getprogname(), afd, buffer);
98*5f4c09ddSEd Maste 	blacklist_sa(1, afd, (void *)&ss, slen, buffer);
99*5f4c09ddSEd Maste 	exit(0);
100*5f4c09ddSEd Maste }
101*5f4c09ddSEd Maste static int
cr(int af,int type,in_port_t p)102*5f4c09ddSEd Maste cr(int af, int type, in_port_t p)
103*5f4c09ddSEd Maste {
104*5f4c09ddSEd Maste 	int sfd;
105*5f4c09ddSEd Maste 	struct sockaddr_storage ss;
106*5f4c09ddSEd Maste 	socklen_t slen;
107*5f4c09ddSEd Maste 	sfd = socket(af == AF_INET ? PF_INET : PF_INET6, type, 0);
108*5f4c09ddSEd Maste 	if (sfd == -1)
109*5f4c09ddSEd Maste 		err(1, "socket");
110*5f4c09ddSEd Maste 
111*5f4c09ddSEd Maste 	p = htons(p);
112*5f4c09ddSEd Maste 	memset(&ss, 0, sizeof(ss));
113*5f4c09ddSEd Maste 	if (af == AF_INET) {
114*5f4c09ddSEd Maste 		struct sockaddr_in *s = (void *)&ss;
115*5f4c09ddSEd Maste 		s->sin_family = AF_INET;
116*5f4c09ddSEd Maste 		slen = sizeof(*s);
117*5f4c09ddSEd Maste 		s->sin_port = p;
118*5f4c09ddSEd Maste 	} else {
119*5f4c09ddSEd Maste 		struct sockaddr_in6 *s6 = (void *)&ss;
120*5f4c09ddSEd Maste 		s6->sin6_family = AF_INET6;
121*5f4c09ddSEd Maste 		slen = sizeof(*s6);
122*5f4c09ddSEd Maste 		s6->sin6_port = p;
123*5f4c09ddSEd Maste 	}
124*5f4c09ddSEd Maste #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
125*5f4c09ddSEd Maste 	ss.ss_len = (uint8_t)slen;
126*5f4c09ddSEd Maste #endif
127*5f4c09ddSEd Maste 
128*5f4c09ddSEd Maste 	if (bind(sfd, (const void *)&ss, slen) == -1)
129*5f4c09ddSEd Maste 		err(1, "bind");
130*5f4c09ddSEd Maste 
131*5f4c09ddSEd Maste 	if (type != SOCK_DGRAM)
132*5f4c09ddSEd Maste 		if (listen(sfd, 5) == -1)
133*5f4c09ddSEd Maste 			err(1, "listen");
134*5f4c09ddSEd Maste 	return sfd;
135*5f4c09ddSEd Maste }
136*5f4c09ddSEd Maste 
137*5f4c09ddSEd Maste static void
handle(int type,int sfd)138*5f4c09ddSEd Maste handle(int type, int sfd)
139*5f4c09ddSEd Maste {
140*5f4c09ddSEd Maste 	struct sockaddr_storage ss;
141*5f4c09ddSEd Maste 	socklen_t alen = sizeof(ss);
142*5f4c09ddSEd Maste 	int afd;
143*5f4c09ddSEd Maste 
144*5f4c09ddSEd Maste 	if (type != SOCK_DGRAM) {
145*5f4c09ddSEd Maste 		if ((afd = accept(sfd, (void *)&ss, &alen)) == -1)
146*5f4c09ddSEd Maste 			err(1, "accept");
147*5f4c09ddSEd Maste 	} else
148*5f4c09ddSEd Maste 		afd = sfd;
149*5f4c09ddSEd Maste 
150*5f4c09ddSEd Maste 	/* Create child process */
151*5f4c09ddSEd Maste 	switch (fork()) {
152*5f4c09ddSEd Maste 	case -1:
153*5f4c09ddSEd Maste 		err(1, "fork");
154*5f4c09ddSEd Maste 	case 0:
155*5f4c09ddSEd Maste 		if (type == SOCK_DGRAM)
156*5f4c09ddSEd Maste 			process_udp(afd);
157*5f4c09ddSEd Maste 		else
158*5f4c09ddSEd Maste 			process_tcp(afd);
159*5f4c09ddSEd Maste 		break;
160*5f4c09ddSEd Maste 	default:
161*5f4c09ddSEd Maste 		close(afd);
162*5f4c09ddSEd Maste 		break;
163*5f4c09ddSEd Maste 	}
164*5f4c09ddSEd Maste }
165*5f4c09ddSEd Maste 
166*5f4c09ddSEd Maste static __dead void
usage(int c)167*5f4c09ddSEd Maste usage(int c)
168*5f4c09ddSEd Maste {
169*5f4c09ddSEd Maste 	warnx("Unknown option `%c'", (char)c);
170*5f4c09ddSEd Maste 	fprintf(stderr, "Usage: %s [-u] [-p <num>]\n", getprogname());
171*5f4c09ddSEd Maste 	exit(EXIT_FAILURE);
172*5f4c09ddSEd Maste }
173*5f4c09ddSEd Maste 
174*5f4c09ddSEd Maste int
main(int argc,char * argv[])175*5f4c09ddSEd Maste main(int argc, char *argv[])
176*5f4c09ddSEd Maste {
177*5f4c09ddSEd Maste #ifdef __linux__
178*5f4c09ddSEd Maste #define NUMFD 1
179*5f4c09ddSEd Maste #else
180*5f4c09ddSEd Maste #define NUMFD 2
181*5f4c09ddSEd Maste #endif
182*5f4c09ddSEd Maste 	struct pollfd pfd[NUMFD];
183*5f4c09ddSEd Maste 	int type = SOCK_STREAM, c;
184*5f4c09ddSEd Maste 	in_port_t port = 6161;
185*5f4c09ddSEd Maste 
186*5f4c09ddSEd Maste 	signal(SIGCHLD, SIG_IGN);
187*5f4c09ddSEd Maste 
188*5f4c09ddSEd Maste #ifdef BLDEBUG
189*5f4c09ddSEd Maste 	b = bl_create(false, "blsock", vsyslog);
190*5f4c09ddSEd Maste #endif
191*5f4c09ddSEd Maste 
192*5f4c09ddSEd Maste 	while ((c = getopt(argc, argv, "up:")) != -1)
193*5f4c09ddSEd Maste 		switch (c) {
194*5f4c09ddSEd Maste 		case 'u':
195*5f4c09ddSEd Maste 			type = SOCK_DGRAM;
196*5f4c09ddSEd Maste 			break;
197*5f4c09ddSEd Maste 		case 'p':
198*5f4c09ddSEd Maste 			port = (in_port_t)atoi(optarg);
199*5f4c09ddSEd Maste 			break;
200*5f4c09ddSEd Maste 		default:
201*5f4c09ddSEd Maste 			usage(c);
202*5f4c09ddSEd Maste 		}
203*5f4c09ddSEd Maste 
204*5f4c09ddSEd Maste 	pfd[0].fd = cr(AF_INET, type, port);
205*5f4c09ddSEd Maste 	pfd[0].events = POLLIN;
206*5f4c09ddSEd Maste #if NUMFD > 1
207*5f4c09ddSEd Maste 	pfd[1].fd = cr(AF_INET6, type, port);
208*5f4c09ddSEd Maste 	pfd[1].events = POLLIN;
209*5f4c09ddSEd Maste #endif
210*5f4c09ddSEd Maste 
211*5f4c09ddSEd Maste 	for (;;) {
212*5f4c09ddSEd Maste 		if (poll(pfd, __arraycount(pfd), INFTIM) == -1)
213*5f4c09ddSEd Maste 			err(1, "poll");
214*5f4c09ddSEd Maste 		for (size_t i = 0; i < __arraycount(pfd); i++) {
215*5f4c09ddSEd Maste 			if ((pfd[i].revents & POLLIN) == 0)
216*5f4c09ddSEd Maste 				continue;
217*5f4c09ddSEd Maste 			handle(type, pfd[i].fd);
218*5f4c09ddSEd Maste 		}
219*5f4c09ddSEd Maste 	}
220*5f4c09ddSEd Maste }
221