1 1.3 riastrad /* $NetBSD: test_server.c,v 1.3 2025/01/05 08:23:33 riastradh Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2021 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation 8 1.1 christos * by James Browning, Gabe Coffland, Alex Gavin, and Solomon Ritzow. 9 1.1 christos * 10 1.1 christos * Redistribution and use in source and binary forms, with or without 11 1.1 christos * modification, are permitted provided that the following conditions 12 1.1 christos * are met: 13 1.1 christos * 1. Redistributions of source code must retain the above copyright 14 1.1 christos * notice, this list of conditions and the following disclaimer. 15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 christos * notice, this list of conditions and the following disclaimer in the 17 1.1 christos * documentation and/or other materials provided with the distribution. 18 1.1 christos * 19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 30 1.1 christos */ 31 1.1 christos 32 1.1 christos #include <sys/cdefs.h> 33 1.3 riastrad __RCSID("$NetBSD: test_server.c,v 1.3 2025/01/05 08:23:33 riastradh Exp $"); 34 1.1 christos 35 1.1 christos #include <sys/socket.h> 36 1.1 christos #include <unistd.h> 37 1.1 christos #include <netdb.h> 38 1.1 christos #include <stdint.h> 39 1.1 christos #include <stdio.h> 40 1.1 christos #include <string.h> 41 1.1 christos #include <errno.h> 42 1.1 christos #include <stdlib.h> 43 1.1 christos #include <syslog.h> 44 1.1 christos 45 1.1 christos #define CHECK(expr) do {\ 46 1.1 christos if ((expr) == -1) {\ 47 1.1 christos syslog(LOG_ERR, "Error at %s:%d: %s", \ 48 1.1 christos __FILE__, __LINE__, \ 49 1.1 christos strerror(errno));\ 50 1.1 christos exit(EXIT_FAILURE);\ 51 1.1 christos }\ 52 1.1 christos } while(0); 53 1.1 christos 54 1.1 christos static void stream_nowait_service(void); 55 1.1 christos static void stream_wait_service(void); 56 1.1 christos static void dgram_wait_service(void); 57 1.1 christos 58 1.1 christos int 59 1.1 christos main(int argc, char **argv) 60 1.1 christos { 61 1.1 christos 62 1.1 christos openlog("inetd_test_server", LOG_PID | LOG_NOWAIT, LOG_DAEMON); 63 1.1 christos 64 1.1 christos if (argc < 3) { 65 1.1 christos syslog(LOG_ERR, "Invalid arg count"); 66 1.1 christos exit(EXIT_FAILURE); 67 1.1 christos } 68 1.1 christos 69 1.1 christos /* Run the correct service according to the args */ 70 1.1 christos if (strcmp(argv[1], "dgram") == 0) { 71 1.1 christos if (strcmp(argv[2], "wait") == 0) { 72 1.1 christos dgram_wait_service(); 73 1.1 christos } else { 74 1.1 christos syslog(LOG_ERR, "Invalid arg %s", argv[2]); 75 1.1 christos exit(EXIT_FAILURE); 76 1.1 christos } 77 1.1 christos } else if (strcmp(argv[1], "stream") == 0) { 78 1.1 christos if (strcmp(argv[2], "wait") == 0) { 79 1.1 christos stream_wait_service(); 80 1.3 riastrad } else if (strcmp(argv[2], "nowait") == 0) { 81 1.1 christos stream_nowait_service(); 82 1.1 christos } else { 83 1.1 christos syslog(LOG_ERR, "Invalid arg %s", argv[2]); 84 1.1 christos exit(EXIT_FAILURE); 85 1.1 christos } 86 1.1 christos } else { 87 1.1 christos syslog(LOG_ERR, "Invalid args %s %s", argv[1], argv[2]); 88 1.1 christos exit(EXIT_FAILURE); 89 1.1 christos } 90 1.1 christos return 0; 91 1.1 christos } 92 1.1 christos 93 1.1 christos static void 94 1.2 christos stream_nowait_service(void) 95 1.1 christos { 96 1.1 christos ssize_t count; 97 1.1 christos char buffer[10]; 98 1.1 christos CHECK(count = recv(0, buffer, sizeof(buffer), 0)); 99 1.1 christos syslog(LOG_WARNING, "Received stream/nowait message \"%.*s\"\n", 100 1.2 christos (int)count, buffer); 101 1.2 christos CHECK(send(1, buffer, (size_t)count, 0)); 102 1.1 christos } 103 1.1 christos 104 1.1 christos static void 105 1.2 christos stream_wait_service(void) 106 1.1 christos { 107 1.1 christos struct sockaddr_storage addr; 108 1.1 christos ssize_t count; 109 1.1 christos int fd; 110 1.1 christos socklen_t addr_len; 111 1.1 christos char buffer[10]; 112 1.1 christos 113 1.1 christos CHECK(fd = accept(0, (struct sockaddr*)&addr, &addr_len)); 114 1.1 christos CHECK(count = recv(fd, buffer, sizeof(buffer), 0)); 115 1.1 christos syslog(LOG_WARNING, "Received stream/wait message \"%.*s\"\n", 116 1.2 christos (int)count, buffer); 117 1.2 christos CHECK(send(fd, buffer, (size_t)count, 0)); 118 1.1 christos CHECK(shutdown(fd, SHUT_RDWR)); 119 1.1 christos CHECK(close(fd)); 120 1.1 christos } 121 1.1 christos 122 1.1 christos static void 123 1.2 christos dgram_wait_service(void) 124 1.1 christos { 125 1.1 christos char buffer[256]; 126 1.1 christos char name[NI_MAXHOST]; 127 1.1 christos struct sockaddr_storage addr; 128 1.1 christos 129 1.1 christos struct iovec store = { 130 1.1 christos .iov_base = &buffer, 131 1.1 christos .iov_len = sizeof(buffer) 132 1.1 christos }; 133 1.1 christos struct msghdr header = { 134 1.1 christos .msg_name = &addr, 135 1.1 christos .msg_namelen = sizeof(struct sockaddr_storage), 136 1.1 christos .msg_iov = &store, 137 1.1 christos .msg_iovlen = 1 138 1.1 christos /* scatter/gather and control info is null */ 139 1.1 christos }; 140 1.2 christos ssize_t count; 141 1.1 christos 142 1.1 christos /* Peek so service can still get the packet */ 143 1.1 christos CHECK(count = recvmsg(0, &header, 0)); 144 1.1 christos 145 1.3 riastrad CHECK(sendto(1, buffer, (size_t)count, 0, 146 1.2 christos (struct sockaddr*)(&addr), addr.ss_len)); 147 1.3 riastrad 148 1.3 riastrad int error = getnameinfo((struct sockaddr*)&addr, 149 1.1 christos addr.ss_len, name, NI_MAXHOST, 150 1.1 christos NULL, 0, NI_NUMERICHOST); 151 1.3 riastrad 152 1.1 christos if (error) { 153 1.1 christos syslog(LOG_ERR, "getnameinfo error: %s\n", gai_strerror(error)); 154 1.1 christos exit(EXIT_FAILURE); 155 1.1 christos } 156 1.3 riastrad syslog(LOG_WARNING, "Received dgram/wait message \"%.*s\" from %s\n", 157 1.2 christos (int)count, buffer, name); 158 1.1 christos } 159