1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
29 #include "telnet_server.h"
37 #include <sys/types.h>
41 service_t *services = NULL;
43 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
44 static int shutdown_openocd = 0;
45 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 int add_connection(service_t *service, command_context_t *cmd_ctx)
49 unsigned int address_size;
53 c = malloc(sizeof(connection_t));
55 memset(&c->sin, 0, sizeof(c->sin));
56 c->cmd_ctx = copy_command_context(cmd_ctx);
62 address_size = sizeof(c->sin);
63 c->fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
65 if ((retval = service->new_connection(c)) == ERROR_OK)
67 INFO("accepted '%s' connection from %i", service->name, c->sin.sin_port);
72 INFO("attempted '%s' connection rejected", service->name);
76 /* add to the end of linked list */
77 for (p = &service->connections; *p; p = &(*p)->next);
80 service->max_connections--;
85 int remove_connection(service_t *service, connection_t *connection)
87 connection_t **p = &service->connections;
93 if (c->fd == connection->fd)
95 service->connection_closed(c);
97 command_done(c->cmd_ctx);
99 /* delete connection */
103 service->max_connections++;
107 /* redirect p to next list pointer */
114 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)
117 int so_reuseaddr_option = 1;
119 c = malloc(sizeof(service_t));
121 c->name = strdup(name);
124 c->max_connections = max_connections;
126 c->connections = NULL;
127 c->new_connection = new_connection_handler;
128 c->input = input_handler;
129 c->connection_closed = connection_closed_handler;
133 if ((c->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
135 ERROR("error creating socket: %s", strerror(errno));
139 setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int));
141 socket_nonblock(c->fd);
143 memset(&c->sin, 0, sizeof(c->sin));
144 c->sin.sin_family = AF_INET;
145 c->sin.sin_addr.s_addr = INADDR_ANY;
146 c->sin.sin_port = htons(port);
148 if (bind(c->fd, (struct sockaddr *)&c->sin, sizeof(c->sin)) == -1)
150 ERROR("couldn't bind to socket: %s", strerror(errno));
154 if (listen(c->fd, 1) == -1)
156 ERROR("couldn't listen on socket: %s", strerror(errno));
160 /* add to the end of linked list */
161 for (p = &services; *p; p = &(*p)->next);
167 int remove_service(unsigned short port)
169 service_t **p = &services;
188 /* redirect p to next list pointer */
195 int remove_services()
197 service_t *c = services;
202 service_t *next = c->next;
213 /* remember the last service for unlinking */
222 int server_loop(command_context_t *command_context)
226 /* used in select() */
231 /* used in accept() */
235 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
236 ERROR("couldn't set SIGPIPE to SIG_IGN");
239 /* do regular tasks after at most 10ms */
243 while(!shutdown_openocd)
245 /* monitor sockets for acitvity */
249 /* add service and connection fds to read_fds */
250 for (service = services; service; service = service->next)
252 if (service->fd != -1)
254 /* listen for new connections */
255 FD_SET(service->fd, &read_fds);
257 if (service->fd > fd_max)
258 fd_max = service->fd;
261 if (service->connections)
265 for (c = service->connections; c; c = c->next)
267 /* check for activity on the connection */
268 FD_SET(c->fd, &read_fds);
276 /* add STDIN to read_fds */
277 FD_SET(fileno(stdin), &read_fds);
280 retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv);
286 errno = WSAGetLastError();
288 if (errno == WSAEINTR)
292 ERROR("error during select: %s", strerror(errno));
303 ERROR("error during select: %s", strerror(errno));
309 target_call_timer_callbacks();
313 /* do regular tasks after at most 100ms */
316 FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */
319 for (service = services; service; service = service->next)
321 /* handle new connections on listeners */
322 if ((service->fd != -1)
323 && (FD_ISSET(service->fd, &read_fds)))
325 if (service->max_connections > 0)
327 add_connection(service, command_context);
331 struct sockaddr_in sin;
332 unsigned int address_size = sizeof(sin);
334 tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size);
335 close_socket(tmp_fd);
336 INFO("rejected '%s' connection, no more connections allowed", service->name);
340 /* handle activity on connections */
341 if (service->connections)
345 for (c = service->connections; c;)
347 if ((FD_ISSET(c->fd, &read_fds)) || c->input_pending)
349 if (service->input(c) != ERROR_OK)
351 connection_t *next = c->next;
352 remove_connection(service, c);
353 INFO("dropped '%s' connection", service->name);
364 if (FD_ISSET(fileno(stdin), &read_fds))
366 if (getc(stdin) == 'x')
368 shutdown_openocd = 1;
373 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
375 if (msg.message == WM_QUIT)
376 shutdown_openocd = 1;
385 BOOL WINAPI ControlHandler(DWORD dwCtrlType)
387 shutdown_openocd = 1;
391 void sig_handler(int sig) {
392 shutdown_openocd = 1;
399 WORD wVersionRequested;
402 wVersionRequested = MAKEWORD( 2, 2 );
404 if (WSAStartup(wVersionRequested, &wsaData) != 0)
406 ERROR("Failed to Open Winsock");
410 SetConsoleCtrlHandler( ControlHandler, TRUE );
412 signal(SIGINT, sig_handler);
413 signal(SIGTERM, sig_handler);
414 signal(SIGBREAK, sig_handler);
415 signal(SIGABRT, sig_handler);
428 SetConsoleCtrlHandler( ControlHandler, FALSE );
434 int server_register_commands(command_context_t *context)
436 register_command(context, NULL, "shutdown", handle_shutdown_command,
437 COMMAND_ANY, "shut the server down");
442 /* tell the server we want to shut down */
443 int handle_shutdown_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
445 shutdown_openocd = 1;
447 return ERROR_COMMAND_CLOSE_CONNECTION;