ARM ADI-V5: cleanup CID/PID addressing
[openocd/dnglaze.git] / src / server / server.c
blob7d8ad51795a10fe423c9de36b9b5b43e77d52f99
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include "server.h"
31 #include <target/target.h>
32 #include "openocd.h"
33 #include "tcl_server.h"
34 #include "telnet_server.h"
36 #include <signal.h>
38 #ifndef _WIN32
39 #include <netinet/tcp.h>
40 #endif
43 static struct service *services = NULL;
45 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
46 static int shutdown_openocd = 0;
48 /* set when using pipes rather than tcp */
49 int server_use_pipes = 0;
51 static int add_connection(struct service *service, struct command_context *cmd_ctx)
53 socklen_t address_size;
54 struct connection *c, **p;
55 int retval;
56 int flag = 1;
58 c = malloc(sizeof(struct connection));
59 c->fd = -1;
60 memset(&c->sin, 0, sizeof(c->sin));
61 c->cmd_ctx = copy_command_context(cmd_ctx);
62 c->service = service;
63 c->input_pending = 0;
64 c->priv = NULL;
65 c->next = NULL;
67 if (service->type == CONNECTION_TCP)
69 address_size = sizeof(c->sin);
71 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
73 /* This increases performance dramatically for e.g. GDB load which
74 * does not have a sliding window protocol. */
75 retval = setsockopt(c->fd, /* socket affected */
76 IPPROTO_TCP, /* set option at TCP level */
77 TCP_NODELAY, /* name of option */
78 (char *)&flag, /* the cast is historical cruft */
79 sizeof(int)); /* length of option value */
81 LOG_INFO("accepting '%s' connection from %i", service->name, service->port);
82 if ((retval = service->new_connection(c)) != ERROR_OK)
84 close_socket(c->fd);
85 LOG_ERROR("attempted '%s' connection rejected", service->name);
86 free(c);
87 return retval;
90 else if (service->type == CONNECTION_PIPE)
92 c->fd = service->fd;
94 /* do not check for new connections again on stdin */
95 service->fd = -1;
97 LOG_INFO("accepting '%s' connection from pipe", service->name);
98 if ((retval = service->new_connection(c)) != ERROR_OK)
100 LOG_ERROR("attempted '%s' connection rejected", service->name);
101 free(c);
102 return retval;
106 /* add to the end of linked list */
107 for (p = &service->connections; *p; p = &(*p)->next);
108 *p = c;
110 service->max_connections--;
112 return ERROR_OK;
115 static int remove_connection(struct service *service, struct connection *connection)
117 struct connection **p = &service->connections;
118 struct connection *c;
120 /* find connection */
121 while ((c = *p))
123 if (c->fd == connection->fd)
125 service->connection_closed(c);
126 if (service->type == CONNECTION_TCP)
127 close_socket(c->fd);
128 command_done(c->cmd_ctx);
130 /* delete connection */
131 *p = c->next;
132 free(c);
134 service->max_connections++;
135 break;
138 /* redirect p to next list pointer */
139 p = &(*p)->next;
142 return ERROR_OK;
145 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)
147 struct service *c, **p;
148 int so_reuseaddr_option = 1;
150 c = malloc(sizeof(struct service));
152 c->name = strdup(name);
153 c->type = type;
154 c->port = port;
155 c->max_connections = max_connections;
156 c->fd = -1;
157 c->connections = NULL;
158 c->new_connection = new_connection_handler;
159 c->input = input_handler;
160 c->connection_closed = connection_closed_handler;
161 c->priv = priv;
162 c->next = NULL;
164 if (type == CONNECTION_TCP)
166 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
168 LOG_ERROR("error creating socket: %s", strerror(errno));
169 exit(-1);
172 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
174 socket_nonblock(c->fd);
176 memset(&c->sin, 0, sizeof(c->sin));
177 c->sin.sin_family = AF_INET;
178 c->sin.sin_addr.s_addr = INADDR_ANY;
179 c->sin.sin_port = htons(port);
181 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
183 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
184 exit(-1);
187 #ifndef _WIN32
188 int segsize = 65536;
189 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
190 #endif
191 int window_size = 128 * 1024;
193 /* These setsockopt()s must happen before the listen() */
195 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
196 (char *)&window_size, sizeof(window_size));
197 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
198 (char *)&window_size, sizeof(window_size));
200 if (listen(c->fd, 1) == -1)
202 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
203 exit(-1);
206 else if (type == CONNECTION_PIPE)
208 /* use stdin */
209 c->fd = STDIN_FILENO;
211 #ifdef _WIN32
212 /* for win32 set stdin/stdout to binary mode */
213 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
214 LOG_WARNING("cannot change stdout mode to binary");
215 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
216 LOG_WARNING("cannot change stdin mode to binary");
217 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
218 LOG_WARNING("cannot change stderr mode to binary");
219 #else
220 socket_nonblock(c->fd);
221 #endif
223 else
225 LOG_ERROR("unknown connection type: %d", type);
226 exit(1);
229 /* add to the end of linked list */
230 for (p = &services; *p; p = &(*p)->next);
231 *p = c;
233 return ERROR_OK;
236 static int remove_services(void)
238 struct service *c = services;
240 /* loop service */
241 while (c)
243 struct service *next = c->next;
245 if (c->name)
246 free(c->name);
248 if (c->priv)
249 free(c->priv);
251 /* delete service */
252 free(c);
254 /* remember the last service for unlinking */
255 c = next;
258 services = NULL;
260 return ERROR_OK;
263 int server_loop(struct command_context *command_context)
265 struct service *service;
267 bool poll_ok = true;
269 /* used in select() */
270 fd_set read_fds;
271 int fd_max;
273 /* used in accept() */
274 int retval;
276 #ifndef _WIN32
277 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
278 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
279 #endif
281 while (!shutdown_openocd)
283 /* monitor sockets for activity */
284 fd_max = 0;
285 FD_ZERO(&read_fds);
287 /* add service and connection fds to read_fds */
288 for (service = services; service; service = service->next)
290 if (service->fd != -1)
292 /* listen for new connections */
293 FD_SET(service->fd, &read_fds);
295 if (service->fd > fd_max)
296 fd_max = service->fd;
299 if (service->connections)
301 struct connection *c;
303 for (c = service->connections; c; c = c->next)
305 /* check for activity on the connection */
306 FD_SET(c->fd, &read_fds);
307 if (c->fd > fd_max)
308 fd_max = c->fd;
313 #ifndef _WIN32
314 #if BUILD_ECOSBOARD == 0
315 if (server_use_pipes == 0)
317 /* add STDIN to read_fds */
318 FD_SET(fileno(stdin), &read_fds);
320 #endif
321 #endif
323 struct timeval tv;
324 tv.tv_sec = 0;
325 if (poll_ok)
327 /* we're just polling this iteration, this is faster on embedded
328 * hosts */
329 tv.tv_usec = 0;
330 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
331 } else
333 /* Every 100ms */
334 tv.tv_usec = 100000;
335 /* Only while we're sleeping we'll let others run */
336 openocd_sleep_prelude();
337 kept_alive();
338 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
339 openocd_sleep_postlude();
342 if (retval == -1)
344 #ifdef _WIN32
346 errno = WSAGetLastError();
348 if (errno == WSAEINTR)
349 FD_ZERO(&read_fds);
350 else
352 LOG_ERROR("error during select: %s", strerror(errno));
353 exit(-1);
355 #else
357 if (errno == EINTR)
359 FD_ZERO(&read_fds);
361 else
363 LOG_ERROR("error during select: %s", strerror(errno));
364 exit(-1);
366 #endif
369 if (retval == 0)
371 /* We only execute these callbacks when there was nothing to do or we timed out */
372 target_call_timer_callbacks();
373 process_jim_events(command_context);
375 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
377 /* We timed out/there was nothing to do, timeout rather than poll next time */
378 poll_ok = false;
379 } else
381 /* There was something to do, next time we'll just poll */
382 poll_ok = true;
385 for (service = services; service; service = service->next)
387 /* handle new connections on listeners */
388 if ((service->fd != -1)
389 && (FD_ISSET(service->fd, &read_fds)))
391 if (service->max_connections > 0)
393 add_connection(service, command_context);
395 else
397 if (service->type != CONNECTION_PIPE)
399 struct sockaddr_in sin;
400 socklen_t address_size = sizeof(sin);
401 int tmp_fd;
402 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
403 close_socket(tmp_fd);
405 LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
409 /* handle activity on connections */
410 if (service->connections)
412 struct connection *c;
414 for (c = service->connections; c;)
416 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
418 if ((retval = service->input(c)) != ERROR_OK)
420 struct connection *next = c->next;
421 if (service->type == CONNECTION_PIPE)
423 /* if connection uses a pipe then shutdown openocd on error */
424 shutdown_openocd = 1;
426 remove_connection(service, c);
427 LOG_INFO("dropped '%s' connection - error %d", service->name, retval);
428 c = next;
429 continue;
432 c = c->next;
437 #ifndef _WIN32
438 #if BUILD_ECOSBOARD == 0
439 /* check for data on stdin if not using pipes */
440 if (server_use_pipes == 0)
442 if (FD_ISSET(fileno(stdin), &read_fds))
444 if (getc(stdin) == 'x')
446 shutdown_openocd = 1;
450 #endif
451 #else
452 MSG msg;
453 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
455 if (msg.message == WM_QUIT)
456 shutdown_openocd = 1;
458 #endif
461 return ERROR_OK;
464 #ifdef _WIN32
465 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
467 shutdown_openocd = 1;
468 return TRUE;
471 void sig_handler(int sig) {
472 shutdown_openocd = 1;
474 #endif
476 int server_preinit(void)
478 /* this currently only calls WSAStartup on native win32 systems
479 * before any socket operations are performed.
480 * This is an issue if you call init in your config script */
482 #ifdef _WIN32
483 WORD wVersionRequested;
484 WSADATA wsaData;
486 wVersionRequested = MAKEWORD(2, 2);
488 if (WSAStartup(wVersionRequested, &wsaData) != 0)
490 LOG_ERROR("Failed to Open Winsock");
491 exit(-1);
494 if (server_use_pipes == 0)
496 /* register ctrl-c handler */
497 SetConsoleCtrlHandler(ControlHandler, TRUE);
499 else
501 /* we are using pipes so ignore ctrl-c */
502 SetConsoleCtrlHandler(NULL, TRUE);
505 signal(SIGINT, sig_handler);
506 signal(SIGTERM, sig_handler);
507 signal(SIGBREAK, sig_handler);
508 signal(SIGABRT, sig_handler);
509 #endif
511 return ERROR_OK;
514 int server_init(struct command_context *cmd_ctx)
516 int ret = tcl_init();
517 if (ERROR_OK != ret)
518 return ret;
520 return telnet_init("Open On-Chip Debugger");
523 int server_quit(void)
525 remove_services();
527 #ifdef _WIN32
528 WSACleanup();
529 SetConsoleCtrlHandler(ControlHandler, FALSE);
530 #endif
532 return ERROR_OK;
535 /* tell the server we want to shut down */
536 COMMAND_HANDLER(handle_shutdown_command)
538 LOG_USER("shutdown command invoked");
540 shutdown_openocd = 1;
542 return ERROR_OK;
545 static const struct command_registration server_command_handlers[] = {
547 .name = "shutdown",
548 .handler = &handle_shutdown_command,
549 .mode = COMMAND_ANY,
550 .help = "shut the server down",
552 COMMAND_REGISTRATION_DONE
555 int server_register_commands(struct command_context *cmd_ctx)
557 int retval = telnet_register_commands(cmd_ctx);
558 if (ERROR_OK != retval)
559 return retval;
561 retval = tcl_register_commands(cmd_ctx);
562 if (ERROR_OK != retval)
563 return retval;
565 return register_commands(cmd_ctx, NULL, server_command_handlers);
568 SERVER_PORT_COMMAND()
570 switch (CMD_ARGC) {
571 case 0:
572 command_print(CMD_CTX, "%d", *out);
573 break;
574 case 1:
576 uint16_t port;
577 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
578 *out = port;
579 break;
581 default:
582 return ERROR_INVALID_ARGUMENTS;
584 return ERROR_OK;