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, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
30 #include <target/target.h>
31 #include <target/target_request.h>
32 #include <target/openrisc/jsp_server.h>
34 #include "tcl_server.h"
35 #include "telnet_server.h"
44 #include <netinet/tcp.h>
47 static struct service
*services
;
49 /* shutdown_openocd == 1: exit the main event loop, and quit the
50 * debugger; 2: quit with non-zero return code */
51 static int shutdown_openocd
;
53 /* store received signal to exit application by killing ourselves */
54 static int last_signal
;
56 /* set the polling period to 100ms */
57 static int polling_period
= 100;
59 /* address by name on which to listen for incoming TCP/IP connections */
60 static char *bindto_name
;
62 static int add_connection(struct service
*service
, struct command_context
*cmd_ctx
)
64 socklen_t address_size
;
65 struct connection
*c
, **p
;
69 c
= malloc(sizeof(struct connection
));
72 memset(&c
->sin
, 0, sizeof(c
->sin
));
73 c
->cmd_ctx
= copy_command_context(cmd_ctx
);
79 if (service
->type
== CONNECTION_TCP
) {
80 address_size
= sizeof(c
->sin
);
82 c
->fd
= accept(service
->fd
, (struct sockaddr
*)&service
->sin
, &address_size
);
85 /* This increases performance dramatically for e.g. GDB load which
86 * does not have a sliding window protocol.
88 * Ignore errors from this fn as it probably just means less performance
90 setsockopt(c
->fd
, /* socket affected */
91 IPPROTO_TCP
, /* set option at TCP level */
92 TCP_NODELAY
, /* name of option */
93 (char *)&flag
, /* the cast is historical cruft */
94 sizeof(int)); /* length of option value */
96 LOG_INFO("accepting '%s' connection on tcp/%s", service
->name
, service
->port
);
97 retval
= service
->new_connection(c
);
98 if (retval
!= ERROR_OK
) {
100 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
101 command_done(c
->cmd_ctx
);
105 } else if (service
->type
== CONNECTION_STDINOUT
) {
107 c
->fd_out
= fileno(stdout
);
110 /* we are using stdin/out so ignore ctrl-c under windoze */
111 SetConsoleCtrlHandler(NULL
, TRUE
);
114 /* do not check for new connections again on stdin */
117 LOG_INFO("accepting '%s' connection from pipe", service
->name
);
118 retval
= service
->new_connection(c
);
119 if (retval
!= ERROR_OK
) {
120 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
121 command_done(c
->cmd_ctx
);
125 } else if (service
->type
== CONNECTION_PIPE
) {
127 /* do not check for new connections again on stdin */
130 char *out_file
= alloc_printf("%so", service
->port
);
131 c
->fd_out
= open(out_file
, O_WRONLY
);
133 if (c
->fd_out
== -1) {
134 LOG_ERROR("could not open %s", service
->port
);
135 command_done(c
->cmd_ctx
);
140 LOG_INFO("accepting '%s' connection from pipe %s", service
->name
, service
->port
);
141 retval
= service
->new_connection(c
);
142 if (retval
!= ERROR_OK
) {
143 LOG_ERROR("attempted '%s' connection rejected", service
->name
);
144 command_done(c
->cmd_ctx
);
150 /* add to the end of linked list */
151 for (p
= &service
->connections
; *p
; p
= &(*p
)->next
)
155 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
156 service
->max_connections
--;
161 static int remove_connection(struct service
*service
, struct connection
*connection
)
163 struct connection
**p
= &service
->connections
;
164 struct connection
*c
;
166 /* find connection */
168 if (c
->fd
== connection
->fd
) {
169 service
->connection_closed(c
);
170 if (service
->type
== CONNECTION_TCP
)
172 else if (service
->type
== CONNECTION_PIPE
) {
173 /* The service will listen to the pipe again */
174 c
->service
->fd
= c
->fd
;
177 command_done(c
->cmd_ctx
);
179 /* delete connection */
183 if (service
->max_connections
!= CONNECTION_LIMIT_UNLIMITED
)
184 service
->max_connections
++;
189 /* redirect p to next list pointer */
196 static void free_service(struct service
*c
)
203 int add_service(char *name
,
206 new_connection_handler_t new_connection_handler
,
207 input_handler_t input_handler
,
208 connection_closed_handler_t connection_closed_handler
,
211 struct service
*c
, **p
;
213 int so_reuseaddr_option
= 1;
215 c
= malloc(sizeof(struct service
));
217 c
->name
= strdup(name
);
218 c
->port
= strdup(port
);
219 c
->max_connections
= 1; /* Only TCP/IP ports can support more than one connection */
221 c
->connections
= NULL
;
222 c
->new_connection
= new_connection_handler
;
223 c
->input
= input_handler
;
224 c
->connection_closed
= connection_closed_handler
;
228 if (strcmp(c
->port
, "pipe") == 0)
229 c
->type
= CONNECTION_STDINOUT
;
232 portnumber
= strtol(c
->port
, &end
, 0);
233 if (!*end
&& (parse_long(c
->port
, &portnumber
) == ERROR_OK
)) {
234 c
->portnumber
= portnumber
;
235 c
->type
= CONNECTION_TCP
;
237 c
->type
= CONNECTION_PIPE
;
240 if (c
->type
== CONNECTION_TCP
) {
241 c
->max_connections
= max_connections
;
243 c
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
245 LOG_ERROR("error creating socket: %s", strerror(errno
));
253 (void *)&so_reuseaddr_option
,
256 socket_nonblock(c
->fd
);
258 memset(&c
->sin
, 0, sizeof(c
->sin
));
259 c
->sin
.sin_family
= AF_INET
;
261 if (bindto_name
== NULL
)
262 c
->sin
.sin_addr
.s_addr
= INADDR_ANY
;
264 hp
= gethostbyname(bindto_name
);
266 LOG_ERROR("couldn't resolve bindto address: %s", bindto_name
);
271 memcpy(&c
->sin
.sin_addr
, hp
->h_addr_list
[0], hp
->h_length
);
273 c
->sin
.sin_port
= htons(c
->portnumber
);
275 if (bind(c
->fd
, (struct sockaddr
*)&c
->sin
, sizeof(c
->sin
)) == -1) {
276 LOG_ERROR("couldn't bind %s to socket on port %d: %s", name
, c
->portnumber
, strerror(errno
));
284 setsockopt(c
->fd
, IPPROTO_TCP
, TCP_MAXSEG
, &segsize
, sizeof(int));
286 int window_size
= 128 * 1024;
288 /* These setsockopt()s must happen before the listen() */
290 setsockopt(c
->fd
, SOL_SOCKET
, SO_SNDBUF
,
291 (char *)&window_size
, sizeof(window_size
));
292 setsockopt(c
->fd
, SOL_SOCKET
, SO_RCVBUF
,
293 (char *)&window_size
, sizeof(window_size
));
295 if (listen(c
->fd
, 1) == -1) {
296 LOG_ERROR("couldn't listen on socket: %s", strerror(errno
));
302 struct sockaddr_in addr_in
;
303 addr_in
.sin_port
= 0;
304 socklen_t addr_in_size
= sizeof(addr_in
);
305 if (getsockname(c
->fd
, (struct sockaddr
*)&addr_in
, &addr_in_size
) == 0)
306 LOG_INFO("Listening on port %hu for %s connections",
307 ntohs(addr_in
.sin_port
), name
);
308 } else if (c
->type
== CONNECTION_STDINOUT
) {
309 c
->fd
= fileno(stdin
);
312 /* for win32 set stdin/stdout to binary mode */
313 if (_setmode(_fileno(stdout
), _O_BINARY
) < 0)
314 LOG_WARNING("cannot change stdout mode to binary");
315 if (_setmode(_fileno(stdin
), _O_BINARY
) < 0)
316 LOG_WARNING("cannot change stdin mode to binary");
317 if (_setmode(_fileno(stderr
), _O_BINARY
) < 0)
318 LOG_WARNING("cannot change stderr mode to binary");
320 socket_nonblock(c
->fd
);
322 } else if (c
->type
== CONNECTION_PIPE
) {
324 /* we currenty do not support named pipes under win32
325 * so exit openocd for now */
326 LOG_ERROR("Named pipes currently not supported under this os");
330 /* Pipe we're reading from */
331 c
->fd
= open(c
->port
, O_RDONLY
| O_NONBLOCK
);
333 LOG_ERROR("could not open %s", c
->port
);
340 /* add to the end of linked list */
341 for (p
= &services
; *p
; p
= &(*p
)->next
)
348 static void remove_connections(struct service
*service
)
350 struct connection
*connection
;
352 connection
= service
->connections
;
355 struct connection
*tmp
;
357 tmp
= connection
->next
;
358 remove_connection(service
, connection
);
363 static int remove_services(void)
365 struct service
*c
= services
;
369 struct service
*next
= c
->next
;
371 remove_connections(c
);
376 if (c
->type
== CONNECTION_PIPE
) {
389 /* remember the last service for unlinking */
398 int server_loop(struct command_context
*command_context
)
400 struct service
*service
;
404 /* used in select() */
408 /* used in accept() */
412 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
413 LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
416 while (!shutdown_openocd
) {
417 /* monitor sockets for activity */
421 /* add service and connection fds to read_fds */
422 for (service
= services
; service
; service
= service
->next
) {
423 if (service
->fd
!= -1) {
424 /* listen for new connections */
425 FD_SET(service
->fd
, &read_fds
);
427 if (service
->fd
> fd_max
)
428 fd_max
= service
->fd
;
431 if (service
->connections
) {
432 struct connection
*c
;
434 for (c
= service
->connections
; c
; c
= c
->next
) {
435 /* check for activity on the connection */
436 FD_SET(c
->fd
, &read_fds
);
446 /* we're just polling this iteration, this is faster on embedded
449 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
451 /* Every 100ms, can be changed with "poll_period" command */
452 tv
.tv_usec
= polling_period
* 1000;
453 /* Only while we're sleeping we'll let others run */
454 openocd_sleep_prelude();
456 retval
= socket_select(fd_max
+ 1, &read_fds
, NULL
, NULL
, &tv
);
457 openocd_sleep_postlude();
463 errno
= WSAGetLastError();
465 if (errno
== WSAEINTR
)
468 LOG_ERROR("error during select: %s", strerror(errno
));
476 LOG_ERROR("error during select: %s", strerror(errno
));
483 /* We only execute these callbacks when there was nothing to do or we timed
485 target_call_timer_callbacks();
486 process_jim_events(command_context
);
488 FD_ZERO(&read_fds
); /* eCos leaves read_fds unchanged in this case! */
490 /* We timed out/there was nothing to do, timeout rather than poll next time
494 /* There was something to do, next time we'll just poll */
498 /* This is a simple back-off algorithm where we immediately
499 * re-poll if we did something this time around.
501 * This greatly improves performance of DCC.
503 poll_ok
= poll_ok
|| target_got_message();
505 for (service
= services
; service
; service
= service
->next
) {
506 /* handle new connections on listeners */
507 if ((service
->fd
!= -1)
508 && (FD_ISSET(service
->fd
, &read_fds
))) {
509 if (service
->max_connections
!= 0)
510 add_connection(service
, command_context
);
512 if (service
->type
== CONNECTION_TCP
) {
513 struct sockaddr_in sin
;
514 socklen_t address_size
= sizeof(sin
);
516 tmp_fd
= accept(service
->fd
,
517 (struct sockaddr
*)&service
->sin
,
519 close_socket(tmp_fd
);
522 "rejected '%s' connection, no more connections allowed",
527 /* handle activity on connections */
528 if (service
->connections
) {
529 struct connection
*c
;
531 for (c
= service
->connections
; c
; ) {
532 if ((FD_ISSET(c
->fd
, &read_fds
)) || c
->input_pending
) {
533 retval
= service
->input(c
);
534 if (retval
!= ERROR_OK
) {
535 struct connection
*next
= c
->next
;
536 if (service
->type
== CONNECTION_PIPE
||
537 service
->type
== CONNECTION_STDINOUT
) {
538 /* if connection uses a pipe then
539 * shutdown openocd on error */
540 shutdown_openocd
= 1;
542 remove_connection(service
, c
);
543 LOG_INFO("dropped '%s' connection",
556 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
557 if (msg
.message
== WM_QUIT
)
558 shutdown_openocd
= 1;
563 return shutdown_openocd
!= 2 ? ERROR_OK
: ERROR_FAIL
;
567 BOOL WINAPI
ControlHandler(DWORD dwCtrlType
)
569 shutdown_openocd
= 1;
574 void sig_handler(int sig
)
576 /* store only first signal that hits us */
579 shutdown_openocd
= 1;
582 int server_preinit(void)
584 /* this currently only calls WSAStartup on native win32 systems
585 * before any socket operations are performed.
586 * This is an issue if you call init in your config script */
589 WORD wVersionRequested
;
592 wVersionRequested
= MAKEWORD(2, 2);
594 if (WSAStartup(wVersionRequested
, &wsaData
) != 0) {
595 LOG_ERROR("Failed to Open Winsock");
599 /* register ctrl-c handler */
600 SetConsoleCtrlHandler(ControlHandler
, TRUE
);
602 signal(SIGBREAK
, sig_handler
);
604 signal(SIGINT
, sig_handler
);
605 signal(SIGTERM
, sig_handler
);
606 signal(SIGABRT
, sig_handler
);
611 int server_init(struct command_context
*cmd_ctx
)
613 int ret
= tcl_init();
618 ret
= telnet_init("Open On-Chip Debugger");
620 if (ret
!= ERROR_OK
) {
628 int server_quit(void)
635 SetConsoleCtrlHandler(ControlHandler
, FALSE
);
640 /* return signal number so we can kill ourselves */
644 void server_free(void)
647 telnet_service_free();
651 void exit_on_signal(int sig
)
654 /* bring back default system handler and kill yourself */
655 signal(sig
, SIG_DFL
);
660 int connection_write(struct connection
*connection
, const void *data
, int len
)
663 /* successful no-op. Sockets and pipes behave differently here... */
666 if (connection
->service
->type
== CONNECTION_TCP
)
667 return write_socket(connection
->fd_out
, data
, len
);
669 return write(connection
->fd_out
, data
, len
);
672 int connection_read(struct connection
*connection
, void *data
, int len
)
674 if (connection
->service
->type
== CONNECTION_TCP
)
675 return read_socket(connection
->fd
, data
, len
);
677 return read(connection
->fd
, data
, len
);
680 /* tell the server we want to shut down */
681 COMMAND_HANDLER(handle_shutdown_command
)
683 LOG_USER("shutdown command invoked");
685 shutdown_openocd
= 1;
688 if (!strcmp(CMD_ARGV
[0], "error")) {
689 shutdown_openocd
= 2;
694 return ERROR_COMMAND_CLOSE_CONNECTION
;
697 COMMAND_HANDLER(handle_poll_period_command
)
700 LOG_WARNING("You need to set a period value");
702 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], polling_period
);
704 LOG_INFO("set servers polling period to %ums", polling_period
);
709 COMMAND_HANDLER(handle_bindto_command
)
713 command_print(CMD_CTX
, "bindto name: %s", bindto_name
);
717 bindto_name
= strdup(CMD_ARGV
[0]);
720 return ERROR_COMMAND_SYNTAX_ERROR
;
725 static const struct command_registration server_command_handlers
[] = {
728 .handler
= &handle_shutdown_command
,
731 .help
= "shut the server down",
734 .name
= "poll_period",
735 .handler
= &handle_poll_period_command
,
738 .help
= "set the servers polling period",
742 .handler
= &handle_bindto_command
,
745 .help
= "Specify address by name on which to listen for "
746 "incoming TCP/IP connections",
748 COMMAND_REGISTRATION_DONE
751 int server_register_commands(struct command_context
*cmd_ctx
)
753 int retval
= telnet_register_commands(cmd_ctx
);
754 if (ERROR_OK
!= retval
)
757 retval
= tcl_register_commands(cmd_ctx
);
758 if (ERROR_OK
!= retval
)
761 retval
= jsp_register_commands(cmd_ctx
);
762 if (ERROR_OK
!= retval
)
765 return register_commands(cmd_ctx
, NULL
, server_command_handlers
);
768 COMMAND_HELPER(server_port_command
, unsigned short *out
)
772 command_print(CMD_CTX
, "%d", *out
);
777 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], port
);
782 return ERROR_COMMAND_SYNTAX_ERROR
;
787 COMMAND_HELPER(server_pipe_command
, char **out
)
791 command_print(CMD_CTX
, "%s", *out
);
795 if (CMD_CTX
->mode
== COMMAND_EXEC
) {
796 LOG_WARNING("unable to change server port after init");
797 return ERROR_COMMAND_ARGUMENT_INVALID
;
800 *out
= strdup(CMD_ARGV
[0]);
804 return ERROR_COMMAND_SYNTAX_ERROR
;
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)