1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Ø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 ***************************************************************************/
32 #include <target/target.h>
33 #include <target/target_request.h>
35 #include "tcl_server.h"
36 #include "telnet_server.h"
41 #include <netinet/tcp.h>
44 static struct service
*services
;
46 /* shutdown_openocd == 1: exit the main event loop, and quit the debugger */
47 static int shutdown_openocd
;
49 static 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
));
59 memset(&c
->sin
, 0, sizeof(c
->sin
));
60 c
->cmd_ctx
= copy_command_context(cmd_ctx
);
66 if (service
->type
== CONNECTION_TCP
) {
67 address_size
= sizeof(c
->sin
);
69 c
->fd
= accept(service
->fd
, (struct sockaddr
*)&service
->sin
, &address_size
);
72 /* This increases performance dramatically for e.g. GDB load which
73 * does not have a sliding window protocol.
75 * Ignore errors from this fn as it probably just means less performance
77 setsockopt(c
->fd
, /* socket affected */
78 IPPROTO_TCP
, /* set option at TCP level */
79 TCP_NODELAY
, /* name of option */
80 (char *)&flag
, /* the cast is historical cruft */
81 sizeof(int)); /* length of option value */
83 LOG_INFO("accepting '%s' connection from %s", service
->name
, service
->port
);
84 retval
= service
->new_connection(c
);
85 if (retval
!= ERROR_OK
) {
87 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
91 } else if (service
->type
== CONNECTION_STDINOUT
) {
93 c
->fd_out
= fileno(stdout
);
96 /* we are using stdin/out so ignore ctrl-c under windoze */
97 SetConsoleCtrlHandler(NULL
, TRUE
);
100 /* do not check for new connections again on stdin */
103 LOG_INFO("accepting '%s' connection from pipe", service
->name
);
104 retval
= service
->new_connection(c
);
105 if (retval
!= ERROR_OK
) {
106 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
110 } else if (service
->type
== CONNECTION_PIPE
) {
112 /* do not check for new connections again on stdin */
115 char *out_file
= alloc_printf("%so", service
->port
);
116 c
->fd_out
= open(out_file
, O_WRONLY
);
118 if (c
->fd_out
== -1) {
119 LOG_ERROR("could not open %s", service
->port
);
123 LOG_INFO("accepting '%s' connection from pipe %s", service
->name
, service
->port
);
124 retval
= service
->new_connection(c
);
125 if (retval
!= ERROR_OK
) {
126 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
132 /* add to the end of linked list */
133 for (p
= &service
->connections
; *p
; p
= &(*p
)->next
)
137 service
->max_connections
--;
142 static int remove_connection(struct service
*service
, struct connection
*connection
)
144 struct connection
**p
= &service
->connections
;
145 struct connection
*c
;
147 /* find connection */
149 if (c
->fd
== connection
->fd
) {
150 service
->connection_closed(c
);
151 if (service
->type
== CONNECTION_TCP
)
153 else if (service
->type
== CONNECTION_PIPE
) {
154 /* The service will listen to the pipe again */
155 c
->service
->fd
= c
->fd
;
158 command_done(c
->cmd_ctx
);
160 /* delete connection */
164 service
->max_connections
++;
168 /* redirect p to next list pointer */
175 /* FIX! make service return error instead of invoking exit() */
176 int add_service(char *name
,
179 new_connection_handler_t new_connection_handler
,
180 input_handler_t input_handler
,
181 connection_closed_handler_t connection_closed_handler
,
184 struct service
*c
, **p
;
185 int so_reuseaddr_option
= 1;
187 c
= malloc(sizeof(struct service
));
189 c
->name
= strdup(name
);
190 c
->port
= strdup(port
);
191 c
->max_connections
= 1; /* Only TCP/IP ports can support more than one connection */
193 c
->connections
= NULL
;
194 c
->new_connection
= new_connection_handler
;
195 c
->input
= input_handler
;
196 c
->connection_closed
= connection_closed_handler
;
200 if (strcmp(c
->port
, "pipe") == 0)
201 c
->type
= CONNECTION_STDINOUT
;
204 portnumber
= strtol(c
->port
, &end
, 0);
205 if (!*end
&& (parse_long(c
->port
, &portnumber
) == ERROR_OK
)) {
206 c
->portnumber
= portnumber
;
207 c
->type
= CONNECTION_TCP
;
209 c
->type
= CONNECTION_PIPE
;
212 if (c
->type
== CONNECTION_TCP
) {
213 c
->max_connections
= max_connections
;
215 c
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
217 LOG_ERROR("error creating socket: %s", strerror(errno
));
224 (void *)&so_reuseaddr_option
,
227 socket_nonblock(c
->fd
);
229 memset(&c
->sin
, 0, sizeof(c
->sin
));
230 c
->sin
.sin_family
= AF_INET
;
231 c
->sin
.sin_addr
.s_addr
= INADDR_ANY
;
232 c
->sin
.sin_port
= htons(c
->portnumber
);
234 if (bind(c
->fd
, (struct sockaddr
*)&c
->sin
, sizeof(c
->sin
)) == -1) {
235 LOG_ERROR("couldn't bind to socket: %s", strerror(errno
));
241 setsockopt(c
->fd
, IPPROTO_TCP
, TCP_MAXSEG
, &segsize
, sizeof(int));
243 int window_size
= 128 * 1024;
245 /* These setsockopt()s must happen before the listen() */
247 setsockopt(c
->fd
, SOL_SOCKET
, SO_SNDBUF
,
248 (char *)&window_size
, sizeof(window_size
));
249 setsockopt(c
->fd
, SOL_SOCKET
, SO_RCVBUF
,
250 (char *)&window_size
, sizeof(window_size
));
252 if (listen(c
->fd
, 1) == -1) {
253 LOG_ERROR("couldn't listen on socket: %s", strerror(errno
));
256 } else if (c
->type
== CONNECTION_STDINOUT
) {
257 c
->fd
= fileno(stdin
);
260 /* for win32 set stdin/stdout to binary mode */
261 if (_setmode(_fileno(stdout
), _O_BINARY
) < 0)
262 LOG_WARNING("cannot change stdout mode to binary");
263 if (_setmode(_fileno(stdin
), _O_BINARY
) < 0)
264 LOG_WARNING("cannot change stdin mode to binary");
265 if (_setmode(_fileno(stderr
), _O_BINARY
) < 0)
266 LOG_WARNING("cannot change stderr mode to binary");
268 socket_nonblock(c
->fd
);
270 } else if (c
->type
== CONNECTION_PIPE
) {
272 /* we currenty do not support named pipes under win32
273 * so exit openocd for now */
274 LOG_ERROR("Named pipes currently not supported under this os");
277 /* Pipe we're reading from */
278 c
->fd
= open(c
->port
, O_RDONLY
| O_NONBLOCK
);
280 LOG_ERROR("could not open %s", c
->port
);
286 /* add to the end of linked list */
287 for (p
= &services
; *p
; p
= &(*p
)->next
)
294 static int remove_services(void)
296 struct service
*c
= services
;
300 struct service
*next
= c
->next
;
303 free((void *)c
->name
);
305 if (c
->type
== CONNECTION_PIPE
) {
310 free((void *)c
->port
);
318 /* remember the last service for unlinking */
327 int server_loop(struct command_context
*command_context
)
329 struct service
*service
;
333 /* used in select() */
337 /* used in accept() */
341 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
342 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
345 while (!shutdown_openocd
) {
346 /* monitor sockets for activity */
350 /* add service and connection fds to read_fds */
351 for (service
= services
; service
; service
= service
->next
) {
352 if (service
->fd
!= -1) {
353 /* listen for new connections */
354 FD_SET(service
->fd
, &read_fds
);
356 if (service
->fd
> fd_max
)
357 fd_max
= service
->fd
;
360 if (service
->connections
) {
361 struct connection
*c
;
363 for (c
= service
->connections
; c
; c
= c
->next
) {
364 /* check for activity on the connection */
365 FD_SET(c
->fd
, &read_fds
);
375 /* we're just polling this iteration, this is faster on embedded
378 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
382 /* Only while we're sleeping we'll let others run */
383 openocd_sleep_prelude();
385 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
386 openocd_sleep_postlude();
392 errno
= WSAGetLastError();
394 if (errno
== WSAEINTR
)
397 LOG_ERROR("error during select: %s", strerror(errno
));
405 LOG_ERROR("error during select: %s", strerror(errno
));
412 /* We only execute these callbacks when there was nothing to do or we timed
414 target_call_timer_callbacks();
415 process_jim_events(command_context
);
417 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
419 /* We timed out/there was nothing to do, timeout rather than poll next time
423 /* There was something to do, next time we'll just poll */
427 /* This is a simple back-off algorithm where we immediately
428 * re-poll if we did something this time around.
430 * This greatly improves performance of DCC.
432 poll_ok
= poll_ok
|| target_got_message();
434 for (service
= services
; service
; service
= service
->next
) {
435 /* handle new connections on listeners */
436 if ((service
->fd
!= -1)
437 && (FD_ISSET(service
->fd
, &read_fds
))) {
438 if (service
->max_connections
> 0)
439 add_connection(service
, command_context
);
441 if (service
->type
== CONNECTION_TCP
) {
442 struct sockaddr_in sin
;
443 socklen_t address_size
= sizeof(sin
);
445 tmp_fd
= accept(service
->fd
,
446 (struct sockaddr
*)&service
->sin
,
448 close_socket(tmp_fd
);
451 "rejected '%s' connection, no more connections allowed",
456 /* handle activity on connections */
457 if (service
->connections
) {
458 struct connection
*c
;
460 for (c
= service
->connections
; c
; ) {
461 if ((FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
) {
462 retval
= service
->input(c
);
463 if (retval
!= ERROR_OK
) {
464 struct connection
*next
= c
->next
;
465 if (service
->type
== CONNECTION_PIPE
) {
466 /* if connection uses a pipe then
467 *shutdown openocd on error */
468 shutdown_openocd
= 1;
470 remove_connection(service
, c
);
471 LOG_INFO("dropped '%s' connection",
484 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
485 if (msg
.message
== WM_QUIT
)
486 shutdown_openocd
= 1;
495 BOOL WINAPI
ControlHandler(DWORD dwCtrlType
)
497 shutdown_openocd
= 1;
501 void sig_handler(int sig
)
503 shutdown_openocd
= 1;
507 int server_preinit(void)
509 /* this currently only calls WSAStartup on native win32 systems
510 * before any socket operations are performed.
511 * This is an issue if you call init in your config script */
514 WORD wVersionRequested
;
517 wVersionRequested
= MAKEWORD(2, 2);
519 if (WSAStartup(wVersionRequested
, &wsaData
) != 0) {
520 LOG_ERROR("Failed to Open Winsock");
524 /* register ctrl-c handler */
525 SetConsoleCtrlHandler(ControlHandler
, TRUE
);
527 signal(SIGINT
, sig_handler
);
528 signal(SIGTERM
, sig_handler
);
529 signal(SIGBREAK
, sig_handler
);
530 signal(SIGABRT
, sig_handler
);
536 int server_init(struct command_context
*cmd_ctx
)
538 int ret
= tcl_init();
542 return telnet_init("Open On-Chip Debugger");
545 int server_quit(void)
551 SetConsoleCtrlHandler(ControlHandler
, FALSE
);
557 int connection_write(struct connection
*connection
, const void *data
, int len
)
560 /* successful no-op. Sockets and pipes behave differently here... */
563 if (connection
->service
->type
== CONNECTION_TCP
)
564 return write_socket(connection
->fd_out
, data
, len
);
566 return write(connection
->fd_out
, data
, len
);
569 int connection_read(struct connection
*connection
, void *data
, int len
)
571 if (connection
->service
->type
== CONNECTION_TCP
)
572 return read_socket(connection
->fd
, data
, len
);
574 return read(connection
->fd
, data
, len
);
577 /* tell the server we want to shut down */
578 COMMAND_HANDLER(handle_shutdown_command
)
580 LOG_USER("shutdown command invoked");
582 shutdown_openocd
= 1;
587 static const struct command_registration server_command_handlers
[] = {
590 .handler
= &handle_shutdown_command
,
593 .help
= "shut the server down",
595 COMMAND_REGISTRATION_DONE
598 int server_register_commands(struct command_context
*cmd_ctx
)
600 int retval
= telnet_register_commands(cmd_ctx
);
601 if (ERROR_OK
!= retval
)
604 retval
= tcl_register_commands(cmd_ctx
);
605 if (ERROR_OK
!= retval
)
608 return register_commands(cmd_ctx
, NULL
, server_command_handlers
);
611 SERVER_PORT_COMMAND()
615 command_print(CMD_CTX
, "%d", *out
);
620 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], port
);
625 return ERROR_COMMAND_SYNTAX_ERROR
;
630 SERVER_PIPE_COMMAND()
634 command_print(CMD_CTX
, "%s", *out
);
638 if (CMD_CTX
->mode
== COMMAND_EXEC
) {
639 LOG_WARNING("unable to change server port after init");
640 return ERROR_COMMAND_ARGUMENT_INVALID
;
642 const char *t
= strdup(CMD_ARGV
[0]);
648 return ERROR_COMMAND_SYNTAX_ERROR
;