From 20301007a15deede9995da5e6cab3d18ecccedf8 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Fri, 21 Jan 2022 14:29:32 +0100 Subject: #915 release existing client on reconnections Trasient network problems might result in duplicated clients, drop client object if it already exists before creating a new one. --- src/core.c | 19 +++++++++++++++---- src/rest.c | 24 ++++++++++++++++-------- src/rest.h | 1 + 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/core.c b/src/core.c index b15efc9..0ff02d4 100644 --- a/src/core.c +++ b/src/core.c @@ -323,10 +323,16 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) struct og_client_wol *cli_wol; struct og_client *cli; int client_sd; + bool agent; if (events & EV_ERROR) return; + if (io->fd == socket_agent_rest) + agent = true; + else + agent = false; + client_sd = accept(io->fd, (struct sockaddr *)&client_addr, &addrlen); if (client_sd < 0) { syslog(LOG_ERR, "cannot accept client connection\n"); @@ -342,6 +348,12 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) if (cli_wol) og_client_wol_destroy(cli_wol); + if (agent) { + cli = __og_client_find(&client_addr.sin_addr); + if (cli) + og_client_release(loop, cli); + } + cli = (struct og_client *)calloc(1, sizeof(struct og_client)); if (!cli) { close(client_sd); @@ -349,7 +361,7 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) } memcpy(&cli->addr, &client_addr, sizeof(client_addr)); - if (io->fd == socket_agent_rest) { + if (agent) { cli->agent = true; ev_io_init(&cli->io, og_agent_read_cb, client_sd, EV_READ); } else { @@ -358,7 +370,7 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) ev_io_start(loop, &cli->io); ev_init(&cli->timer, og_client_timer_cb); - if (io->fd == socket_agent_rest) + if (agent) cli->timer.repeat = OG_AGENT_CLIENT_TIMEOUT; else cli->timer.repeat = OG_CLIENT_TIMEOUT; @@ -366,9 +378,8 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events) ev_timer_again(loop, &cli->timer); og_client_add(cli); - if (io->fd == socket_agent_rest) { + if (agent) og_agent_send_refresh(cli); - } } int og_socket_server_init(const char *port) diff --git a/src/rest.c b/src/rest.c index 11ef988..dab4ee2 100644 --- a/src/rest.c +++ b/src/rest.c @@ -86,9 +86,23 @@ void og_client_add(struct og_client *cli) list_add(&cli->list, &client_list); } -static struct og_client *og_client_find(const char *ip) +struct og_client *__og_client_find(const struct in_addr *addr) { struct og_client *client; + + list_for_each_entry(client, &client_list, list) { + if (!client->agent) + continue; + + if (client->addr.sin_addr.s_addr == addr->s_addr) + return client; + } + + return NULL; +} + +static struct og_client *og_client_find(const char *ip) +{ struct in_addr addr; int res; @@ -98,13 +112,7 @@ static struct og_client *og_client_find(const char *ip) return NULL; } - list_for_each_entry(client, &client_list, list) { - if (client->addr.sin_addr.s_addr == addr.s_addr && client->agent) { - return client; - } - } - - return NULL; + return __og_client_find(&addr); } static const char *og_client_status(const struct og_client *cli) diff --git a/src/rest.h b/src/rest.h index fb1c1b6..d586ef6 100644 --- a/src/rest.h +++ b/src/rest.h @@ -65,6 +65,7 @@ struct og_client { }; void og_client_add(struct og_client *cli); +struct og_client *__og_client_find(const struct in_addr *addr); static inline int og_client_socket(const struct og_client *cli) { -- cgit v1.2.3-18-g5258