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 | f65f27d | 2009-03-19 02:32:04 -0400 | [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 }, |
| 37 | { TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO }, |
| 38 | { TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT }, |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 39 | { -1, 0, 0 } |
| 40 | }; |
| 41 | |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 42 | static void _cleanup(void) { |
| 43 | tcsetattr(STDOUT_FILENO, TCSADRAIN, &orig_tios); |
| 44 | } |
| 45 | |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 46 | static void _input(char *buffer, int size) { |
| 47 | static char crlf[] = { '\r', '\n' }; |
Sean Middleditch | e7c4426 | 2009-03-15 20:59:48 -0400 | [diff] [blame] | 48 | int i; |
| 49 | |
| 50 | for (i = 0; i != size; ++i) { |
| 51 | /* if we got a CR or LF, replace with CRLF |
| 52 | * NOTE that usually you'd get a CR in UNIX, but in raw |
| 53 | * mode we get LF instead (not sure why) |
| 54 | */ |
| 55 | if (buffer[i] == '\r' || buffer[i] == '\n') { |
| 56 | if (do_echo) |
| 57 | write(STDOUT_FILENO, crlf, 2); |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 58 | telnet_send(&telnet, crlf, 2); |
Sean Middleditch | e7c4426 | 2009-03-15 20:59:48 -0400 | [diff] [blame] | 59 | } else { |
| 60 | if (do_echo) |
| 61 | write(STDOUT_FILENO, buffer + i, 1); |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 62 | telnet_send(&telnet, buffer + i, 1); |
Sean Middleditch | e7c4426 | 2009-03-15 20:59:48 -0400 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 67 | static void _send(int sock, const char *buffer, size_t size) { |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 68 | int rs; |
| 69 | |
| 70 | /* send data */ |
| 71 | while (size > 0) { |
| 72 | if ((rs = send(sock, buffer, size, 0)) == -1) { |
| 73 | fprintf(stderr, "send() failed: %s\n", strerror(errno)); |
| 74 | exit(1); |
| 75 | } else if (rs == 0) { |
| 76 | fprintf(stderr, "send() unexpectedly returned 0\n"); |
| 77 | exit(1); |
| 78 | } |
| 79 | |
| 80 | /* update pointer and size to see if we've got more to send */ |
| 81 | buffer += rs; |
| 82 | size -= rs; |
| 83 | } |
| 84 | } |
| 85 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 86 | static void _event_handler(telnet_t *telnet, telnet_event_t *ev, |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 87 | void *user_data) { |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 88 | int sock = *(int*)user_data; |
| 89 | |
| 90 | switch (ev->type) { |
| 91 | /* data received */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 92 | case TELNET_EV_DATA: |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 93 | write(STDOUT_FILENO, ev->buffer, ev->size); |
| 94 | break; |
| 95 | /* data must be sent */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 96 | case TELNET_EV_SEND: |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 97 | _send(sock, ev->buffer, ev->size); |
| 98 | break; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 99 | /* request to enable remote feature (or receipt) */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 100 | case TELNET_EV_WILL: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 101 | /* 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] | 102 | if (ev->telopt == TELNET_TELOPT_ECHO) |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 103 | do_echo = 0; |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 104 | break; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 105 | /* notification of disabling remote feature (or receipt) */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 106 | case TELNET_EV_WONT: |
| 107 | if (ev->telopt == TELNET_TELOPT_ECHO) |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 108 | do_echo = 1; |
| 109 | break; |
| 110 | /* request to enable local feature (or receipt) */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 111 | case TELNET_EV_DO: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 112 | break; |
| 113 | /* demand to disable local feature (or receipt) */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 114 | case TELNET_EV_DONT: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 115 | break; |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 116 | /* respond to particular subnegotiations */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 117 | case TELNET_EV_SUBNEGOTIATION: |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 118 | /* respond with our terminal type */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 119 | if (ev->telopt == TELNET_TELOPT_TTYPE) { |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 120 | /* NOTE: we just assume the server sent a legitimate |
| 121 | * sub-negotiation, as there really isn't anything else |
| 122 | * it's allowed to send |
| 123 | */ |
| 124 | char buffer[64]; |
| 125 | buffer[0] = 0; /* IS code for RFC 1091 */ |
| 126 | snprintf(buffer + 1, sizeof(buffer) - 1, "%s", getenv("TERM")); |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 127 | telnet_subnegotiation(telnet, TELNET_TELOPT_TTYPE, buffer, |
| 128 | 1 + strlen(buffer + 1)); |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 129 | } |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 130 | break; |
| 131 | /* error */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 132 | case TELNET_EV_ERROR: |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 133 | fprintf(stderr, "ERROR: %s\n", ev->buffer); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 134 | exit(1); |
| 135 | default: |
| 136 | /* ignore */ |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | int main(int argc, char **argv) { |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 142 | char buffer[512]; |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 143 | int rs; |
| 144 | int sock; |
| 145 | struct sockaddr_in addr; |
| 146 | struct pollfd pfd[2]; |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 147 | struct addrinfo *ai; |
| 148 | struct addrinfo hints; |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 149 | struct termios tios; |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 150 | |
| 151 | /* check usage */ |
| 152 | if (argc != 3) { |
| 153 | fprintf(stderr, "Usage:\n ./telnet-client <host> <port>\n"); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | /* look up server host */ |
| 158 | memset(&hints, 0, sizeof(hints)); |
| 159 | hints.ai_family = AF_UNSPEC; |
| 160 | hints.ai_socktype = SOCK_STREAM; |
| 161 | if ((rs = getaddrinfo(argv[1], argv[2], &hints, &ai)) != 0) { |
| 162 | fprintf(stderr, "getaddrinfo() failed for %s: %s\n", argv[1], |
| 163 | gai_strerror(rs)); |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | /* create server socket */ |
| 168 | if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
| 169 | fprintf(stderr, "socket() failed: %s\n", strerror(errno)); |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | /* bind server socket */ |
| 174 | memset(&addr, 0, sizeof(addr)); |
| 175 | addr.sin_family = AF_INET; |
| 176 | if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { |
| 177 | fprintf(stderr, "bind() failed: %s\n", strerror(errno)); |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | /* connect */ |
| 182 | if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) { |
| 183 | fprintf(stderr, "server() failed: %s\n", strerror(errno)); |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | /* free address lookup info */ |
| 188 | freeaddrinfo(ai); |
| 189 | |
Sean Middleditch | a7896d1 | 2009-03-15 20:41:24 -0400 | [diff] [blame] | 190 | /* get current terminal settings, set raw mode, make sure we |
| 191 | * register atexit handler to restore terminal settings |
| 192 | */ |
| 193 | tcgetattr(STDOUT_FILENO, &orig_tios); |
| 194 | atexit(_cleanup); |
| 195 | tios = orig_tios; |
| 196 | cfmakeraw(&tios); |
| 197 | tcsetattr(STDOUT_FILENO, TCSADRAIN, &tios); |
| 198 | |
| 199 | /* set input echoing on by default */ |
| 200 | do_echo = 1; |
| 201 | |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 202 | /* initialize telnet box */ |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 203 | telnet_init(&telnet, telopts, _event_handler, 0, &sock); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 204 | |
| 205 | /* initialize poll descriptors */ |
| 206 | memset(pfd, 0, sizeof(pfd)); |
| 207 | pfd[0].fd = STDIN_FILENO; |
| 208 | pfd[0].events = POLLIN; |
| 209 | pfd[1].fd = sock; |
| 210 | pfd[1].events = POLLIN; |
| 211 | |
| 212 | /* loop while both connections are open */ |
| 213 | while (poll(pfd, 2, -1) != -1) { |
| 214 | /* read from stdin */ |
| 215 | if (pfd[0].revents & POLLIN) { |
| 216 | if ((rs = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) { |
Sean Middleditch | e7c4426 | 2009-03-15 20:59:48 -0400 | [diff] [blame] | 217 | _input(buffer, rs); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 218 | } else if (rs == 0) { |
| 219 | break; |
| 220 | } else { |
| 221 | fprintf(stderr, "recv(server) failed: %s\n", |
| 222 | strerror(errno)); |
| 223 | exit(1); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* read from client */ |
| 228 | if (pfd[1].revents & POLLIN) { |
| 229 | if ((rs = recv(sock, buffer, sizeof(buffer), 0)) > 0) { |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 230 | telnet_recv(&telnet, buffer, rs); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 231 | } else if (rs == 0) { |
| 232 | break; |
| 233 | } else { |
| 234 | fprintf(stderr, "recv(client) failed: %s\n", |
| 235 | strerror(errno)); |
| 236 | exit(1); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* clean up */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 242 | telnet_free(&telnet); |
Sean Middleditch | da394be | 2009-03-15 14:19:40 -0400 | [diff] [blame] | 243 | close(sock); |
| 244 | |
| 245 | return 0; |
| 246 | } |