From 6c137a2fc0bf53b9c0b8eda51e6f5361552b0112 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=98yvind=20Harboe?= Date: Mon, 27 Sep 2010 08:48:31 +0200 Subject: [PATCH] server: specify port as a string MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This will allow switching to using named pipes. Split this out as a seperate commit to make changes easier to follow. Signed-off-by: Øyvind Harboe --- src/server/gdb_server.c | 57 +++++++++++++++++++++----------------- src/server/server.c | 49 ++++++++++++++++++++++++++++++++ src/server/server.h | 9 ++++++ src/server/tcl_server.c | 14 ++++------ src/server/telnet_server.c | 16 +++++------ 5 files changed, 104 insertions(+), 41 deletions(-) diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c index 170dadc55f..54899589dc 100644 --- a/src/server/gdb_server.c +++ b/src/server/gdb_server.c @@ -80,8 +80,8 @@ static int gdb_breakpoint_override; static enum breakpoint_type gdb_breakpoint_override_type; static int gdb_error(struct connection *connection, int retval); -static unsigned short gdb_port = 3333; -static unsigned short gdb_port_next = 0; +static const char *gdb_port; +static const char *gdb_port_next; static const char DIGITS[16] = "0123456789abcdef"; static void gdb_log_callback(void *priv, const char *file, unsigned line, @@ -2410,32 +2410,37 @@ static int gdb_target_start(struct target *target, uint16_t port) static int gdb_target_add_one(struct target *target) { - if (gdb_port == 0 && server_use_pipes == 0) - { - LOG_INFO("gdb port disabled"); - return ERROR_OK; - } - if (0 == gdb_port_next) - gdb_port_next = gdb_port; - - bool use_pipes = server_use_pipes; - static bool server_started_with_pipes = false; - if (server_started_with_pipes) + long portnumber_parsed; + /* If we can parse the port number + * then we increment the port number for the next target. + */ + char *end_parse; + portnumber_parsed = strtol(gdb_port_next, &end_parse, 0); + if (!*end_parse) { - LOG_WARNING("gdb service permits one target when using pipes"); - if (0 == gdb_port) - return ERROR_OK; - - use_pipes = false; + LOG_ERROR("Illegal port number"); + return ERROR_FAIL; } - int e = gdb_target_start(target, use_pipes ? 0 : gdb_port_next); - if (ERROR_OK == e) + int retval = gdb_target_start(target, portnumber_parsed); + if (retval == ERROR_OK) { - server_started_with_pipes |= use_pipes; - gdb_port_next++; + long portnumber; + /* If we can parse the port number + * then we increment the port number for the next target. + */ + char *end; + strtol(gdb_port_next, &end, 0); + if (!*end) + { + if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) + { + free((void *)gdb_port_next); + gdb_port_next = alloc_printf("%d", portnumber+1); + } + } } - return e; + return retval; } int gdb_target_add_all(struct target *target) @@ -2480,9 +2485,9 @@ COMMAND_HANDLER(handle_gdb_sync_command) /* daemon configuration command gdb_port */ COMMAND_HANDLER(handle_gdb_port_command) { - int retval = CALL_COMMAND_HANDLER(server_port_command, &gdb_port); + int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port); if (ERROR_OK == retval) - gdb_port_next = gdb_port; + gdb_port_next = strdup(gdb_port); return retval; } @@ -2599,5 +2604,7 @@ static const struct command_registration gdb_command_handlers[] = { int gdb_register_commands(struct command_context *cmd_ctx) { + gdb_port = strdup("3333"); + gdb_port_next = strdup("3333"); return register_commands(cmd_ctx, NULL, gdb_command_handlers); } diff --git a/src/server/server.c b/src/server/server.c index 1c556638af..435ddbb7d4 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -238,6 +238,29 @@ int add_service(char *name, enum connection_type type, unsigned short port, int return ERROR_OK; } +int add_service_pipe(char *name, const char *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) +{ + enum connection_type type = CONNECTION_TCP; + long portnumber; + char *end; + strtol(port, &end, 0); + if (!*end) + { + if ((parse_long(port, &portnumber) == ERROR_OK) && (portnumber == 0)) + { + type = CONNECTION_PIPE; + } + } else + { + LOG_ERROR("Illegal port number %s", port); + return ERROR_FAIL; + } + return add_service(name, type, portnumber, max_connections, new_connection_handler, + input_handler, connection_closed_handler, priv); +} + static int remove_services(void) { struct service *c = services; @@ -250,6 +273,12 @@ static int remove_services(void) if (c->name) free(c->name); + if (c->type == CONNECTION_PIPE) + { + if (c->fd != -1) + close(c->fd); + } + if (c->priv) free(c->priv); @@ -591,3 +620,23 @@ SERVER_PORT_COMMAND() } return ERROR_OK; } + +SERVER_PIPE_COMMAND() +{ + switch (CMD_ARGC) { + case 0: + command_print(CMD_CTX, "%s", *out); + break; + case 1: + { + const char * t = strdup(CMD_ARGV[0]); + free((void *)*out); + *out = t; + break; + } + default: + return ERROR_INVALID_ARGUMENTS; + } + return ERROR_OK; +} + diff --git a/src/server/server.h b/src/server/server.h index 46188bb44e..2c9ed44570 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -75,6 +75,11 @@ int add_service(char *name, enum connection_type type, unsigned short port, input_handler_t in_handler, connection_closed_handler_t close_handler, void *priv); +int add_service_pipe(char *name, const char *port, + int max_connections, new_connection_handler_t new_connection_handler, + input_handler_t in_handler, connection_closed_handler_t close_handler, + void *priv); + int server_preinit(void); int server_init(struct command_context *cmd_ctx); int server_quit(void); @@ -101,6 +106,10 @@ void openocd_sleep_postlude(void); * Call server_port like a normal COMMAND_HANDLER with an extra @a out parameter * to receive the specified port number. */ +#define SERVER_PIPE_COMMAND() \ + COMMAND_HELPER(server_pipe_command, const char **out) +SERVER_PIPE_COMMAND(); + #define SERVER_PORT_COMMAND() \ COMMAND_HELPER(server_port_command, unsigned short *out) diff --git a/src/server/tcl_server.c b/src/server/tcl_server.c index 7d84de7320..f82cafefd5 100644 --- a/src/server/tcl_server.c +++ b/src/server/tcl_server.c @@ -35,7 +35,7 @@ struct tcl_connection { int tc_outerror; /* flag an output error */ }; -static unsigned short tcl_port = 6666; +static const char *tcl_port; /* handlers */ static int tcl_new_connection(struct connection *connection); @@ -160,23 +160,20 @@ static int tcl_closed(struct connection *connection) int tcl_init(void) { - int retval; - - if (tcl_port == 0) + if (strcmp(tcl_port, "disabled") == 0) { - LOG_INFO("tcl port disabled"); + LOG_INFO("tcl server disabled"); return ERROR_OK; } - retval = add_service("tcl", CONNECTION_TCP, tcl_port, 1, + return add_service_pipe("tcl", tcl_port, 1, &tcl_new_connection, &tcl_input, &tcl_closed, NULL); - return retval; } COMMAND_HANDLER(handle_tcl_port_command) { - return CALL_COMMAND_HANDLER(server_port_command, &tcl_port); + return CALL_COMMAND_HANDLER(server_pipe_command, &tcl_port); } static const struct command_registration tcl_command_handlers[] = { @@ -194,5 +191,6 @@ static const struct command_registration tcl_command_handlers[] = { int tcl_register_commands(struct command_context *cmd_ctx) { + tcl_port = strdup("6666"); return register_commands(cmd_ctx, NULL, tcl_command_handlers); } diff --git a/src/server/telnet_server.c b/src/server/telnet_server.c index ee8d3b16a9..420f5d70a0 100644 --- a/src/server/telnet_server.c +++ b/src/server/telnet_server.c @@ -30,7 +30,7 @@ #include "telnet_server.h" #include -static unsigned short telnet_port = 4444; +static const char *telnet_port; static char *negotiate = "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */ @@ -582,18 +582,17 @@ static int telnet_connection_closed(struct connection *connection) int telnet_init(char *banner) { - struct telnet_service *telnet_service = malloc(sizeof(struct telnet_service)); - - if (telnet_port == 0) + if (strcmp(telnet_port, "disabled") == 0) { - LOG_INFO("telnet port disabled"); - free(telnet_service); + LOG_INFO("telnet server disabled"); return ERROR_OK; } + struct telnet_service *telnet_service = malloc(sizeof(struct telnet_service)); + telnet_service->banner = banner; - add_service("telnet", CONNECTION_TCP, telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service); + add_service_pipe("telnet", telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service); return ERROR_OK; } @@ -601,7 +600,7 @@ int telnet_init(char *banner) /* daemon configuration command telnet_port */ COMMAND_HANDLER(handle_telnet_port_command) { - return CALL_COMMAND_HANDLER(server_port_command, &telnet_port); + return CALL_COMMAND_HANDLER(server_pipe_command, &telnet_port); } COMMAND_HANDLER(handle_exit_command) @@ -630,5 +629,6 @@ static const struct command_registration telnet_command_handlers[] = { int telnet_register_commands(struct command_context *cmd_ctx) { + telnet_port = strdup("4444"); return register_commands(cmd_ctx, NULL, telnet_command_handlers); } -- 2.30.2