]> git.sur5r.net Git - openocd/blob - src/target/openrisc/jsp_server.c
telnet_server: drop unused options
[openocd] / src / target / openrisc / jsp_server.c
1 /***************************************************************************
2  *   Copyright (C) 2014 by Franck Jullien                                  *
3  *   franck.jullien@gmail.com                                              *
4  *                                                                         *
5  *   Based on ./src/server/telnet_server.c                                 *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
19  ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <server/telnet_server.h>
26
27 #include "or1k_tap.h"
28 #include "or1k_du.h"
29 #include "jsp_server.h"
30
31 static char *jsp_port;
32
33 /**A skim of the relevant RFCs suggests that if my application simply sent the
34  * characters IAC DONT LINEMODE (\377\376\042) as soon as the client connects,
35  * the client should be forced into character mode. However it doesn't make any difference.
36  */
37
38 static const char * const negotiate =
39         "\xFF\xFB\x03"                  /* IAC WILL Suppress Go Ahead */
40         "\xFF\xFB\x01"                  /* IAC WILL Echo */
41         "\xFF\xFD\x03"                  /* IAC DO Suppress Go Ahead */
42         "\xFF\xFE\x01";                 /* IAC DON'T Echo */
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, int len)
49 {
50         struct telnet_connection *t_con = connection->priv;
51         if (t_con->closed)
52                 return ERROR_SERVER_REMOTE_CLOSED;
53
54         if (connection_write(connection, data, len) == len)
55                 return ERROR_OK;
56         t_con->closed = 1;
57         return ERROR_SERVER_REMOTE_CLOSED;
58 }
59
60 int jsp_poll_read(void *priv)
61 {
62         struct jsp_service *jsp_service = (struct jsp_service *)priv;
63         unsigned char out_buffer[10];
64         unsigned char in_buffer[10];
65         int out_len = 0;
66         int in_len;
67
68         if (!jsp_service->connection)
69                 return ERROR_FAIL;
70
71         memset(out_buffer, 0, 10);
72
73         or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info, &out_len, out_buffer, &in_len, in_buffer);
74         if (in_len)
75                 telnet_write(jsp_service->connection, in_buffer, in_len);
76
77         return ERROR_OK;
78 }
79
80 static int jsp_new_connection(struct connection *connection)
81 {
82         struct telnet_connection *telnet_connection = malloc(sizeof(struct telnet_connection));
83         struct jsp_service *jsp_service = connection->service->priv;
84
85         connection->priv = telnet_connection;
86
87         /* initialize telnet connection information */
88         telnet_connection->closed = 0;
89         telnet_connection->line_size = 0;
90         telnet_connection->line_cursor = 0;
91         telnet_connection->state = TELNET_STATE_DATA;
92
93         /* negotiate telnet options */
94         telnet_write(connection, negotiate, strlen(negotiate));
95
96         /* print connection banner */
97         if (jsp_service->banner) {
98                 telnet_write(connection, jsp_service->banner, strlen(jsp_service->banner));
99                 telnet_write(connection, "\r\n", 2);
100         }
101
102         jsp_service->connection = connection;
103
104         int retval = target_register_timer_callback(&jsp_poll_read, 1, 1, jsp_service);
105         if (ERROR_OK != retval)
106                 return retval;
107
108         return ERROR_OK;
109 }
110
111 static int jsp_input(struct connection *connection)
112 {
113         int bytes_read;
114         unsigned char buffer[TELNET_BUFFER_SIZE];
115         unsigned char *buf_p;
116         struct telnet_connection *t_con = connection->priv;
117         struct jsp_service *jsp_service = connection->service->priv;
118
119         bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
120
121         if (bytes_read == 0)
122                 return ERROR_SERVER_REMOTE_CLOSED;
123         else if (bytes_read == -1) {
124                 LOG_ERROR("error during read: %s", strerror(errno));
125                 return ERROR_SERVER_REMOTE_CLOSED;
126         }
127
128         buf_p = buffer;
129         while (bytes_read) {
130                 switch (t_con->state) {
131                         case TELNET_STATE_DATA:
132                                 if (*buf_p == 0xff)
133                                         t_con->state = TELNET_STATE_IAC;
134                                 else {
135                                         int out_len = 1;
136                                         int in_len;
137                                         unsigned char in_buffer[10];
138                                         or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info,
139                                                                &out_len, buf_p, &in_len,
140                                                                in_buffer);
141                                         if (in_len)
142                                                 telnet_write(connection,
143                                                              in_buffer, in_len);
144                                 }
145                                 break;
146                         case TELNET_STATE_IAC:
147                                 switch (*buf_p) {
148                                 case 0xfe:
149                                         t_con->state = TELNET_STATE_DONT;
150                                         break;
151                                 case 0xfd:
152                                         t_con->state = TELNET_STATE_DO;
153                                         break;
154                                 case 0xfc:
155                                         t_con->state = TELNET_STATE_WONT;
156                                         break;
157                                 case 0xfb:
158                                         t_con->state = TELNET_STATE_WILL;
159                                         break;
160                                 }
161                                 break;
162                         case TELNET_STATE_SB:
163                                 break;
164                         case TELNET_STATE_SE:
165                                 break;
166                         case TELNET_STATE_WILL:
167                         case TELNET_STATE_WONT:
168                         case TELNET_STATE_DO:
169                         case TELNET_STATE_DONT:
170                                 t_con->state = TELNET_STATE_DATA;
171                                 break;
172                         default:
173                                 LOG_ERROR("unknown telnet state");
174                                 exit(-1);
175                 }
176
177                 bytes_read--;
178                 buf_p++;
179         }
180
181         return ERROR_OK;
182 }
183
184 static int jsp_connection_closed(struct connection *connection)
185 {
186         struct telnet_connection *t_con = connection->priv;
187         struct jsp_service *jsp_service = connection->service->priv;
188
189         if (t_con->prompt) {
190                 free(t_con->prompt);
191                 t_con->prompt = NULL;
192         }
193
194         int retval = target_unregister_timer_callback(&jsp_poll_read, jsp_service);
195         if (ERROR_OK != retval)
196                 return retval;
197
198         if (connection->priv) {
199                 free(connection->priv);
200                 connection->priv = NULL;
201         } else
202                 LOG_ERROR("BUG: connection->priv == NULL");
203
204         return ERROR_OK;
205 }
206
207 int jsp_init(struct or1k_jtag *jtag_info, char *banner)
208 {
209         struct jsp_service *jsp_service = malloc(sizeof(struct jsp_service));
210         jsp_service->banner = banner;
211         jsp_service->jtag_info = jtag_info;
212
213         return add_service("jsp",
214                 jsp_port,
215                 1,
216                 jsp_new_connection,
217                 jsp_input,
218                 jsp_connection_closed,
219                 jsp_service);
220 }
221
222 COMMAND_HANDLER(handle_jsp_port_command)
223 {
224         return CALL_COMMAND_HANDLER(server_pipe_command, &jsp_port);
225 }
226
227 static const struct command_registration jsp_command_handlers[] = {
228         {
229                 .name = "jsp_port",
230                 .handler = handle_jsp_port_command,
231                 .mode = COMMAND_ANY,
232                 .help = "Specify port on which to listen "
233                         "for incoming JSP telnet connections.",
234                 .usage = "[port_num]",
235         },
236         COMMAND_REGISTRATION_DONE
237 };
238
239 int jsp_register_commands(struct command_context *cmd_ctx)
240 {
241         jsp_port = strdup("7777");
242         return register_commands(cmd_ctx, NULL, jsp_command_handlers);
243 }
244