2 * Copyright (c) 2012 Cisco Systems, Inc.
5 * This software was developed by Bjoern Zeeb under contract to
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/socket.h>
33 #include <sys/types.h>
35 #include <arpa/inet.h>
37 #include <netinet/in.h>
49 static char *testcase;
52 static u_int fib = -1;
53 static u_int reflectfib = -1;
54 static uint16_t port = 6666;
59 reflect_conn(int s, char *buf, size_t buflen, ssize_t l, struct sockaddr *sa,
65 err(EX_OSERR, "read()");
67 errx(EX_NOINPUT, "EOF");
68 if ((size_t)l > (buflen - 1))
69 errx(EX_DATAERR, "Input too long");
70 /* Nuke the \n from echo | netcat. */
74 * Match three cases: (1) START, (2) DONE, (3) anything else.
75 * For anything but START and DONE we just reflect everything.
78 * We expected a "START testcase" on first connect. Otherwise it means
79 * that we are out of sync. Exit to not produce weird results.
81 if (accepts == 0 && nostart == 0) {
82 if (strncmp(buf, "START ", 6) != 0)
83 errx(EX_PROTOCOL, "Not received START on first "
86 errx(EX_PROTOCOL, "START without test case name: %s",
88 if (strcmp(buf+6, testcase) != 0)
89 errx(EX_PROTOCOL, "START test case does not match "
90 "'%s': '%s'", testcase, buf+6);
92 /* If debug is on, log. */
94 fprintf(stderr, "<< %s: %s\n", testcase, buf);
96 if (reflectfib != (u_int)-1)
97 l = snprintf(buf, buflen, "FIB %u\n", reflectfib);
99 /* If debug is on, log. */
102 fprintf(stderr, ">> %s: %s\n", testcase, buf);
105 /* Reflect data with \n again. */
109 m = sendto(s, buf, l, 0, sa, salen);
111 m = write(s, buf, l);
112 /* XXX This is simplified handling. */
113 if (m == -1 && sa != NULL && errno == EHOSTUNREACH)
114 warn("ignored expected: sendto(%s, %zd)", buf, l);
115 else if (m == -1 && (sa == NULL || errno != EHOSTUNREACH))
116 err(EX_OSERR, "write(%s, %zd)", buf, l);
118 err(EX_OSERR, "short write(%s, %zd) %zd", buf, l, m);
123 /* See if we got an end signal. */
124 if (strncmp(buf, "DONE", 4) == 0)
130 reflect_tcp6_conn(int as)
136 s = accept(as, NULL, NULL);
138 err(EX_OSERR, "accept()");
140 l = read(s, buf, sizeof(buf));
141 error = reflect_conn(s, buf, sizeof(buf), l, NULL, 0);
148 reflect_udp6_conn(int s)
151 struct sockaddr_in6 from;
156 fromlen = sizeof(from);
157 l = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&from,
161 rc = connect(s, (struct sockaddr *)&from, fromlen);
163 if (inet_ntop(PF_INET6, &from, buf, sizeof(buf)) == NULL)
165 err(EX_OSERR, "connect(%d, %s, %u)", s, buf, fromlen);
169 error = reflect_conn(s, buf, sizeof(buf), l, (struct sockaddr *)&from,
173 /* Undo the connect binding again. */
174 fromlen = sizeof(from);
175 bzero(&from, fromlen);
176 from.sin6_len = fromlen;
177 from.sin6_family = AF_INET6;
178 from.sin6_port = htons(port); /* This only gives us a ::1:port ::1:port binding */
179 rc = connect(s, (struct sockaddr *)&from, fromlen);
181 if (inet_ntop(PF_INET6, &from.sin6_addr, buf,
182 sizeof(buf)) == NULL)
184 err(EX_OSERR, "un-connect(%d, %s, %u)", s, buf, fromlen);
193 reflect_6(int domain, int type)
195 struct sockaddr_in6 sin6;
199 /* Get us a listen socket. */
200 s = socket(domain, type, 0);
202 err(EX_OSERR, "socket()");
205 * In case a FIB was given on cmd line, set it. Let the kernel do the
208 if (fib != (u_int)-1) {
209 rc = setsockopt(s, SOL_SOCKET, SO_SETFIB, &fib, sizeof(fib));
211 err(EX_OSERR, "setsockopt(SO_SETFIB)");
214 /* Allow re-use. Otherwise restarting for the next test might error. */
216 rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
218 err(EX_OSERR, "setsockopt(SO_REUSEADDR)");
220 rc = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &i, sizeof(i));
222 err(EX_OSERR, "setsockopt(SO_REUSEPORT)");
224 /* Bind address and port or just port. */
225 sin6.sin6_len = sizeof(sin6);
226 sin6.sin6_family = AF_INET6;
227 sin6.sin6_port = htons(port);
228 sin6.sin6_flowinfo = 0;
229 bzero(&sin6.sin6_addr, sizeof(sin6.sin6_addr));
231 rc = inet_pton(PF_INET6, addr, &sin6.sin6_addr);
233 errx(EX_USAGE, "inet_pton()");
235 err(EX_OSERR, "inet_pton()");
237 errx(EX_SOFTWARE, "inet_pton()");
239 sin6.sin6_scope_id = 0;
240 rc = bind(s, (struct sockaddr *)&sin6, sizeof(sin6));
242 err(EX_OSERR, "bind(%d)", s);
244 if (type == SOCK_STREAM) {
245 rc = listen(s, port);
247 err(EX_OSERR, "listen(%d, %u)", s, port);
251 * We shall never do more than one connection in parallel so can keep
257 rc = select(s + 1, &rset, NULL, NULL, NULL);
258 if (rc == -1 && errno != EINTR)
259 err(EX_OSERR, "select()");
261 if (rc == 0 || errno == EINTR)
265 errx(EX_OSERR, "select() miscounted 1 to %d", rc);
266 if (!FD_ISSET(s, &rset))
267 errx(EX_OSERR, "select() did not return our socket");
269 if (type == SOCK_STREAM)
270 rc = reflect_tcp6_conn(s);
271 else if (type == SOCK_DGRAM)
272 rc = reflect_udp6_conn(s);
274 errx(EX_SOFTWARE, "Unsupported socket type %d", type);
276 /* Turn end flagging into no error. */
280 /* Close listen socket. */
290 return (reflect_6(PF_INET6, SOCK_STREAM));
297 return (reflect_6(PF_INET6, SOCK_DGRAM));
301 main(int argc, char *argv[])
304 char *dummy, *afname;
308 while ((ch = getopt(argc, argv, "A:dF:f:Np:t:T:")) != -1) {
317 l = strtoll(optarg, &dummy, 10);
318 if (*dummy != '\0' || l < 0)
319 errx(EX_USAGE, "Invalid FIB number");
323 l = strtoll(optarg, &dummy, 10);
324 if (*dummy != '\0' || l < 0)
325 errx(EX_USAGE, "Invalid FIB number");
326 reflectfib = (u_int)l;
332 l = strtoll(optarg, &dummy, 10);
333 if (*dummy != '\0' || l < 0)
334 errx(EX_USAGE, "Invalid port number");
345 errx(EX_USAGE, "Unknown command line option at '%c'",
351 if (testcase == NULL)
352 errx(EX_USAGE, "Mandatory option -t <testcase> not given");
354 errx(EX_USAGE, "Mandatory option -T <afname> not given");
356 if (strcmp(afname, "TCP6") == 0)
358 else if (strcmp(afname, "UDP6") == 0)
361 errx(EX_USAGE, "Mandatory option -T %s not a valid option",