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, see <http://www.gnu.org/licenses/>. *
19 ***************************************************************************/
25 #include <server/telnet_server.h>
29 #include "jsp_server.h"
31 static char *jsp_port;
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.
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 */
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
48 static int telnet_write(struct connection *connection, const void *data, int len)
50 struct telnet_connection *t_con = connection->priv;
52 return ERROR_SERVER_REMOTE_CLOSED;
54 if (connection_write(connection, data, len) == len)
57 return ERROR_SERVER_REMOTE_CLOSED;
60 int jsp_poll_read(void *priv)
62 struct jsp_service *jsp_service = (struct jsp_service *)priv;
63 unsigned char out_buffer[10];
64 unsigned char in_buffer[10];
68 if (!jsp_service->connection)
71 memset(out_buffer, 0, 10);
73 or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info, &out_len, out_buffer, &in_len, in_buffer);
75 telnet_write(jsp_service->connection, in_buffer, in_len);
80 static int jsp_new_connection(struct connection *connection)
82 struct telnet_connection *telnet_connection = malloc(sizeof(struct telnet_connection));
83 struct jsp_service *jsp_service = connection->service->priv;
85 connection->priv = telnet_connection;
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;
93 /* negotiate telnet options */
94 telnet_write(connection, negotiate, strlen(negotiate));
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);
102 jsp_service->connection = connection;
104 int retval = target_register_timer_callback(&jsp_poll_read, 1, 1, jsp_service);
105 if (ERROR_OK != retval)
111 static int jsp_input(struct connection *connection)
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;
119 bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
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;
130 switch (t_con->state) {
131 case TELNET_STATE_DATA:
133 t_con->state = TELNET_STATE_IAC;
137 unsigned char in_buffer[10];
138 or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info,
139 &out_len, buf_p, &in_len,
142 telnet_write(connection,
146 case TELNET_STATE_IAC:
149 t_con->state = TELNET_STATE_DONT;
152 t_con->state = TELNET_STATE_DO;
155 t_con->state = TELNET_STATE_WONT;
158 t_con->state = TELNET_STATE_WILL;
162 case TELNET_STATE_SB:
164 case TELNET_STATE_SE:
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;
173 LOG_ERROR("unknown telnet state");
184 static int jsp_connection_closed(struct connection *connection)
186 struct telnet_connection *t_con = connection->priv;
187 struct jsp_service *jsp_service = connection->service->priv;
191 t_con->prompt = NULL;
194 int retval = target_unregister_timer_callback(&jsp_poll_read, jsp_service);
195 if (ERROR_OK != retval)
198 if (connection->priv) {
199 free(connection->priv);
200 connection->priv = NULL;
202 LOG_ERROR("BUG: connection->priv == NULL");
207 int jsp_init(struct or1k_jtag *jtag_info, char *banner)
209 struct jsp_service *jsp_service = malloc(sizeof(struct jsp_service));
210 jsp_service->banner = banner;
211 jsp_service->jtag_info = jtag_info;
213 return add_service("jsp",
218 jsp_connection_closed,
222 COMMAND_HANDLER(handle_jsp_port_command)
224 return CALL_COMMAND_HANDLER(server_pipe_command, &jsp_port);
227 static const struct command_registration jsp_command_handlers[] = {
230 .handler = handle_jsp_port_command,
232 .help = "Specify port on which to listen "
233 "for incoming JSP telnet connections.",
234 .usage = "[port_num]",
236 COMMAND_REGISTRATION_DONE
239 int jsp_register_commands(struct command_context *cmd_ctx)
241 jsp_port = strdup("7777");
242 return register_commands(cmd_ctx, NULL, jsp_command_handlers);
245 void jsp_service_free(void)