1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2014 by Franck Jullien *
5 * franck.jullien@gmail.com *
7 * Based on ./src/server/telnet_server.c *
8 ***************************************************************************/
14 #include <server/telnet_server.h>
18 #include "jsp_server.h"
20 static char *jsp_port;
22 /**A skim of the relevant RFCs suggests that if my application simply sent the
23 * characters IAC DONT LINEMODE (\377\376\042) as soon as the client connects,
24 * the client should be forced into character mode. However it doesn't make any difference.
27 static const char * const negotiate =
28 "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
29 "\xFF\xFB\x01" /* IAC WILL Echo */
30 "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
31 "\xFF\xFE\x01"; /* IAC DON'T Echo */
33 /* The only way we can detect that the socket is closed is the first time
34 * we write to it, we will fail. Subsequent write operations will
37 static int telnet_write(struct connection *connection, const void *data, int len)
39 struct telnet_connection *t_con = connection->priv;
41 return ERROR_SERVER_REMOTE_CLOSED;
43 if (connection_write(connection, data, len) == len)
46 return ERROR_SERVER_REMOTE_CLOSED;
49 static int jsp_poll_read(void *priv)
51 struct jsp_service *jsp_service = (struct jsp_service *)priv;
52 unsigned char out_buffer[10];
53 unsigned char in_buffer[10];
57 if (!jsp_service->connection)
60 memset(out_buffer, 0, 10);
62 or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info, &out_len, out_buffer, &in_len, in_buffer);
64 telnet_write(jsp_service->connection, in_buffer, in_len);
69 static int jsp_new_connection(struct connection *connection)
71 struct telnet_connection *telnet_connection = malloc(sizeof(struct telnet_connection));
72 struct jsp_service *jsp_service = connection->service->priv;
74 connection->priv = telnet_connection;
76 /* initialize telnet connection information */
77 telnet_connection->closed = 0;
78 telnet_connection->line_size = 0;
79 telnet_connection->line_cursor = 0;
80 telnet_connection->state = TELNET_STATE_DATA;
82 /* negotiate telnet options */
83 telnet_write(connection, negotiate, strlen(negotiate));
85 /* print connection banner */
86 if (jsp_service->banner) {
87 telnet_write(connection, jsp_service->banner, strlen(jsp_service->banner));
88 telnet_write(connection, "\r\n", 2);
91 jsp_service->connection = connection;
93 int retval = target_register_timer_callback(&jsp_poll_read, 1,
94 TARGET_TIMER_TYPE_PERIODIC, jsp_service);
95 if (retval != ERROR_OK)
101 static int jsp_input(struct connection *connection)
104 unsigned char buffer[TELNET_BUFFER_SIZE];
105 unsigned char *buf_p;
106 struct telnet_connection *t_con = connection->priv;
107 struct jsp_service *jsp_service = connection->service->priv;
109 bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
112 return ERROR_SERVER_REMOTE_CLOSED;
113 else if (bytes_read == -1) {
114 LOG_ERROR("error during read: %s", strerror(errno));
115 return ERROR_SERVER_REMOTE_CLOSED;
120 switch (t_con->state) {
121 case TELNET_STATE_DATA:
123 t_con->state = TELNET_STATE_IAC;
127 unsigned char in_buffer[10];
128 or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info,
129 &out_len, buf_p, &in_len,
132 telnet_write(connection,
136 case TELNET_STATE_IAC:
139 t_con->state = TELNET_STATE_DONT;
142 t_con->state = TELNET_STATE_DO;
145 t_con->state = TELNET_STATE_WONT;
148 t_con->state = TELNET_STATE_WILL;
152 case TELNET_STATE_SB:
154 case TELNET_STATE_SE:
156 case TELNET_STATE_WILL:
157 case TELNET_STATE_WONT:
158 case TELNET_STATE_DO:
159 case TELNET_STATE_DONT:
160 t_con->state = TELNET_STATE_DATA;
163 LOG_ERROR("unknown telnet state");
174 static int jsp_connection_closed(struct connection *connection)
176 struct jsp_service *jsp_service = connection->service->priv;
178 int retval = target_unregister_timer_callback(&jsp_poll_read, jsp_service);
179 if (retval != ERROR_OK)
182 free(connection->priv);
183 connection->priv = NULL;
187 static const struct service_driver jsp_service_driver = {
189 .new_connection_during_keep_alive_handler = NULL,
190 .new_connection_handler = jsp_new_connection,
191 .input_handler = jsp_input,
192 .connection_closed_handler = jsp_connection_closed,
193 .keep_client_alive_handler = NULL,
196 int jsp_init(struct or1k_jtag *jtag_info, char *banner)
198 struct jsp_service *jsp_service = malloc(sizeof(struct jsp_service));
199 jsp_service->banner = banner;
200 jsp_service->jtag_info = jtag_info;
202 return add_service(&jsp_service_driver, jsp_port, 1, jsp_service);
205 COMMAND_HANDLER(handle_jsp_port_command)
207 return CALL_COMMAND_HANDLER(server_pipe_command, &jsp_port);
210 static const struct command_registration jsp_command_handlers[] = {
213 .handler = handle_jsp_port_command,
215 .help = "Specify port on which to listen "
216 "for incoming JSP telnet connections.",
217 .usage = "[port_num]",
219 COMMAND_REGISTRATION_DONE
222 int jsp_register_commands(struct command_context *cmd_ctx)
224 jsp_port = strdup("7777");
225 return register_commands(cmd_ctx, NULL, jsp_command_handlers);
228 void jsp_service_free(void)