From 4aced9e9c12e04bad22ed07f8e275af35beebe34 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Thu, 25 Jun 2020 20:37:12 -0400 Subject: [PATCH] ...I've been busy. --- include/patty/ax25.h | 3 + include/patty/ax25/frame.h | 2 +- include/patty/ax25/if.h | 6 +- include/patty/ax25/route.h | 22 ++++ include/patty/ax25/sock.h | 4 + include/patty/dict.h | 2 + include/patty/hash.h | 8 +- src/Makefile | 6 +- src/ax25.c | 16 +++ src/call.c | 1 + src/dict.c | 16 +-- src/hash.c | 34 +++-- src/if.c | 8 +- src/route.c | 99 +++++++++++++++ src/server.c | 253 ++++++++++++++++++++----------------- src/sock.c | 29 +++++ src/testclient.c | 23 +++- src/testserver.c | 5 + 18 files changed, 384 insertions(+), 153 deletions(-) create mode 100644 src/route.c create mode 100644 src/sock.c diff --git a/include/patty/ax25.h b/include/patty/ax25.h index afdf89e..e7fcf41 100644 --- a/include/patty/ax25.h +++ b/include/patty/ax25.h @@ -43,4 +43,7 @@ int patty_ax25_ntop(const patty_ax25_addr *addr, uint8_t *ssid, size_t len); +void patty_ax25_addr_hash(uint32_t *hash, + const patty_ax25_addr *addr); + #endif /* _PATTY_AX25_H */ diff --git a/include/patty/ax25/frame.h b/include/patty/ax25/frame.h index 02ba5e0..1cba2b0 100644 --- a/include/patty/ax25/frame.h +++ b/include/patty/ax25/frame.h @@ -9,7 +9,7 @@ #define PATTY_AX25_FRAME_SIZE(hops, mode, infolen) \ ((sizeof(patty_ax25_addr) * (2 + hops)) \ - + (mode == PATTY_SOCK_SABME? 2: 1) \ + + (mode == PATTY_AX25_SOCK_SABME? 2: 1) \ + (infolen > 0? 1 + infolen: 0)) enum patty_ax25_frame_type { diff --git a/include/patty/ax25/if.h b/include/patty/ax25/if.h index 9d6f370..abe10f0 100644 --- a/include/patty/ax25/if.h +++ b/include/patty/ax25/if.h @@ -47,13 +47,13 @@ patty_ax25_if *patty_ax25_if_new(int opts, void *info); void patty_ax25_if_destroy(patty_ax25_if *iface); -int patty_ax25_if_each_addr(patty_ax25_if *iface, +int patty_ax25_if_addr_each(patty_ax25_if *iface, int (*callback)(char *, uint8_t, void *), void *ctx); -int patty_ax25_if_add_addr(patty_ax25_if *iface, +int patty_ax25_if_addr_add(patty_ax25_if *iface, const char *callsign, uint8_t ssid); -int patty_ax25_if_delete_addr(patty_ax25_if *iface, +int patty_ax25_if_addr_delete(patty_ax25_if *iface, const char *callsign, uint8_t ssid); ssize_t patty_ax25_if_recv(patty_ax25_if *iface, diff --git a/include/patty/ax25/route.h b/include/patty/ax25/route.h index c9b7b4a..7479127 100644 --- a/include/patty/ax25/route.h +++ b/include/patty/ax25/route.h @@ -5,7 +5,29 @@ typedef struct _patty_ax25_route { patty_ax25_addr dest, hops[PATTY_AX25_MAX_HOPS]; + size_t nhops; + patty_ax25_if *iface; } patty_ax25_route; +typedef struct _patty_dict patty_ax25_route_table; + +patty_ax25_route *patty_ax25_route_new(patty_ax25_if *iface, + const char *callsign, + uint8_t ssid); + +patty_ax25_route *patty_ax25_route_new_default(patty_ax25_if *iface); + +int patty_ax25_route_add_hop(patty_ax25_route *route, + const char *callsign, + uint8_t ssid); + +patty_ax25_route_table *patty_ax25_route_table_new(); + +patty_ax25_route *patty_ax25_route_table_find(patty_ax25_route_table *table, + patty_ax25_addr *dest); + +int patty_ax25_route_table_add(patty_ax25_route_table *table, + patty_ax25_route *route); + #endif /* _PATTY_AX25_ROUTE_H */ diff --git a/include/patty/ax25/sock.h b/include/patty/ax25/sock.h index 6b86d30..b26c3ab 100644 --- a/include/patty/ax25/sock.h +++ b/include/patty/ax25/sock.h @@ -12,6 +12,8 @@ enum patty_ax25_sock_type { enum patty_ax25_sock_status { PATTY_AX25_SOCK_CLOSED, PATTY_AX25_SOCK_LISTENING, + PATTY_AX25_SOCK_PENDING_ACCEPT, + PATTY_AX25_SOCK_PENDING_CONNECT, PATTY_AX25_SOCK_ESTABLISHED }; @@ -51,4 +53,6 @@ typedef struct _patty_ax25_sock { patty_ax25_sock *patty_ax25_sock_new(enum patty_ax25_sock_type type); +void patty_ax25_sock_destroy(patty_ax25_sock *sock); + #endif /* _PATTY_AX25_SOCK_H */ diff --git a/include/patty/dict.h b/include/patty/dict.h index f14d04b..003fd7f 100644 --- a/include/patty/dict.h +++ b/include/patty/dict.h @@ -38,6 +38,8 @@ int patty_dict_each(patty_dict *dict, patty_dict_callback callback, void *ctx); +void *patty_dict_get_with_hash(patty_dict *dict, uint32_t hash); + void *patty_dict_get(patty_dict *dict, void *key, size_t keysz); void *patty_dict_set_with_hash(patty_dict *dict, diff --git a/include/patty/hash.h b/include/patty/hash.h index 280bb91..ee70548 100644 --- a/include/patty/hash.h +++ b/include/patty/hash.h @@ -4,8 +4,12 @@ #include #include -#define PATTY_DICT_BUCKET_SIZE 16 +void patty_hash_init(uint32_t *hash); -uint32_t patty_hash(void *key, size_t size); +void patty_hash_data(uint32_t *hash, void *data, size_t len); + +void patty_hash_end(uint32_t *hash); + +uint32_t patty_hash(void *data, size_t len); #endif /* _PATTY_HASH_H */ diff --git a/src/Makefile b/src/Makefile index c787134..2b529f7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,10 +8,10 @@ CFLAGS = $(CGFLAGS) -fPIC -Wall -O2 -I$(INCLUDE_PATH) LDFLAGS = HEADERS = kiss.h ax25.h ax25/if.h ax25/macros.h ax25/proto.h \ - ax25/call.h ax25/frame.h ax25/sock.h ax25/server.h \ - list.h hash.h dict.h + ax25/call.h ax25/frame.h ax25/sock.h ax25/route.h \ + ax25/server.h list.h hash.h dict.h -OBJS = kiss.o ax25.o if.o call.o frame.o server.o \ +OBJS = kiss.o ax25.o if.o call.o frame.o sock.o route.o server.o \ list.o hash.o dict.o EXAMPLES = decode ptmx testclient testserver diff --git a/src/ax25.c b/src/ax25.c index 7db7920..0dc1129 100644 --- a/src/ax25.c +++ b/src/ax25.c @@ -90,3 +90,19 @@ int patty_ax25_ntop(const patty_ax25_addr *addr, error_invalid_args: return -1; } + +static inline void hash_byte(uint32_t *hash, uint8_t c) { + *hash += c; + *hash += (*hash << 10); + *hash ^= (*hash >> 6); +} + +void patty_ax25_addr_hash(uint32_t *hash, const patty_ax25_addr *addr) { + size_t i; + + for (i=0; icallsign[i] >> 1); + } + + hash_byte(hash, PATTY_AX25_ADDRESS_SSID_NUMBER(addr->ssid)); +} diff --git a/src/call.c b/src/call.c index 88a7fd2..5cb7cf5 100644 --- a/src/call.c +++ b/src/call.c @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/src/dict.c b/src/dict.c index e5a71c2..9ade654 100644 --- a/src/dict.c +++ b/src/dict.c @@ -96,11 +96,9 @@ int patty_dict_each(patty_dict *dict, patty_dict_callback callback, void *ctx) { return bucket_each_slot(0, &dict->bucket, callback, ctx); } -void *patty_dict_get(patty_dict *dict, void *key, size_t keysz) { +void *patty_dict_get_with_hash(patty_dict *dict, uint32_t hash) { patty_dict_slot *slot; - uint32_t hash = patty_hash(key, keysz); - if ((slot = patty_dict_slot_find(dict, hash)) == NULL) { return NULL; } @@ -108,6 +106,10 @@ void *patty_dict_get(patty_dict *dict, void *key, size_t keysz) { return slot->value; } +void *patty_dict_get(patty_dict *dict, void *key, size_t keysz) { + return patty_dict_get_with_hash(dict, patty_hash(key, keysz)); +} + void *patty_dict_set_with_hash(patty_dict *dict, void *key, size_t keysz, @@ -193,14 +195,6 @@ int patty_dict_delete(patty_dict *dict, void *key, size_t keysz) { return patty_dict_delete_with_hash(dict, patty_hash(key, keysz)); } -static int dict_item_release(void *key, size_t keysz, void *value, void *ctx) { - free(value); - - return 0; -} - void patty_dict_destroy(patty_dict *dict) { - (void)patty_dict_each(dict, dict_item_release, NULL); - free(dict); } diff --git a/src/hash.c b/src/hash.c index 75232e6..e34a203 100644 --- a/src/hash.c +++ b/src/hash.c @@ -3,18 +3,34 @@ #include -uint32_t patty_hash(void *key, size_t size) { - uint32_t hash = 0xffffffdf, i; +void patty_hash_init(uint32_t *hash) { + *hash = 0xffffffdf; +} - for (i=0; i> 6); +void patty_hash_data(uint32_t *hash, void *data, size_t len) { + size_t i; + + for (i=0; i> 6); } +} - hash += (hash << 3); - hash ^= (hash >> 11); - hash += (hash << 15); +void patty_hash_end(uint32_t *hash) { + *hash += (*hash << 3); + *hash ^= (*hash >> 11); + *hash += (*hash << 15); +} + +uint32_t patty_hash(void *data, size_t len) { + uint32_t hash; + + patty_hash_init(&hash); + patty_hash_data(&hash, data, len); + patty_hash_end(&hash); return hash; } diff --git a/src/if.c b/src/if.c index 4da2fd9..654a0a7 100644 --- a/src/if.c +++ b/src/if.c @@ -102,7 +102,7 @@ void patty_ax25_if_destroy(patty_ax25_if *iface) { free(iface); } -int patty_ax25_if_each_addr(patty_ax25_if *iface, +int patty_ax25_if_addr_each(patty_ax25_if *iface, int (*callback)(char *, uint8_t, void *), void *ctx) { patty_list_iterator *iter; patty_ax25_addr *addr; @@ -168,8 +168,6 @@ static patty_ax25_addr *find_addr(patty_ax25_if *iface, return addr; } - patty_list_finish(iter); - error_ntop: patty_list_finish(iter); @@ -177,7 +175,7 @@ error_list_start: return NULL; } -int patty_ax25_if_add_addr(patty_ax25_if *iface, +int patty_ax25_if_addr_add(patty_ax25_if *iface, const char *callsign, uint8_t ssid) { patty_ax25_addr *addr; @@ -211,7 +209,7 @@ error_exists: return -1; } -int patty_ax25_if_delete_addr(patty_ax25_if *iface, +int patty_ax25_if_addr_delete(patty_ax25_if *iface, const char *callsign, uint8_t ssid) { patty_list_item *item = iface->addrs->first; diff --git a/src/route.c b/src/route.c new file mode 100644 index 0000000..546aed4 --- /dev/null +++ b/src/route.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include + +#include + +#include + +patty_ax25_route *patty_ax25_route_new(patty_ax25_if *iface, + const char *callsign, + uint8_t ssid) { + patty_ax25_route *route; + + if ((route = malloc(sizeof(*route))) == NULL) { + goto error_malloc_route; + } + + memset(route, '\0', sizeof(*route)); + + if (callsign) { + if (patty_ax25_pton(callsign, ssid, &route->dest) < 0) { + goto error_pton; + } + } + + route->iface = iface; + + return route; + +error_pton: + free(route); + +error_malloc_route: + return NULL; +} + +patty_ax25_route *patty_ax25_route_new_default(patty_ax25_if *iface) { + return patty_ax25_route_new(iface, NULL, 0); +} + +int patty_ax25_route_add_hop(patty_ax25_route *route, + const char *callsign, + uint8_t ssid) { + if (route->nhops == PATTY_AX25_MAX_HOPS) { + errno = ENOMEM; + + goto error_max_hops; + } + + return patty_ax25_pton(callsign, ssid, &route->hops[route->nhops++]); + +error_max_hops: + return -1; +} + +patty_ax25_route_table *patty_ax25_route_table_new() { + return patty_dict_new(); +} + +patty_ax25_route *patty_ax25_route_table_find(patty_ax25_route_table *table, + patty_ax25_addr *dest) { + uint32_t hash; + + patty_hash_init(&hash); + patty_ax25_addr_hash(&hash, dest); + patty_hash_end(&hash); + + return patty_dict_get_with_hash(table, hash); +} + +int patty_ax25_route_table_add(patty_ax25_route_table *table, + patty_ax25_route *route) { + uint32_t hash; + + patty_hash_init(&hash); + patty_ax25_addr_hash(&hash, &route->dest); + patty_hash_end(&hash); + + if (patty_ax25_route_table_find(table, &route->dest) != NULL) { + errno = EEXIST; + + goto error_exists; + } + + if (patty_dict_set_with_hash(table, + &route->dest, + sizeof(route->dest), + route, + hash) == NULL) { + goto error_dict_set_with_hash; + } + + return 0; + +error_dict_set_with_hash: +error_exists: + return -1; +} diff --git a/src/server.c b/src/server.c index a18ffa4..53a6657 100644 --- a/src/server.c +++ b/src/server.c @@ -27,6 +27,7 @@ struct _patty_ax25_server { fds_w; /* fds select()ed for writing */ patty_list *ifaces; + patty_dict *routes; patty_dict *socks_by_fd, *socks_pending_accept, @@ -107,47 +108,9 @@ static int destroy_if(patty_ax25_if *iface, void *ctx) { return 0; } -static void sock_init(patty_ax25_sock *sock, enum patty_ax25_sock_type type) { - memset(sock, '\0', sizeof(*sock)); - - sock->status = PATTY_AX25_SOCK_CLOSED; - sock->mode = PATTY_AX25_SOCK_DM; - sock->type = type; - sock->n_maxlen = PATTY_AX25_FRAME_DEFAULT_MAXLEN; - sock->n_window = PATTY_AX25_FRAME_DEFAULT_WINDOW; -} - -static inline void hash_init(uint32_t *hash) { - *hash = 0xffffffdf; -} - -static inline void hash_byte(uint32_t *hash, uint8_t c) { - *hash += c; - *hash += (*hash << 10); - *hash ^= (*hash >> 6); -} - -static inline void hash_addr(uint32_t *hash, patty_ax25_addr *addr) { - int i; - - for (i=0; icallsign[i] >> 1); - } - - hash_byte(hash, PATTY_AX25_ADDRESS_SSID_NUMBER(addr->ssid)); -} - -static inline void hash_end(uint32_t *hash) { - *hash += (*hash << 3); - *hash ^= (*hash >> 11); - *hash += (*hash << 15); -} - static patty_ax25_sock *sock_by_fd(patty_dict *dict, int fd) { - return patty_dict_get(dict, - NULL + fd, - sizeof(fd)); + return patty_dict_get_with_hash(dict, (uint32_t)fd); } static patty_ax25_sock *sock_by_addr(patty_dict *dict, @@ -156,9 +119,9 @@ static patty_ax25_sock *sock_by_addr(patty_dict *dict, uint32_t hash; - hash_init(&hash); - hash_addr(&hash, addr); - hash_end(&hash); + patty_hash_init(&hash); + patty_ax25_addr_hash(&hash, addr); + patty_hash_end(&hash); if ((slot = patty_dict_slot_find(dict, hash)) == NULL) { errno = EEXIST; @@ -179,10 +142,10 @@ static patty_ax25_sock *sock_by_addrpair(patty_dict *dict, uint32_t hash; - hash_init(&hash); - hash_addr(&hash, local); - hash_addr(&hash, remote); - hash_end(&hash); + patty_hash_init(&hash); + patty_ax25_addr_hash(&hash, local); + patty_ax25_addr_hash(&hash, remote); + patty_hash_end(&hash); if ((slot = patty_dict_slot_find(dict, hash)) == NULL) { errno = EEXIST; @@ -197,10 +160,11 @@ error_dict_slot_find: } 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) { + if (patty_dict_set_with_hash(dict, + NULL + sock->fd, + sizeof(sock->fd), + sock, + (uint32_t)sock->fd) == NULL) { goto error_dict_set; } @@ -215,9 +179,9 @@ static int sock_save_by_addr(patty_dict *dict, patty_ax25_addr *addr) { uint32_t hash; - hash_init(&hash); - hash_addr(&hash, addr); - hash_end(&hash); + patty_hash_init(&hash); + patty_ax25_addr_hash(&hash, addr); + patty_hash_end(&hash); if (patty_dict_set_with_hash(dict, addr, @@ -239,10 +203,10 @@ static int sock_save_by_addrpair(patty_dict *dict, patty_ax25_addr *remote) { uint32_t hash; - hash_init(&hash); - hash_addr(&hash, local); - hash_addr(&hash, remote); - hash_end(&hash); + patty_hash_init(&hash); + patty_ax25_addr_hash(&hash, local); + patty_ax25_addr_hash(&hash, remote); + patty_hash_end(&hash); if (patty_dict_set_with_hash(dict, sock, @@ -260,32 +224,44 @@ error_dict_set_with_hash: static int sock_delete(patty_ax25_server *server, patty_ax25_sock *sock) { - uint32_t hash_established, - hash_pending_connect, - hash_pending_accept; + if (sock->status == PATTY_AX25_SOCK_ESTABLISHED) { + uint32_t hash; - hash_init(&hash_established); - hash_addr(&hash_established, &sock->local); - hash_addr(&hash_established, &sock->remote); - hash_end(&hash_established); + patty_hash_init(&hash); + patty_ax25_addr_hash(&hash, &sock->local); + patty_ax25_addr_hash(&hash, &sock->remote); + patty_hash_end(&hash); - if (patty_dict_delete_with_hash(server->socks_established, hash_established) < 0) { - goto error_dict_delete_established; + if (patty_dict_delete_with_hash(server->socks_established, hash) < 0) { + goto error_dict_delete_established; + } } - hash_init(&hash_pending_connect); - hash_addr(&hash_pending_connect, &sock->local); - hash_end(&hash_pending_connect); + if (sock->status == PATTY_AX25_SOCK_PENDING_CONNECT) { + uint32_t hash; - if (patty_dict_delete_with_hash(server->socks_pending_connect, hash_pending_connect) < 0) { - goto error_dict_delete_pending_connect; + patty_hash_init(&hash); + patty_ax25_addr_hash(&hash, &sock->local); + patty_hash_end(&hash); + + if (patty_dict_delete_with_hash(server->socks_pending_connect, hash) < 0) { + goto error_dict_delete_pending_connect; + } } - hash_init(&hash_pending_accept); - hash_addr(&hash_pending_accept, &sock->local); - hash_end(&hash_pending_accept); + if (sock->status == PATTY_AX25_SOCK_PENDING_ACCEPT) { + uint32_t hash; - if (patty_dict_delete(server->socks_by_fd, NULL + sock->fd, sizeof(sock->fd)) < 0) { + patty_hash_init(&hash); + patty_ax25_addr_hash(&hash, &sock->local); + patty_hash_end(&hash); + + if (patty_dict_delete_with_hash(server->socks_pending_accept, hash) < 0) { + goto error_dict_delete_pending_accept; + } + } + + if (patty_dict_delete_with_hash(server->socks_by_fd, sock->fd) < 0) { goto error_dict_delete_by_fd; } @@ -294,21 +270,58 @@ static int sock_delete(patty_ax25_server *server, return 0; error_dict_delete_by_fd: +error_dict_delete_pending_accept: error_dict_delete_pending_connect: error_dict_delete_established: return -1; } +static inline void watch_fd(patty_ax25_server *server, int fd) { + FD_SET(fd, &server->fds_watch); + + if (server->fd_max <= fd) { + server->fd_max = fd + 1; + } +} + +static inline void clear_fd(patty_ax25_server *server, int fd) { + FD_CLR(fd, &server->fds_watch); + + if (server->fd_max == fd + 1) { + server->fd_max--; + } +} + +static inline void watch_sock(patty_ax25_server *server, patty_ax25_sock *sock) { + watch_fd(server, sock->fd); + + FD_SET(sock->fd, &server->fds_socks); +} + +static inline void clear_sock(patty_ax25_server *server, patty_ax25_sock *sock) { + clear_fd(server, sock->fd); + + FD_CLR(sock->fd, &server->fds_socks); +} + +static inline void watch_client(patty_ax25_server *server, int fd) { + watch_fd(server, fd); + + FD_SET(fd, &server->fds_clients); +} + +static inline void clear_client(patty_ax25_server *server, int fd) { + clear_fd(server, fd); + + FD_CLR(fd, &server->fds_clients); +} + int patty_ax25_server_add_if(patty_ax25_server *server, patty_ax25_if *iface) { int fd; if ((fd = patty_kiss_tnc_fd(iface->tnc)) >= 0) { - FD_SET(fd, &server->fds_watch); - - if (server->fd_max < fd + 1) { - server->fd_max = fd + 1; - } + watch_fd(server, fd); } if (patty_list_append(server->ifaces, iface) == NULL) { @@ -341,11 +354,7 @@ int patty_ax25_server_delete_if(patty_ax25_server *server, } if ((fd = patty_kiss_tnc_fd(iface->tnc)) >= 0) { - FD_CLR(fd, &server->fds_watch); - - if (server->fd_max == fd) { - server->fd_max--; - } + watch_fd(server, fd); if (patty_dict_delete(server->socks_by_fd, NULL + fd, sizeof(fd)) < 0) { goto error_dict_delete; @@ -416,18 +425,16 @@ static int server_socket(patty_ax25_server *server, patty_ax25_sock *sock; - if ((sock = malloc(sizeof(*sock))) == NULL) { - goto error_malloc_sock; + if (read(client, &request, sizeof(request)) < 0) { + goto error_read; } - sock_init(sock, request.type); + if ((sock = patty_ax25_sock_new(request.type)) == NULL) { + goto error_sock_new; + } if ((sock->fd = open("/dev/null", O_RDWR)) < 0) { - goto error_io; - } - - if (read(client, &request, sizeof(request)) < 0) { - goto error_io; + goto error_open; } response.ret = sock->fd; @@ -442,15 +449,19 @@ static int server_socket(patty_ax25_server *server, error_sock_save_by_fd: if (write(client, &response, sizeof(response)) < 0) { - goto error_io; + goto error_write; } return 0; -error_io: - free(sock); +error_write: + close(sock->fd); -error_malloc_sock: +error_open: + patty_ax25_sock_destroy(sock); + +error_sock_new: +error_read: return -1; } @@ -640,12 +651,10 @@ static int server_accept(patty_ax25_server *server, goto error_sock_by_fd; } - if ((remote = malloc(sizeof(*remote))) == NULL) { - goto error_malloc_remote; + if ((remote = patty_ax25_sock_new(local->type)) == NULL) { + goto error_sock_new_remote; } - sock_init(remote, local->type); - memcpy(&remote->local, &local->local, sizeof(remote->local)); if (local->type & PATTY_AX25_SOCK_PTY) { @@ -658,6 +667,8 @@ static int server_accept(patty_ax25_server *server, } } + remote->status = PATTY_AX25_SOCK_PENDING_ACCEPT; + if (sock_save_by_addr(server->socks_pending_accept, remote, &local->local) < 0) { @@ -681,9 +692,9 @@ error_save_by_fd: error_save_by_addr: error_server_accept_unix: error_server_accept_pty: - free(remote); + patty_ax25_sock_destroy(remote); -error_malloc_remote: +error_sock_new_remote: error_io: return -1; } @@ -729,6 +740,8 @@ static int server_connect(patty_ax25_server *server, memcpy(&sock->remote, &request.peer, sizeof(request.peer)); + sock->status = PATTY_AX25_SOCK_PENDING_CONNECT; + if (sock_save_by_addrpair(server->socks_pending_connect, sock, &sock->local, @@ -848,7 +861,7 @@ static int listen_unix(patty_ax25_server *server, const char *path) { goto error_listen; } - FD_SET(server->fd, &server->fds_watch); + watch_fd(server, server->fd); return 0; @@ -864,7 +877,9 @@ error_listening: static int accept_client(patty_ax25_server *server) { int fd; struct sockaddr addr; - socklen_t addrlen; + socklen_t addrlen = sizeof(addr); + + memset(&addr, '\0', addrlen); if (!FD_ISSET(server->fd, &server->fds_r)) { goto done; @@ -874,14 +889,15 @@ static int accept_client(patty_ax25_server *server) { goto error_accept; } - if (patty_dict_set(server->clients, - NULL + fd, - sizeof(fd), - NULL + fd) == NULL) { + if (patty_dict_set_with_hash(server->clients, + NULL + fd, + sizeof(fd), + NULL + fd, + (uint32_t)fd) == NULL) { goto error_dict_set; } - FD_SET(fd, &server->fds_clients); + watch_client(server, fd); done: return 0; @@ -907,15 +923,13 @@ static int handle_client(void *key, goto done; } - if ((readlen = read(server->fd, &call, sizeof(call))) < 0) { + if ((readlen = read(client, &call, sizeof(call))) < 0) { goto error_io; } else if (readlen == 0) { - FD_CLR(client, &server->fds_watch); - FD_CLR(client, &server->fds_clients); + clear_client(server, client); - if (patty_dict_delete(server->clients, - key, - keysz) < 0) { + if (patty_dict_delete_with_hash(server->clients, + (uint32_t)(key - NULL)) < 0) { goto error_dict_delete; } @@ -954,6 +968,9 @@ static int handle_packet_rx(patty_ax25_server *server, goto error_io; } + if (PATTY_AX25_CONTROL_UNNUMBERED_SABM(frame.control)) { + fprintf(stderr, "Got SABM packet\n"); + } /* * TODO: Handle inbound packet */ @@ -977,7 +994,7 @@ static int handle_iface(patty_ax25_server *server, patty_ax25_if *iface) { if ((readlen = patty_ax25_if_recv(iface, &buf)) < 0) { goto error_io; } else if (readlen == 0) { - FD_CLR(fd, &server->fds_watch); + clear_fd(server, fd); goto done; } @@ -1038,7 +1055,7 @@ static int handle_sock(void *key, if ((len = read(fd, iface->tx_buf, iface->tx_bufsz)) < 0) { goto error_io; } else if (len == 0) { - FD_CLR(fd, &server->fds_watch); + clear_sock(server, sock); if (patty_dict_delete(server->socks_established, NULL + fd, diff --git a/src/sock.c b/src/sock.c new file mode 100644 index 0000000..9be16e8 --- /dev/null +++ b/src/sock.c @@ -0,0 +1,29 @@ +#include +#include + +#include + +patty_ax25_sock *patty_ax25_sock_new(enum patty_ax25_sock_type type) { + patty_ax25_sock *sock; + + if ((sock = malloc(sizeof(*sock))) == NULL) { + goto error_malloc_sock; + } + + memset(sock, '\0', sizeof(*sock)); + + sock->status = PATTY_AX25_SOCK_CLOSED; + sock->mode = PATTY_AX25_SOCK_DM; + sock->type = type; + sock->n_maxlen = PATTY_AX25_FRAME_DEFAULT_MAXLEN; + sock->n_window = PATTY_AX25_FRAME_DEFAULT_WINDOW; + + return sock; + +error_malloc_sock: + return NULL; +} + +void patty_ax25_sock_destroy(patty_ax25_sock *sock) { + free(sock); +} diff --git a/src/testclient.c b/src/testclient.c index b06286c..89721a8 100644 --- a/src/testclient.c +++ b/src/testclient.c @@ -29,7 +29,8 @@ static void usage(int argc, char **argv, const char *message, ...) { int main(int argc, char **argv) { struct sockaddr_un addr; - int fd; + int fd, + sock; if (argc != 2) { usage(argc, argv, "No patty socket provided"); @@ -50,10 +51,30 @@ int main(int argc, char **argv) { goto error_connect; } + if ((sock = patty_ax25_call_socket(fd, PATTY_AX25_SOCK_PTY)) < 0) { + fprintf(stderr, "%s: %s: %s\n", + argv[0], "patty_ax25_call_socket()", strerror(errno)); + + goto error_call_socket; + } + + if (patty_ax25_call_close(fd, sock) < 0) { + fprintf(stderr, "%s: %s: %s\n", + argv[0], "patty_ax25_call_close()", strerror(errno)); + + goto error_call_close; + } + + fprintf(stderr, "got sock %d\n", sock); + close(fd); return 0; +error_call_close: +error_call_socket: + close(fd); + error_connect: error_socket: return 1; diff --git a/src/testserver.c b/src/testserver.c index bbe8f20..abb3548 100644 --- a/src/testserver.c +++ b/src/testserver.c @@ -38,6 +38,10 @@ int main(int argc, char **argv) { goto error_if_new; } + if (patty_ax25_if_addr_add(iface, "KZ3ROX", 0) < 0) { + goto error_if_addr_add; + } + if (patty_ax25_server_add_if(server, iface) < 0) { goto error_server_add_if; } @@ -55,6 +59,7 @@ int main(int argc, char **argv) { error_server_run: error_server_add_if: +error_if_addr_add: error_if_new: patty_ax25_server_destroy(server);