Sean Middleditch | ce753de | 2009-03-14 02:20:03 -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 | |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 12 | #include <sys/socket.h> |
| 13 | #include <netinet/in.h> |
| 14 | #include <arpa/inet.h> |
Sean Middleditch | d88f183 | 2009-03-15 01:06:17 -0400 | [diff] [blame] | 15 | #include <netdb.h> |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 16 | #include <poll.h> |
| 17 | #include <errno.h> |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <ctype.h> |
| 22 | #include <unistd.h> |
| 23 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 24 | #ifdef HAVE_ZLIB |
| 25 | #include "zlib.h" |
| 26 | #endif |
| 27 | |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 28 | #include "libtelnet.h" |
| 29 | |
Sean Middleditch | 93b8980 | 2009-03-15 13:10:08 -0400 | [diff] [blame] | 30 | #ifdef ENABLE_COLOR |
| 31 | # define COLOR_SERVER "\e[35m" |
| 32 | # define COLOR_CLIENT "\e[34m" |
| 33 | # define COLOR_BOLD "\e[1m" |
| 34 | # define COLOR_UNBOLD "\e[22m" |
| 35 | # define COLOR_NORMAL "\e[0m" |
| 36 | #else |
| 37 | # define COLOR_SERVER "" |
| 38 | # define COLOR_CLIENT "" |
| 39 | # define COLOR_BOLD "" |
| 40 | # define COLOR_UNBOLD "" |
| 41 | # define COLOR_NORMAL "" |
| 42 | #endif |
| 43 | |
Sean Middleditch | 5b43a58 | 2009-03-14 02:04:28 -0400 | [diff] [blame] | 44 | struct conn_t { |
| 45 | const char *name; |
| 46 | int sock; |
| 47 | struct libtelnet_t telnet; |
| 48 | struct conn_t *remote; |
| 49 | }; |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 50 | |
Sean Middleditch | 0f91258 | 2009-03-14 01:57:07 -0400 | [diff] [blame] | 51 | static const char *get_cmd(unsigned char cmd) { |
| 52 | static char buffer[4]; |
| 53 | |
| 54 | switch (cmd) { |
| 55 | case 255: return "IAC"; |
| 56 | case 254: return "DONT"; |
| 57 | case 253: return "DO"; |
| 58 | case 252: return "WONT"; |
| 59 | case 251: return "WILL"; |
| 60 | case 250: return "SB"; |
| 61 | case 249: return "GA"; |
| 62 | case 248: return "EL"; |
| 63 | case 247: return "EC"; |
| 64 | case 246: return "AYT"; |
| 65 | case 245: return "AO"; |
| 66 | case 244: return "IP"; |
| 67 | case 243: return "BREAK"; |
| 68 | case 242: return "DM"; |
| 69 | case 241: return "NOP"; |
| 70 | case 240: return "SE"; |
| 71 | case 239: return "EOR"; |
| 72 | case 238: return "ABORT"; |
| 73 | case 237: return "SUSP"; |
| 74 | case 236: return "xEOF"; |
| 75 | default: |
| 76 | snprintf(buffer, sizeof(buffer), "%d", (int)cmd); |
| 77 | return buffer; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static const char *get_opt(unsigned char opt) { |
| 82 | switch (opt) { |
| 83 | case 0: return "BINARY"; |
| 84 | case 1: return "ECHO"; |
| 85 | case 2: return "RCP"; |
| 86 | case 3: return "SGA"; |
| 87 | case 4: return "NAMS"; |
| 88 | case 5: return "STATUS"; |
| 89 | case 6: return "TM"; |
| 90 | case 7: return "RCTE"; |
| 91 | case 8: return "NAOL"; |
| 92 | case 9: return "NAOP"; |
| 93 | case 10: return "NAOCRD"; |
| 94 | case 11: return "NAOHTS"; |
| 95 | case 12: return "NAOHTD"; |
| 96 | case 13: return "NAOFFD"; |
| 97 | case 14: return "NAOVTS"; |
| 98 | case 15: return "NAOVTD"; |
| 99 | case 16: return "NAOLFD"; |
| 100 | case 17: return "XASCII"; |
| 101 | case 18: return "LOGOUT"; |
| 102 | case 19: return "BM"; |
| 103 | case 20: return "DET"; |
| 104 | case 21: return "SUPDUP"; |
| 105 | case 22: return "SUPDUPOUTPUT"; |
| 106 | case 23: return "SNDLOC"; |
| 107 | case 24: return "TTYPE"; |
| 108 | case 25: return "EOR"; |
| 109 | case 26: return "TUID"; |
| 110 | case 27: return "OUTMRK"; |
| 111 | case 28: return "TTYLOC"; |
| 112 | case 29: return "3270REGIME"; |
| 113 | case 30: return "X3PAD"; |
| 114 | case 31: return "NAWS"; |
| 115 | case 32: return "TSPEED"; |
| 116 | case 33: return "LFLOW"; |
| 117 | case 34: return "LINEMODE"; |
| 118 | case 35: return "XDISPLOC"; |
| 119 | case 36: return "ENVIRON"; |
| 120 | case 37: return "AUTHENTICATION"; |
| 121 | case 38: return "ENCRYPT"; |
| 122 | case 39: return "NEW-ENVIRON"; |
| 123 | case 70: return "MSSP"; |
| 124 | case 85: return "COMPRESS"; |
| 125 | case 86: return "COMPRESS2"; |
| 126 | case 93: return "ZMP"; |
| 127 | case 255: return "EXOPL"; |
| 128 | default: return "unknown"; |
| 129 | } |
| 130 | } |
| 131 | |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 132 | static void print_buffer(unsigned char *buffer, unsigned int size) { |
| 133 | unsigned int i; |
| 134 | for (i = 0; i != size; ++i) { |
| 135 | if (buffer[i] == ' ' || (isprint(buffer[i]) && !isspace(buffer[i]))) |
| 136 | printf("%c", (char)buffer[i]); |
| 137 | else if (buffer[i] == '\n') |
Sean Middleditch | 93b8980 | 2009-03-15 13:10:08 -0400 | [diff] [blame] | 138 | printf("<" COLOR_BOLD "[1m0x%02X" COLOR_UNBOLD ">\n", |
| 139 | (int)buffer[i]); |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 140 | else |
Sean Middleditch | 93b8980 | 2009-03-15 13:10:08 -0400 | [diff] [blame] | 141 | printf("<" COLOR_BOLD "0x%02X" COLOR_UNBOLD ">", (int)buffer[i]); |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 145 | static void _send(int sock, unsigned char *buffer, unsigned int size) { |
Sean Middleditch | d99b36d | 2009-03-14 02:13:50 -0400 | [diff] [blame] | 146 | int rs; |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 147 | |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 148 | /* send data */ |
Sean Middleditch | d99b36d | 2009-03-14 02:13:50 -0400 | [diff] [blame] | 149 | while (size > 0) { |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 150 | if ((rs = send(sock, buffer, size, 0)) == -1) { |
Sean Middleditch | d99b36d | 2009-03-14 02:13:50 -0400 | [diff] [blame] | 151 | fprintf(stderr, "send() failed: %s\n", strerror(errno)); |
| 152 | exit(1); |
| 153 | } else if (rs == 0) { |
| 154 | fprintf(stderr, "send() unexpectedly returned 0\n"); |
| 155 | exit(1); |
| 156 | } |
| 157 | |
| 158 | /* update pointer and size to see if we've got more to send */ |
| 159 | buffer += rs; |
| 160 | size -= rs; |
| 161 | } |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 164 | static void _event_handler(struct libtelnet_t *telnet, |
| 165 | struct libtelnet_event_t *ev, void *user_data) { |
Sean Middleditch | 5b43a58 | 2009-03-14 02:04:28 -0400 | [diff] [blame] | 166 | struct conn_t *conn = (struct conn_t*)user_data; |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 167 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 168 | switch (ev->type) { |
| 169 | /* data received */ |
| 170 | case LIBTELNET_EV_DATA: |
| 171 | printf("%s DATA: ", conn->name); |
| 172 | print_buffer(ev->buffer, ev->size); |
Sean Middleditch | 93b8980 | 2009-03-15 13:10:08 -0400 | [diff] [blame] | 173 | printf(COLOR_NORMAL "\n"); |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 174 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 175 | libtelnet_send_data(&conn->remote->telnet, ev->buffer, ev->size, |
| 176 | conn->remote); |
| 177 | break; |
| 178 | /* data must be sent */ |
| 179 | case LIBTELNET_EV_SEND: |
| 180 | /* DONT SPAM |
| 181 | printf("%s SEND: ", conn->name); |
| 182 | print_buffer(ev->buffer, ev->size); |
Sean Middleditch | 93b8980 | 2009-03-15 13:10:08 -0400 | [diff] [blame] | 183 | printf(COLOR_BOLD "\n"); |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 184 | */ |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 185 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 186 | _send(conn->sock, ev->buffer, ev->size); |
| 187 | break; |
| 188 | /* IAC command */ |
| 189 | case LIBTELNET_EV_IAC: |
Sean Middleditch | 93b8980 | 2009-03-15 13:10:08 -0400 | [diff] [blame] | 190 | printf("%s IAC %s" COLOR_NORMAL "\n", conn->name, |
| 191 | get_cmd(ev->command)); |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 192 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 193 | libtelnet_send_command(&conn->remote->telnet, ev->command, |
| 194 | conn->remote); |
| 195 | break; |
| 196 | /* negotiation */ |
| 197 | case LIBTELNET_EV_NEGOTIATE: |
Sean Middleditch | 93b8980 | 2009-03-15 13:10:08 -0400 | [diff] [blame] | 198 | printf("%s IAC %s %d (%s)" COLOR_NORMAL "\n", conn->name, |
| 199 | get_cmd(ev->command), (int)ev->telopt, get_opt(ev->telopt)); |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 200 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 201 | libtelnet_send_negotiate(&conn->remote->telnet, ev->command, |
| 202 | ev->telopt, conn->remote); |
| 203 | break; |
| 204 | /* subnegotiation */ |
| 205 | case LIBTELNET_EV_SUBNEGOTIATION: |
| 206 | printf("%s SUB %d (%s)", conn->name, (int)ev->telopt, |
| 207 | get_opt(ev->telopt)); |
| 208 | if (ev->size > 0) { |
| 209 | printf(" [%u]: ", ev->size); |
| 210 | print_buffer(ev->buffer, ev->size); |
| 211 | } |
Sean Middleditch | 93b8980 | 2009-03-15 13:10:08 -0400 | [diff] [blame] | 212 | printf(COLOR_NORMAL "\n"); |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 213 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 214 | libtelnet_send_subnegotiation(&conn->remote->telnet, ev->telopt, |
| 215 | ev->buffer, ev->size, conn->remote); |
| 216 | break; |
| 217 | /* compression notification */ |
| 218 | case LIBTELNET_EV_COMPRESS: |
Sean Middleditch | 93b8980 | 2009-03-15 13:10:08 -0400 | [diff] [blame] | 219 | printf("%s COMPRESSION %s" COLOR_NORMAL "\n", conn->name, |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 220 | ev->command ? "ON" : "OFF"); |
| 221 | break; |
| 222 | /* error */ |
| 223 | case LIBTELNET_EV_ERROR: |
Sean Middleditch | 93b8980 | 2009-03-15 13:10:08 -0400 | [diff] [blame] | 224 | printf("%s ERROR: %.*s" COLOR_NORMAL "\n", conn->name, ev->size, |
| 225 | ev->buffer); |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 226 | exit(1); |
Sean Middleditch | 0f91258 | 2009-03-14 01:57:07 -0400 | [diff] [blame] | 227 | } |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | int main(int argc, char **argv) { |
| 231 | unsigned char buffer[512]; |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 232 | short listen_port; |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 233 | int listen_sock; |
| 234 | int rs; |
| 235 | struct sockaddr_in addr; |
| 236 | socklen_t addrlen; |
| 237 | struct pollfd pfd[2]; |
Sean Middleditch | 5b43a58 | 2009-03-14 02:04:28 -0400 | [diff] [blame] | 238 | struct conn_t server; |
| 239 | struct conn_t client; |
Sean Middleditch | d88f183 | 2009-03-15 01:06:17 -0400 | [diff] [blame] | 240 | struct addrinfo *ai; |
| 241 | struct addrinfo hints; |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 242 | |
| 243 | /* check usage */ |
| 244 | if (argc != 4) { |
Sean Middleditch | 5b43a58 | 2009-03-14 02:04:28 -0400 | [diff] [blame] | 245 | fprintf(stderr, "Usage:\n ./telnet-proxy <remote ip> <remote port> " |
| 246 | "<local port>\n"); |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 247 | return 1; |
| 248 | } |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 249 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 250 | /* parse listening port */ |
| 251 | listen_port = strtol(argv[3], 0, 10); |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 252 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 253 | /* loop forever, until user kills process */ |
| 254 | for (;;) { |
| 255 | /* create listening socket */ |
| 256 | if ((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
| 257 | fprintf(stderr, "socket() failed: %s\n", strerror(errno)); |
| 258 | return 1; |
| 259 | } |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 260 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 261 | /* reuse address option */ |
| 262 | rs = 1; |
| 263 | setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &rs, sizeof(rs)); |
Sean Middleditch | d88f183 | 2009-03-15 01:06:17 -0400 | [diff] [blame] | 264 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 265 | /* bind to listening addr/port */ |
| 266 | memset(&addr, 0, sizeof(addr)); |
| 267 | addr.sin_family = AF_INET; |
| 268 | addr.sin_addr.s_addr = INADDR_ANY; |
| 269 | addr.sin_port = htons(listen_port); |
| 270 | if (bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { |
| 271 | fprintf(stderr, "bind() failed: %s\n", strerror(errno)); |
| 272 | return 1; |
| 273 | } |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 274 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 275 | printf("LISTENING ON PORT %d\n", listen_port); |
Sean Middleditch | d88f183 | 2009-03-15 01:06:17 -0400 | [diff] [blame] | 276 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 277 | /* wait for client */ |
| 278 | if (listen(listen_sock, 1) == -1) { |
| 279 | fprintf(stderr, "listen() failed: %s\n", strerror(errno)); |
| 280 | return 1; |
| 281 | } |
| 282 | addrlen = sizeof(addr); |
| 283 | if ((client.sock = accept(listen_sock, (struct sockaddr *)&addr, |
| 284 | &addrlen)) == -1) { |
| 285 | fprintf(stderr, "accept() failed: %s\n", strerror(errno)); |
| 286 | return 1; |
| 287 | } |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 288 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 289 | printf("CLIENT CONNECTION RECEIVED\n"); |
| 290 | |
| 291 | /* stop listening now that we have a client */ |
| 292 | close(listen_sock); |
Sean Middleditch | d88f183 | 2009-03-15 01:06:17 -0400 | [diff] [blame] | 293 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 294 | /* look up server host */ |
| 295 | memset(&hints, 0, sizeof(hints)); |
| 296 | hints.ai_family = AF_UNSPEC; |
| 297 | hints.ai_socktype = SOCK_STREAM; |
| 298 | if ((rs = getaddrinfo(argv[1], argv[2], &hints, &ai)) != 0) { |
| 299 | fprintf(stderr, "getaddrinfo() failed for %s: %s\n", argv[1], |
| 300 | gai_strerror(rs)); |
| 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | /* create server socket */ |
| 305 | if ((server.sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
| 306 | fprintf(stderr, "socket() failed: %s\n", strerror(errno)); |
| 307 | return 1; |
| 308 | } |
Sean Middleditch | 5b43a58 | 2009-03-14 02:04:28 -0400 | [diff] [blame] | 309 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 310 | /* bind server socket */ |
| 311 | memset(&addr, 0, sizeof(addr)); |
| 312 | addr.sin_family = AF_INET; |
| 313 | if (bind(server.sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { |
| 314 | fprintf(stderr, "bind() failed: %s\n", strerror(errno)); |
| 315 | return 1; |
| 316 | } |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 317 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 318 | /* connect */ |
| 319 | if (connect(server.sock, ai->ai_addr, ai->ai_addrlen) == -1) { |
| 320 | fprintf(stderr, "server() failed: %s\n", strerror(errno)); |
| 321 | return 1; |
| 322 | } |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 323 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 324 | /* free address lookup info */ |
| 325 | freeaddrinfo(ai); |
| 326 | |
| 327 | printf("SERVER CONNECTION ESTABLISHED\n"); |
| 328 | |
| 329 | /* initialize connection structs */ |
| 330 | server.name = COLOR_SERVER "SERVER"; |
| 331 | server.remote = &client; |
| 332 | client.name = COLOR_CLIENT "CLIENT"; |
| 333 | client.remote = &server; |
| 334 | |
| 335 | /* initialize telnet boxes */ |
| 336 | libtelnet_init(&server.telnet, _event_handler, LIBTELNET_MODE_PROXY); |
| 337 | libtelnet_init(&client.telnet, _event_handler, LIBTELNET_MODE_PROXY); |
| 338 | |
| 339 | /* initialize poll descriptors */ |
| 340 | memset(pfd, 0, sizeof(pfd)); |
| 341 | pfd[0].fd = server.sock; |
| 342 | pfd[0].events = POLLIN; |
| 343 | pfd[1].fd = client.sock; |
| 344 | pfd[1].events = POLLIN; |
| 345 | |
| 346 | /* loop while both connections are open */ |
| 347 | while (poll(pfd, 2, -1) != -1) { |
| 348 | /* read from server */ |
| 349 | if (pfd[0].revents & POLLIN) { |
| 350 | if ((rs = recv(server.sock, buffer, sizeof(buffer), 0)) > 0) { |
| 351 | libtelnet_push(&server.telnet, buffer, rs, (void*)&server); |
| 352 | } else if (rs == 0) { |
| 353 | printf("%s DISCONNECTED" COLOR_NORMAL "\n", server.name); |
| 354 | break; |
| 355 | } else { |
| 356 | fprintf(stderr, "recv(server) failed: %s\n", |
| 357 | strerror(errno)); |
| 358 | exit(1); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /* read from client */ |
| 363 | if (pfd[1].revents & POLLIN) { |
| 364 | if ((rs = recv(client.sock, buffer, sizeof(buffer), 0)) > 0) { |
| 365 | libtelnet_push(&client.telnet, buffer, rs, (void*)&client); |
| 366 | } else if (rs == 0) { |
| 367 | printf("%s DISCONNECTED" COLOR_NORMAL "\n", client.name); |
| 368 | break; |
| 369 | } else { |
| 370 | fprintf(stderr, "recv(client) failed: %s\n", |
| 371 | strerror(errno)); |
| 372 | exit(1); |
| 373 | } |
Sean Middleditch | d99b36d | 2009-03-14 02:13:50 -0400 | [diff] [blame] | 374 | } |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 375 | } |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 376 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 377 | /* clean up */ |
| 378 | libtelnet_free(&server.telnet); |
| 379 | libtelnet_free(&client.telnet); |
| 380 | close(server.sock); |
| 381 | close(client.sock); |
| 382 | |
| 383 | /* all done */ |
| 384 | printf("BOTH CONNECTIONS CLOSED\n"); |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 385 | } |
| 386 | |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame^] | 387 | /* not that we can reach this, but GCC will cry if it's not here */ |
Sean Middleditch | b9ab0b6 | 2009-03-14 00:18:44 -0400 | [diff] [blame] | 388 | return 0; |
| 389 | } |