1 /***************************************************************************
2 * Copyright (C) 2014 by Franck Jullien *
3 * franck.jullien@gmail.com *
5 * Based on ./src/server/telnet_server.c *
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. *
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. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
21 ***************************************************************************/
27 #include <server/telnet_server.h>
31 #include "jsp_server.h"
33 static char *jsp_port;
35 /**A skim of the relevant RFCs suggests that if my application simply sent the
36 * characters IAC DONT LINEMODE (\377\376\042) as soon as the client connects,
37 * the client should be forced into character mode. However it doesn't make any difference.
40 static const char * const negotiate =
41 "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
42 "\xFF\xFB\x01" /* IAC WILL Echo */
43 "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
44 "\xFF\xFE\x01"; /* IAC DON'T Echo */
46 /* The only way we can detect that the socket is closed is the first time
47 * we write to it, we will fail. Subsequent write operations will
50 static int telnet_write(struct connection *connection, const void *data, int len)
52 struct telnet_connection *t_con = connection->priv;
54 return ERROR_SERVER_REMOTE_CLOSED;
56 if (connection_write(connection, data, len) == len)
59 return ERROR_SERVER_REMOTE_CLOSED;
62 int jsp_poll_read(void *priv)
64 struct jsp_service *jsp_service = (struct jsp_service *)priv;
65 unsigned char out_buffer[10];
66 unsigned char in_buffer[10];
70 if (!jsp_service->connection)
73 memset(out_buffer, 0, 10);
75 or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info, &out_len, out_buffer, &in_len, in_buffer);
77 telnet_write(jsp_service->connection, in_buffer, in_len);
82 static int jsp_new_connection(struct connection *connection)
84 struct telnet_connection *telnet_connection = malloc(sizeof(struct telnet_connection));
85 struct jsp_service *jsp_service = connection->service->priv;
87 connection->priv = telnet_connection;
89 /* initialize telnet connection information */
90 telnet_connection->closed = 0;
91 telnet_connection->line_size = 0;
92 telnet_connection->line_cursor = 0;
93 telnet_connection->option_size = 0;
94 telnet_connection->state = TELNET_STATE_DATA;
96 /* negotiate telnet options */
97 telnet_write(connection, negotiate, strlen(negotiate));
99 /* print connection banner */
100 if (jsp_service->banner) {
101 telnet_write(connection, jsp_service->banner, strlen(jsp_service->banner));
102 telnet_write(connection, "\r\n", 2);
105 jsp_service->connection = connection;
107 int retval = target_register_timer_callback(&jsp_poll_read, 1, 1, jsp_service);
108 if (ERROR_OK != retval)
114 static int jsp_input(struct connection *connection)
117 unsigned char buffer[TELNET_BUFFER_SIZE];
118 unsigned char *buf_p;
119 struct telnet_connection *t_con = connection->priv;
120 struct jsp_service *jsp_service = connection->service->priv;
122 bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
125 return ERROR_SERVER_REMOTE_CLOSED;
126 else if (bytes_read == -1) {
127 LOG_ERROR("error during read: %s", strerror(errno));
128 return ERROR_SERVER_REMOTE_CLOSED;
133 switch (t_con->state) {
134 case TELNET_STATE_DATA:
136 t_con->state = TELNET_STATE_IAC;
140 unsigned char in_buffer[10];
141 or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info,
142 &out_len, buf_p, &in_len,
145 telnet_write(connection,
149 case TELNET_STATE_IAC:
152 t_con->state = TELNET_STATE_DONT;
155 t_con->state = TELNET_STATE_DO;
158 t_con->state = TELNET_STATE_WONT;
161 t_con->state = TELNET_STATE_WILL;
165 case TELNET_STATE_SB:
167 case TELNET_STATE_SE:
169 case TELNET_STATE_WILL:
170 case TELNET_STATE_WONT:
171 case TELNET_STATE_DO:
172 case TELNET_STATE_DONT:
173 t_con->state = TELNET_STATE_DATA;
176 LOG_ERROR("unknown telnet state");
187 static int jsp_connection_closed(struct connection *connection)
189 struct telnet_connection *t_con = connection->priv;
190 struct jsp_service *jsp_service = connection->service->priv;
194 t_con->prompt = NULL;
197 int retval = target_unregister_timer_callback(&jsp_poll_read, jsp_service);
198 if (ERROR_OK != retval)
201 if (connection->priv) {
202 free(connection->priv);
203 connection->priv = NULL;
205 LOG_ERROR("BUG: connection->priv == NULL");
210 int jsp_init(struct or1k_jtag *jtag_info, char *banner)
212 struct jsp_service *jsp_service = malloc(sizeof(struct jsp_service));
213 jsp_service->banner = banner;
214 jsp_service->jtag_info = jtag_info;
216 return add_service("jsp",
221 jsp_connection_closed,
225 COMMAND_HANDLER(handle_jsp_port_command)
227 return CALL_COMMAND_HANDLER(server_pipe_command, &jsp_port);
230 static const struct command_registration jsp_command_handlers[] = {
233 .handler = handle_jsp_port_command,
235 .help = "Specify port on which to listen "
236 "for incoming JSP telnet connections.",
237 .usage = "[port_num]",
239 COMMAND_REGISTRATION_DONE
242 int jsp_register_commands(struct command_context *cmd_ctx)
244 jsp_port = strdup("7777");
245 return register_commands(cmd_ctx, NULL, jsp_command_handlers);