xref: /minix3/external/bsd/blacklist/test/cltest.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: cltest.c,v 1.6 2015/01/22 05:44:28 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2015 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*0a6a1f1dSLionel Sambuc  * by Christos Zoulas.
9*0a6a1f1dSLionel Sambuc  *
10*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
12*0a6a1f1dSLionel Sambuc  * are met:
13*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc  *
19*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0a6a1f1dSLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0a6a1f1dSLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0a6a1f1dSLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0a6a1f1dSLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0a6a1f1dSLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0a6a1f1dSLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0a6a1f1dSLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0a6a1f1dSLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0a6a1f1dSLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0a6a1f1dSLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc  */
31*0a6a1f1dSLionel Sambuc #ifdef HAVE_CONFIG_H
32*0a6a1f1dSLionel Sambuc #include "config.h"
33*0a6a1f1dSLionel Sambuc #endif
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
36*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: cltest.c,v 1.6 2015/01/22 05:44:28 christos Exp $");
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc #include <sys/types.h>
39*0a6a1f1dSLionel Sambuc #include <sys/socket.h>
40*0a6a1f1dSLionel Sambuc #include <netinet/in.h>
41*0a6a1f1dSLionel Sambuc #include <arpa/inet.h>
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc #include <stdio.h>
44*0a6a1f1dSLionel Sambuc #include <string.h>
45*0a6a1f1dSLionel Sambuc #include <unistd.h>
46*0a6a1f1dSLionel Sambuc #include <stdlib.h>
47*0a6a1f1dSLionel Sambuc #include <err.h>
48*0a6a1f1dSLionel Sambuc #ifdef HAVE_UTIL_H
49*0a6a1f1dSLionel Sambuc #include <util.h>
50*0a6a1f1dSLionel Sambuc #endif
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc static __dead void
usage(int c)53*0a6a1f1dSLionel Sambuc usage(int c)
54*0a6a1f1dSLionel Sambuc {
55*0a6a1f1dSLionel Sambuc 	warnx("Unknown option `%c'", (char)c);
56*0a6a1f1dSLionel Sambuc 	fprintf(stderr, "Usage: %s [-u] [-a <addr>] [-m <msg>] [-p <port>]\n",
57*0a6a1f1dSLionel Sambuc 	    getprogname());
58*0a6a1f1dSLionel Sambuc 	exit(EXIT_FAILURE);
59*0a6a1f1dSLionel Sambuc }
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc static void
getaddr(const char * a,in_port_t p,struct sockaddr_storage * ss,socklen_t * slen)62*0a6a1f1dSLionel Sambuc getaddr(const char *a, in_port_t p, struct sockaddr_storage *ss,
63*0a6a1f1dSLionel Sambuc     socklen_t *slen)
64*0a6a1f1dSLionel Sambuc {
65*0a6a1f1dSLionel Sambuc 	int c;
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc 	memset(ss, 0, sizeof(*ss));
68*0a6a1f1dSLionel Sambuc 	p = htons(p);
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc 	if (strchr(a, ':')) {
71*0a6a1f1dSLionel Sambuc 		struct sockaddr_in6 *s6 = (void *)ss;
72*0a6a1f1dSLionel Sambuc 		c = inet_pton(AF_INET6, a, &s6->sin6_addr);
73*0a6a1f1dSLionel Sambuc 		s6->sin6_family = AF_INET6;
74*0a6a1f1dSLionel Sambuc 		*slen = sizeof(*s6);
75*0a6a1f1dSLionel Sambuc 		s6->sin6_port = p;
76*0a6a1f1dSLionel Sambuc 	} else {
77*0a6a1f1dSLionel Sambuc 		struct sockaddr_in *s = (void *)ss;
78*0a6a1f1dSLionel Sambuc 		c = inet_pton(AF_INET, a, &s->sin_addr);
79*0a6a1f1dSLionel Sambuc 		s->sin_family = AF_INET;
80*0a6a1f1dSLionel Sambuc 		*slen = sizeof(*s);
81*0a6a1f1dSLionel Sambuc 		s->sin_port = p;
82*0a6a1f1dSLionel Sambuc 	}
83*0a6a1f1dSLionel Sambuc #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
84*0a6a1f1dSLionel Sambuc 	ss->ss_len = (uint8_t)*slen;
85*0a6a1f1dSLionel Sambuc #endif
86*0a6a1f1dSLionel Sambuc 	if (c == -1)
87*0a6a1f1dSLionel Sambuc 		err(EXIT_FAILURE, "Invalid address `%s'", a);
88*0a6a1f1dSLionel Sambuc }
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc int
main(int argc,char * argv[])91*0a6a1f1dSLionel Sambuc main(int argc, char *argv[])
92*0a6a1f1dSLionel Sambuc {
93*0a6a1f1dSLionel Sambuc 	int sfd;
94*0a6a1f1dSLionel Sambuc 	int c;
95*0a6a1f1dSLionel Sambuc 	struct sockaddr_storage ss;
96*0a6a1f1dSLionel Sambuc 	const char *msg = "hello";
97*0a6a1f1dSLionel Sambuc 	const char *addr = "127.0.0.1";
98*0a6a1f1dSLionel Sambuc 	int type = SOCK_STREAM;
99*0a6a1f1dSLionel Sambuc 	in_port_t port = 6161;
100*0a6a1f1dSLionel Sambuc 	socklen_t slen;
101*0a6a1f1dSLionel Sambuc 	char buf[128];
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc 	while ((c = getopt(argc, argv, "a:m:p:u")) != -1) {
104*0a6a1f1dSLionel Sambuc 		switch (c) {
105*0a6a1f1dSLionel Sambuc 		case 'a':
106*0a6a1f1dSLionel Sambuc 			addr = optarg;
107*0a6a1f1dSLionel Sambuc 			break;
108*0a6a1f1dSLionel Sambuc 		case 'm':
109*0a6a1f1dSLionel Sambuc 			msg = optarg;
110*0a6a1f1dSLionel Sambuc 			break;
111*0a6a1f1dSLionel Sambuc 		case 'p':
112*0a6a1f1dSLionel Sambuc 			port = (in_port_t)atoi(optarg);
113*0a6a1f1dSLionel Sambuc 			break;
114*0a6a1f1dSLionel Sambuc 		case 'u':
115*0a6a1f1dSLionel Sambuc 			type = SOCK_DGRAM;
116*0a6a1f1dSLionel Sambuc 			break;
117*0a6a1f1dSLionel Sambuc 		default:
118*0a6a1f1dSLionel Sambuc 			usage(c);
119*0a6a1f1dSLionel Sambuc 		}
120*0a6a1f1dSLionel Sambuc 	}
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc 	getaddr(addr, port, &ss, &slen);
123*0a6a1f1dSLionel Sambuc 
124*0a6a1f1dSLionel Sambuc 	if ((sfd = socket(AF_INET, type, 0)) == -1)
125*0a6a1f1dSLionel Sambuc 		err(EXIT_FAILURE, "socket");
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc 	sockaddr_snprintf(buf, sizeof(buf), "%a:%p", (const void *)&ss);
128*0a6a1f1dSLionel Sambuc 	printf("connecting to: %s\n", buf);
129*0a6a1f1dSLionel Sambuc 	if (connect(sfd, (const void *)&ss, slen) == -1)
130*0a6a1f1dSLionel Sambuc 		err(EXIT_FAILURE, "connect");
131*0a6a1f1dSLionel Sambuc 
132*0a6a1f1dSLionel Sambuc 	size_t len = strlen(msg) + 1;
133*0a6a1f1dSLionel Sambuc 	if (write(sfd, msg, len) != (ssize_t)len)
134*0a6a1f1dSLionel Sambuc 		err(EXIT_FAILURE, "write");
135*0a6a1f1dSLionel Sambuc 	return 0;
136*0a6a1f1dSLionel Sambuc }
137