server/telnet: Handle Ctrl+A and Ctrl+E
[fw/openocd] / src / server / telnet_server.c
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, see <http://www.gnu.org/licenses/>. *
23  ***************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "telnet_server.h"
30 #include <target/target_request.h>
31 #include <helper/configuration.h>
32
33 static char *telnet_port;
34
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 */
40
41 #define CTRL(c) (c - '@')
42 #define TELNET_HISTORY  ".openocd_history"
43
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
46  * succeed. Shudder!
47  */
48 static int telnet_write(struct connection *connection, const void *data,
49         int len)
50 {
51         struct telnet_connection *t_con = connection->priv;
52         if (t_con->closed)
53                 return ERROR_SERVER_REMOTE_CLOSED;
54
55         if (connection_write(connection, data, len) == len)
56                 return ERROR_OK;
57         t_con->closed = 1;
58         return ERROR_SERVER_REMOTE_CLOSED;
59 }
60
61 static int telnet_prompt(struct connection *connection)
62 {
63         struct telnet_connection *t_con = connection->priv;
64
65         return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
66 }
67
68 static int telnet_outputline(struct connection *connection, const char *line)
69 {
70         int len;
71
72         /* process lines in buffer */
73         while (*line) {
74                 char *line_end = strchr(line, '\n');
75
76                 if (line_end)
77                         len = line_end-line;
78                 else
79                         len = strlen(line);
80
81                 telnet_write(connection, line, len);
82                 if (line_end) {
83                         telnet_write(connection, "\r\n", 2);
84                         line += len + 1;
85                 } else
86                         line += len;
87         }
88
89         return ERROR_OK;
90 }
91
92 static int telnet_output(struct command_context *cmd_ctx, const char *line)
93 {
94         struct connection *connection = cmd_ctx->output_handler_priv;
95
96         return telnet_outputline(connection, line);
97 }
98
99 static void telnet_log_callback(void *priv, const char *file, unsigned line,
100         const char *function, const char *string)
101 {
102         struct connection *connection = priv;
103         struct telnet_connection *t_con = connection->priv;
104         int i;
105
106         /* if there is no prompt, simply output the message */
107         if (t_con->line_cursor < 0) {
108                 telnet_outputline(connection, string);
109                 return;
110         }
111
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);
119
120         /* output the message */
121         telnet_outputline(connection, string);
122
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);
128 }
129
130 static void telnet_load_history(struct telnet_connection *t_con)
131 {
132         FILE *histfp;
133         char buffer[TELNET_BUFFER_SIZE];
134         int i = 0;
135
136         char *history = get_home_dir(TELNET_HISTORY);
137
138         if (history == NULL) {
139                 LOG_INFO("unable to get user home directory, telnet history will be disabled");
140                 return;
141         }
142
143         histfp = fopen(history, "rb");
144
145         if (histfp) {
146
147                 while (fgets(buffer, sizeof(buffer), histfp) != NULL) {
148
149                         char *p = strchr(buffer, '\n');
150                         if (p)
151                                 *p = '\0';
152                         if (buffer[0] && i < TELNET_LINE_HISTORY_SIZE)
153                                 t_con->history[i++] = strdup(buffer);
154                 }
155
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;
160                 fclose(histfp);
161         }
162
163         free(history);
164 }
165
166 static void telnet_save_history(struct telnet_connection *t_con)
167 {
168         FILE *histfp;
169         int i;
170         int num;
171
172         char *history = get_home_dir(TELNET_HISTORY);
173
174         if (history == NULL) {
175                 LOG_INFO("unable to get user home directory, telnet history will be disabled");
176                 return;
177         }
178
179         histfp = fopen(history, "wb");
180
181         if (histfp) {
182
183                 num = TELNET_LINE_HISTORY_SIZE;
184                 i = t_con->current_history + 1;
185                 i %= TELNET_LINE_HISTORY_SIZE;
186
187                 while (t_con->history[i] == NULL && num > 0) {
188                         i++;
189                         i %= TELNET_LINE_HISTORY_SIZE;
190                         num--;
191                 }
192
193                 if (num > 0) {
194                         for (; num > 0; num--) {
195                                 fprintf(histfp, "%s\n", t_con->history[i]);
196                                 i++;
197                                 i %= TELNET_LINE_HISTORY_SIZE;
198                         }
199                 }
200                 fclose(histfp);
201         }
202
203         free(history);
204 }
205
206 static int telnet_new_connection(struct connection *connection)
207 {
208         struct telnet_connection *telnet_connection;
209         struct telnet_service *telnet_service = connection->service->priv;
210         int i;
211
212         telnet_connection = malloc(sizeof(struct telnet_connection));
213
214         if (!telnet_connection) {
215                 LOG_ERROR("Failed to allocate telnet connection.");
216                 return ERROR_FAIL;
217         }
218
219         connection->priv = telnet_connection;
220
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;
227
228         /* output goes through telnet connection */
229         command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
230
231         /* negotiate telnet options */
232         telnet_write(connection, negotiate, strlen(negotiate));
233
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);
238         }
239
240         /* the prompt is always placed at the line beginning */
241         telnet_write(connection, "\r", 1);
242         telnet_prompt(connection);
243
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);
250
251         log_add_callback(telnet_log_callback, connection);
252
253         return ERROR_OK;
254 }
255
256 static void telnet_clear_line(struct connection *connection,
257         struct telnet_connection *t_con)
258 {
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);
264
265         /* backspace, overwrite with space, backspace */
266         while (t_con->line_size > 0) {
267                 telnet_write(connection, "\b \b", 3);
268                 t_con->line_size--;
269         }
270         t_con->line_cursor = 0;
271 }
272
273 static void telnet_history_go(struct connection *connection, int idx)
274 {
275         struct telnet_connection *t_con = connection->priv;
276
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;
284         }
285         t_con->state = TELNET_STATE_DATA;
286 }
287
288 static void telnet_history_up(struct connection *connection)
289 {
290         struct telnet_connection *t_con = connection->priv;
291
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);
296 }
297
298 static void telnet_history_down(struct connection *connection)
299 {
300         struct telnet_connection *t_con = connection->priv;
301
302         int next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
303         telnet_history_go(connection, next_history);
304 }
305
306 static void telnet_move_cursor(struct connection *connection, size_t pos)
307 {
308         struct telnet_connection *tc;
309         size_t tmp;
310
311         tc = connection->priv;
312
313         if (pos < tc->line_cursor) {
314                 tmp = tc->line_cursor - pos;
315
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",
318                                 MIN(tmp - i, 16));
319         } else {
320                 tmp = pos - tc->line_cursor;
321
322                 for (size_t i = 0; i < tmp; i += 16)
323                         telnet_write(connection, tc->line + tc->line_cursor + i,
324                                 MIN(tmp - i, 16));
325         }
326
327         tc->line_cursor = pos;
328 }
329
330 static int telnet_input(struct connection *connection)
331 {
332         int bytes_read;
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;
337
338         bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
339
340         if (bytes_read == 0)
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;
345         }
346
347         buf_p = buffer;
348         while (bytes_read) {
349                 switch (t_con->state) {
350                         case TELNET_STATE_DATA:
351                                 if (*buf_p == 0xff)
352                                         t_con->state = TELNET_STATE_IAC;
353                                 else {
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++;
365                                                 } else {
366                                                         int i;
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;
371                                                         t_con->line_size++;
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);
378                                                 }
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 */
384                                                         int retval;
385
386                                                         /* skip over combinations with CR/LF and NUL characters */
387                                                         if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) ||
388                                                                         (*(buf_p + 1) == 0xd))) {
389                                                                 buf_p++;
390                                                                 bytes_read--;
391                                                         }
392                                                         if ((bytes_read > 1) && (*(buf_p + 1) == 0)) {
393                                                                 buf_p++;
394                                                                 bytes_read--;
395                                                         }
396                                                         t_con->line[t_con->line_size] = 0;
397
398                                                         telnet_write(connection, "\r\n\x00", 3);
399
400                                                         if (strcmp(t_con->line, "history") == 0) {
401                                                                 int i;
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->
406                                                                                         next_history + i) %
407                                                                                         TELNET_LINE_HISTORY_SIZE];
408                                                                         if (history_line) {
409                                                                                 telnet_write(connection, history_line,
410                                                                                                 strlen(history_line));
411                                                                                 telnet_write(connection, "\r\n\x00", 3);
412                                                                         }
413                                                                 }
414                                                                 t_con->line_size = 0;
415                                                                 t_con->line_cursor = 0;
416                                                                 continue;
417                                                         }
418
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]);
427
428                                                                 /* add line to history */
429                                                                 t_con->history[t_con->next_history] = strdup(t_con->line);
430
431                                                                 /* wrap history at TELNET_LINE_HISTORY_SIZE */
432                                                                 t_con->next_history = (t_con->next_history + 1) %
433                                                                                 TELNET_LINE_HISTORY_SIZE;
434
435                                                                 /* current history line starts at the new entry */
436                                                                 t_con->current_history =
437                                                                                 t_con->next_history;
438
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("");
442                                                         }
443
444                                                         t_con->line_size = 0;
445
446                                                         /* to suppress prompt in log callback during command execution */
447                                                         t_con->line_cursor = -1;
448
449                                                         if (strcmp(t_con->line, "shutdown") == 0)
450                                                                 telnet_save_history(t_con);
451
452                                                         retval = command_run_line(command_context, t_con->line);
453
454                                                         t_con->line_cursor = 0;
455
456                                                         if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
457                                                                 return ERROR_SERVER_REMOTE_CLOSED;
458
459                                                         /* the prompt is always * placed at the line beginning */
460                                                         telnet_write(connection, "\r", 1);
461
462                                                         retval = telnet_prompt(connection);
463                                                         if (retval == ERROR_SERVER_REMOTE_CLOSED)
464                                                                 return ERROR_SERVER_REMOTE_CLOSED;
465
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) {
469                                                                         int i;
470                                                                         telnet_write(connection, "\b", 1);
471                                                                         t_con->line_cursor--;
472                                                                         t_con->line_size--;
473                                                                         memmove(t_con->line + t_con->line_cursor,
474                                                                                         t_con->line + t_con->line_cursor + 1,
475                                                                                         t_con->line_size -
476                                                                                         t_con->line_cursor);
477
478                                                                         telnet_write(connection,
479                                                                                         t_con->line + t_con->line_cursor,
480                                                                                         t_con->line_size -
481                                                                                         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);
485                                                                 } else {
486                                                                         t_con->line_size--;
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);
491                                                                 }
492                                                         }
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--;
499                                                         }
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);
513                                                 else
514                                                         LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p);
515                                         }
516                                 }
517                                 break;
518                         case TELNET_STATE_IAC:
519                                 switch (*buf_p) {
520                                 case 0xfe:
521                                         t_con->state = TELNET_STATE_DONT;
522                                         break;
523                                 case 0xfd:
524                                         t_con->state = TELNET_STATE_DO;
525                                         break;
526                                 case 0xfc:
527                                         t_con->state = TELNET_STATE_WONT;
528                                         break;
529                                 case 0xfb:
530                                         t_con->state = TELNET_STATE_WILL;
531                                         break;
532                                 }
533                                 break;
534                         case TELNET_STATE_SB:
535                                 break;
536                         case TELNET_STATE_SE:
537                                 break;
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;
543                                 break;
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--;
550                                                 }
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;
563                                         else
564                                                 t_con->state = TELNET_STATE_DATA;
565                                 } else if (t_con->last_escape == '3') {
566                                         /* Remove character */
567                                         if (*buf_p == '~') {
568                                                 if (t_con->line_cursor < t_con->line_size) {
569                                                         int i;
570                                                         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);
575
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);
581
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);
585                                                 }
586
587                                                 t_con->state = TELNET_STATE_DATA;
588                                         } else
589                                                 t_con->state = TELNET_STATE_DATA;
590                                 } else if (t_con->last_escape == '\x00') {
591                                         if (*buf_p == '[')
592                                                 t_con->last_escape = *buf_p;
593                                         else
594                                                 t_con->state = TELNET_STATE_DATA;
595                                 } else {
596                                         LOG_ERROR("BUG: unexpected value in t_con->last_escape");
597                                         t_con->state = TELNET_STATE_DATA;
598                                 }
599
600                                 break;
601                         default:
602                                 LOG_ERROR("unknown telnet state");
603                                 return ERROR_FAIL;
604                 }
605
606                 bytes_read--;
607                 buf_p++;
608         }
609
610         return ERROR_OK;
611 }
612
613 static int telnet_connection_closed(struct connection *connection)
614 {
615         struct telnet_connection *t_con = connection->priv;
616         int i;
617
618         log_remove_callback(telnet_log_callback, connection);
619
620         if (t_con->prompt) {
621                 free(t_con->prompt);
622                 t_con->prompt = NULL;
623         }
624
625         /* save telnet history */
626         telnet_save_history(t_con);
627
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;
632                 }
633         }
634
635         /* if this connection registered a debug-message receiver delete it */
636         delete_debug_msg_receiver(connection->cmd_ctx, NULL);
637
638         if (connection->priv) {
639                 free(connection->priv);
640                 connection->priv = NULL;
641         } else
642                 LOG_ERROR("BUG: connection->priv == NULL");
643
644         return ERROR_OK;
645 }
646
647 int telnet_init(char *banner)
648 {
649         if (strcmp(telnet_port, "disabled") == 0) {
650                 LOG_INFO("telnet server disabled");
651                 return ERROR_OK;
652         }
653
654         struct telnet_service *telnet_service =
655                 malloc(sizeof(struct telnet_service));
656
657         if (!telnet_service) {
658                 LOG_ERROR("Failed to allocate telnet service.");
659                 return ERROR_FAIL;
660         }
661
662         telnet_service->banner = banner;
663
664         int ret = add_service("telnet", telnet_port, CONNECTION_LIMIT_UNLIMITED,
665                 telnet_new_connection, telnet_input, telnet_connection_closed,
666                 telnet_service);
667
668         if (ret != ERROR_OK) {
669                 free(telnet_service);
670                 return ret;
671         }
672
673         return ERROR_OK;
674 }
675
676 /* daemon configuration command telnet_port */
677 COMMAND_HANDLER(handle_telnet_port_command)
678 {
679         return CALL_COMMAND_HANDLER(server_pipe_command, &telnet_port);
680 }
681
682 COMMAND_HANDLER(handle_exit_command)
683 {
684         return ERROR_COMMAND_CLOSE_CONNECTION;
685 }
686
687 static const struct command_registration telnet_command_handlers[] = {
688         {
689                 .name = "exit",
690                 .handler = handle_exit_command,
691                 .mode = COMMAND_EXEC,
692                 .usage = "",
693                 .help = "exit telnet session",
694         },
695         {
696                 .name = "telnet_port",
697                 .handler = handle_telnet_port_command,
698                 .mode = COMMAND_ANY,
699                 .help = "Specify port on which to listen "
700                         "for incoming telnet connections.  "
701                         "Read help on 'gdb_port'.",
702                 .usage = "[port_num]",
703         },
704         COMMAND_REGISTRATION_DONE
705 };
706
707 int telnet_register_commands(struct command_context *cmd_ctx)
708 {
709         telnet_port = strdup("4444");
710         return register_commands(cmd_ctx, NULL, telnet_command_handlers);
711 }