Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Sean Middleditch |
| 3 | * sean@sourcemud.org |
| 4 | * |
| 5 | * The author or authors of this code dedicate any and all copyright interest |
| 6 | * in this code to the public domain. We make this dedication for the benefit |
| 7 | * of the public at large and to the detriment of our heirs and successors. We |
| 8 | * intend this dedication to be an overt act of relinquishment in perpetuity of |
| 9 | * all present and future rights to this code under copyright law. |
| 10 | */ |
| 11 | |
| 12 | #include <sys/socket.h> |
| 13 | #include <netinet/in.h> |
| 14 | #include <arpa/inet.h> |
| 15 | #include <netdb.h> |
| 16 | #include <poll.h> |
| 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <ctype.h> |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 22 | #include <termios.h> |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
| 25 | #ifdef HAVE_ZLIB |
| 26 | #include "zlib.h" |
| 27 | #endif |
| 28 | |
| 29 | #include "libtelnet.h" |
| 30 | |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 31 | static struct termios orig_tios; |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame] | 32 | static telnet_t *telnet; |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 33 | static int do_echo; |
| 34 | |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 35 | static const telnet_telopt_t telopts[] = { |
Sean Middleditch | bfc641e | 2009-03-22 16:26:06 -0400 | [diff] [blame] | 36 | { TELNET_TELOPT_ECHO, TELNET_WONT, TELNET_DO }, |
Sean Middleditch | bfc641e | 2009-03-22 16:26:06 -0400 | [diff] [blame] | 37 | { TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT }, |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 38 | { TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO }, |
| 39 | { TELNET_TELOPT_MSSP, TELNET_WONT, TELNET_DO }, |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 40 | { -1, 0, 0 } |
| 41 | }; |
| 42 | |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 43 | static void _cleanup(void) { |
| 44 | tcsetattr(STDOUT_FILENO, TCSADRAIN, &orig_tios); |
| 45 | } |
| 46 | |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 47 | static void _input(char *buffer, int size) { |
| 48 | static char crlf[] = { '\r', '\n' }; |
Sean Middleditch | e7c4426 | 2009-03-15 20:59:48 -0400 | [diff] [blame] | 49 | int i; |
| 50 | |
| 51 | for (i = 0; i != size; ++i) { |
| 52 | /* if we got a CR or LF, replace with CRLF |
| 53 | * NOTE that usually you'd get a CR in UNIX, but in raw |
| 54 | * mode we get LF instead (not sure why) |
| 55 | */ |
| 56 | if (buffer[i] == '\r' || buffer[i] == '\n') { |
| 57 | if (do_echo) |
Sean Middleditch | 54c1e3e | 2009-08-31 15:04:39 -0700 | [diff] [blame] | 58 | printf("\r\n"); |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame] | 59 | telnet_send(telnet, crlf, 2); |
Sean Middleditch | e7c4426 | 2009-03-15 20:59:48 -0400 | [diff] [blame] | 60 | } else { |
| 61 | if (do_echo) |
Sean Middleditch | 54c1e3e | 2009-08-31 15:04:39 -0700 | [diff] [blame] | 62 | putchar(buffer[i]); |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame] | 63 | telnet_send(telnet, buffer + i, 1); |
Sean Middleditch | e7c4426 | 2009-03-15 20:59:48 -0400 | [diff] [blame] | 64 | } |
| 65 | } |
Sean Middleditch | 54c1e3e | 2009-08-31 15:04:39 -0700 | [diff] [blame] | 66 | fflush(stdout); |
Sean Middleditch | e7c4426 | 2009-03-15 20:59:48 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 69 | static void _send(int sock, const char *buffer, size_t size) { |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 70 | int rs; |
| 71 | |
| 72 | /* send data */ |
| 73 | while (size > 0) { |
| 74 | if ((rs = send(sock, buffer, size, 0)) == -1) { |
| 75 | fprintf(stderr, "send() failed: %s\n", strerror(errno)); |
| 76 | exit(1); |
| 77 | } else if (rs == 0) { |
| 78 | fprintf(stderr, "send() unexpectedly returned 0\n"); |
| 79 | exit(1); |
| 80 | } |
| 81 | |
| 82 | /* update pointer and size to see if we've got more to send */ |
| 83 | buffer += rs; |
| 84 | size -= rs; |
| 85 | } |
| 86 | } |
| 87 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 88 | static void _event_handler(telnet_t *telnet, telnet_event_t *ev, |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 89 | void *user_data) { |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 90 | int sock = *(int*)user_data; |
| 91 | |
| 92 | switch (ev->type) { |
| 93 | /* data received */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 94 | case TELNET_EV_DATA: |
Sean Middleditch | 4adfdf3 | 2009-08-31 15:06:09 -0700 | [diff] [blame] | 95 | printf("%.*s", (int)ev->size, ev->buffer); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 96 | break; |
| 97 | /* data must be sent */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 98 | case TELNET_EV_SEND: |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 99 | _send(sock, ev->buffer, ev->size); |
| 100 | break; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 101 | /* request to enable remote feature (or receipt) */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 102 | case TELNET_EV_WILL: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 103 | /* we'll agree to turn off our echo if server wants us to stop */ |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 104 | if (ev->telopt == TELNET_TELOPT_ECHO) |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 105 | do_echo = 0; |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 106 | break; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 107 | /* notification of disabling remote feature (or receipt) */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 108 | case TELNET_EV_WONT: |
| 109 | if (ev->telopt == TELNET_TELOPT_ECHO) |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 110 | do_echo = 1; |
| 111 | break; |
| 112 | /* request to enable local feature (or receipt) */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 113 | case TELNET_EV_DO: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 114 | break; |
| 115 | /* demand to disable local feature (or receipt) */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 116 | case TELNET_EV_DONT: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 117 | break; |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 118 | /* respond to particular subnegotiations */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 119 | case TELNET_EV_SUBNEGOTIATION: |
Sean Middleditch | eb950a8 | 2009-03-22 23:04:28 -0400 | [diff] [blame] | 120 | /* if they just asked for our terminal type, response with it */ |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 121 | /* respond with our terminal type */ |
Sean Middleditch | eb950a8 | 2009-03-22 23:04:28 -0400 | [diff] [blame] | 122 | if (ev->telopt == TELNET_TELOPT_TTYPE && |
| 123 | ev->argc >= 1 && ev->argv[0][0] == TELNET_TTYPE_SEND) { |
| 124 | telnet_format_sb(telnet, TELNET_TELOPT_TTYPE, 1, |
| 125 | TELNET_TTYPE_IS, getenv("TERM")); |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 126 | } |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 127 | break; |
| 128 | /* error */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 129 | case TELNET_EV_ERROR: |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 130 | fprintf(stderr, "ERROR: %s\n", ev->buffer); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 131 | exit(1); |
| 132 | default: |
| 133 | /* ignore */ |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | int main(int argc, char **argv) { |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 139 | char buffer[512]; |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 140 | int rs; |
| 141 | int sock; |
| 142 | struct sockaddr_in addr; |
| 143 | struct pollfd pfd[2]; |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 144 | struct addrinfo *ai; |
| 145 | struct addrinfo hints; |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 146 | struct termios tios; |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 147 | |
| 148 | /* check usage */ |
| 149 | if (argc != 3) { |
| 150 | fprintf(stderr, "Usage:\n ./telnet-client <host> <port>\n"); |
| 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | /* look up server host */ |
| 155 | memset(&hints, 0, sizeof(hints)); |
| 156 | hints.ai_family = AF_UNSPEC; |
| 157 | hints.ai_socktype = SOCK_STREAM; |
| 158 | if ((rs = getaddrinfo(argv[1], argv[2], &hints, &ai)) != 0) { |
| 159 | fprintf(stderr, "getaddrinfo() failed for %s: %s\n", argv[1], |
| 160 | gai_strerror(rs)); |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | /* create server socket */ |
| 165 | if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
| 166 | fprintf(stderr, "socket() failed: %s\n", strerror(errno)); |
| 167 | return 1; |
| 168 | } |
| 169 | |
| 170 | /* bind server socket */ |
| 171 | memset(&addr, 0, sizeof(addr)); |
| 172 | addr.sin_family = AF_INET; |
| 173 | if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { |
| 174 | fprintf(stderr, "bind() failed: %s\n", strerror(errno)); |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | /* connect */ |
| 179 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) { |
| 180 | fprintf(stderr, "server() failed: %s\n", strerror(errno)); |
| 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | /* free address lookup info */ |
| 185 | freeaddrinfo(ai); |
| 186 | |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 187 | /* get current terminal settings, set raw mode, make sure we |
| 188 | * register atexit handler to restore terminal settings |
| 189 | */ |
| 190 | tcgetattr(STDOUT_FILENO, &orig_tios); |
| 191 | atexit(_cleanup); |
| 192 | tios = orig_tios; |
| 193 | cfmakeraw(&tios); |
| 194 | tcsetattr(STDOUT_FILENO, TCSADRAIN, &tios); |
| 195 | |
| 196 | /* set input echoing on by default */ |
| 197 | do_echo = 1; |
| 198 | |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 199 | /* initialize telnet box */ |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame] | 200 | telnet = telnet_init(telopts, _event_handler, 0, &sock); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 201 | |
| 202 | /* initialize poll descriptors */ |
| 203 | memset(pfd, 0, sizeof(pfd)); |
| 204 | pfd[0].fd = STDIN_FILENO; |
| 205 | pfd[0].events = POLLIN; |
| 206 | pfd[1].fd = sock; |
| 207 | pfd[1].events = POLLIN; |
| 208 | |
| 209 | /* loop while both connections are open */ |
| 210 | while (poll(pfd, 2, -1) != -1) { |
| 211 | /* read from stdin */ |
| 212 | if (pfd[0].revents & POLLIN) { |
| 213 | if ((rs = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) { |
Sean Middleditch | e7c4426 | 2009-03-15 20:59:48 -0400 | [diff] [blame] | 214 | _input(buffer, rs); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 215 | } else if (rs == 0) { |
| 216 | break; |
| 217 | } else { |
| 218 | fprintf(stderr, "recv(server) failed: %s\n", |
| 219 | strerror(errno)); |
| 220 | exit(1); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* read from client */ |
| 225 | if (pfd[1].revents & POLLIN) { |
| 226 | if ((rs = recv(sock, buffer, sizeof(buffer), 0)) > 0) { |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame] | 227 | telnet_recv(telnet, buffer, rs); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 228 | } else if (rs == 0) { |
| 229 | break; |
| 230 | } else { |
| 231 | fprintf(stderr, "recv(client) failed: %s\n", |
| 232 | strerror(errno)); |
| 233 | exit(1); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /* clean up */ |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame] | 239 | telnet_free(telnet); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 240 | close(sock); |
| 241 | |
| 242 | return 0; |
| 243 | } |