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 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
27 #include "replacements.h"
32 #include "telnet_server.h"
40 #include <sys/types.h>
44 #include <netinet/tcp.h>
47 service_t *services = NULL;
49 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
50 static int shutdown_openocd = 0;
51 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 int add_connection(service_t *service, command_context_t *cmd_ctx)
55 unsigned int address_size;
60 c = malloc(sizeof(connection_t));
62 memset(&c->sin, 0, sizeof(c->sin));
63 c->cmd_ctx = copy_command_context(cmd_ctx);
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)
88 LOG_ERROR("attempted '%s' connection rejected", service->name);
93 /* add to the end of linked list */
94 for (p = &service->connections; *p; p = &(*p)->next);
97 service->max_connections--;
102 int remove_connection(service_t *service, connection_t *connection)
104 connection_t **p = &service->connections;
107 /* find connection */
110 if (c->fd == connection->fd)
112 service->connection_closed(c);
114 command_done(c->cmd_ctx);
116 /* delete connection */
120 service->max_connections++;
124 /* redirect p to next list pointer */
131 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)
134 int so_reuseaddr_option = 1;
136 c = malloc(sizeof(service_t));
138 c->name = strdup(name);
141 c->max_connections = max_connections;
143 c->connections = NULL;
144 c->new_connection = new_connection_handler;
145 c->input = input_handler;
146 c->connection_closed = connection_closed_handler;
150 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
152 LOG_ERROR("error creating socket: %s", strerror(errno));
156 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
158 socket_nonblock(c->fd);
160 memset(&c->sin, 0, sizeof(c->sin));
161 c->sin.sin_family = AF_INET;
162 c->sin.sin_addr.s_addr = INADDR_ANY;
163 c->sin.sin_port = htons(port);
165 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
167 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
173 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
175 int window_size = 128 * 1024;
177 /* These setsockopt()s must happen before the listen() */
179 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
180 (char *)&window_size, sizeof(window_size));
181 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
182 (char *)&window_size, sizeof(window_size));
184 if (listen(c->fd, 1) == -1)
186 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
190 /* add to the end of linked list */
191 for (p = &services; *p; p = &(*p)->next);
197 int remove_service(unsigned short port)
199 service_t **p = &services;
218 /* redirect p to next list pointer */
225 int remove_services(void)
227 service_t *c = services;
232 service_t *next = c->next;
243 /* remember the last service for unlinking */
252 extern void openocd_sleep_prelude(void);
253 extern void openocd_sleep_postlude(void);
255 int server_loop(command_context_t *command_context)
259 /* used in select() */
264 /* used in accept() */
268 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
269 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
272 /* do regular tasks after at most 10ms */
276 while(!shutdown_openocd)
278 /* monitor sockets for acitvity */
282 /* add service and connection fds to read_fds */
283 for (service = services; service; service = service->next)
285 if (service->fd != -1)
287 /* listen for new connections */
288 FD_SET(service->fd, &read_fds);
290 if (service->fd > fd_max)
291 fd_max = service->fd;
294 if (service->connections)
298 for (c = service->connections; c; c = c->next)
300 /* check for activity on the connection */
301 FD_SET(c->fd, &read_fds);
309 #ifndef BUILD_ECOSBOARD
310 /* add STDIN to read_fds */
311 FD_SET(fileno(stdin), &read_fds);
315 openocd_sleep_prelude();
317 // Only while we're sleeping we'll let others run
318 retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv);
319 openocd_sleep_postlude();
325 errno = WSAGetLastError();
327 if (errno == WSAEINTR)
331 LOG_ERROR("error during select: %s", strerror(errno));
342 LOG_ERROR("error during select: %s", strerror(errno));
348 target_call_timer_callbacks();
352 /* do regular tasks after at most 100ms */
355 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
358 for (service = services; service; service = service->next)
360 /* handle new connections on listeners */
361 if ((service->fd != -1)
362 && (FD_ISSET(service->fd, &read_fds)))
364 if (service->max_connections > 0)
366 add_connection(service, command_context);
370 struct sockaddr_in sin;
371 unsigned int address_size = sizeof(sin);
373 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
374 close_socket(tmp_fd);
375 LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
379 /* handle activity on connections */
380 if (service->connections)
384 for (c = service->connections; c;)
386 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
388 if (service->input(c) != ERROR_OK)
390 connection_t *next = c->next;
391 remove_connection(service, c);
392 LOG_INFO("dropped '%s' connection", service->name);
403 #ifndef BUILD_ECOSBOARD
404 if (FD_ISSET(fileno(stdin), &read_fds))
406 if (getc(stdin) == 'x')
408 shutdown_openocd = 1;
414 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
416 if (msg.message == WM_QUIT)
417 shutdown_openocd = 1;
426 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
428 shutdown_openocd = 1;
432 void sig_handler(int sig) {
433 shutdown_openocd = 1;
437 int server_init(void)
440 WORD wVersionRequested;
443 wVersionRequested = MAKEWORD( 2, 2 );
445 if (WSAStartup(wVersionRequested, &wsaData) != 0)
447 LOG_ERROR("Failed to Open Winsock");
451 SetConsoleCtrlHandler( ControlHandler, TRUE );
453 signal(SIGINT, sig_handler);
454 signal(SIGTERM, sig_handler);
455 signal(SIGBREAK, sig_handler);
456 signal(SIGABRT, sig_handler);
463 int server_quit(void)
469 SetConsoleCtrlHandler( ControlHandler, FALSE );
475 int server_register_commands(command_context_t *context)
477 register_command(context, NULL, "shutdown", handle_shutdown_command,
478 COMMAND_ANY, "shut the server down");
483 /* tell the server we want to shut down */
484 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
486 shutdown_openocd = 1;
488 return ERROR_COMMAND_CLOSE_CONNECTION;