Getting a little better at this whole refactoring endeavor

This commit is contained in:
XANTRONIX Development 2020-06-18 18:25:11 -04:00 committed by XANTRONIX Industrial
parent d2e278f715
commit 710b5ef069

View file

@ -13,17 +13,17 @@
struct _patty_ax25_server {
patty_list *ifaces;
patty_dict *socks,
*socks_by_addrpair,
*socks_bound;
patty_dict *socks_by_fd,
*socks_bound,
*socks_pending_accept,
*socks_pending_connect,
*socks_established;
int fd_max;
fd_set fds_watch,
fds_r,
fds_w;
patty_list *pending_accept;
};
int patty_ax25_server_init(patty_ax25_server *server) {
@ -33,34 +33,41 @@ int patty_ax25_server_init(patty_ax25_server *server) {
goto error_list_new_ifaces;
}
if ((server->socks = patty_dict_new()) == NULL) {
goto error_dict_new_socks;
}
if ((server->socks_by_addrpair = patty_dict_new()) == NULL) {
goto error_dict_new_socks_by_addrpair;
if ((server->socks_by_fd = patty_dict_new()) == NULL) {
goto error_dict_new_socks_by_fd;
}
if ((server->socks_bound = patty_dict_new()) == NULL) {
goto error_dict_new_socks_bound;
}
if ((server->pending_accept = patty_list_new()) == NULL) {
goto error_list_new_pending_accept;
if ((server->socks_pending_accept = patty_dict_new()) == NULL) {
goto error_dict_new_socks_pending_accept;
}
if ((server->socks_pending_connect = patty_dict_new()) == NULL) {
goto error_dict_new_socks_pending_connect;
}
if ((server->socks_established = patty_dict_new()) == NULL) {
goto error_dict_new_socks_established;
}
return 0;
error_list_new_pending_accept:
error_dict_new_socks_established:
patty_dict_destroy(server->socks_pending_connect);
error_dict_new_socks_pending_connect:
patty_dict_destroy(server->socks_pending_accept);
error_dict_new_socks_pending_accept:
patty_dict_destroy(server->socks_bound);
error_dict_new_socks_bound:
patty_dict_destroy(server->socks_by_addrpair);
patty_dict_destroy(server->socks_by_fd);
error_dict_new_socks_by_addrpair:
patty_dict_destroy(server->socks);
error_dict_new_socks:
error_dict_new_socks_by_fd:
patty_list_destroy(server->ifaces);
error_list_new_ifaces:
@ -74,8 +81,11 @@ static int destroy_if(patty_ax25_if *iface, void *ctx) {
}
void patty_ax25_server_stop(patty_ax25_server *server) {
patty_dict_destroy(server->socks_by_addrpair);
patty_dict_destroy(server->socks);
patty_dict_destroy(server->socks_established);
patty_dict_destroy(server->socks_pending_connect);
patty_dict_destroy(server->socks_pending_accept);
patty_dict_destroy(server->socks_bound);
patty_dict_destroy(server->socks_by_fd);
patty_ax25_server_each_if(server, destroy_if, NULL);
patty_list_destroy(server->ifaces);
@ -131,61 +141,25 @@ static uint32_t hash_addrpair(patty_ax25_addr *local,
return hash;
}
static int sock_save(patty_ax25_server *server, patty_ax25_sock *sock) {
uint32_t hash = hash_addrpair(&sock->local, &sock->remote);
if (patty_dict_set(server->socks,
NULL + sock->fd,
sizeof(sock->fd),
sock) == NULL) {
goto error_dict_set;
}
if (patty_dict_set_with_hash(server->socks_by_addrpair,
NULL + sock->fd,
sizeof(sock->fd),
sock,
hash) == NULL) {
goto error_dict_set;
}
return sock->fd;
error_dict_set:
return -1;
}
static patty_ax25_sock *sock_by_fd(patty_ax25_server *server, int fd) {
patty_ax25_sock *sock;
if ((sock = patty_dict_get(server->socks, &fd, sizeof(fd))) == NULL) {
errno = EBADF;
goto error_dict_get;
}
return sock;
error_dict_get:
return NULL;
}
static patty_ax25_sock *sock_get_fd(patty_ax25_server *server,
int fd) {
return patty_dict_get(server->socks,
static patty_ax25_sock *sock_by_fd(patty_dict *dict,
int fd) {
return patty_dict_get(dict,
NULL + fd,
sizeof(fd));
}
static patty_ax25_sock *sock_get_addrpair(patty_ax25_server *server,
patty_ax25_addr *local,
patty_ax25_addr *remote) {
static patty_ax25_sock *sock_by_addr(patty_dict *dict,
patty_ax25_addr *addr) {
patty_dict_slot *slot;
uint32_t hash = hash_addrpair(local, remote);
uint32_t hash;
if ((slot = patty_dict_slot_find(server->socks_by_addrpair, hash)) == NULL) {
errno = EBADF;
hash_init(&hash);
hash_addr(&hash, addr);
hash_end(&hash);
if ((slot = patty_dict_slot_find(dict, hash)) == NULL) {
errno = EEXIST;
goto error_dict_slot_find;
}
@ -196,13 +170,89 @@ error_dict_slot_find:
return NULL;
}
static patty_ax25_sock *sock_by_addrpair(patty_dict *dict,
patty_ax25_addr *local,
patty_ax25_addr *remote) {
patty_dict_slot *slot;
uint32_t hash = hash_addrpair(local, remote);
if ((slot = patty_dict_slot_find(dict, hash)) == NULL) {
errno = EEXIST;
goto error_dict_slot_find;
}
return (patty_ax25_sock *)slot->value;
error_dict_slot_find:
return NULL;
}
static int sock_save_by_fd(patty_dict *dict, patty_ax25_sock *sock) {
if (patty_dict_set(dict,
NULL + sock->fd,
sizeof(sock->fd),
sock) == NULL) {
goto error_dict_set;
}
return sock->fd;
error_dict_set:
return -1;
}
static int sock_save_by_addr(patty_dict *dict,
patty_ax25_sock *sock,
patty_ax25_addr *addr) {
uint32_t hash;
hash_init(&hash);
hash_addr(&hash, addr);
hash_end(&hash);
if (patty_dict_set_with_hash(dict,
addr,
sizeof(*addr),
sock,
hash) == NULL) {
goto error_dict_set_with_hash;
}
return 0;
error_dict_set_with_hash:
return -1;
}
static int sock_save_by_addrpair(patty_dict *dict,
patty_ax25_sock *sock,
patty_ax25_addr *local,
patty_ax25_addr *remote) {
uint32_t hash = hash_addrpair(local, remote);
if (patty_dict_set_with_hash(dict,
sock,
sizeof(*sock),
sock,
hash) == NULL) {
goto error_dict_set_with_hash;
}
return 0;
error_dict_set_with_hash:
return -1;
}
static int server_connect(patty_ax25_server *server,
int socket,
patty_ax25_addr *addr) {
patty_ax25_sock *sock;
if ((sock = sock_get_fd(server, socket)) == NULL) {
goto error_sock_get_fd;
if ((sock = sock_by_fd(server->socks_by_fd, socket)) == NULL) {
goto error_sock_by_fd;
}
if (sock->remote.callsign[0] != '\0') {
@ -216,7 +266,7 @@ static int server_connect(patty_ax25_server *server,
return 0;
error_exists:
error_sock_get_fd:
error_sock_by_fd:
return -1;
}
@ -232,7 +282,7 @@ int patty_ax25_server_add_if(patty_ax25_server *server,
}
}
if (patty_dict_set(server->socks, NULL + fd, sizeof(fd), iface) == NULL) {
if (patty_dict_set(server->socks_by_fd, NULL + fd, sizeof(fd), iface) == NULL) {
goto error_dict_set;
}
@ -273,7 +323,7 @@ int patty_ax25_server_delete_if(patty_ax25_server *server,
server->fd_max--;
}
if (patty_dict_delete(server->socks, NULL + fd, sizeof(fd)) == NULL) {
if (patty_dict_delete(server->socks_by_fd, NULL + fd, sizeof(fd)) == NULL) {
goto error_dict_delete;
}
}
@ -359,14 +409,14 @@ static int server_socket(patty_ax25_server *server,
response.ret = sock->fd;
response.eno = 0;
if (sock_save(server, sock) < 0) {
if (sock_save_by_fd(server->socks_by_fd, sock) < 0) {
response.ret = -1;
response.eno = errno;
goto error_sock_save;
goto error_sock_save_by_fd;
}
error_sock_save:
error_sock_save_by_fd:
if (write(client, &response, sizeof(response)) < 0) {
goto error_io;
}
@ -391,11 +441,11 @@ static int server_bind(patty_ax25_server *server,
goto error_io;
}
if ((sock = sock_get_fd(server, request.socket)) == NULL) {
if ((sock = sock_by_fd(server->socks_by_fd, request.socket)) == NULL) {
response.ret = -1;
response.eno = EBADF;
goto error_sock_get_fd;
goto error_sock_by_fd;
}
/*
@ -436,7 +486,7 @@ static int server_bind(patty_ax25_server *server,
error_inuse:
error_bound:
error_sock_get_fd:
error_sock_by_fd:
return 0;
error_dict_set:
@ -455,11 +505,11 @@ static int server_listen(patty_ax25_server *server,
goto error_io;
}
if ((sock = sock_get_fd(server, request.socket)) == NULL) {
if ((sock = sock_by_fd(server->socks_by_fd, request.socket)) == NULL) {
response.ret = -1;
response.eno = EBADF;
goto error_sock_get_fd;
goto error_sock_by_fd;
}
if (sock->local.callsign[0] == '\0') {
@ -474,7 +524,7 @@ static int server_listen(patty_ax25_server *server,
response.eno = 0;
error_invalid_fd:
error_sock_get_fd:
error_sock_by_fd:
if (write(client, &response, sizeof(response)) < 0) {
goto error_io;
}
@ -564,11 +614,11 @@ static int server_accept(patty_ax25_server *server,
goto error_io;
}
if ((local = sock_get_fd(server, request.socket)) == NULL) {
if ((local = sock_by_fd(server->socks_by_fd, request.socket)) == NULL) {
response.ret = -1;
response.eno = EBADF;
goto error_sock_get_fd;
goto error_sock_by_fd;
}
if ((remote = malloc(sizeof(*remote))) == NULL) {
@ -587,7 +637,7 @@ static int server_accept(patty_ax25_server *server,
}
}
error_sock_get_fd:
error_sock_by_fd:
if (write(client, &response, sizeof(response)) < 0) {
goto error_io;
}