35 #include <netinet/tcp.h>
59 socklen_t address_size;
67 memset(&c->
sin, 0,
sizeof(c->
sin));
75 address_size =
sizeof(c->
sin);
102 c->
fd_out = fileno(stdout);
106 SetConsoleCtrlHandler(
NULL, TRUE);
126 c->
fd_out = open(out_file, O_WRONLY);
199 int max_connections,
void *
priv)
203 int so_reuseaddr_option = 1;
205 c = malloc(
sizeof(
struct service));
208 c->
port = strdup(port);
220 if (strcmp(c->
port,
"pipe") == 0)
224 portnumber = strtol(c->
port, &end, 0);
235 c->
fd = socket(AF_INET, SOCK_STREAM, 0);
237 LOG_ERROR(
"error creating socket: %s", strerror(errno));
245 (
void *)&so_reuseaddr_option,
250 memset(&c->
sin, 0,
sizeof(c->
sin));
251 c->
sin.sin_family = AF_INET;
254 c->
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
263 memcpy(&c->
sin.sin_addr, hp->h_addr_list[0], hp->h_length);
267 if (bind(c->
fd, (
struct sockaddr *)&c->
sin,
sizeof(c->
sin)) == -1) {
276 setsockopt(c->
fd, IPPROTO_TCP, TCP_MAXSEG, &segsize,
sizeof(
int));
278 int window_size = 128 * 1024;
282 setsockopt(c->
fd, SOL_SOCKET, SO_SNDBUF,
283 (
char *)&window_size,
sizeof(window_size));
284 setsockopt(c->
fd, SOL_SOCKET, SO_RCVBUF,
285 (
char *)&window_size,
sizeof(window_size));
287 if (listen(c->
fd, 1) == -1) {
288 LOG_ERROR(
"couldn't listen on socket: %s", strerror(errno));
294 struct sockaddr_in addr_in;
295 addr_in.sin_port = 0;
296 socklen_t addr_in_size =
sizeof(addr_in);
297 if (getsockname(c->
fd, (
struct sockaddr *)&addr_in, &addr_in_size) == 0)
298 LOG_INFO(
"Listening on port %hu for %s connections",
299 ntohs(addr_in.sin_port), c->
name);
301 c->
fd = fileno(stdin);
305 if (_setmode(_fileno(stdout), _O_BINARY) < 0)
306 LOG_WARNING(
"cannot change stdout mode to binary");
307 if (_setmode(_fileno(stdin), _O_BINARY) < 0)
309 if (_setmode(_fileno(stderr), _O_BINARY) < 0)
310 LOG_WARNING(
"cannot change stderr mode to binary");
318 LOG_ERROR(
"Named pipes currently not supported under this os");
323 c->
fd = open(c->
port, O_RDONLY | O_NONBLOCK);
362 for (tmp =
services; tmp; prev = tmp, tmp = tmp->
next) {
417 if (s->keep_client_alive)
419 s->keep_client_alive(c);
438 if (
signal(SIGPIPE, SIG_IGN) == SIG_ERR)
439 LOG_ERROR(
"couldn't set SIGPIPE to SIG_IGN");
462 FD_SET(c->
fd, &read_fds);
483 tv.
tv_usec = timeout_ms * 1000;
491 errno = WSAGetLastError();
493 if (errno == WSAEINTR)
496 LOG_ERROR(
"error during select: %s", strerror(errno));
504 LOG_ERROR(
"error during select: %s", strerror(errno));
545 struct sockaddr_in sin;
546 socklen_t address_size =
sizeof(sin);
554 "rejected '%s' connection, no more connections allowed",
588 while (PeekMessage(&msg,
NULL, 0, 0, PM_REMOVE)) {
589 if (msg.message == WM_QUIT)
608 LOG_DEBUG(
"Terminating on Signal %d", sig);
610 LOG_DEBUG(
"Ignored extra Signal %d", sig);
615 BOOL WINAPI control_handler(
DWORD ctrl_type)
625 if (tcgetpgrp(STDIN_FILENO) > 0)
640 WORD version_requested;
643 version_requested = MAKEWORD(2, 2);
645 if (WSAStartup(version_requested, &wsadata) != 0) {
665 SetConsoleCtrlHandler(control_handler, TRUE);
704 SetConsoleCtrlHandler(control_handler, FALSE);
760 LOG_USER(
"shutdown command invoked");
767 if (!strcmp(
CMD_ARGV[0],
"error")) {
807 .handler = &handle_shutdown_command,
810 .help =
"shut the server down",
813 .name =
"poll_period",
814 .handler = &handle_poll_period_command,
817 .help =
"set the servers polling period",
821 .handler = &handle_bindto_command,
824 .help =
"Specify address by name on which to listen for "
825 "incoming TCP/IP connections",
875 LOG_WARNING(
"unable to change server port after init");
void command_done(struct command_context *cmd_ctx)
Frees the resources associated with a command context.
void command_print(struct command_invocation *cmd, const char *format,...)
void process_jim_events(struct command_context *cmd_ctx)
struct command_context * copy_command_context(struct command_context *context)
Creates a copy of an existing command context.
int command_run_line(struct command_context *context, char *line)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define ERROR_COMMAND_CLOSE_CONNECTION
int parse_long(const char *str, long *ul)
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_COMMAND_ARGUMENT_INVALID
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
static struct esp_usb_jtag * priv
int ipdbg_server_free(void)
void jsp_service_free(void)
int jsp_register_commands(struct command_context *cmd_ctx)
char * alloc_printf(const char *format,...)
#define LOG_USER(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static int socket_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
static int read_socket(int handle, void *buffer, unsigned int count)
static int close_socket(int sock)
static int write_socket(int handle, const void *buffer, unsigned int count)
static void socket_nonblock(int fd)
void exit_on_signal(int sig)
int connection_write(struct connection *connection, const void *data, int len)
static void sig_handler(int sig)
int connection_read(struct connection *connection, void *data, int len)
static void free_service(struct service *c)
int server_host_os_close(void)
static char * bindto_name
static const struct command_registration server_command_handlers[]
int server_host_os_entry(void)
COMMAND_HANDLER(handle_shutdown_command)
static void remove_connections(struct service *service)
static enum shutdown_reason shutdown_openocd
static int remove_services(void)
static int remove_connection(struct service *service, struct connection *connection)
void server_keep_clients_alive(void)
@ SHUTDOWN_WITH_ERROR_CODE
@ SHUTDOWN_WITH_SIGNAL_CODE
bool openocd_is_shutdown_pending(void)
int server_loop(struct command_context *command_context)
int remove_service(const char *name, const char *port)
static int add_connection(struct service *service, struct command_context *cmd_ctx)
int server_register_commands(struct command_context *cmd_ctx)
static struct service * services
COMMAND_HELPER(server_port_command, unsigned short *out)
int add_service(const struct service_driver *driver, const char *port, int max_connections, void *priv)
int server_init(struct command_context *cmd_ctx)
static void sigkey_handler(int sig)
static int polling_period
#define CONNECTION_LIMIT_UNLIMITED
struct command_context * cmd_ctx
const char * name
the name of the server
int(* connection_closed_handler)(struct connection *connection)
callback to tear down the connection
void(* keep_client_alive_handler)(struct connection *connection)
called periodically to send keep-alive messages on the connection
int(* new_connection_handler)(struct connection *connection)
complete code to accept a new connection.
int(* new_connection_during_keep_alive_handler)(struct connection *connection)
optional minimal setup to accept a connection during keep-alive
int(* input_handler)(struct connection *connection)
callback to handle incoming data
int(* new_connection_during_keep_alive)(struct connection *connection)
struct connection * connections
int(* input)(struct connection *connection)
int(* connection_closed)(struct connection *connection)
unsigned short portnumber
void(* keep_client_alive)(struct connection *connection)
enum connection_type type
int(* new_connection)(struct connection *connection)
int target_call_timer_callbacks(void)
int64_t target_timer_next_event(void)
Returns when the next registered event will take place.
void target_quit(void)
Free all the resources allocated by targets and the target layer.
bool target_got_message(void)
Read and clear the flag as to whether we got a message.
int tcl_register_commands(struct command_context *cmd_ctx)
void tcl_service_free(void)
void telnet_service_free(void)
int telnet_register_commands(struct command_context *cmd_ctx)
int telnet_init(char *banner)