Implement clients_by_sock convenience methods
Implement clients_by_sock convenience methods in src/server.c
This commit is contained in:
parent
3d2922f2a4
commit
84809755a1
1 changed files with 53 additions and 27 deletions
80
src/server.c
80
src/server.c
|
@ -197,6 +197,43 @@ error_dict_set:
|
||||||
return -1;
|
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) {
|
static inline uint32_t hash_addr(patty_ax25_addr *addr) {
|
||||||
uint32_t hash;
|
uint32_t hash;
|
||||||
|
|
||||||
|
@ -306,7 +343,7 @@ static int sock_close(patty_ax25_server *server,
|
||||||
goto error_dict_delete_by_fd_socks;
|
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);
|
clear_fd(server, sock->fd);
|
||||||
|
|
||||||
|
@ -439,12 +476,8 @@ static int server_socket(patty_ax25_server *server,
|
||||||
goto error_sock_save_by_fd;
|
goto error_sock_save_by_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (patty_dict_set_with_hash(server->clients_by_sock,
|
if (client_save_by_sock(server, client, sock) < 0) {
|
||||||
NULL + sock->fd,
|
goto error_client_save_by_sock;
|
||||||
sizeof(sock->fd),
|
|
||||||
NULL + client,
|
|
||||||
(uint32_t)sock->fd) < 0) {
|
|
||||||
goto error_dict_set_with_hash;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
response.ret = sock->fd;
|
response.ret = sock->fd;
|
||||||
|
@ -454,7 +487,7 @@ static int server_socket(patty_ax25_server *server,
|
||||||
|
|
||||||
return write(client, &response, sizeof(response));
|
return write(client, &response, sizeof(response));
|
||||||
|
|
||||||
error_dict_set_with_hash:
|
error_client_save_by_sock:
|
||||||
error_sock_save_by_fd:
|
error_sock_save_by_fd:
|
||||||
patty_ax25_sock_destroy(sock);
|
patty_ax25_sock_destroy(sock);
|
||||||
|
|
||||||
|
@ -739,12 +772,8 @@ static int server_connect(patty_ax25_server *server,
|
||||||
goto error_sock_save_by_addrpair;
|
goto error_sock_save_by_addrpair;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (patty_dict_set_with_hash(server->clients_by_sock,
|
if (client_save_by_sock(server, client, sock) < 0) {
|
||||||
NULL + sock->fd,
|
goto error_client_save_by_sock;
|
||||||
sizeof(sock->fd),
|
|
||||||
NULL + client,
|
|
||||||
(uint32_t)client) < 0) {
|
|
||||||
goto error_dict_set_with_hash;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -768,7 +797,7 @@ error_invalid_status:
|
||||||
error_sock_by_fd:
|
error_sock_by_fd:
|
||||||
return write(client, &response, sizeof(response));
|
return write(client, &response, sizeof(response));
|
||||||
|
|
||||||
error_dict_set_with_hash:
|
error_client_save_by_sock:
|
||||||
error_sock_save_by_addrpair:
|
error_sock_save_by_addrpair:
|
||||||
error_io:
|
error_io:
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -1051,9 +1080,8 @@ static int handle_sabm(patty_ax25_server *server,
|
||||||
return reply_dm(iface, frame, PATTY_AX25_FRAME_U_FINAL);
|
return reply_dm(iface, frame, PATTY_AX25_FRAME_U_FINAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((client = (int)patty_dict_get_with_hash(server->clients_by_sock,
|
if ((client = client_by_sock(server, local)) < 0) {
|
||||||
(uint32_t)local->fd)) == 0) {
|
goto error_client_by_sock;
|
||||||
goto error_lookup_client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((remote = patty_ax25_sock_new(local->proto, local->type)) == NULL) {
|
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);
|
patty_ax25_sock_destroy(remote);
|
||||||
|
|
||||||
error_sock_new:
|
error_sock_new:
|
||||||
error_lookup_client:
|
error_client_by_sock:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1127,9 +1155,8 @@ static int handle_ua(patty_ax25_server *server,
|
||||||
return reply_frmr(iface, frame, PATTY_AX25_FRAME_U_FINAL);
|
return reply_frmr(iface, frame, PATTY_AX25_FRAME_U_FINAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((client = (int)patty_dict_get_with_hash(server->clients_by_sock,
|
if ((client = client_by_sock(server, sock)) < 0) {
|
||||||
(uint32_t)sock->fd)) == 0) {
|
goto error_client_by_sock;
|
||||||
goto error_lookup_client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sock->status = PATTY_AX25_SOCK_ESTABLISHED;
|
sock->status = PATTY_AX25_SOCK_ESTABLISHED;
|
||||||
|
@ -1164,7 +1191,7 @@ error_write:
|
||||||
|
|
||||||
error_delete_by_addrpair:
|
error_delete_by_addrpair:
|
||||||
error_save_by_addrpair:
|
error_save_by_addrpair:
|
||||||
error_lookup_client:
|
error_client_by_sock:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1187,9 +1214,8 @@ static int handle_dm(patty_ax25_server *server,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((client = (int)patty_dict_get_with_hash(server->clients_by_sock,
|
if ((client = client_by_sock(server, sock)) < 0) {
|
||||||
(uint32_t)sock->fd)) == 0) {
|
goto error_client_by_sock;
|
||||||
goto error_lookup_client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sock_delete_by_addrpair(server->socks_pending_connect,
|
if (sock_delete_by_addrpair(server->socks_pending_connect,
|
||||||
|
@ -1211,7 +1237,7 @@ static int handle_dm(patty_ax25_server *server,
|
||||||
|
|
||||||
error_write:
|
error_write:
|
||||||
error_delete_by_addrpair:
|
error_delete_by_addrpair:
|
||||||
error_lookup_client:
|
error_client_by_sock:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue