This is an automated email from Gerrit. "Antonio Borneo <borneo.anto...@gmail.com>" just uploaded a new patch set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/6842
-- gerrit commit 8d0998995fb99a76fd36f315d68bce33fb7d10ce Author: Antonio Borneo <borneo.anto...@gmail.com> Date: Tue Feb 1 18:14:15 2022 +0100 server: rework add_connection() and reject connection() Split alloc_new_connection() from add_connection(). Split reject_connection() from server_loop(). No functional change. Change-Id: I25b2172ac26651078c91e831677565773da7a47c Signed-off-by: Antonio Borneo <borneo.anto...@gmail.com> diff --git a/src/server/server.c b/src/server/server.c index 542662f83..59ead6d5e 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -64,14 +64,32 @@ static int polling_period = 100; /* address by name on which to listen for incoming TCP/IP connections */ static char *bindto_name; -static int add_connection(struct service *service, struct command_context *cmd_ctx) +static int remove_connection(struct service *service, struct connection *connection); + +static int reject_connection(struct service *service) +{ + if (service->type == CONNECTION_TCP) { + struct sockaddr_in sin; + socklen_t address_size = sizeof(sin); + int fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size); + close_socket(fd); + } + LOG_INFO("rejected '%s' connection, no more connections allowed", service->name); + return ERROR_OK; +} + +static struct connection *alloc_new_connection(struct service *service, + struct command_context *cmd_ctx) { socklen_t address_size; struct connection *c, **p; - int retval; int flag = 1; c = malloc(sizeof(struct connection)); + if (!c) { + LOG_ERROR("Out of memory"); + return NULL; + } c->fd = -1; c->fd_out = -1; memset(&c->sin, 0, sizeof(c->sin)); @@ -100,14 +118,6 @@ static int add_connection(struct service *service, struct command_context *cmd_c sizeof(int)); /* length of option value */ LOG_INFO("accepting '%s' connection on tcp/%s", service->name, service->port); - retval = service->new_connection(c); - if (retval != ERROR_OK) { - close_socket(c->fd); - LOG_ERROR("attempted '%s' connection rejected", service->name); - command_done(c->cmd_ctx); - free(c); - return retval; - } } else if (service->type == CONNECTION_STDINOUT) { c->fd = service->fd; c->fd_out = fileno(stdout); @@ -121,13 +131,6 @@ static int add_connection(struct service *service, struct command_context *cmd_c service->fd = -1; LOG_INFO("accepting '%s' connection from pipe", service->name); - retval = service->new_connection(c); - if (retval != ERROR_OK) { - LOG_ERROR("attempted '%s' connection rejected", service->name); - command_done(c->cmd_ctx); - free(c); - return retval; - } } else if (service->type == CONNECTION_PIPE) { c->fd = service->fd; /* do not check for new connections again on stdin */ @@ -140,17 +143,10 @@ static int add_connection(struct service *service, struct command_context *cmd_c LOG_ERROR("could not open %s", service->port); command_done(c->cmd_ctx); free(c); - return ERROR_FAIL; + return NULL; } LOG_INFO("accepting '%s' connection from pipe %s", service->name, service->port); - retval = service->new_connection(c); - if (retval != ERROR_OK) { - LOG_ERROR("attempted '%s' connection rejected", service->name); - command_done(c->cmd_ctx); - free(c); - return retval; - } } /* add to the end of linked list */ @@ -161,6 +157,22 @@ static int add_connection(struct service *service, struct command_context *cmd_c if (service->max_connections != CONNECTION_LIMIT_UNLIMITED) service->max_connections--; + return c; +} + +static int add_connection(struct service *service, struct command_context *cmd_ctx) +{ + struct connection *c = alloc_new_connection(service, cmd_ctx); + if (!c) + return ERROR_FAIL; + + int retval = service->new_connection(c); + if (retval != ERROR_OK) { + LOG_ERROR("attempted '%s' connection rejected", service->name); + remove_connection(service, c); + return retval; + } + return ERROR_OK; } @@ -563,20 +575,8 @@ int server_loop(struct command_context *command_context) && (FD_ISSET(service->fd, &read_fds))) { if (service->max_connections != 0) add_connection(service, command_context); - else { - if (service->type == CONNECTION_TCP) { - struct sockaddr_in sin; - socklen_t address_size = sizeof(sin); - int tmp_fd; - tmp_fd = accept(service->fd, - (struct sockaddr *)&service->sin, - &address_size); - close_socket(tmp_fd); - } - LOG_INFO( - "rejected '%s' connection, no more connections allowed", - service->name); - } + else + reject_connection(service); } /* handle activity on connections */ --