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 ***************************************************************************/
36 #include <netinet/tcp.h>
40 service_t
*services
= NULL
;
42 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
43 static int shutdown_openocd
= 0;
44 int handle_shutdown_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
46 /* set when using pipes rather than tcp */
47 int server_use_pipes
= 0;
49 int add_connection(service_t
*service
, command_context_t
*cmd_ctx
)
51 socklen_t address_size
;
56 c
= malloc(sizeof(connection_t
));
58 memset(&c
->sin
, 0, sizeof(c
->sin
));
59 c
->cmd_ctx
= copy_command_context(cmd_ctx
);
65 if (service
->type
== CONNECTION_TCP
)
67 address_size
= sizeof(c
->sin
);
69 c
->fd
= accept(service
->fd
, (struct sockaddr
*)&service
->sin
, &address_size
);
71 /* This increases performance dramatically for e.g. GDB load which
72 * does not have a sliding window protocol. */
73 retval
=setsockopt(c
->fd
, /* socket affected */
74 IPPROTO_TCP
, /* set option at TCP level */
75 TCP_NODELAY
, /* name of option */
76 (char *)&flag
, /* the cast is historical cruft */
77 sizeof(int)); /* length of option value */
79 LOG_INFO("accepting '%s' connection from %i", service
->name
, c
->sin
.sin_port
);
80 if ((retval
= service
->new_connection(c
)) != ERROR_OK
)
83 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
88 else if (service
->type
== CONNECTION_PIPE
)
92 /* do not check for new connections again on stdin */
95 LOG_INFO("accepting '%s' connection from pipe", service
->name
);
96 if ((retval
= service
->new_connection(c
)) != ERROR_OK
)
98 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
104 /* add to the end of linked list */
105 for (p
= &service
->connections
; *p
; p
= &(*p
)->next
);
108 service
->max_connections
--;
113 int remove_connection(service_t
*service
, connection_t
*connection
)
115 connection_t
**p
= &service
->connections
;
118 /* find connection */
121 if (c
->fd
== connection
->fd
)
123 service
->connection_closed(c
);
124 if (service
->type
== CONNECTION_TCP
)
126 command_done(c
->cmd_ctx
);
128 /* delete connection */
132 service
->max_connections
++;
136 /* redirect p to next list pointer */
143 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
)
146 int so_reuseaddr_option
= 1;
148 c
= malloc(sizeof(service_t
));
150 c
->name
= strdup(name
);
153 c
->max_connections
= max_connections
;
155 c
->connections
= NULL
;
156 c
->new_connection
= new_connection_handler
;
157 c
->input
= input_handler
;
158 c
->connection_closed
= connection_closed_handler
;
162 if (type
== CONNECTION_TCP
)
164 if ((c
->fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
166 LOG_ERROR("error creating socket: %s", strerror(errno
));
170 setsockopt(c
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&so_reuseaddr_option
, sizeof(int));
172 socket_nonblock(c
->fd
);
174 memset(&c
->sin
, 0, sizeof(c
->sin
));
175 c
->sin
.sin_family
= AF_INET
;
176 c
->sin
.sin_addr
.s_addr
= INADDR_ANY
;
177 c
->sin
.sin_port
= htons(port
);
179 if (bind(c
->fd
, (struct sockaddr
*)&c
->sin
, sizeof(c
->sin
)) == -1)
181 LOG_ERROR("couldn't bind to socket: %s", strerror(errno
));
187 setsockopt(c
->fd
, IPPROTO_TCP
, TCP_MAXSEG
, &segsize
, sizeof(int));
189 int window_size
= 128 * 1024;
191 /* These setsockopt()s must happen before the listen() */
193 setsockopt(c
->fd
, SOL_SOCKET
, SO_SNDBUF
,
194 (char *)&window_size
, sizeof(window_size
));
195 setsockopt(c
->fd
, SOL_SOCKET
, SO_RCVBUF
,
196 (char *)&window_size
, sizeof(window_size
));
198 if (listen(c
->fd
, 1) == -1)
200 LOG_ERROR("couldn't listen on socket: %s", strerror(errno
));
204 else if (type
== CONNECTION_PIPE
)
207 c
->fd
= STDIN_FILENO
;
210 /* for win32 set stdin/stdout to binary mode */
211 if (_setmode(_fileno(stdout
), _O_BINARY
) < 0)
212 LOG_WARNING("cannot change stdout mode to binary");
213 if (_setmode(_fileno(stdin
), _O_BINARY
) < 0)
214 LOG_WARNING("cannot change stdin mode to binary");
215 if (_setmode(_fileno(stderr
), _O_BINARY
) < 0)
216 LOG_WARNING("cannot change stderr mode to binary");
218 socket_nonblock(c
->fd
);
223 LOG_ERROR("unknown connection type: %d", type
);
227 /* add to the end of linked list */
228 for (p
= &services
; *p
; p
= &(*p
)->next
);
234 int remove_service(unsigned short port
)
236 service_t
**p
= &services
;
255 /* redirect p to next list pointer */
262 int remove_services(void)
264 service_t
*c
= services
;
269 service_t
*next
= c
->next
;
280 /* remember the last service for unlinking */
289 extern void openocd_sleep_prelude(void);
290 extern void openocd_sleep_postlude(void);
292 int server_loop(command_context_t
*command_context
)
296 /* used in select() */
301 /* used in accept() */
305 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
306 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
309 /* do regular tasks after at most 10ms */
313 while(!shutdown_openocd
)
315 /* monitor sockets for acitvity */
319 /* add service and connection fds to read_fds */
320 for (service
= services
; service
; service
= service
->next
)
322 if (service
->fd
!= -1)
324 /* listen for new connections */
325 FD_SET(service
->fd
, &read_fds
);
327 if (service
->fd
> fd_max
)
328 fd_max
= service
->fd
;
331 if (service
->connections
)
335 for (c
= service
->connections
; c
; c
= c
->next
)
337 /* check for activity on the connection */
338 FD_SET(c
->fd
, &read_fds
);
346 #if BUILD_ECOSBOARD == 0
347 if (server_use_pipes
== 0)
349 /* add STDIN to read_fds */
350 FD_SET(fileno(stdin
), &read_fds
);
355 openocd_sleep_prelude();
358 /* Only while we're sleeping we'll let others run */
359 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
360 openocd_sleep_postlude();
366 errno
= WSAGetLastError();
368 if (errno
== WSAEINTR
)
372 LOG_ERROR("error during select: %s", strerror(errno
));
383 LOG_ERROR("error during select: %s", strerror(errno
));
389 target_call_timer_callbacks();
390 process_jim_events ();
394 /* do regular tasks after at most 100ms */
397 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
400 for (service
= services
; service
; service
= service
->next
)
402 /* handle new connections on listeners */
403 if ((service
->fd
!= -1)
404 && (FD_ISSET(service
->fd
, &read_fds
)))
406 if (service
->max_connections
> 0)
408 add_connection(service
, command_context
);
412 if (service
->type
!= CONNECTION_PIPE
)
414 struct sockaddr_in sin
;
415 socklen_t address_size
= sizeof(sin
);
417 tmp_fd
= accept(service
->fd
, (struct sockaddr
*)&service
->sin
, &address_size
);
418 close_socket(tmp_fd
);
420 LOG_INFO("rejected '%s' connection, no more connections allowed", service
->name
);
424 /* handle activity on connections */
425 if (service
->connections
)
429 for (c
= service
->connections
; c
;)
431 if ((FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
)
433 if ((retval
= service
->input(c
)) != ERROR_OK
)
435 connection_t
*next
= c
->next
;
436 if (service
->type
== CONNECTION_PIPE
)
438 /* if connection uses a pipe then shutdown openocd on error */
439 shutdown_openocd
= 1;
441 remove_connection(service
, c
);
442 LOG_INFO("dropped '%s' connection - error %d", service
->name
, retval
);
453 #if BUILD_ECOSBOARD == 0
454 /* check for data on stdin if not using pipes */
455 if (server_use_pipes
== 0)
457 if (FD_ISSET(fileno(stdin
), &read_fds
))
459 if (getc(stdin
) == 'x')
461 shutdown_openocd
= 1;
468 while (PeekMessage(&msg
,NULL
,0,0,PM_REMOVE
))
470 if (msg
.message
== WM_QUIT
)
471 shutdown_openocd
= 1;
480 BOOL WINAPI
ControlHandler(DWORD dwCtrlType
)
482 shutdown_openocd
= 1;
486 void sig_handler(int sig
) {
487 shutdown_openocd
= 1;
491 int server_init(void)
494 WORD wVersionRequested
;
497 wVersionRequested
= MAKEWORD(2, 2);
499 if (WSAStartup(wVersionRequested
, &wsaData
) != 0)
501 LOG_ERROR("Failed to Open Winsock");
505 if (server_use_pipes
== 0)
507 /* register ctrl-c handler */
508 SetConsoleCtrlHandler(ControlHandler
, TRUE
);
512 /* we are using pipes so ignore ctrl-c */
513 SetConsoleCtrlHandler(NULL
, 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
;