1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
31 #include <target/target.h>
33 #include "tcl_server.h"
34 #include "telnet_server.h"
39 #include <netinet/tcp.h>
43 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 int add_connection(struct service *service, struct command_context *cmd_ctx)
53 socklen_t address_size;
54 struct connection *c, **p;
58 c = malloc(sizeof(struct connection));
60 memset(&c->sin, 0, sizeof(c->sin));
61 c->cmd_ctx = copy_command_context(cmd_ctx);
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, c->sin.sin_port);
82 if ((retval = service->new_connection(c)) != ERROR_OK)
85 LOG_ERROR("attempted '%s' connection rejected", service->name);
90 else if (service->type == CONNECTION_PIPE)
94 /* do not check for new connections again on stdin */
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);
106 /* add to the end of linked list */
107 for (p = &service->connections; *p; p = &(*p)->next);
110 service->max_connections--;
115 int remove_connection(struct service *service, struct connection *connection)
117 struct connection **p = &service->connections;
118 struct connection *c;
120 /* find connection */
123 if (c->fd == connection->fd)
125 service->connection_closed(c);
126 if (service->type == CONNECTION_TCP)
128 command_done(c->cmd_ctx);
130 /* delete connection */
134 service->max_connections++;
138 /* redirect p to next list pointer */
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);
155 c->max_connections = max_connections;
157 c->connections = NULL;
158 c->new_connection = new_connection_handler;
159 c->input = input_handler;
160 c->connection_closed = connection_closed_handler;
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));
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));
189 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
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));
206 else if (type == CONNECTION_PIPE)
209 c->fd = STDIN_FILENO;
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");
220 socket_nonblock(c->fd);
225 LOG_ERROR("unknown connection type: %d", type);
229 /* add to the end of linked list */
230 for (p = &services; *p; p = &(*p)->next);
236 int remove_service(unsigned short port)
238 struct service **p = &services;
257 /* redirect p to next list pointer */
264 int remove_services(void)
266 struct service *c = services;
271 struct service *next = c->next;
282 /* remember the last service for unlinking */
291 int server_loop(struct command_context *command_context)
293 struct service *service;
295 /* used in select() */
300 /* used in accept() */
304 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
305 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
308 /* do regular tasks after at most 10ms */
312 while (!shutdown_openocd)
314 /* monitor sockets for activity */
318 /* add service and connection fds to read_fds */
319 for (service = services; service; service = service->next)
321 if (service->fd != -1)
323 /* listen for new connections */
324 FD_SET(service->fd, &read_fds);
326 if (service->fd > fd_max)
327 fd_max = service->fd;
330 if (service->connections)
332 struct connection *c;
334 for (c = service->connections; c; c = c->next)
336 /* check for activity on the connection */
337 FD_SET(c->fd, &read_fds);
345 #if BUILD_ECOSBOARD == 0
346 if (server_use_pipes == 0)
348 /* add STDIN to read_fds */
349 FD_SET(fileno(stdin), &read_fds);
354 openocd_sleep_prelude();
357 /* Only while we're sleeping we'll let others run */
358 retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
359 openocd_sleep_postlude();
365 errno = WSAGetLastError();
367 if (errno == WSAEINTR)
371 LOG_ERROR("error during select: %s", strerror(errno));
382 LOG_ERROR("error during select: %s", strerror(errno));
388 target_call_timer_callbacks();
389 process_jim_events(command_context);
393 /* do regular tasks after at most 100ms */
396 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
399 for (service = services; service; service = service->next)
401 /* handle new connections on listeners */
402 if ((service->fd != -1)
403 && (FD_ISSET(service->fd, &read_fds)))
405 if (service->max_connections > 0)
407 add_connection(service, command_context);
411 if (service->type != CONNECTION_PIPE)
413 struct sockaddr_in sin;
414 socklen_t address_size = sizeof(sin);
416 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
417 close_socket(tmp_fd);
419 LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
423 /* handle activity on connections */
424 if (service->connections)
426 struct connection *c;
428 for (c = service->connections; c;)
430 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
432 if ((retval = service->input(c)) != ERROR_OK)
434 struct connection *next = c->next;
435 if (service->type == CONNECTION_PIPE)
437 /* if connection uses a pipe then shutdown openocd on error */
438 shutdown_openocd = 1;
440 remove_connection(service, c);
441 LOG_INFO("dropped '%s' connection - error %d", service->name, retval);
452 #if BUILD_ECOSBOARD == 0
453 /* check for data on stdin if not using pipes */
454 if (server_use_pipes == 0)
456 if (FD_ISSET(fileno(stdin), &read_fds))
458 if (getc(stdin) == 'x')
460 shutdown_openocd = 1;
467 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
469 if (msg.message == WM_QUIT)
470 shutdown_openocd = 1;
479 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
481 shutdown_openocd = 1;
485 void sig_handler(int sig) {
486 shutdown_openocd = 1;
490 int server_init(struct command_context *cmd_ctx)
493 WORD wVersionRequested;
496 wVersionRequested = MAKEWORD(2, 2);
498 if (WSAStartup(wVersionRequested, &wsaData) != 0)
500 LOG_ERROR("Failed to Open Winsock");
504 if (server_use_pipes == 0)
506 /* register ctrl-c handler */
507 SetConsoleCtrlHandler(ControlHandler, TRUE);
511 /* we are using pipes so ignore ctrl-c */
512 SetConsoleCtrlHandler(NULL, TRUE);
515 signal(SIGINT, sig_handler);
516 signal(SIGTERM, sig_handler);
517 signal(SIGBREAK, sig_handler);
518 signal(SIGABRT, sig_handler);
521 int ret = tcl_init(cmd_ctx);
525 return telnet_init("Open On-Chip Debugger");
528 int server_quit(void)
534 SetConsoleCtrlHandler(ControlHandler, FALSE);
540 /* tell the server we want to shut down */
541 COMMAND_HANDLER(handle_shutdown_command)
543 shutdown_openocd = 1;
545 return ERROR_COMMAND_CLOSE_CONNECTION;
548 static const struct command_registration server_command_handlers[] = {
551 .handler = &handle_shutdown_command,
553 .help = "shut the server down",
555 COMMAND_REGISTRATION_DONE
558 int server_register_commands(struct command_context *cmd_ctx)
560 int retval = telnet_register_commands(cmd_ctx);
561 if (ERROR_OK != retval)
564 retval = tcl_register_commands(cmd_ctx);
565 if (ERROR_OK != retval)
568 return register_commands(cmd_ctx, NULL, server_command_handlers);
571 SERVER_PORT_COMMAND()
575 command_print(CMD_CTX, "%d", *out);
580 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
585 return ERROR_INVALID_ARGUMENTS;