aboutsummaryrefslogtreecommitdiff
path: root/src/target/openrisc/jsp_server.c
blob: e581fb870e84ac37f90fcf7d9df335b50d5b594f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/***************************************************************************
 *   Copyright (C) 2014 by Franck Jullien                                  *
 *   franck.jullien@gmail.com                                              *
 *                                                                         *
 *   Based on ./src/server/telnet_server.c                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <server/telnet_server.h>

#include "or1k_tap.h"
#include "or1k_du.h"
#include "jsp_server.h"

static char *jsp_port;

/**A skim of the relevant RFCs suggests that if my application simply sent the
 * characters IAC DONT LINEMODE (\377\376\042) as soon as the client connects,
 * the client should be forced into character mode. However it doesn't make any difference.
 */

static const char * const negotiate =
	"\xFF\xFB\x03"			/* IAC WILL Suppress Go Ahead */
	"\xFF\xFB\x01"			/* IAC WILL Echo */
	"\xFF\xFD\x03"			/* IAC DO Suppress Go Ahead */
	"\xFF\xFE\x01";			/* IAC DON'T Echo */

/* The only way we can detect that the socket is closed is the first time
 * we write to it, we will fail. Subsequent write operations will
 * succeed. Shudder!
 */
static int telnet_write(struct connection *connection, const void *data, int len)
{
	struct telnet_connection *t_con = connection->priv;
	if (t_con->closed)
		return ERROR_SERVER_REMOTE_CLOSED;

	if (connection_write(connection, data, len) == len)
		return ERROR_OK;
	t_con->closed = 1;
	return ERROR_SERVER_REMOTE_CLOSED;
}

int jsp_poll_read(void *priv)
{
	struct jsp_service *jsp_service = (struct jsp_service *)priv;
	unsigned char out_buffer[10];
	unsigned char in_buffer[10];
	int out_len = 0;
	int in_len;

	if (!jsp_service->connection)
		return ERROR_FAIL;

	memset(out_buffer, 0, 10);

	or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info, &out_len, out_buffer, &in_len, in_buffer);
	if (in_len)
		telnet_write(jsp_service->connection, in_buffer, in_len);

	return ERROR_OK;
}

static int jsp_new_connection(struct connection *connection)
{
	struct telnet_connection *telnet_connection = malloc(sizeof(struct telnet_connection));
	struct jsp_service *jsp_service = connection->service->priv;

	connection->priv = telnet_connection;

	/* initialize telnet connection information */
	telnet_connection->closed = 0;
	telnet_connection->line_size = 0;
	telnet_connection->line_cursor = 0;
	telnet_connection->option_size = 0;
	telnet_connection->state = TELNET_STATE_DATA;

	/* negotiate telnet options */
	telnet_write(connection, negotiate, strlen(negotiate));

	/* print connection banner */
	if (jsp_service->banner) {
		telnet_write(connection, jsp_service->banner, strlen(jsp_service->banner));
		telnet_write(connection, "\r\n", 2);
	}

	jsp_service->connection = connection;

	int retval = target_register_timer_callback(&jsp_poll_read, 1, 1, jsp_service);
	if (ERROR_OK != retval)
		return retval;

	return ERROR_OK;
}

static int jsp_input(struct connection *connection)
{
	int bytes_read;
	unsigned char buffer[TELNET_BUFFER_SIZE];
	unsigned char *buf_p;
	struct telnet_connection *t_con = connection->priv;
	struct jsp_service *jsp_service = connection->service->priv;

	bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);

	if (bytes_read == 0)
		return ERROR_SERVER_REMOTE_CLOSED;
	else if (bytes_read == -1) {
		LOG_ERROR("error during read: %s", strerror(errno));
		return ERROR_SERVER_REMOTE_CLOSED;
	}

	buf_p = buffer;
	while (bytes_read) {
		switch (t_con->state) {
			case TELNET_STATE_DATA:
				if (*buf_p == 0xff)
					t_con->state = TELNET_STATE_IAC;
				else {
					int out_len = 1;
					int in_len;
					unsigned char in_buffer[10];
					or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info,
							       &out_len, buf_p, &in_len,
							       in_buffer);
					if (in_len)
						telnet_write(connection,
							     in_buffer, in_len);
				}
				break;
			case TELNET_STATE_IAC:
				switch (*buf_p) {
				case 0xfe:
					t_con->state = TELNET_STATE_DONT;
					break;
				case 0xfd:
					t_con->state = TELNET_STATE_DO;
					break;
				case 0xfc:
					t_con->state = TELNET_STATE_WONT;
					break;
				case 0xfb:
					t_con->state = TELNET_STATE_WILL;
					break;
				}
				break;
			case TELNET_STATE_SB:
				break;
			case TELNET_STATE_SE:
				break;
			case TELNET_STATE_WILL:
			case TELNET_STATE_WONT:
			case TELNET_STATE_DO:
			case TELNET_STATE_DONT:
				t_con->state = TELNET_STATE_DATA;
				break;
			default:
				LOG_ERROR("unknown telnet state");
				exit(-1);
		}

		bytes_read--;
		buf_p++;
	}

	return ERROR_OK;
}

static int jsp_connection_closed(struct connection *connection)
{
	struct telnet_connection *t_con = connection->priv;
	struct jsp_service *jsp_service = connection->service->priv;

	if (t_con->prompt) {
		free(t_con->prompt);
		t_con->prompt = NULL;
	}

	int retval = target_unregister_timer_callback(&jsp_poll_read, jsp_service);
	if (ERROR_OK != retval)
		return retval;

	if (connection->priv) {
		free(connection->priv);
		connection->priv = NULL;
	} else
		LOG_ERROR("BUG: connection->priv == NULL");

	return ERROR_OK;
}

int jsp_init(struct or1k_jtag *jtag_info, char *banner)
{
	struct jsp_service *jsp_service = malloc(sizeof(struct jsp_service));
	jsp_service->banner = banner;
	jsp_service->jtag_info = jtag_info;

	return add_service("jsp",
		jsp_port,
		1,
		jsp_new_connection,
		jsp_input,
		jsp_connection_closed,
		jsp_service);
}

COMMAND_HANDLER(handle_jsp_port_command)
{
	return CALL_COMMAND_HANDLER(server_pipe_command, &jsp_port);
}

static const struct command_registration jsp_command_handlers[] = {
	{
		.name = "jsp_port",
		.handler = handle_jsp_port_command,
		.mode = COMMAND_ANY,
		.help = "Specify port on which to listen "
			"for incoming JSP telnet connections.",
		.usage = "[port_num]",
	},
	COMMAND_REGISTRATION_DONE
};

int jsp_register_commands(struct command_context *cmd_ctx)
{
	jsp_port = strdup("7777");
	return register_commands(cmd_ctx, NULL, jsp_command_handlers);
}