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 int add_connection(service_t *service, command_context_t *cmd_ctx)
58 unsigned int address_size;
63 c = malloc(sizeof(connection_t));
65 memset(&c->sin, 0, sizeof(c->sin));
66 c->cmd_ctx = copy_command_context(cmd_ctx);
72 address_size = sizeof(c->sin);
74 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
76 /* This increases performance dramatically for e.g. GDB load which
77 * does not have a sliding window protocol. */
78 retval=setsockopt(c->fd, /* socket affected */
79 IPPROTO_TCP, /* set option at TCP level */
80 TCP_NODELAY, /* name of option */
81 (char *)&flag, /* the cast is historical cruft */
82 sizeof(int)); /* length of option value */
84 LOG_INFO("accepting '%s' connection from %i", service->name, c->sin.sin_port);
85 if ((retval = service->new_connection(c)) == ERROR_OK)
91 LOG_ERROR("attempted '%s' connection rejected", service->name);
96 /* add to the end of linked list */
97 for (p = &service->connections; *p; p = &(*p)->next);
100 service->max_connections--;
105 int remove_connection(service_t *service, connection_t *connection)
107 connection_t **p = &service->connections;
110 /* find connection */
113 if (c->fd == connection->fd)
115 service->connection_closed(c);
117 command_done(c->cmd_ctx);
119 /* delete connection */
123 service->max_connections++;
127 /* redirect p to next list pointer */
134 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)
137 int so_reuseaddr_option = 1;
139 c = malloc(sizeof(service_t));
141 c->name = strdup(name);
144 c->max_connections = max_connections;
146 c->connections = NULL;
147 c->new_connection = new_connection_handler;
148 c->input = input_handler;
149 c->connection_closed = connection_closed_handler;
153 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
155 LOG_ERROR("error creating socket: %s", strerror(errno));
159 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
161 socket_nonblock(c->fd);
163 memset(&c->sin, 0, sizeof(c->sin));
164 c->sin.sin_family = AF_INET;
165 c->sin.sin_addr.s_addr = INADDR_ANY;
166 c->sin.sin_port = htons(port);
168 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
170 LOG_ERROR("couldn't bind to socket: %s", strerror(errno));
176 setsockopt(c->fd, IPPROTO_TCP, TCP_MAXSEG, &segsize, sizeof(int));
178 int window_size = 128 * 1024;
180 /* These setsockopt()s must happen before the listen() */
182 setsockopt(c->fd, SOL_SOCKET, SO_SNDBUF,
183 (char *)&window_size, sizeof(window_size));
184 setsockopt(c->fd, SOL_SOCKET, SO_RCVBUF,
185 (char *)&window_size, sizeof(window_size));
187 if (listen(c->fd, 1) == -1)
189 LOG_ERROR("couldn't listen on socket: %s", strerror(errno));
193 /* add to the end of linked list */
194 for (p = &services; *p; p = &(*p)->next);
200 int remove_service(unsigned short port)
202 service_t **p = &services;
221 /* redirect p to next list pointer */
228 int remove_services(void)
230 service_t *c = services;
235 service_t *next = c->next;
246 /* remember the last service for unlinking */
255 extern void openocd_sleep_prelude(void);
256 extern void openocd_sleep_postlude(void);
258 int server_loop(command_context_t *command_context)
262 /* used in select() */
267 /* used in accept() */
271 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
272 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
275 /* do regular tasks after at most 10ms */
279 while(!shutdown_openocd)
281 /* monitor sockets for acitvity */
285 /* add service and connection fds to read_fds */
286 for (service = services; service; service = service->next)
288 if (service->fd != -1)
290 /* listen for new connections */
291 FD_SET(service->fd, &read_fds);
293 if (service->fd > fd_max)
294 fd_max = service->fd;
297 if (service->connections)
301 for (c = service->connections; c; c = c->next)
303 /* check for activity on the connection */
304 FD_SET(c->fd, &read_fds);
312 #if BUILD_ECOSBOARD == 0
313 /* add STDIN to read_fds */
314 FD_SET(fileno(stdin), &read_fds);
318 openocd_sleep_prelude();
320 // Only while we're sleeping we'll let others run
321 retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv);
322 openocd_sleep_postlude();
328 errno = WSAGetLastError();
330 if (errno == WSAEINTR)
334 LOG_ERROR("error during select: %s", strerror(errno));
345 LOG_ERROR("error during select: %s", strerror(errno));
351 target_call_timer_callbacks();
352 process_jim_events ();
356 /* do regular tasks after at most 100ms */
359 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
362 for (service = services; service; service = service->next)
364 /* handle new connections on listeners */
365 if ((service->fd != -1)
366 && (FD_ISSET(service->fd, &read_fds)))
368 if (service->max_connections > 0)
370 add_connection(service, command_context);
374 struct sockaddr_in sin;
375 unsigned int address_size = sizeof(sin);
377 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
378 close_socket(tmp_fd);
379 LOG_INFO("rejected '%s' connection, no more connections allowed", service->name);
383 /* handle activity on connections */
384 if (service->connections)
388 for (c = service->connections; c;)
390 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
392 if (service->input(c) != ERROR_OK)
394 connection_t *next = c->next;
395 remove_connection(service, c);
396 LOG_INFO("dropped '%s' connection", service->name);
407 #if BUILD_ECOSBOARD == 0
408 if (FD_ISSET(fileno(stdin), &read_fds))
410 if (getc(stdin) == 'x')
412 shutdown_openocd = 1;
418 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
420 if (msg.message == WM_QUIT)
421 shutdown_openocd = 1;
430 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
432 shutdown_openocd = 1;
436 void sig_handler(int sig) {
437 shutdown_openocd = 1;
441 int server_init(void)
444 WORD wVersionRequested;
447 wVersionRequested = MAKEWORD( 2, 2 );
449 if (WSAStartup(wVersionRequested, &wsaData) != 0)
451 LOG_ERROR("Failed to Open Winsock");
455 SetConsoleCtrlHandler( ControlHandler, TRUE );
457 signal(SIGINT, sig_handler);
458 signal(SIGTERM, sig_handler);
459 signal(SIGBREAK, sig_handler);
460 signal(SIGABRT, sig_handler);
467 int server_quit(void)
473 SetConsoleCtrlHandler( ControlHandler, FALSE );
479 int server_register_commands(command_context_t *context)
481 register_command(context, NULL, "shutdown", handle_shutdown_command,
482 COMMAND_ANY, "shut the server down");
487 /* tell the server we want to shut down */
488 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
490 shutdown_openocd = 1;
492 return ERROR_COMMAND_CLOSE_CONNECTION;