1 /* $NetBSD: srvtest.c,v 1.10 2015/05/30 22:40:38 christos Exp $ */
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: srvtest.c,v 1.10 2015/05/30 22:40:38 christos Exp $");
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
51 #include "blacklist.h"
67 memset(buffer, 0, sizeof(buffer));
69 if ((n = read(afd, buffer, sizeof(buffer))) == -1)
71 buffer[sizeof(buffer) - 1] = '\0';
72 printf("%s: sending %d %s\n", getprogname(), afd, buffer);
74 blacklist_r(b, 1, afd, buffer);
76 blacklist(1, afd, buffer);
86 struct sockaddr_storage ss;
89 memset(buffer, 0, sizeof(buffer));
91 slen = (socklen_t)sizeof(ss);
92 memset(&ss, 0, sizeof(ss));
93 if ((n = recvfrom(afd, buffer, sizeof(buffer), 0, (void *)&ss,
96 buffer[sizeof(buffer) - 1] = '\0';
97 printf("%s: sending %d %s\n", getprogname(), afd, buffer);
98 blacklist_sa(1, afd, (void *)&ss, slen, buffer);
102 cr(int af, int type, in_port_t p)
105 struct sockaddr_storage ss;
107 sfd = socket(af == AF_INET ? PF_INET : PF_INET6, type, 0);
112 memset(&ss, 0, sizeof(ss));
114 struct sockaddr_in *s = (void *)&ss;
115 s->sin_family = AF_INET;
119 struct sockaddr_in6 *s6 = (void *)&ss;
120 s6->sin6_family = AF_INET6;
124 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
125 ss.ss_len = (uint8_t)slen;
128 if (bind(sfd, (const void *)&ss, slen) == -1)
131 if (type != SOCK_DGRAM)
132 if (listen(sfd, 5) == -1)
138 handle(int type, int sfd)
140 struct sockaddr_storage ss;
141 socklen_t alen = sizeof(ss);
144 if (type != SOCK_DGRAM) {
145 if ((afd = accept(sfd, (void *)&ss, &alen)) == -1)
150 /* Create child process */
155 if (type == SOCK_DGRAM)
169 warnx("Unknown option `%c'", (char)c);
170 fprintf(stderr, "Usage: %s [-u] [-p <num>]\n", getprogname());
175 main(int argc, char *argv[])
182 struct pollfd pfd[NUMFD];
183 int type = SOCK_STREAM, c;
184 in_port_t port = 6161;
186 signal(SIGCHLD, SIG_IGN);
189 b = bl_create(false, "blsock", vsyslog);
192 while ((c = getopt(argc, argv, "up:")) != -1)
198 port = (in_port_t)atoi(optarg);
204 pfd[0].fd = cr(AF_INET, type, port);
205 pfd[0].events = POLLIN;
207 pfd[1].fd = cr(AF_INET6, type, port);
208 pfd[1].events = POLLIN;
212 if (poll(pfd, __arraycount(pfd), INFTIM) == -1)
214 for (size_t i = 0; i < __arraycount(pfd); i++) {
215 if ((pfd[i].revents & POLLIN) == 0)
217 handle(type, pfd[i].fd);