1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
26 #include "telnet_server.h"
32 #include "target_request.h"
40 static unsigned short telnet_port
= 0;
42 int handle_exit_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
43 int handle_telnet_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
45 static char *negotiate
=
46 "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
47 "\xFF\xFB\x01" /* IAC WILL Echo */
48 "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
49 "\xFF\xFE\x01"; /* IAC DON'T Echo */
51 #define CTRL(c) (c - '@')
53 /* The only way we can detect that the socket is closed is the first time
54 * we write to it, we will fail. Subsequent write operations will
57 int telnet_write(connection_t
*connection
, const void *data
, int len
)
59 telnet_connection_t
*t_con
= connection
->priv
;
61 return ERROR_SERVER_REMOTE_CLOSED
;
63 if (write_socket(connection
->fd
, data
, len
) == len
)
68 return ERROR_SERVER_REMOTE_CLOSED
;
71 int telnet_prompt(connection_t
*connection
)
73 telnet_connection_t
*t_con
= connection
->priv
;
75 return telnet_write(connection
, t_con
->prompt
, strlen(t_con
->prompt
));
78 int telnet_outputline(connection_t
*connection
, const char *line
)
82 /* process lines in buffer */
84 char *line_end
= strchr(line
, '\n');
91 telnet_write(connection
, line
, len
);
94 telnet_write(connection
, "\r\n\0", 3);
106 int telnet_output(struct command_context_s
*cmd_ctx
, char* line
)
108 connection_t
*connection
= cmd_ctx
->output_handler_priv
;
110 return telnet_outputline(connection
, line
);
113 void telnet_log_callback(void *priv
, const char *file
, int line
,
114 const char *function
, const char *string
)
116 connection_t
*connection
= priv
;
118 telnet_write(connection
, "\b\b \b\b", strlen("\b\b \b\b"));
120 telnet_outputline(connection
, string
);
122 telnet_prompt(connection
);
125 int telnet_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
)
129 case TARGET_EVENT_HALTED
:
130 target_arch_state(target
);
139 int telnet_new_connection(connection_t
*connection
)
141 telnet_connection_t
*telnet_connection
= malloc(sizeof(telnet_connection_t
));
142 telnet_service_t
*telnet_service
= connection
->service
->priv
;
145 connection
->priv
= telnet_connection
;
147 /* initialize telnet connection information */
148 telnet_connection
->closed
= 0;
149 telnet_connection
->line_size
= 0;
150 telnet_connection
->line_cursor
= 0;
151 telnet_connection
->option_size
= 0;
152 telnet_connection
->prompt
= strdup("> ");
153 telnet_connection
->state
= TELNET_STATE_DATA
;
155 /* output goes through telnet connection */
156 command_set_output_handler(connection
->cmd_ctx
, telnet_output
, connection
);
158 /* negotiate telnet options */
159 telnet_write(connection
, negotiate
, strlen(negotiate
));
161 /* print connection banner */
162 if (telnet_service
->banner
)
164 telnet_write(connection
, telnet_service
->banner
, strlen(telnet_service
->banner
));
165 telnet_write(connection
, "\r\n\0", 3);
168 telnet_prompt(connection
);
170 /* initialize history */
171 for (i
= 0; i
< TELNET_LINE_HISTORY_SIZE
; i
++)
173 telnet_connection
->history
[i
] = NULL
;
175 telnet_connection
->next_history
= 0;
176 telnet_connection
->current_history
= 0;
178 target_register_event_callback(telnet_target_callback_event_handler
, connection
->cmd_ctx
);
180 log_add_callback(telnet_log_callback
, connection
);
187 void telnet_clear_line(connection_t
*connection
, telnet_connection_t
*t_con
)
189 /* move to end of line */
190 if (t_con
->line_cursor
< t_con
->line_size
)
192 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
, t_con
->line_size
- t_con
->line_cursor
);
195 /* backspace, overwrite with space, backspace */
196 while (t_con
->line_size
> 0)
198 telnet_write(connection
, "\b \b", 3);
201 t_con
->line_cursor
= 0;
204 int telnet_input(connection_t
*connection
)
207 char buffer
[TELNET_BUFFER_SIZE
];
209 telnet_connection_t
*t_con
= connection
->priv
;
210 command_context_t
*command_context
= connection
->cmd_ctx
;
212 bytes_read
= read_socket(connection
->fd
, buffer
, TELNET_BUFFER_SIZE
);
215 return ERROR_SERVER_REMOTE_CLOSED
;
216 else if (bytes_read
== -1)
218 ERROR("error during read: %s", strerror(errno
));
219 return ERROR_SERVER_REMOTE_CLOSED
;
225 switch (t_con
->state
)
227 case TELNET_STATE_DATA
:
228 if (*buf_p
== '\xff')
230 t_con
->state
= TELNET_STATE_IAC
;
234 if (isprint(*buf_p
)) /* printable character */
236 telnet_write(connection
, buf_p
, 1);
237 if (t_con
->line_cursor
== t_con
->line_size
)
239 t_con
->line
[t_con
->line_size
++] = *buf_p
;
240 t_con
->line_cursor
++;
245 memmove(t_con
->line
+ t_con
->line_cursor
+ 1, t_con
->line
+ t_con
->line_cursor
, t_con
->line_size
- t_con
->line_cursor
);
246 t_con
->line
[t_con
->line_cursor
++] = *buf_p
;
248 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
, t_con
->line_size
- t_con
->line_cursor
);
249 for (i
= t_con
->line_cursor
; i
< t_con
->line_size
; i
++)
251 telnet_write(connection
, "\b", 1);
255 else /* non-printable */
257 if (*buf_p
== 0x1b) /* escape */
259 t_con
->state
= TELNET_STATE_ESCAPE
;
260 t_con
->last_escape
= '\x00';
262 else if ((*buf_p
== 0xd) || (*buf_p
== 0xa)) /* CR/LF */
266 /* skip over combinations with CR/LF + NUL */
267 if (((*(buf_p
+ 1) == 0xa) || (*(buf_p
+ 1) == 0xd)) && (bytes_read
> 1))
272 if ((*(buf_p
+ 1) == 0) && (bytes_read
> 1))
277 t_con
->line
[t_con
->line_size
] = 0;
279 telnet_write(connection
, "\r\n\x00", 3);
281 if (strcmp(t_con
->line
, "history") == 0)
284 for (i
= 0; i
< TELNET_LINE_HISTORY_SIZE
; i
++)
286 if (t_con
->history
[i
])
288 telnet_write(connection
, t_con
->history
[i
], strlen(t_con
->history
[i
]));
289 telnet_write(connection
, "\r\n\x00", 3);
292 telnet_prompt(connection
);
293 t_con
->line_size
= 0;
294 t_con
->line_cursor
= 0;
298 retval
= command_run_line(command_context
, t_con
->line
);
300 if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
302 return ERROR_SERVER_REMOTE_CLOSED
;
305 /* Save only non-blank lines in the history */
306 if (t_con
->line_size
> 0)
308 /* if the history slot is already taken, free it */
309 if (t_con
->history
[t_con
->next_history
])
311 free(t_con
->history
[t_con
->next_history
]);
314 /* add line to history */
315 t_con
->history
[t_con
->next_history
] = strdup(t_con
->line
);
317 /* wrap history at TELNET_LINE_HISTORY_SIZE */
318 t_con
->next_history
= (t_con
->next_history
+ 1) % TELNET_LINE_HISTORY_SIZE
;
320 /* current history line starts at the new entry */
321 t_con
->current_history
= t_con
->next_history
;
323 if (t_con
->history
[t_con
->current_history
])
325 free(t_con
->history
[t_con
->current_history
]);
327 t_con
->history
[t_con
->current_history
] = strdup("");
330 int t
= telnet_prompt(connection
);
331 if (t
== ERROR_SERVER_REMOTE_CLOSED
)
334 t_con
->line_size
= 0;
335 t_con
->line_cursor
= 0;
337 else if ((*buf_p
== 0x7f) || (*buf_p
== 0x8)) /* delete character */
339 if (t_con
->line_cursor
> 0)
341 if (t_con
->line_cursor
!= t_con
->line_size
)
344 telnet_write(connection
, "\b", 1);
345 t_con
->line_cursor
--;
347 memmove(t_con
->line
+ t_con
->line_cursor
, t_con
->line
+ t_con
->line_cursor
+ 1, t_con
->line_size
- t_con
->line_cursor
);
349 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
, t_con
->line_size
- t_con
->line_cursor
);
350 telnet_write(connection
, " \b", 2);
351 for (i
= t_con
->line_cursor
; i
< t_con
->line_size
; i
++)
353 telnet_write(connection
, "\b", 1);
359 t_con
->line_cursor
--;
360 /* back space: move the 'printer' head one char back, overwrite with space, move back again */
361 telnet_write(connection
, "\b \b", 3);
365 else if (*buf_p
== 0x15) /* clear line */
367 telnet_clear_line(connection
, t_con
);
369 else if (*buf_p
== CTRL('B')) /* cursor left */
371 if (t_con
->line_cursor
> 0)
373 telnet_write(connection
, "\b", 1);
374 t_con
->line_cursor
--;
376 t_con
->state
= TELNET_STATE_DATA
;
378 else if (*buf_p
== CTRL('F')) /* cursor right */
380 if (t_con
->line_cursor
< t_con
->line_size
)
382 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
++, 1);
384 t_con
->state
= TELNET_STATE_DATA
;
388 DEBUG("unhandled nonprintable: %2.2x", *buf_p
);
393 case TELNET_STATE_IAC
:
397 t_con
->state
= TELNET_STATE_DONT
;
400 t_con
->state
= TELNET_STATE_DO
;
403 t_con
->state
= TELNET_STATE_WONT
;
406 t_con
->state
= TELNET_STATE_WILL
;
410 case TELNET_STATE_SB
:
412 case TELNET_STATE_SE
:
414 case TELNET_STATE_WILL
:
415 case TELNET_STATE_WONT
:
416 case TELNET_STATE_DO
:
417 case TELNET_STATE_DONT
:
418 t_con
->state
= TELNET_STATE_DATA
;
420 case TELNET_STATE_ESCAPE
:
421 if (t_con
->last_escape
== '[')
423 if (*buf_p
== 'D') /* cursor left */
425 if (t_con
->line_cursor
> 0)
427 telnet_write(connection
, "\b", 1);
428 t_con
->line_cursor
--;
430 t_con
->state
= TELNET_STATE_DATA
;
432 else if (*buf_p
== 'C') /* cursor right */
434 if (t_con
->line_cursor
< t_con
->line_size
)
436 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
++, 1);
438 t_con
->state
= TELNET_STATE_DATA
;
440 else if (*buf_p
== 'A') /* cursor up */
442 int last_history
= (t_con
->current_history
> 0) ? t_con
->current_history
- 1 : TELNET_LINE_HISTORY_SIZE
-1;
443 if (t_con
->history
[last_history
])
445 telnet_clear_line(connection
, t_con
);
446 t_con
->line_size
= strlen(t_con
->history
[last_history
]);
447 t_con
->line_cursor
= t_con
->line_size
;
448 memcpy(t_con
->line
, t_con
->history
[last_history
], t_con
->line_size
+ 1);
449 telnet_write(connection
, t_con
->line
, t_con
->line_size
);
450 t_con
->current_history
= last_history
;
452 t_con
->state
= TELNET_STATE_DATA
;
454 else if (*buf_p
== 'B') /* cursor down */
456 int next_history
= (t_con
->current_history
+ 1) % TELNET_LINE_HISTORY_SIZE
;
457 if (t_con
->history
[next_history
])
459 telnet_clear_line(connection
, t_con
);
460 t_con
->line_size
= strlen(t_con
->history
[next_history
]);
461 t_con
->line_cursor
= t_con
->line_size
;
462 memcpy(t_con
->line
, t_con
->history
[next_history
], t_con
->line_size
+ 1);
463 telnet_write(connection
, t_con
->line
, t_con
->line_size
);
464 t_con
->current_history
= next_history
;
466 t_con
->state
= TELNET_STATE_DATA
;
468 else if (*buf_p
== '3')
470 t_con
->last_escape
= *buf_p
;
474 t_con
->state
= TELNET_STATE_DATA
;
477 else if (t_con
->last_escape
== '3')
479 /* Remove character */
482 if (t_con
->line_cursor
< t_con
->line_size
)
486 /* remove char from line buffer */
487 memmove(t_con
->line
+ t_con
->line_cursor
, t_con
->line
+ t_con
->line_cursor
+ 1, t_con
->line_size
- t_con
->line_cursor
);
489 /* print remainder of buffer */
490 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
, t_con
->line_size
- t_con
->line_cursor
);
491 /* overwrite last char with whitespace */
492 telnet_write(connection
, " \b", 2);
494 /* move back to cursor position*/
495 for (i
= t_con
->line_cursor
; i
< t_con
->line_size
; i
++)
497 telnet_write(connection
, "\b", 1);
501 t_con
->state
= TELNET_STATE_DATA
;
505 t_con
->state
= TELNET_STATE_DATA
;
508 else if (t_con
->last_escape
== '\x00')
512 t_con
->last_escape
= *buf_p
;
516 t_con
->state
= TELNET_STATE_DATA
;
521 ERROR("BUG: unexpected value in t_con->last_escape");
522 t_con
->state
= TELNET_STATE_DATA
;
527 ERROR("unknown telnet state");
538 int telnet_connection_closed(connection_t
*connection
)
540 telnet_connection_t
*t_con
= connection
->priv
;
543 log_remove_callback(telnet_log_callback
, connection
);
548 t_con
->prompt
= NULL
;
551 for (i
= 0; i
< TELNET_LINE_HISTORY_SIZE
; i
++)
553 if (t_con
->history
[i
])
555 free(t_con
->history
[i
]);
556 t_con
->history
[i
] = NULL
;
560 /* if this connection registered a debug-message receiver delete it */
561 delete_debug_msg_receiver(connection
->cmd_ctx
, NULL
);
563 if (connection
->priv
)
565 free(connection
->priv
);
566 connection
->priv
= NULL
;
570 ERROR("BUG: connection->priv == NULL");
573 target_unregister_event_callback(telnet_target_callback_event_handler
, connection
->cmd_ctx
);
578 int telnet_set_prompt(connection_t
*connection
, char *prompt
)
580 telnet_connection_t
*t_con
= connection
->priv
;
582 t_con
->prompt
= strdup(prompt
);
587 int telnet_init(char *banner
)
589 telnet_service_t
*telnet_service
= malloc(sizeof(telnet_service_t
));
591 if (telnet_port
== 0)
593 WARNING("no telnet port specified, using default port 4444");
597 telnet_service
->banner
= banner
;
599 add_service("telnet", CONNECTION_TELNET
, telnet_port
, 1, telnet_new_connection
, telnet_input
, telnet_connection_closed
, telnet_service
);
604 int telnet_register_commands(command_context_t
*command_context
)
606 register_command(command_context
, NULL
, "exit", handle_exit_command
,
607 COMMAND_EXEC
, "exit telnet session");
609 register_command(command_context
, NULL
, "telnet_port", handle_telnet_port_command
,
615 /* daemon configuration command telnet_port */
616 int handle_telnet_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
621 telnet_port
= strtoul(args
[0], NULL
, 0);
626 int handle_exit_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
628 return ERROR_COMMAND_CLOSE_CONNECTION
;
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)