From 84809755a125b692a6f6a4a62e96c92599e7014c Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Fri, 3 Jul 2020 19:13:34 -0400 Subject: [PATCH] Implement clients_by_sock convenience methods Implement clients_by_sock convenience methods in src/server.c --- src/server.c | 80 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 27 deletions(-) diff --git a/src/server.c b/src/server.c index 0cdc4bd..c6909fc 100644 --- a/src/server.c +++ b/src/server.c @@ -197,6 +197,43 @@ error_dict_set: return -1; } +static inline int client_by_sock(patty_ax25_server *server, + patty_ax25_sock *sock) { + void *value; + + if ((value = patty_dict_get_with_hash(server->clients_by_sock, + (uint32_t)sock->fd)) == NULL) { + goto error_dict_get_with_hash; + } + + return (int)((int64_t)value); + +error_dict_get_with_hash: + return -1; +} + +static inline int client_save_by_sock(patty_ax25_server *server, + int client, + patty_ax25_sock *sock) { + if (patty_dict_set_with_hash(server->clients_by_sock, + NULL + sock->fd, + sizeof(sock->fd), + NULL + client, + (uint32_t)sock->fd) == NULL) { + goto error_dict_set_with_hash; + } + + return 0; + +error_dict_set_with_hash: + return -1; +} + +static inline int client_delete_by_sock(patty_ax25_server *server, + patty_ax25_sock *sock) { + return patty_dict_delete_with_hash(server->clients_by_sock, sock->fd); +} + static inline uint32_t hash_addr(patty_ax25_addr *addr) { uint32_t hash; @@ -306,7 +343,7 @@ static int sock_close(patty_ax25_server *server, goto error_dict_delete_by_fd_socks; } - (void)patty_dict_delete_with_hash(server->clients_by_sock, sock->fd); + (void)client_delete_by_sock(server, sock); clear_fd(server, sock->fd); @@ -439,12 +476,8 @@ static int server_socket(patty_ax25_server *server, goto error_sock_save_by_fd; } - if (patty_dict_set_with_hash(server->clients_by_sock, - NULL + sock->fd, - sizeof(sock->fd), - NULL + client, - (uint32_t)sock->fd) < 0) { - goto error_dict_set_with_hash; + if (client_save_by_sock(server, client, sock) < 0) { + goto error_client_save_by_sock; } response.ret = sock->fd; @@ -454,7 +487,7 @@ static int server_socket(patty_ax25_server *server, return write(client, &response, sizeof(response)); -error_dict_set_with_hash: +error_client_save_by_sock: error_sock_save_by_fd: patty_ax25_sock_destroy(sock); @@ -739,12 +772,8 @@ static int server_connect(patty_ax25_server *server, goto error_sock_save_by_addrpair; } - if (patty_dict_set_with_hash(server->clients_by_sock, - NULL + sock->fd, - sizeof(sock->fd), - NULL + client, - (uint32_t)client) < 0) { - goto error_dict_set_with_hash; + if (client_save_by_sock(server, client, sock) < 0) { + goto error_client_save_by_sock; } /* @@ -768,7 +797,7 @@ error_invalid_status: error_sock_by_fd: return write(client, &response, sizeof(response)); -error_dict_set_with_hash: +error_client_save_by_sock: error_sock_save_by_addrpair: error_io: return -1; @@ -1051,9 +1080,8 @@ static int handle_sabm(patty_ax25_server *server, return reply_dm(iface, frame, PATTY_AX25_FRAME_U_FINAL); } - if ((client = (int)patty_dict_get_with_hash(server->clients_by_sock, - (uint32_t)local->fd)) == 0) { - goto error_lookup_client; + if ((client = client_by_sock(server, local)) < 0) { + goto error_client_by_sock; } if ((remote = patty_ax25_sock_new(local->proto, local->type)) == NULL) { @@ -1110,7 +1138,7 @@ error_sock_save_by_fd: patty_ax25_sock_destroy(remote); error_sock_new: -error_lookup_client: +error_client_by_sock: return -1; } @@ -1127,9 +1155,8 @@ static int handle_ua(patty_ax25_server *server, return reply_frmr(iface, frame, PATTY_AX25_FRAME_U_FINAL); } - if ((client = (int)patty_dict_get_with_hash(server->clients_by_sock, - (uint32_t)sock->fd)) == 0) { - goto error_lookup_client; + if ((client = client_by_sock(server, sock)) < 0) { + goto error_client_by_sock; } sock->status = PATTY_AX25_SOCK_ESTABLISHED; @@ -1164,7 +1191,7 @@ error_write: error_delete_by_addrpair: error_save_by_addrpair: -error_lookup_client: +error_client_by_sock: return -1; } @@ -1187,9 +1214,8 @@ static int handle_dm(patty_ax25_server *server, return 0; } - if ((client = (int)patty_dict_get_with_hash(server->clients_by_sock, - (uint32_t)sock->fd)) == 0) { - goto error_lookup_client; + if ((client = client_by_sock(server, sock)) < 0) { + goto error_client_by_sock; } if (sock_delete_by_addrpair(server->socks_pending_connect, @@ -1211,7 +1237,7 @@ static int handle_dm(patty_ax25_server *server, error_write: error_delete_by_addrpair: -error_lookup_client: +error_client_by_sock: return -1; }