blob: aa25e4a2dbd74979895eed346b3c0fa5f207d8f4 [file] [log] [blame]
Sean Middleditchda394be2009-03-15 14:19:40 -04001/*
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 Middleditcha7896d12009-03-15 20:41:24 -040022#include <termios.h>
Sean Middleditchda394be2009-03-15 14:19:40 -040023#include <unistd.h>
24
25#ifdef HAVE_ZLIB
26#include "zlib.h"
27#endif
28
29#include "libtelnet.h"
30
Sean Middleditcha7896d12009-03-15 20:41:24 -040031static struct termios orig_tios;
Sean Middleditchf65f27d2009-03-19 02:32:04 -040032static telnet_t telnet;
Sean Middleditcha7896d12009-03-15 20:41:24 -040033static int do_echo;
34
Sean Middleditch34bb0992009-03-21 00:20:44 -040035static const telnet_telopt_t telopts[] = {
Sean Middleditchbfc641e2009-03-22 16:26:06 -040036 { TELNET_TELOPT_ECHO, TELNET_WONT, TELNET_DO },
Sean Middleditchbfc641e2009-03-22 16:26:06 -040037 { TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT },
Sean Middleditch2d5c4992009-03-22 22:21:42 -040038 { TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO },
39 { TELNET_TELOPT_MSSP, TELNET_WONT, TELNET_DO },
Sean Middleditch34bb0992009-03-21 00:20:44 -040040 { -1, 0, 0 }
41};
42
Sean Middleditcha7896d12009-03-15 20:41:24 -040043static void _cleanup(void) {
44 tcsetattr(STDOUT_FILENO, TCSADRAIN, &orig_tios);
45}
46
Sean Middleditch8daf7742009-03-19 02:05:24 -040047static void _input(char *buffer, int size) {
48 static char crlf[] = { '\r', '\n' };
Sean Middleditche7c44262009-03-15 20:59:48 -040049 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)
58 write(STDOUT_FILENO, crlf, 2);
Sean Middleditch4f0c37f2009-03-20 23:08:55 -040059 telnet_send(&telnet, crlf, 2);
Sean Middleditche7c44262009-03-15 20:59:48 -040060 } else {
61 if (do_echo)
62 write(STDOUT_FILENO, buffer + i, 1);
Sean Middleditch4f0c37f2009-03-20 23:08:55 -040063 telnet_send(&telnet, buffer + i, 1);
Sean Middleditche7c44262009-03-15 20:59:48 -040064 }
65 }
66}
67
Sean Middleditch340a51b2009-03-19 02:08:46 -040068static void _send(int sock, const char *buffer, size_t size) {
Sean Middleditchda394be2009-03-15 14:19:40 -040069 int rs;
70
71 /* send data */
72 while (size > 0) {
73 if ((rs = send(sock, buffer, size, 0)) == -1) {
74 fprintf(stderr, "send() failed: %s\n", strerror(errno));
75 exit(1);
76 } else if (rs == 0) {
77 fprintf(stderr, "send() unexpectedly returned 0\n");
78 exit(1);
79 }
80
81 /* update pointer and size to see if we've got more to send */
82 buffer += rs;
83 size -= rs;
84 }
85}
86
Sean Middleditchf65f27d2009-03-19 02:32:04 -040087static void _event_handler(telnet_t *telnet, telnet_event_t *ev,
Sean Middleditch812358d2009-03-15 23:24:03 -040088 void *user_data) {
Sean Middleditchda394be2009-03-15 14:19:40 -040089 int sock = *(int*)user_data;
90
91 switch (ev->type) {
92 /* data received */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040093 case TELNET_EV_DATA:
Sean Middleditchda394be2009-03-15 14:19:40 -040094 write(STDOUT_FILENO, ev->buffer, ev->size);
95 break;
96 /* data must be sent */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040097 case TELNET_EV_SEND:
Sean Middleditchda394be2009-03-15 14:19:40 -040098 _send(sock, ev->buffer, ev->size);
99 break;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400100 /* request to enable remote feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400101 case TELNET_EV_WILL:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400102 /* we'll agree to turn off our echo if server wants us to stop */
Sean Middleditch34bb0992009-03-21 00:20:44 -0400103 if (ev->telopt == TELNET_TELOPT_ECHO)
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400104 do_echo = 0;
Sean Middleditcha7896d12009-03-15 20:41:24 -0400105 break;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400106 /* notification of disabling remote feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400107 case TELNET_EV_WONT:
108 if (ev->telopt == TELNET_TELOPT_ECHO)
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400109 do_echo = 1;
110 break;
111 /* request to enable local feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400112 case TELNET_EV_DO:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400113 break;
114 /* demand to disable local feature (or receipt) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400115 case TELNET_EV_DONT:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400116 break;
Sean Middleditcha7896d12009-03-15 20:41:24 -0400117 /* respond to particular subnegotiations */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400118 case TELNET_EV_SUBNEGOTIATION:
Sean Middleditcha7896d12009-03-15 20:41:24 -0400119 /* respond with our terminal type */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400120 if (ev->telopt == TELNET_TELOPT_TTYPE) {
Sean Middleditcha7896d12009-03-15 20:41:24 -0400121 /* NOTE: we just assume the server sent a legitimate
122 * sub-negotiation, as there really isn't anything else
123 * it's allowed to send
124 */
125 char buffer[64];
126 buffer[0] = 0; /* IS code for RFC 1091 */
127 snprintf(buffer + 1, sizeof(buffer) - 1, "%s", getenv("TERM"));
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400128 telnet_subnegotiation(telnet, TELNET_TELOPT_TTYPE, buffer,
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400129 strlen(getenv("TERM")) + 1);
Sean Middleditcha7896d12009-03-15 20:41:24 -0400130 }
Sean Middleditchda394be2009-03-15 14:19:40 -0400131 break;
132 /* error */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400133 case TELNET_EV_ERROR:
Sean Middleditch340a51b2009-03-19 02:08:46 -0400134 fprintf(stderr, "ERROR: %s\n", ev->buffer);
Sean Middleditchda394be2009-03-15 14:19:40 -0400135 exit(1);
136 default:
137 /* ignore */
138 break;
139 }
140}
141
142int main(int argc, char **argv) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400143 char buffer[512];
Sean Middleditchda394be2009-03-15 14:19:40 -0400144 int rs;
145 int sock;
146 struct sockaddr_in addr;
147 struct pollfd pfd[2];
Sean Middleditchda394be2009-03-15 14:19:40 -0400148 struct addrinfo *ai;
149 struct addrinfo hints;
Sean Middleditcha7896d12009-03-15 20:41:24 -0400150 struct termios tios;
Sean Middleditchda394be2009-03-15 14:19:40 -0400151
152 /* check usage */
153 if (argc != 3) {
154 fprintf(stderr, "Usage:\n ./telnet-client <host> <port>\n");
155 return 1;
156 }
157
158 /* look up server host */
159 memset(&hints, 0, sizeof(hints));
160 hints.ai_family = AF_UNSPEC;
161 hints.ai_socktype = SOCK_STREAM;
162 if ((rs = getaddrinfo(argv[1], argv[2], &hints, &ai)) != 0) {
163 fprintf(stderr, "getaddrinfo() failed for %s: %s\n", argv[1],
164 gai_strerror(rs));
165 return 1;
166 }
167
168 /* create server socket */
169 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
170 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
171 return 1;
172 }
173
174 /* bind server socket */
175 memset(&addr, 0, sizeof(addr));
176 addr.sin_family = AF_INET;
177 if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
178 fprintf(stderr, "bind() failed: %s\n", strerror(errno));
179 return 1;
180 }
181
182 /* connect */
183 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
184 fprintf(stderr, "server() failed: %s\n", strerror(errno));
185 return 1;
186 }
187
188 /* free address lookup info */
189 freeaddrinfo(ai);
190
Sean Middleditcha7896d12009-03-15 20:41:24 -0400191 /* get current terminal settings, set raw mode, make sure we
192 * register atexit handler to restore terminal settings
193 */
194 tcgetattr(STDOUT_FILENO, &orig_tios);
195 atexit(_cleanup);
196 tios = orig_tios;
197 cfmakeraw(&tios);
198 tcsetattr(STDOUT_FILENO, TCSADRAIN, &tios);
199
200 /* set input echoing on by default */
201 do_echo = 1;
202
Sean Middleditchda394be2009-03-15 14:19:40 -0400203 /* initialize telnet box */
Sean Middleditch34bb0992009-03-21 00:20:44 -0400204 telnet_init(&telnet, telopts, _event_handler, 0, &sock);
Sean Middleditchda394be2009-03-15 14:19:40 -0400205
206 /* initialize poll descriptors */
207 memset(pfd, 0, sizeof(pfd));
208 pfd[0].fd = STDIN_FILENO;
209 pfd[0].events = POLLIN;
210 pfd[1].fd = sock;
211 pfd[1].events = POLLIN;
212
213 /* loop while both connections are open */
214 while (poll(pfd, 2, -1) != -1) {
215 /* read from stdin */
216 if (pfd[0].revents & POLLIN) {
217 if ((rs = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) {
Sean Middleditche7c44262009-03-15 20:59:48 -0400218 _input(buffer, rs);
Sean Middleditchda394be2009-03-15 14:19:40 -0400219 } else if (rs == 0) {
220 break;
221 } else {
222 fprintf(stderr, "recv(server) failed: %s\n",
223 strerror(errno));
224 exit(1);
225 }
226 }
227
228 /* read from client */
229 if (pfd[1].revents & POLLIN) {
230 if ((rs = recv(sock, buffer, sizeof(buffer), 0)) > 0) {
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400231 telnet_recv(&telnet, buffer, rs);
Sean Middleditchda394be2009-03-15 14:19:40 -0400232 } else if (rs == 0) {
233 break;
234 } else {
235 fprintf(stderr, "recv(client) failed: %s\n",
236 strerror(errno));
237 exit(1);
238 }
239 }
240 }
241
242 /* clean up */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400243 telnet_free(&telnet);
Sean Middleditchda394be2009-03-15 14:19:40 -0400244 close(sock);
245
246 return 0;
247}