1 /* $NetBSD: cltest.c,v 1.2 2025/02/11 17:48:31 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 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. 30 */ 31 #ifdef HAVE_CONFIG_H 32 #include "config.h" 33 #endif 34 35 #ifdef HAVE_SYS_CDEFS_H 36 #include <sys/cdefs.h> 37 #endif 38 __RCSID("$NetBSD: cltest.c,v 1.2 2025/02/11 17:48:31 christos Exp $"); 39 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 #include <netinet/in.h> 43 #include <arpa/inet.h> 44 45 #include <stdio.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <stdlib.h> 49 #include <err.h> 50 #ifdef HAVE_UTIL_H 51 #include <util.h> 52 #endif 53 54 static __dead void 55 usage(int c) 56 { 57 warnx("Unknown option `%c'", (char)c); 58 fprintf(stderr, "Usage: %s [-u] [-a <addr>] [-m <msg>] [-p <port>]\n", 59 getprogname()); 60 exit(EXIT_FAILURE); 61 } 62 63 static void 64 getaddr(const char *a, in_port_t p, struct sockaddr_storage *ss, 65 socklen_t *slen) 66 { 67 int c; 68 69 memset(ss, 0, sizeof(*ss)); 70 p = htons(p); 71 72 if (strchr(a, ':')) { 73 struct sockaddr_in6 *s6 = (void *)ss; 74 c = inet_pton(AF_INET6, a, &s6->sin6_addr); 75 s6->sin6_family = AF_INET6; 76 *slen = sizeof(*s6); 77 s6->sin6_port = p; 78 } else { 79 struct sockaddr_in *s = (void *)ss; 80 c = inet_pton(AF_INET, a, &s->sin_addr); 81 s->sin_family = AF_INET; 82 *slen = sizeof(*s); 83 s->sin_port = p; 84 } 85 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 86 ss->ss_len = (uint8_t)*slen; 87 #endif 88 if (c == -1) 89 err(EXIT_FAILURE, "Invalid address `%s'", a); 90 } 91 92 int 93 main(int argc, char *argv[]) 94 { 95 int sfd; 96 int c; 97 struct sockaddr_storage ss; 98 const char *msg = "hello"; 99 const char *addr = "127.0.0.1"; 100 int type = SOCK_STREAM; 101 in_port_t port = 6161; 102 socklen_t slen; 103 char buf[128]; 104 105 while ((c = getopt(argc, argv, "a:m:p:u")) != -1) { 106 switch (c) { 107 case 'a': 108 addr = optarg; 109 break; 110 case 'm': 111 msg = optarg; 112 break; 113 case 'p': 114 port = (in_port_t)atoi(optarg); 115 break; 116 case 'u': 117 type = SOCK_DGRAM; 118 break; 119 default: 120 usage(c); 121 } 122 } 123 124 getaddr(addr, port, &ss, &slen); 125 126 if ((sfd = socket(AF_INET, type, 0)) == -1) 127 err(EXIT_FAILURE, "socket"); 128 129 sockaddr_snprintf(buf, sizeof(buf), "%a:%p", (const void *)&ss); 130 printf("connecting to: %s\n", buf); 131 if (connect(sfd, (const void *)&ss, slen) == -1) 132 err(EXIT_FAILURE, "connect"); 133 134 size_t len = strlen(msg) + 1; 135 if (write(sfd, msg, len) != (ssize_t)len) 136 err(EXIT_FAILURE, "write"); 137 return 0; 138 } 139