File: tcpecho.c 1 /* 2 The MIT License (MIT) 3 4 Copyright © 2020-2025 pacman64 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 this software and associated documentation files (the “Software”), to deal 8 in the Software without restriction, including without limitation the rights to 9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 10 of the Software, and to permit persons to whom the Software is furnished to do 11 so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in all 14 copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 SOFTWARE. 23 */ 24 25 /* 26 You can build this command-line app by running 27 28 cc -Wall -s -O2 -o ./tcpecho ./tcpecho.c 29 */ 30 31 #include <arpa/inet.h> 32 #include <fcntl.h> 33 #include <netinet/in.h> 34 #include <stdbool.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <sys/socket.h> 39 #include <sys/types.h> 40 #include <unistd.h> 41 42 #ifdef _WIN32 43 #include <windows.h> 44 #endif 45 46 const char* info = "" 47 "tcpecho [port] [words...]\n" 48 "\n" 49 "\n" 50 "Start a TCP server which always replies with a plain-text line made from\n" 51 "the arguments given after the port number, joined by single spaces.\n" 52 "\n" 53 "\n" 54 "Options, all of which can start with either 1 or 2 dashes:\n" 55 "\n" 56 "\n" 57 " -h show this help message\n" 58 " -help show this help message\n" 59 ""; 60 61 // is_help_option simplifies control-flow for func main 62 bool is_help_option(char* s) { 63 return (s[0] == '-') && ( 64 strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 || 65 strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0 66 ); 67 } 68 69 int main(int argc, char** argv) { 70 #ifdef _WIN32 71 setmode(fileno(stdin), O_BINARY); 72 // ensure output lines end in LF instead of CRLF on windows 73 setmode(fileno(stdout), O_BINARY); 74 setmode(fileno(stderr), O_BINARY); 75 #endif 76 77 // handle any of the help options, if given 78 if (argc > 1 && is_help_option(argv[1])) { 79 puts(info); 80 return 0; 81 } 82 83 if (argc < 2) { 84 char* msg = "you forgot the port number as the leading argument"; 85 fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg); 86 return 1; 87 } 88 89 char* end; 90 int port = strtol(argv[1], &end, 10); 91 if (*end != 0 || port < 1 || port > 65535) { 92 char* msg; 93 msg = "isn't a valid port number"; 94 fprintf(stderr, "\x1b[31m%s %s\x1b[0m\n", argv[1], msg); 95 msg = "maybe you forgot the port number as the leading argument"; 96 fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg); 97 return 1; 98 } 99 100 // count spaces between words 101 size_t msglen = argc - 2; 102 // count word lengths 103 for (size_t i = 2; i < argc; i++) { 104 msglen += strlen(argv[i]); 105 } 106 // remember the final line-feed 107 msglen++; 108 109 // also remember that strings are ended with an extra null byte 110 char* msg = malloc(msglen + 1); 111 if (msg == NULL) { 112 const char* m = "can't get enough memory for the response message"; 113 fprintf(stderr, "\x1b[31m%s\x1b[0m\n", m); 114 return 1; 115 } 116 117 for (size_t i = 2, pos = 0; i < argc; i++) { 118 if (i > 2) { 119 msg[pos] = ' '; 120 pos++; 121 } 122 123 for (size_t j = 0; argv[i][j] != 0; j++) { 124 msg[pos] = argv[i][j]; 125 pos++; 126 } 127 } 128 129 msg[msglen - 1] = '\n'; 130 msg[msglen] = 0; 131 132 struct sockaddr_in srv; 133 memset(&srv, 0, sizeof(srv)); 134 135 int fd = socket(AF_INET, SOCK_STREAM, 0); 136 srv.sin_family = AF_INET; 137 srv.sin_addr.s_addr = htonl(INADDR_ANY); 138 srv.sin_port = htons(port); 139 140 bind(fd, (struct sockaddr*)&srv, sizeof(srv)); 141 listen(fd, 1); 142 143 while (true) { 144 int conn = accept(fd, NULL, NULL); 145 send(conn, msg, msglen, 0); 146 close(conn); 147 } 148 149 free(msg); 150 return 0; 151 }