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 ***************************************************************************/
30 #include "replacements.h"
35 #include "telnet_server.h"
43 #include <sys/types.h>
47 #include <netinet/tcp.h>
50 service_t *services = NULL;
52 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
53 static int shutdown_openocd = 0;
54 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 /* set when using pipes rather than tcp */
57 int server_use_pipes = 0;
59 int add_connection(service_t *service, command_context_t *cmd_ctx)
61 unsigned int address_size;
66 c = malloc(sizeof(connection_t));
68 memset(&c->sin, 0, sizeof(c->sin));
69 c->cmd_ctx = copy_command_context(cmd_ctx);
75 if (service->type == CONNECTION_TCP)
77 address_size = sizeof(c->sin);
79 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
81 /* This increases performance dramatically for e.g. GDB load which
82 * does not have a sliding window protocol. */
83 retval=setsockopt(c->fd, /* socket affected */
84 IPPROTO_TCP, /* set option at TCP level */
85 TCP_NODELAY, /* name of option */
86 (char *)&flag, /* the cast is historical cruft */
87 sizeof(int)); /* length of option value */
89 LOG_INFO("accepting '%s' connection from %i", service->name, c->sin.sin_port);
90 if ((retval = service->new_connection(c)) != ERROR_OK)
93 LOG_ERROR("attempted '%s' connection rejected", service->name);
98 else if (service->type == CONNECTION_PIPE)
103 /* do not check for new connections again on stdin */
106 LOG_INFO("accepting '%s' connection from pipe", service->name);
107 if ((retval = service->new_connection(c)) != ERROR_OK)
109 LOG_ERROR("attempted '%s' connection rejected", service->name);
115 /* add to the end of linked list */
116 for (p = &service->connections; *p; p = &(*p)->next);
119 service->max_connections--;
124 int remove_connection(service_t *service, connection_t *connection)
126 connection_t **p = &service->connections;
129 /* find connection */
132 if (c->fd == connection->fd)
134 service->connection_closed(c);
135 if (service->type == CONNECTION_TCP)
137 command_done(c->cmd_ctx);
139 /* delete connection */
143 service->max_connections++;
147 /* redirect p to next list pointer */
154 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)
157 int so_reuseaddr_option = 1;
159 c = malloc(sizeof(service_t));
161 c->name = strdup(name);
164 c->max_connections = max_connections;
166 c->connections = NULL;
167 c->new_connection = new_connection_handler;
168 c->input = input_handler;
169 c->connection_closed = connection_closed_handler;
173 if (type == CONNECTION_TCP)
175 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
177 LOG_ERROR("error creating socket: %s", strerror(errno));
181 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
183 socket_nonblock(c->fd);
185 memset(&c->sin, 0, sizeof(c->sin));
186 c->sin.sin_family = AF_INET;
187 c->sin.sin_addr.s_addr = INADDR_ANY;
188 c->sin.sin_port = htons(port);
190 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
192 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
198 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
200 int window_size = 128 * 1024;
202 /* These setsockopt()s must happen before the listen() */
204 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
205 (char *)&window_size, sizeof(window_size));
206 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
207 (char *)&window_size, sizeof(window_size));
209 if (listen(c->fd, 1) == -1)
211 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
215 else if (type == CONNECTION_PIPE)
218 c->fd = STDIN_FILENO;
221 /* for win32 set stdin/stdout to binary mode */
222 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
223 LOG_WARNING("cannot change stdout mode to binary");
224 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
225 LOG_WARNING("cannot change stdin mode to binary");
227 socket_nonblock(c->fd);
232 LOG_ERROR("unknown connection type: %d", type);
236 /* add to the end of linked list */
237 for (p = &services; *p; p = &(*p)->next);
243 int remove_service(unsigned short port)
245 service_t **p = &services;
264 /* redirect p to next list pointer */
271 int remove_services(void)
273 service_t *c = services;
278 service_t *next = c->next;
289 /* remember the last service for unlinking */
298 extern void openocd_sleep_prelude(void);
299 extern void openocd_sleep_postlude(void);
301 int server_loop(command_context_t *command_context)
305 /* used in select() */
310 /* used in accept() */
314 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
315 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
318 /* do regular tasks after at most 10ms */
322 while(!shutdown_openocd)
324 /* monitor sockets for acitvity */
328 /* add service and connection fds to read_fds */
329 for (service = services; service; service = service->next)
331 if (service->fd != -1)
333 /* listen for new connections */
334 FD_SET(service->fd, &read_fds);
336 if (service->fd > fd_max)
337 fd_max = service->fd;
340 if (service->connections)
344 for (c = service->connections; c; c = c->next)
346 /* check for activity on the connection */
347 FD_SET(c->fd, &read_fds);
355 #if BUILD_ECOSBOARD == 0
356 if (server_use_pipes == 0)
358 /* add STDIN to read_fds */
359 FD_SET(fileno(stdin), &read_fds);
364 openocd_sleep_prelude();
367 /* Only while we're sleeping we'll let others run */
368 retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv);
369 openocd_sleep_postlude();
375 errno = WSAGetLastError();
377 if (errno == WSAEINTR)
381 LOG_ERROR("error during select: %s", strerror(errno));
392 LOG_ERROR("error during select: %s", strerror(errno));
398 target_call_timer_callbacks();
399 process_jim_events ();
403 /* do regular tasks after at most 100ms */
406 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
409 for (service = services; service; service = service->next)
411 /* handle new connections on listeners */
412 if ((service->fd != -1)
413 && (FD_ISSET(service->fd, &read_fds)))
415 if (service->max_connections > 0)
417 add_connection(service, command_context);
421 if (service->type != CONNECTION_PIPE)
423 struct sockaddr_in sin;
424 unsigned int address_size = sizeof(sin);
426 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
427 close_socket(tmp_fd);
429 LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
433 /* handle activity on connections */
434 if (service->connections)
438 for (c = service->connections; c;)
440 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
442 if ((retval = service->input(c)) != ERROR_OK)
444 connection_t *next = c->next;
445 if (service->type == CONNECTION_PIPE)
447 /* if connection uses a pipe then shutdown openocd on error */
448 shutdown_openocd = 1;
450 remove_connection(service, c);
451 LOG_INFO("dropped '%s' connection - error %d", service->name, retval);
462 #if BUILD_ECOSBOARD == 0
463 /* check for data on stdin if not using pipes */
464 if (server_use_pipes == 0)
466 if (FD_ISSET(fileno(stdin), &read_fds))
468 if (getc(stdin) == 'x')
470 shutdown_openocd = 1;
477 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
479 if (msg.message == WM_QUIT)
480 shutdown_openocd = 1;
489 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
491 shutdown_openocd = 1;
495 void sig_handler(int sig) {
496 shutdown_openocd = 1;
500 int server_init(void)
503 WORD wVersionRequested;
506 wVersionRequested = MAKEWORD( 2, 2 );
508 if (WSAStartup(wVersionRequested, &wsaData) != 0)
510 LOG_ERROR("Failed to Open Winsock");
514 SetConsoleCtrlHandler( ControlHandler, TRUE );
516 signal(SIGINT, sig_handler);
517 signal(SIGTERM, sig_handler);
518 signal(SIGBREAK, sig_handler);
519 signal(SIGABRT, sig_handler);
525 int server_quit(void)
531 SetConsoleCtrlHandler( ControlHandler, FALSE );
537 int server_register_commands(command_context_t *context)
539 register_command(context, NULL, "shutdown", handle_shutdown_command,
540 COMMAND_ANY, "shut the server down");
545 /* tell the server we want to shut down */
546 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
548 shutdown_openocd = 1;
550 return ERROR_COMMAND_CLOSE_CONNECTION;