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 // info is the message shown when this app is given any of its help options
  47 const char* info = ""
  48 "tcpecho [port] [words...]\n"
  49 "\n"
  50 "\n"
  51 "Start a TCP server which always replies with a plain-text line made from\n"
  52 "the arguments given after the port number, joined by single spaces.\n"
  53 "\n"
  54 "\n"
  55 "Options, all of which can start with either 1 or 2 dashes:\n"
  56 "\n"
  57 "\n"
  58 "  -h          show this help message\n"
  59 "  -help       show this help message\n"
  60 "";
  61 
  62 // is_help_option simplifies control-flow for func main
  63 bool is_help_option(char* s) {
  64     return (s[0] == '-') && (
  65         strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 ||
  66         strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0
  67     );
  68 }
  69 
  70 int main(int argc, char** argv) {
  71 #ifdef _WIN32
  72     setmode(fileno(stdin), O_BINARY);
  73     // ensure output lines end in LF instead of CRLF on windows
  74     setmode(fileno(stdout), O_BINARY);
  75     setmode(fileno(stderr), O_BINARY);
  76 #endif
  77 
  78     // handle any of the help options, if given
  79     if (argc > 1 && is_help_option(argv[1])) {
  80         puts(info);
  81         return 0;
  82     }
  83 
  84     if (argc < 2) {
  85         char* msg = "you forgot the port number as the leading argument";
  86         fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg);
  87         return 1;
  88     }
  89 
  90     char* end;
  91     int port = strtol(argv[1], &end, 10);
  92     if (*end != 0 || port < 1 || port > 65535) {
  93         char* msg;
  94         msg = "isn't a valid port number";
  95         fprintf(stderr, "\x1b[31m%s %s\x1b[0m\n", argv[1], msg);
  96         msg = "maybe you forgot the port number as the leading argument";
  97         fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg);
  98         return 1;
  99     }
 100 
 101     // count spaces between words
 102     size_t msglen = argc - 2;
 103     // count word lengths
 104     for (size_t i = 2; i < argc; i++) {
 105         msglen += strlen(argv[i]);
 106     }
 107     // remember the final line-feed
 108     msglen++;
 109 
 110     // also remember that strings are ended with an extra null byte
 111     char* msg = malloc(msglen + 1);
 112     if (msg == NULL) {
 113         const char* m = "can't get enough memory for the response message";
 114         fprintf(stderr, "\x1b[31m%s\x1b[0m\n", m);
 115         return 1;
 116     }
 117 
 118     for (size_t i = 2, pos = 0; i < argc; i++) {
 119         if (i > 2) {
 120             msg[pos] = ' ';
 121             pos++;
 122         }
 123 
 124         for (size_t j = 0; argv[i][j] != 0; j++) {
 125             msg[pos] = argv[i][j];
 126             pos++;
 127         }
 128     }
 129 
 130     msg[msglen - 1] = '\n';
 131     msg[msglen] = 0;
 132 
 133     struct sockaddr_in srv;
 134     memset(&srv, 0, sizeof(srv));
 135 
 136     int fd = socket(AF_INET, SOCK_STREAM, 0);
 137     srv.sin_family = AF_INET;
 138     srv.sin_addr.s_addr = htonl(INADDR_ANY);
 139     srv.sin_port = htons(port);
 140 
 141     bind(fd, (struct sockaddr*)&srv, sizeof(srv));
 142     listen(fd, 1);
 143 
 144     while (true) {
 145         int conn = accept(fd, NULL, NULL);
 146         send(conn, msg, msglen, 0);
 147         close(conn);
 148     }
 149 
 150     free(msg);
 151     return 0;
 152 }