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 ***************************************************************************/
37 #include <netinet/tcp.h>
41 struct service
*services
= NULL
;
43 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
44 static int shutdown_openocd
= 0;
46 /* set when using pipes rather than tcp */
47 int server_use_pipes
= 0;
49 int add_connection(struct service
*service
, struct command_context
*cmd_ctx
)
51 socklen_t address_size
;
52 struct connection
*c
, **p
;
56 c
= malloc(sizeof(struct connection
));
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(struct service
*service
, struct connection
*connection
)
115 struct connection
**p
= &service
->connections
;
116 struct connection
*c
;
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
)
145 struct service
*c
, **p
;
146 int so_reuseaddr_option
= 1;
148 c
= malloc(sizeof(struct service
));
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 struct service
**p
= &services
;
255 /* redirect p to next list pointer */
262 int remove_services(void)
264 struct service
*c
= services
;
269 struct service
*next
= c
->next
;
280 /* remember the last service for unlinking */
289 int server_loop(struct command_context
*command_context
)
291 struct service
*service
;
293 /* used in select() */
298 /* used in accept() */
302 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
303 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
306 /* do regular tasks after at most 10ms */
310 while (!shutdown_openocd
)
312 /* monitor sockets for acitvity */
316 /* add service and connection fds to read_fds */
317 for (service
= services
; service
; service
= service
->next
)
319 if (service
->fd
!= -1)
321 /* listen for new connections */
322 FD_SET(service
->fd
, &read_fds
);
324 if (service
->fd
> fd_max
)
325 fd_max
= service
->fd
;
328 if (service
->connections
)
330 struct connection
*c
;
332 for (c
= service
->connections
; c
; c
= c
->next
)
334 /* check for activity on the connection */
335 FD_SET(c
->fd
, &read_fds
);
343 #if BUILD_ECOSBOARD == 0
344 if (server_use_pipes
== 0)
346 /* add STDIN to read_fds */
347 FD_SET(fileno(stdin
), &read_fds
);
352 openocd_sleep_prelude();
355 /* Only while we're sleeping we'll let others run */
356 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
357 openocd_sleep_postlude();
363 errno
= WSAGetLastError();
365 if (errno
== WSAEINTR
)
369 LOG_ERROR("error during select: %s", strerror(errno
));
380 LOG_ERROR("error during select: %s", strerror(errno
));
386 target_call_timer_callbacks();
387 process_jim_events ();
391 /* do regular tasks after at most 100ms */
394 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
397 for (service
= services
; service
; service
= service
->next
)
399 /* handle new connections on listeners */
400 if ((service
->fd
!= -1)
401 && (FD_ISSET(service
->fd
, &read_fds
)))
403 if (service
->max_connections
> 0)
405 add_connection(service
, command_context
);
409 if (service
->type
!= CONNECTION_PIPE
)
411 struct sockaddr_in sin
;
412 socklen_t address_size
= sizeof(sin
);
414 tmp_fd
= accept(service
->fd
, (struct sockaddr
*)&service
->sin
, &address_size
);
415 close_socket(tmp_fd
);
417 LOG_INFO("rejected '%s' connection, no more connections allowed", service
->name
);
421 /* handle activity on connections */
422 if (service
->connections
)
424 struct connection
*c
;
426 for (c
= service
->connections
; c
;)
428 if ((FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
)
430 if ((retval
= service
->input(c
)) != ERROR_OK
)
432 struct connection
*next
= c
->next
;
433 if (service
->type
== CONNECTION_PIPE
)
435 /* if connection uses a pipe then shutdown openocd on error */
436 shutdown_openocd
= 1;
438 remove_connection(service
, c
);
439 LOG_INFO("dropped '%s' connection - error %d", service
->name
, retval
);
450 #if BUILD_ECOSBOARD == 0
451 /* check for data on stdin if not using pipes */
452 if (server_use_pipes
== 0)
454 if (FD_ISSET(fileno(stdin
), &read_fds
))
456 if (getc(stdin
) == 'x')
458 shutdown_openocd
= 1;
465 while (PeekMessage(&msg
,NULL
,0,0,PM_REMOVE
))
467 if (msg
.message
== WM_QUIT
)
468 shutdown_openocd
= 1;
477 BOOL WINAPI
ControlHandler(DWORD dwCtrlType
)
479 shutdown_openocd
= 1;
483 void sig_handler(int sig
) {
484 shutdown_openocd
= 1;
488 int server_init(void)
491 WORD wVersionRequested
;
494 wVersionRequested
= MAKEWORD(2, 2);
496 if (WSAStartup(wVersionRequested
, &wsaData
) != 0)
498 LOG_ERROR("Failed to Open Winsock");
502 if (server_use_pipes
== 0)
504 /* register ctrl-c handler */
505 SetConsoleCtrlHandler(ControlHandler
, TRUE
);
509 /* we are using pipes so ignore ctrl-c */
510 SetConsoleCtrlHandler(NULL
, TRUE
);
513 signal(SIGINT
, sig_handler
);
514 signal(SIGTERM
, sig_handler
);
515 signal(SIGBREAK
, sig_handler
);
516 signal(SIGABRT
, sig_handler
);
522 int server_quit(void)
528 SetConsoleCtrlHandler(ControlHandler
, FALSE
);
534 /* tell the server we want to shut down */
535 COMMAND_HANDLER(handle_shutdown_command
)
537 shutdown_openocd
= 1;
539 return ERROR_COMMAND_CLOSE_CONNECTION
;
542 int server_register_commands(struct command_context
*context
)
544 register_command(context
, NULL
, "shutdown",
545 handle_shutdown_command
, COMMAND_ANY
,
546 "shut the server down");
552 SERVER_PORT_COMMAND()
556 command_print(cmd_ctx
, "%d", *out
);
561 COMMAND_PARSE_NUMBER(u16
, args
[0], port
);
566 return ERROR_INVALID_ARGUMENTS
;