- added patch for new flash functionality like:
[openocd.git] / src / server / server.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "replacements.h"
25
26 #include "server.h"
27
28 #include "log.h"
29 #include "telnet_server.h"
30 #include "target.h"
31
32 #include <command.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <fcntl.h>
39 #include <signal.h>
40
41 service_t *services = NULL;
42
43 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
44 static int shutdown_openocd = 0;
45 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46
47 int add_connection(service_t *service, command_context_t *cmd_ctx)
48 {
49 unsigned int address_size;
50 connection_t *c, *p;
51 int retval;
52
53 c = malloc(sizeof(connection_t));
54 c->fd = -1;
55 memset(&c->sin, 0, sizeof(c->sin));
56 c->cmd_ctx = copy_command_context(cmd_ctx);
57 c->service = service;
58 c->input_pending = 0;
59 c->priv = NULL;
60 c->next = NULL;
61
62 address_size = sizeof(c->sin);
63 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
64
65 if ((retval = service->new_connection(c)) == ERROR_OK)
66 {
67 INFO("accepted '%s' connection from %i", service->name, c->sin.sin_port);
68 }
69 else
70 {
71 close_socket(c->fd);
72 INFO("attempted '%s' connection rejected", service->name);
73 free(c);
74 }
75
76 if (service->connections)
77 {
78 for (p = service->connections; p && p->next; p = p->next);
79 if (p)
80 p->next = c;
81 }
82 else
83 {
84 service->connections = c;
85 }
86
87 service->max_connections--;
88
89 return ERROR_OK;
90 }
91
92 int remove_connection(service_t *service, connection_t *connection)
93 {
94 connection_t *c = service->connections;
95
96 /* find connection */
97 while(c)
98 {
99 connection_t *next = c->next;
100
101 if (c->fd == connection->fd)
102 {
103 service->connections = next;
104 service->connection_closed(c);
105 close_socket(c->fd);
106
107 command_done(c->cmd_ctx);
108
109 /* delete connection */
110 free(c);
111
112 service->max_connections++;
113 break;
114 }
115
116 /* remember the last connection for unlinking */
117 c = next;
118 }
119
120 return ERROR_OK;
121 }
122
123 int add_service(char *name, enum connection_type type, unsigned short port, int max_connections, new_connection_handler_t new_connection_handler, input_handler_t input_handler, connection_closed_handler_t connection_closed_handler, void *priv)
124 {
125 service_t *c, *p;
126 int so_reuseaddr_option = 1;
127
128 c = malloc(sizeof(service_t));
129
130 c->name = strdup(name);
131 c->type = type;
132 c->port = port;
133 c->max_connections = max_connections;
134 c->fd = -1;
135 c->connections = NULL;
136 c->new_connection = new_connection_handler;
137 c->input = input_handler;
138 c->connection_closed = connection_closed_handler;
139 c->priv = priv;
140 c->next = NULL;
141
142 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
143 {
144 ERROR("error creating socket: %s", strerror(errno));
145 exit(-1);
146 }
147
148 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
149
150 socket_nonblock(c->fd);
151
152 memset(&c->sin, 0, sizeof(c->sin));
153 c->sin.sin_family = AF_INET;
154 c->sin.sin_addr.s_addr = INADDR_ANY;
155 c->sin.sin_port = htons(port);
156
157 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
158 {
159 ERROR("couldn't bind to socket: %s", strerror(errno));
160 exit(-1);
161 }
162
163 if (listen(c->fd, 1) == -1)
164 {
165 ERROR("couldn't listen on socket: %s", strerror(errno));
166 exit(-1);
167 }
168
169 if (services)
170 {
171 for (p = services; p && p->next; p = p->next);
172 if (p)
173 p->next = c;
174 }
175 else
176 {
177 services = c;
178 }
179
180 return ERROR_OK;
181 }
182
183 int remove_service(unsigned short port)
184 {
185 service_t *c = services;
186
187 /* find service */
188 while(c)
189 {
190 service_t *next = c->next;
191
192 if (c->port == port)
193 {
194 if (c->name)
195 free(c->name);
196
197 if (c->priv)
198 free(c->priv);
199
200 /* delete service */
201 free(c);
202 }
203
204 /* remember the last service for unlinking */
205 c = next;
206 }
207
208 return ERROR_OK;
209 }
210
211 int remove_services()
212 {
213 service_t *c = services;
214
215 /* loop service */
216 while(c)
217 {
218 service_t *next = c->next;
219
220 if (c->name)
221 free(c->name);
222
223 if (c->priv)
224 free(c->priv);
225
226 /* delete service */
227 free(c);
228
229 /* remember the last service for unlinking */
230 c = next;
231 }
232
233 services = NULL;
234
235 return ERROR_OK;
236 }
237
238 int server_loop(command_context_t *command_context)
239 {
240 service_t *service;
241
242 /* used in select() */
243 fd_set read_fds;
244 struct timeval tv;
245 int fd_max;
246
247 /* used in accept() */
248 int retval;
249
250 #ifndef _WIN32
251 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
252 ERROR("couldn't set SIGPIPE to SIG_IGN");
253 #endif
254
255 /* do regular tasks after at most 10ms */
256 tv.tv_sec = 0;
257 tv.tv_usec = 10000;
258
259 while(!shutdown_openocd)
260 {
261 /* monitor sockets for acitvity */
262 fd_max = 0;
263 FD_ZERO(&read_fds);
264
265 /* add service and connection fds to read_fds */
266 for (service = services; service; service = service->next)
267 {
268 if (service->fd != -1)
269 {
270 /* listen for new connections */
271 FD_SET(service->fd, &read_fds);
272
273 if (service->fd > fd_max)
274 fd_max = service->fd;
275 }
276
277 if (service->connections)
278 {
279 connection_t *c;
280
281 for (c = service->connections; c; c = c->next)
282 {
283 /* check for activity on the connection */
284 FD_SET(c->fd, &read_fds);
285 if (c->fd > fd_max)
286 fd_max = c->fd;
287 }
288 }
289 }
290
291 #ifndef _WIN32
292 /* add STDIN to read_fds */
293 FD_SET(fileno(stdin), &read_fds);
294 #endif
295
296 if ((retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv)) == -1)
297 {
298 #ifdef _WIN32
299
300 errno = WSAGetLastError();
301
302 if (errno == WSAEINTR)
303 FD_ZERO(&read_fds);
304 else
305 {
306 ERROR("error during select: %s", strerror(errno));
307 exit(-1);
308 }
309 #else
310
311 if (errno == EINTR)
312 FD_ZERO(&read_fds);
313 else
314 {
315 ERROR("error during select: %s", strerror(errno));
316 exit(-1);
317 }
318 #endif
319 }
320
321 target_call_timer_callbacks();
322
323 if (retval == 0)
324 {
325 /* do regular tasks after at most 100ms */
326 tv.tv_sec = 0;
327 tv.tv_usec = 10000;
328 }
329
330 for (service = services; service; service = service->next)
331 {
332 /* handle new connections on listeners */
333 if ((service->fd != -1)
334 && (FD_ISSET(service->fd, &read_fds)))
335 {
336 if (service->max_connections > 0)
337 add_connection(service, command_context);
338 else
339 {
340 struct sockaddr_in sin;
341 unsigned int address_size = sizeof(sin);
342 int tmp_fd;
343 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
344 close_socket(tmp_fd);
345 INFO("rejected '%s' connection, no more connections allowed", service->name);
346 }
347 }
348
349 /* handle activity on connections */
350 if (service->connections)
351 {
352 connection_t *c;
353
354 for (c = service->connections; c;)
355 {
356 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
357 {
358 if (service->input(c) != ERROR_OK)
359 {
360 connection_t *next = c->next;
361 remove_connection(service, c);
362 INFO("dropped '%s' connection", service->name);
363 c = next;
364 continue;
365 }
366 }
367 c = c->next;
368 }
369 }
370 }
371
372 #ifndef _WIN32
373 if (FD_ISSET(fileno(stdin), &read_fds))
374 {
375 if (getc(stdin) == 'x')
376 {
377 shutdown_openocd = 1;
378 }
379 }
380 #else
381 MSG msg;
382 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
383 {
384 if (msg.message == WM_QUIT)
385 shutdown_openocd = 1;
386 }
387 #endif
388 }
389
390 return ERROR_OK;
391 }
392
393 #ifdef _WIN32
394 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
395 {
396 shutdown_openocd = 1;
397 return TRUE;
398 }
399
400 void sig_handler(int sig) {
401 shutdown_openocd = 1;
402 }
403 #endif
404
405 int server_init()
406 {
407 #ifdef _WIN32
408 WORD wVersionRequested;
409 WSADATA wsaData;
410
411 wVersionRequested = MAKEWORD( 2, 2 );
412
413 if (WSAStartup(wVersionRequested, &wsaData) != 0)
414 {
415 ERROR("Failed to Open Winsock");
416 exit(-1);
417 }
418
419 SetConsoleCtrlHandler( ControlHandler, TRUE );
420
421 signal(SIGINT, sig_handler);
422 signal(SIGTERM, sig_handler);
423 signal(SIGBREAK, sig_handler);
424 signal(SIGABRT, sig_handler);
425 #endif
426
427
428 return ERROR_OK;
429 }
430
431 int server_quit()
432 {
433 remove_services();
434
435 #ifdef _WIN32
436 WSACleanup();
437 SetConsoleCtrlHandler( ControlHandler, FALSE );
438 #endif
439
440 return ERROR_OK;
441 }
442
443 int server_register_commands(command_context_t *context)
444 {
445 register_command(context, NULL, "shutdown", handle_shutdown_command,
446 COMMAND_ANY, "shut the server down");
447
448 return ERROR_OK;
449 }
450
451 /* tell the server we want to shut down */
452 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
453 {
454 shutdown_openocd = 1;
455
456 return ERROR_COMMAND_CLOSE_CONNECTION;
457 }
458
459

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)