1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
29 #include "telnet_server.h"
30 #include <target/target_request.h>
31 #include <helper/configuration.h>
33 static char *telnet_port;
35 static char *negotiate =
36 "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
37 "\xFF\xFB\x01" /* IAC WILL Echo */
38 "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
39 "\xFF\xFE\x01"; /* IAC DON'T Echo */
41 #define CTRL(c) (c - '@')
42 #define TELNET_HISTORY ".openocd_history"
44 /* The only way we can detect that the socket is closed is the first time
45 * we write to it, we will fail. Subsequent write operations will
48 static int telnet_write(struct connection *connection, const void *data,
51 struct telnet_connection *t_con = connection->priv;
53 return ERROR_SERVER_REMOTE_CLOSED;
55 if (connection_write(connection, data, len) == len)
58 return ERROR_SERVER_REMOTE_CLOSED;
61 static int telnet_prompt(struct connection *connection)
63 struct telnet_connection *t_con = connection->priv;
65 return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
68 static int telnet_outputline(struct connection *connection, const char *line)
72 /* process lines in buffer */
74 char *line_end = strchr(line, '\n');
81 telnet_write(connection, line, len);
83 telnet_write(connection, "\r\n", 2);
92 static int telnet_output(struct command_context *cmd_ctx, const char *line)
94 struct connection *connection = cmd_ctx->output_handler_priv;
96 return telnet_outputline(connection, line);
99 static void telnet_log_callback(void *priv, const char *file, unsigned line,
100 const char *function, const char *string)
102 struct connection *connection = priv;
103 struct telnet_connection *t_con = connection->priv;
106 /* if there is no prompt, simply output the message */
107 if (t_con->line_cursor < 0) {
108 telnet_outputline(connection, string);
112 /* clear the command line */
113 for (i = strlen(t_con->prompt) + t_con->line_size; i > 0; i -= 16)
114 telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", i > 16 ? 16 : i);
115 for (i = strlen(t_con->prompt) + t_con->line_size; i > 0; i -= 16)
116 telnet_write(connection, " ", i > 16 ? 16 : i);
117 for (i = strlen(t_con->prompt) + t_con->line_size; i > 0; i -= 16)
118 telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", i > 16 ? 16 : i);
120 /* output the message */
121 telnet_outputline(connection, string);
123 /* put the command line to its previous state */
124 telnet_prompt(connection);
125 telnet_write(connection, t_con->line, t_con->line_size);
126 for (i = t_con->line_size; i > t_con->line_cursor; i--)
127 telnet_write(connection, "\b", 1);
130 static void telnet_load_history(struct telnet_connection *t_con)
133 char buffer[TELNET_BUFFER_SIZE];
136 char *history = get_home_dir(TELNET_HISTORY);
138 if (history == NULL) {
139 LOG_INFO("unable to get user home directory, telnet history will be disabled");
143 histfp = fopen(history, "rb");
147 while (fgets(buffer, sizeof(buffer), histfp) != NULL) {
149 char *p = strchr(buffer, '\n');
152 if (buffer[0] && i < TELNET_LINE_HISTORY_SIZE)
153 t_con->history[i++] = strdup(buffer);
156 t_con->next_history = i;
157 t_con->next_history %= TELNET_LINE_HISTORY_SIZE;
158 /* try to set to last entry - 1, that way we skip over any exit/shutdown cmds */
159 t_con->current_history = t_con->next_history > 0 ? i - 1 : 0;
166 static void telnet_save_history(struct telnet_connection *t_con)
172 char *history = get_home_dir(TELNET_HISTORY);
174 if (history == NULL) {
175 LOG_INFO("unable to get user home directory, telnet history will be disabled");
179 histfp = fopen(history, "wb");
183 num = TELNET_LINE_HISTORY_SIZE;
184 i = t_con->current_history + 1;
185 i %= TELNET_LINE_HISTORY_SIZE;
187 while (t_con->history[i] == NULL && num > 0) {
189 i %= TELNET_LINE_HISTORY_SIZE;
194 for (; num > 0; num--) {
195 fprintf(histfp, "%s\n", t_con->history[i]);
197 i %= TELNET_LINE_HISTORY_SIZE;
206 static int telnet_new_connection(struct connection *connection)
208 struct telnet_connection *telnet_connection;
209 struct telnet_service *telnet_service = connection->service->priv;
212 telnet_connection = malloc(sizeof(struct telnet_connection));
214 if (!telnet_connection) {
215 LOG_ERROR("Failed to allocate telnet connection.");
219 connection->priv = telnet_connection;
221 /* initialize telnet connection information */
222 telnet_connection->closed = 0;
223 telnet_connection->line_size = 0;
224 telnet_connection->line_cursor = 0;
225 telnet_connection->prompt = strdup("> ");
226 telnet_connection->state = TELNET_STATE_DATA;
228 /* output goes through telnet connection */
229 command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
231 /* negotiate telnet options */
232 telnet_write(connection, negotiate, strlen(negotiate));
234 /* print connection banner */
235 if (telnet_service->banner) {
236 telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));
237 telnet_write(connection, "\r\n", 2);
240 /* the prompt is always placed at the line beginning */
241 telnet_write(connection, "\r", 1);
242 telnet_prompt(connection);
244 /* initialize history */
245 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
246 telnet_connection->history[i] = NULL;
247 telnet_connection->next_history = 0;
248 telnet_connection->current_history = 0;
249 telnet_load_history(telnet_connection);
251 log_add_callback(telnet_log_callback, connection);
256 static void telnet_clear_line(struct connection *connection,
257 struct telnet_connection *t_con)
259 /* move to end of line */
260 if (t_con->line_cursor < t_con->line_size)
261 telnet_write(connection,
262 t_con->line + t_con->line_cursor,
263 t_con->line_size - t_con->line_cursor);
265 /* backspace, overwrite with space, backspace */
266 while (t_con->line_size > 0) {
267 telnet_write(connection, "\b \b", 3);
270 t_con->line_cursor = 0;
273 static void telnet_history_go(struct connection *connection, int idx)
275 struct telnet_connection *t_con = connection->priv;
277 if (t_con->history[idx]) {
278 telnet_clear_line(connection, t_con);
279 t_con->line_size = strlen(t_con->history[idx]);
280 t_con->line_cursor = t_con->line_size;
281 memcpy(t_con->line, t_con->history[idx], t_con->line_size);
282 telnet_write(connection, t_con->line, t_con->line_size);
283 t_con->current_history = idx;
285 t_con->state = TELNET_STATE_DATA;
288 static void telnet_history_up(struct connection *connection)
290 struct telnet_connection *t_con = connection->priv;
292 int last_history = (t_con->current_history > 0) ?
293 t_con->current_history - 1 :
294 TELNET_LINE_HISTORY_SIZE-1;
295 telnet_history_go(connection, last_history);
298 static void telnet_history_down(struct connection *connection)
300 struct telnet_connection *t_con = connection->priv;
302 int next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
303 telnet_history_go(connection, next_history);
306 static void telnet_move_cursor(struct connection *connection, size_t pos)
308 struct telnet_connection *tc;
311 tc = connection->priv;
313 if (pos < tc->line_cursor) {
314 tmp = tc->line_cursor - pos;
316 for (size_t i = 0; i < tmp; i += 16)
317 telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",
320 tmp = pos - tc->line_cursor;
322 for (size_t i = 0; i < tmp; i += 16)
323 telnet_write(connection, tc->line + tc->line_cursor + i,
327 tc->line_cursor = pos;
330 static int telnet_input(struct connection *connection)
333 unsigned char buffer[TELNET_BUFFER_SIZE];
334 unsigned char *buf_p;
335 struct telnet_connection *t_con = connection->priv;
336 struct command_context *command_context = connection->cmd_ctx;
338 bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
341 return ERROR_SERVER_REMOTE_CLOSED;
342 else if (bytes_read == -1) {
343 LOG_ERROR("error during read: %s", strerror(errno));
344 return ERROR_SERVER_REMOTE_CLOSED;
349 switch (t_con->state) {
350 case TELNET_STATE_DATA:
352 t_con->state = TELNET_STATE_IAC;
354 if (isprint(*buf_p)) { /* printable character */
355 /* watch buffer size leaving one spare character for
356 * string null termination */
357 if (t_con->line_size == TELNET_LINE_MAX_SIZE-1) {
358 /* output audible bell if buffer is full
359 * "\a" does not work, at least on windows */
360 telnet_write(connection, "\x07", 1);
361 } else if (t_con->line_cursor == t_con->line_size) {
362 telnet_write(connection, buf_p, 1);
363 t_con->line[t_con->line_size++] = *buf_p;
364 t_con->line_cursor++;
367 memmove(t_con->line + t_con->line_cursor + 1,
368 t_con->line + t_con->line_cursor,
369 t_con->line_size - t_con->line_cursor);
370 t_con->line[t_con->line_cursor] = *buf_p;
372 telnet_write(connection,
373 t_con->line + t_con->line_cursor,
374 t_con->line_size - t_con->line_cursor);
375 t_con->line_cursor++;
376 for (i = t_con->line_cursor; i < t_con->line_size; i++)
377 telnet_write(connection, "\b", 1);
379 } else { /* non-printable */
380 if (*buf_p == 0x1b) { /* escape */
381 t_con->state = TELNET_STATE_ESCAPE;
382 t_con->last_escape = '\x00';
383 } else if ((*buf_p == 0xd) || (*buf_p == 0xa)) { /* CR/LF */
386 /* skip over combinations with CR/LF and NUL characters */
387 if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) ||
388 (*(buf_p + 1) == 0xd))) {
392 if ((bytes_read > 1) && (*(buf_p + 1) == 0)) {
396 t_con->line[t_con->line_size] = 0;
398 telnet_write(connection, "\r\n\x00", 3);
400 if (strcmp(t_con->line, "history") == 0) {
402 for (i = 1; i < TELNET_LINE_HISTORY_SIZE; i++) {
403 /* the t_con->next_history line contains empty string
404 * (unless NULL), thus it is not printed */
405 char *history_line = t_con->history[(t_con->
407 TELNET_LINE_HISTORY_SIZE];
409 telnet_write(connection, history_line,
410 strlen(history_line));
411 telnet_write(connection, "\r\n\x00", 3);
414 t_con->line_size = 0;
415 t_con->line_cursor = 0;
419 /* save only non-blank not repeating lines in the history */
420 char *prev_line = t_con->history[(t_con->current_history > 0) ?
421 t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1];
422 if (*t_con->line && (prev_line == NULL ||
423 strcmp(t_con->line, prev_line))) {
424 /* if the history slot is already taken, free it */
425 if (t_con->history[t_con->next_history])
426 free(t_con->history[t_con->next_history]);
428 /* add line to history */
429 t_con->history[t_con->next_history] = strdup(t_con->line);
431 /* wrap history at TELNET_LINE_HISTORY_SIZE */
432 t_con->next_history = (t_con->next_history + 1) %
433 TELNET_LINE_HISTORY_SIZE;
435 /* current history line starts at the new entry */
436 t_con->current_history =
439 if (t_con->history[t_con->current_history])
440 free(t_con->history[t_con->current_history]);
441 t_con->history[t_con->current_history] = strdup("");
444 t_con->line_size = 0;
446 /* to suppress prompt in log callback during command execution */
447 t_con->line_cursor = -1;
449 if (strcmp(t_con->line, "shutdown") == 0)
450 telnet_save_history(t_con);
452 retval = command_run_line(command_context, t_con->line);
454 t_con->line_cursor = 0;
456 if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
457 return ERROR_SERVER_REMOTE_CLOSED;
459 /* the prompt is always * placed at the line beginning */
460 telnet_write(connection, "\r", 1);
462 retval = telnet_prompt(connection);
463 if (retval == ERROR_SERVER_REMOTE_CLOSED)
464 return ERROR_SERVER_REMOTE_CLOSED;
466 } else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) { /* delete character */
467 if (t_con->line_cursor > 0) {
468 if (t_con->line_cursor != t_con->line_size) {
470 telnet_write(connection, "\b", 1);
471 t_con->line_cursor--;
473 memmove(t_con->line + t_con->line_cursor,
474 t_con->line + t_con->line_cursor + 1,
478 telnet_write(connection,
479 t_con->line + t_con->line_cursor,
482 telnet_write(connection, " \b", 2);
483 for (i = t_con->line_cursor; i < t_con->line_size; i++)
484 telnet_write(connection, "\b", 1);
487 t_con->line_cursor--;
488 /* back space: move the 'printer' head one char
489 * back, overwrite with space, move back again */
490 telnet_write(connection, "\b \b", 3);
493 } else if (*buf_p == 0x15) /* clear line */
494 telnet_clear_line(connection, t_con);
495 else if (*buf_p == CTRL('B')) { /* cursor left */
496 if (t_con->line_cursor > 0) {
497 telnet_write(connection, "\b", 1);
498 t_con->line_cursor--;
500 t_con->state = TELNET_STATE_DATA;
501 } else if (*buf_p == CTRL('F')) { /* cursor right */
502 if (t_con->line_cursor < t_con->line_size)
503 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
504 t_con->state = TELNET_STATE_DATA;
505 } else if (*buf_p == CTRL('P')) /* cursor up */
506 telnet_history_up(connection);
507 else if (*buf_p == CTRL('N')) /* cursor down */
508 telnet_history_down(connection);
509 else if (*buf_p == CTRL('A'))
510 telnet_move_cursor(connection, 0);
511 else if (*buf_p == CTRL('E'))
512 telnet_move_cursor(connection, t_con->line_size);
514 LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p);
518 case TELNET_STATE_IAC:
521 t_con->state = TELNET_STATE_DONT;
524 t_con->state = TELNET_STATE_DO;
527 t_con->state = TELNET_STATE_WONT;
530 t_con->state = TELNET_STATE_WILL;
534 case TELNET_STATE_SB:
536 case TELNET_STATE_SE:
538 case TELNET_STATE_WILL:
539 case TELNET_STATE_WONT:
540 case TELNET_STATE_DO:
541 case TELNET_STATE_DONT:
542 t_con->state = TELNET_STATE_DATA;
544 case TELNET_STATE_ESCAPE:
545 if (t_con->last_escape == '[') {
546 if (*buf_p == 'D') { /* cursor left */
547 if (t_con->line_cursor > 0) {
548 telnet_write(connection, "\b", 1);
549 t_con->line_cursor--;
551 t_con->state = TELNET_STATE_DATA;
552 } else if (*buf_p == 'C') { /* cursor right */
553 if (t_con->line_cursor < t_con->line_size)
554 telnet_write(connection,
555 t_con->line + t_con->line_cursor++, 1);
556 t_con->state = TELNET_STATE_DATA;
557 } else if (*buf_p == 'A') { /* cursor up */
558 telnet_history_up(connection);
559 } else if (*buf_p == 'B') { /* cursor down */
560 telnet_history_down(connection);
561 } else if (*buf_p == '3')
562 t_con->last_escape = *buf_p;
564 t_con->state = TELNET_STATE_DATA;
565 } else if (t_con->last_escape == '3') {
566 /* Remove character */
568 if (t_con->line_cursor < t_con->line_size) {
571 /* remove char from line buffer */
572 memmove(t_con->line + t_con->line_cursor,
573 t_con->line + t_con->line_cursor + 1,
574 t_con->line_size - t_con->line_cursor);
576 /* print remainder of buffer */
577 telnet_write(connection, t_con->line + t_con->line_cursor,
578 t_con->line_size - t_con->line_cursor);
579 /* overwrite last char with whitespace */
580 telnet_write(connection, " \b", 2);
582 /* move back to cursor position*/
583 for (i = t_con->line_cursor; i < t_con->line_size; i++)
584 telnet_write(connection, "\b", 1);
587 t_con->state = TELNET_STATE_DATA;
589 t_con->state = TELNET_STATE_DATA;
590 } else if (t_con->last_escape == '\x00') {
592 t_con->last_escape = *buf_p;
594 t_con->state = TELNET_STATE_DATA;
596 LOG_ERROR("BUG: unexpected value in t_con->last_escape");
597 t_con->state = TELNET_STATE_DATA;
602 LOG_ERROR("unknown telnet state");
613 static int telnet_connection_closed(struct connection *connection)
615 struct telnet_connection *t_con = connection->priv;
618 log_remove_callback(telnet_log_callback, connection);
622 t_con->prompt = NULL;
625 /* save telnet history */
626 telnet_save_history(t_con);
628 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++) {
629 if (t_con->history[i]) {
630 free(t_con->history[i]);
631 t_con->history[i] = NULL;
635 /* if this connection registered a debug-message receiver delete it */
636 delete_debug_msg_receiver(connection->cmd_ctx, NULL);
638 if (connection->priv) {
639 free(connection->priv);
640 connection->priv = NULL;
642 LOG_ERROR("BUG: connection->priv == NULL");
647 int telnet_init(char *banner)
649 if (strcmp(telnet_port, "disabled") == 0) {
650 LOG_INFO("telnet server disabled");
654 struct telnet_service *telnet_service =
655 malloc(sizeof(struct telnet_service));
657 if (!telnet_service) {
658 LOG_ERROR("Failed to allocate telnet service.");
662 telnet_service->banner = banner;
664 int ret = add_service("telnet", telnet_port, CONNECTION_LIMIT_UNLIMITED,
665 telnet_new_connection, telnet_input, telnet_connection_closed,
668 if (ret != ERROR_OK) {
669 free(telnet_service);
676 /* daemon configuration command telnet_port */
677 COMMAND_HANDLER(handle_telnet_port_command)
679 return CALL_COMMAND_HANDLER(server_pipe_command, &telnet_port);
682 COMMAND_HANDLER(handle_exit_command)
684 return ERROR_COMMAND_CLOSE_CONNECTION;
687 static const struct command_registration telnet_command_handlers[] = {
690 .handler = handle_exit_command,
691 .mode = COMMAND_EXEC,
693 .help = "exit telnet session",
696 .name = "telnet_port",
697 .handler = handle_telnet_port_command,
699 .help = "Specify port on which to listen "
700 "for incoming telnet connections. "
701 "Read help on 'gdb_port'.",
702 .usage = "[port_num]",
704 COMMAND_REGISTRATION_DONE
707 int telnet_register_commands(struct command_context *cmd_ctx)
709 telnet_port = strdup("4444");
710 return register_commands(cmd_ctx, NULL, telnet_command_handlers);