server: specify port as a string
[openocd/cortex.git] / src / server / telnet_server.c
blob420f5d70a05e407c27807caad310fadbeff4a00d
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
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. *
15 * *
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. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include "telnet_server.h"
31 #include <target/target_request.h>
33 static const 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 - '@')
43 /* The only way we can detect that the socket is closed is the first time
44 * we write to it, we will fail. Subsequent write operations will
45 * succeed. Shudder!
47 static int telnet_write(struct connection *connection, const void *data,
48 int len)
50 struct telnet_connection *t_con = connection->priv;
51 if (t_con->closed)
52 return ERROR_SERVER_REMOTE_CLOSED;
54 if (connection_write(connection, data, len) == len)
56 return ERROR_OK;
58 t_con->closed = 1;
59 return ERROR_SERVER_REMOTE_CLOSED;
62 static int telnet_prompt(struct connection *connection)
64 struct telnet_connection *t_con = connection->priv;
66 return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
69 static int telnet_outputline(struct connection *connection, const char *line)
71 int len;
73 /* process lines in buffer */
74 while (*line) {
75 char *line_end = strchr(line, '\n');
77 if (line_end)
78 len = line_end-line;
79 else
80 len = strlen(line);
82 telnet_write(connection, line, len);
83 if (line_end)
85 telnet_write(connection, "\r\n", 2);
86 line += len + 1;
88 else
90 line += len;
94 return ERROR_OK;
97 static int telnet_output(struct command_context *cmd_ctx, const char* line)
99 struct connection *connection = cmd_ctx->output_handler_priv;
101 return telnet_outputline(connection, line);
104 static void telnet_log_callback(void *priv, const char *file, unsigned line,
105 const char *function, const char *string)
107 struct connection *connection = priv;
108 struct telnet_connection *t_con = connection->priv;
109 int i;
111 /* if there is no prompt, simply output the message */
112 if (t_con->line_cursor < 0)
114 telnet_outputline(connection, string);
115 return;
118 /* clear the command line */
119 for (i = strlen(t_con->prompt) + t_con->line_size; i > 0; i -= 16)
120 telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", i > 16 ? 16 : i);
121 for (i = strlen(t_con->prompt) + t_con->line_size; i > 0; i -= 16)
122 telnet_write(connection, " ", i > 16 ? 16 : i);
123 for (i = strlen(t_con->prompt) + t_con->line_size; i > 0; i -= 16)
124 telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", i > 16 ? 16 : i);
126 /* output the message */
127 telnet_outputline(connection, string);
129 /* put the command line to its previous state */
130 telnet_prompt(connection);
131 telnet_write(connection, t_con->line, t_con->line_size);
132 for (i = t_con->line_size; i > t_con->line_cursor; i--)
133 telnet_write(connection, "\b", 1);
136 static int telnet_new_connection(struct connection *connection)
138 struct telnet_connection *telnet_connection = malloc(sizeof(struct telnet_connection));
139 struct telnet_service *telnet_service = connection->service->priv;
140 int i;
142 connection->priv = telnet_connection;
144 /* initialize telnet connection information */
145 telnet_connection->closed = 0;
146 telnet_connection->line_size = 0;
147 telnet_connection->line_cursor = 0;
148 telnet_connection->option_size = 0;
149 telnet_connection->prompt = strdup("> ");
150 telnet_connection->state = TELNET_STATE_DATA;
152 /* output goes through telnet connection */
153 command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
155 /* negotiate telnet options */
156 telnet_write(connection, negotiate, strlen(negotiate));
158 /* print connection banner */
159 if (telnet_service->banner)
161 telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));
162 telnet_write(connection, "\r\n", 2);
165 telnet_write(connection, "\r", 1); /* the prompt is always placed at the line beginning */
166 telnet_prompt(connection);
168 /* initialize history */
169 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
171 telnet_connection->history[i] = NULL;
173 telnet_connection->next_history = 0;
174 telnet_connection->current_history = 0;
176 log_add_callback(telnet_log_callback, connection);
178 return ERROR_OK;
181 static void telnet_clear_line(struct connection *connection,
182 struct telnet_connection *t_con)
184 /* move to end of line */
185 if (t_con->line_cursor < t_con->line_size)
187 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
190 /* backspace, overwrite with space, backspace */
191 while (t_con->line_size > 0)
193 telnet_write(connection, "\b \b", 3);
194 t_con->line_size--;
196 t_con->line_cursor = 0;
199 static int telnet_input(struct connection *connection)
201 int bytes_read;
202 unsigned char buffer[TELNET_BUFFER_SIZE];
203 unsigned char *buf_p;
204 struct telnet_connection *t_con = connection->priv;
205 struct command_context *command_context = connection->cmd_ctx;
207 bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
209 if (bytes_read == 0)
210 return ERROR_SERVER_REMOTE_CLOSED;
211 else if (bytes_read == -1)
213 LOG_ERROR("error during read: %s", strerror(errno));
214 return ERROR_SERVER_REMOTE_CLOSED;
217 buf_p = buffer;
218 while (bytes_read)
220 switch (t_con->state)
222 case TELNET_STATE_DATA:
223 if (*buf_p == 0xff)
225 t_con->state = TELNET_STATE_IAC;
227 else
229 if (isprint(*buf_p)) /* printable character */
231 /* watch buffer size leaving one spare character for string null termination */
232 if (t_con->line_size == TELNET_LINE_MAX_SIZE-1)
234 /* output audible bell if buffer is full */
235 telnet_write(connection, "\x07", 1); /* "\a" does not work, at least on windows */
237 else if (t_con->line_cursor == t_con->line_size)
239 telnet_write(connection, buf_p, 1);
240 t_con->line[t_con->line_size++] = *buf_p;
241 t_con->line_cursor++;
243 else
245 int i;
246 memmove(t_con->line + t_con->line_cursor + 1, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
247 t_con->line[t_con->line_cursor] = *buf_p;
248 t_con->line_size++;
249 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
250 t_con->line_cursor++;
251 for (i = t_con->line_cursor; i < t_con->line_size; i++)
253 telnet_write(connection, "\b", 1);
257 else /* non-printable */
259 if (*buf_p == 0x1b) /* escape */
261 t_con->state = TELNET_STATE_ESCAPE;
262 t_con->last_escape = '\x00';
264 else if ((*buf_p == 0xd) || (*buf_p == 0xa)) /* CR/LF */
266 int retval;
268 /* skip over combinations with CR/LF and NUL characters */
269 if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) || (*(buf_p + 1) == 0xd)))
271 buf_p++;
272 bytes_read--;
274 if ((bytes_read > 1) && (*(buf_p + 1) == 0))
276 buf_p++;
277 bytes_read--;
279 t_con->line[t_con->line_size] = 0;
281 telnet_write(connection, "\r\n\x00", 3);
283 if (strcmp(t_con->line, "history") == 0)
285 int i;
286 for (i = 1; i < TELNET_LINE_HISTORY_SIZE; i++)
288 /* the t_con->next_history line contains empty string (unless NULL), thus it is not printed */
289 char *history_line = t_con->history[(t_con->next_history + i) % TELNET_LINE_HISTORY_SIZE];
290 if (history_line)
292 telnet_write(connection, history_line, strlen(history_line));
293 telnet_write(connection, "\r\n\x00", 3);
296 t_con->line_size = 0;
297 t_con->line_cursor = 0;
298 continue;
301 /* save only non-blank not repeating lines in the history */
302 char *prev_line = t_con->history[(t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1];
303 if (*t_con->line && (prev_line == NULL || strcmp(t_con->line, prev_line)))
305 /* if the history slot is already taken, free it */
306 if (t_con->history[t_con->next_history])
308 free(t_con->history[t_con->next_history]);
311 /* add line to history */
312 t_con->history[t_con->next_history] = strdup(t_con->line);
314 /* wrap history at TELNET_LINE_HISTORY_SIZE */
315 t_con->next_history = (t_con->next_history + 1) % TELNET_LINE_HISTORY_SIZE;
317 /* current history line starts at the new entry */
318 t_con->current_history = t_con->next_history;
320 if (t_con->history[t_con->current_history])
322 free(t_con->history[t_con->current_history]);
324 t_con->history[t_con->current_history] = strdup("");
327 t_con->line_size = 0;
329 t_con->line_cursor = -1; /* to supress prompt in log callback during command execution */
331 retval = command_run_line(command_context, t_con->line);
333 t_con->line_cursor = 0;
335 if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
336 return ERROR_SERVER_REMOTE_CLOSED;
338 telnet_write(connection, "\r", 1); /* the prompt is always placed at the line beginning */
339 retval = telnet_prompt(connection);
340 if (retval == ERROR_SERVER_REMOTE_CLOSED)
341 return ERROR_SERVER_REMOTE_CLOSED;
344 else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) /* delete character */
346 if (t_con->line_cursor > 0)
348 if (t_con->line_cursor != t_con->line_size)
350 int i;
351 telnet_write(connection, "\b", 1);
352 t_con->line_cursor--;
353 t_con->line_size--;
354 memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);
356 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
357 telnet_write(connection, " \b", 2);
358 for (i = t_con->line_cursor; i < t_con->line_size; i++)
360 telnet_write(connection, "\b", 1);
363 else
365 t_con->line_size--;
366 t_con->line_cursor--;
367 /* back space: move the 'printer' head one char back, overwrite with space, move back again */
368 telnet_write(connection, "\b \b", 3);
372 else if (*buf_p == 0x15) /* clear line */
374 telnet_clear_line(connection, t_con);
376 else if (*buf_p == CTRL('B')) /* cursor left */
378 if (t_con->line_cursor > 0)
380 telnet_write(connection, "\b", 1);
381 t_con->line_cursor--;
383 t_con->state = TELNET_STATE_DATA;
385 else if (*buf_p == CTRL('F')) /* cursor right */
387 if (t_con->line_cursor < t_con->line_size)
389 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
391 t_con->state = TELNET_STATE_DATA;
393 else
395 LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p);
399 break;
400 case TELNET_STATE_IAC:
401 switch (*buf_p)
403 case 0xfe:
404 t_con->state = TELNET_STATE_DONT;
405 break;
406 case 0xfd:
407 t_con->state = TELNET_STATE_DO;
408 break;
409 case 0xfc:
410 t_con->state = TELNET_STATE_WONT;
411 break;
412 case 0xfb:
413 t_con->state = TELNET_STATE_WILL;
414 break;
416 break;
417 case TELNET_STATE_SB:
418 break;
419 case TELNET_STATE_SE:
420 break;
421 case TELNET_STATE_WILL:
422 case TELNET_STATE_WONT:
423 case TELNET_STATE_DO:
424 case TELNET_STATE_DONT:
425 t_con->state = TELNET_STATE_DATA;
426 break;
427 case TELNET_STATE_ESCAPE:
428 if (t_con->last_escape == '[')
430 if (*buf_p == 'D') /* cursor left */
432 if (t_con->line_cursor > 0)
434 telnet_write(connection, "\b", 1);
435 t_con->line_cursor--;
437 t_con->state = TELNET_STATE_DATA;
439 else if (*buf_p == 'C') /* cursor right */
441 if (t_con->line_cursor < t_con->line_size)
443 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
445 t_con->state = TELNET_STATE_DATA;
447 else if (*buf_p == 'A') /* cursor up */
449 int last_history = (t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1;
450 if (t_con->history[last_history])
452 telnet_clear_line(connection, t_con);
453 t_con->line_size = strlen(t_con->history[last_history]);
454 t_con->line_cursor = t_con->line_size;
455 memcpy(t_con->line, t_con->history[last_history], t_con->line_size);
456 telnet_write(connection, t_con->line, t_con->line_size);
457 t_con->current_history = last_history;
459 t_con->state = TELNET_STATE_DATA;
461 else if (*buf_p == 'B') /* cursor down */
463 int next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
464 if (t_con->history[next_history])
466 telnet_clear_line(connection, t_con);
467 t_con->line_size = strlen(t_con->history[next_history]);
468 t_con->line_cursor = t_con->line_size;
469 memcpy(t_con->line, t_con->history[next_history], t_con->line_size);
470 telnet_write(connection, t_con->line, t_con->line_size);
471 t_con->current_history = next_history;
473 t_con->state = TELNET_STATE_DATA;
475 else if (*buf_p == '3')
477 t_con->last_escape = *buf_p;
479 else
481 t_con->state = TELNET_STATE_DATA;
484 else if (t_con->last_escape == '3')
486 /* Remove character */
487 if (*buf_p == '~')
489 if (t_con->line_cursor < t_con->line_size)
491 int i;
492 t_con->line_size--;
493 /* remove char from line buffer */
494 memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);
496 /* print remainder of buffer */
497 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
498 /* overwrite last char with whitespace */
499 telnet_write(connection, " \b", 2);
501 /* move back to cursor position*/
502 for (i = t_con->line_cursor; i < t_con->line_size; i++)
504 telnet_write(connection, "\b", 1);
508 t_con->state = TELNET_STATE_DATA;
510 else
512 t_con->state = TELNET_STATE_DATA;
515 else if (t_con->last_escape == '\x00')
517 if (*buf_p == '[')
519 t_con->last_escape = *buf_p;
521 else
523 t_con->state = TELNET_STATE_DATA;
526 else
528 LOG_ERROR("BUG: unexpected value in t_con->last_escape");
529 t_con->state = TELNET_STATE_DATA;
532 break;
533 default:
534 LOG_ERROR("unknown telnet state");
535 exit(-1);
538 bytes_read--;
539 buf_p++;
542 return ERROR_OK;
545 static int telnet_connection_closed(struct connection *connection)
547 struct telnet_connection *t_con = connection->priv;
548 int i;
550 log_remove_callback(telnet_log_callback, connection);
552 if (t_con->prompt)
554 free(t_con->prompt);
555 t_con->prompt = NULL;
558 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
560 if (t_con->history[i])
562 free(t_con->history[i]);
563 t_con->history[i] = NULL;
567 /* if this connection registered a debug-message receiver delete it */
568 delete_debug_msg_receiver(connection->cmd_ctx, NULL);
570 if (connection->priv)
572 free(connection->priv);
573 connection->priv = NULL;
575 else
577 LOG_ERROR("BUG: connection->priv == NULL");
580 return ERROR_OK;
583 int telnet_init(char *banner)
585 if (strcmp(telnet_port, "disabled") == 0)
587 LOG_INFO("telnet server disabled");
588 return ERROR_OK;
591 struct telnet_service *telnet_service = malloc(sizeof(struct telnet_service));
593 telnet_service->banner = banner;
595 add_service_pipe("telnet", telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service);
597 return ERROR_OK;
600 /* daemon configuration command telnet_port */
601 COMMAND_HANDLER(handle_telnet_port_command)
603 return CALL_COMMAND_HANDLER(server_pipe_command, &telnet_port);
606 COMMAND_HANDLER(handle_exit_command)
608 return ERROR_COMMAND_CLOSE_CONNECTION;
611 static const struct command_registration telnet_command_handlers[] = {
613 .name = "exit",
614 .handler = handle_exit_command,
615 .mode = COMMAND_EXEC,
616 .help = "exit telnet session",
619 .name = "telnet_port",
620 .handler = handle_telnet_port_command,
621 .mode = COMMAND_ANY,
622 .help = "Specify port on which to listen "
623 "for incoming telnet connections. "
624 "No arguments reports telnet port; zero disables.",
625 .usage = "[port_num]",
627 COMMAND_REGISTRATION_DONE
630 int telnet_register_commands(struct command_context *cmd_ctx)
632 telnet_port = strdup("4444");
633 return register_commands(cmd_ctx, NULL, telnet_command_handlers);