From 6adb4f098b9f39e58f008db257d369522338e527 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Tue, 28 Jul 2015 02:16:56 +0000 Subject: [PATCH] I think that's much nicer --- include/patty/ax25.h | 8 ++- include/patty/ax25/address.h | 3 ++ include/patty/ax25/defs.h | 8 --- include/patty/ax25/if.h | 5 +- src/ax25.c | 69 ++++++++++++++++++-------- src/frame.c | 4 +- src/hash.c | 2 +- src/if.c | 4 +- src/test.c | 94 +++++++++++++++++------------------- 9 files changed, 109 insertions(+), 88 deletions(-) diff --git a/include/patty/ax25.h b/include/patty/ax25.h index 8a2beca..acb6130 100644 --- a/include/patty/ax25.h +++ b/include/patty/ax25.h @@ -23,8 +23,14 @@ enum patty_ax25_event { PATTY_AX25_IO_WRITE }; +enum patty_ax25_sock_status { + PATTY_AX25_SOCK_OPEN, + PATTY_AX25_SOCK_LISTENING, + PATTY_AX25_SOCK_ESTABLISHED +}; + typedef struct _patty_ax25_sock { - enum patty_ax25_obj_type type; + enum patty_ax25_sock_status status; patty_ax25_if * iface; patty_ax25_address * local; diff --git a/include/patty/ax25/address.h b/include/patty/ax25/address.h index 3919830..c4afbbb 100644 --- a/include/patty/ax25/address.h +++ b/include/patty/ax25/address.h @@ -4,6 +4,9 @@ #include #include +#define PATTY_AX25_ADDRESS_CALLSIGN_LEN 6 +#define PATTY_AX25_ADDRESS_SIZE 7 + typedef struct _patty_ax25_address { char callsign[7]; diff --git a/include/patty/ax25/defs.h b/include/patty/ax25/defs.h index af3a726..76c6d57 100644 --- a/include/patty/ax25/defs.h +++ b/include/patty/ax25/defs.h @@ -6,14 +6,6 @@ #include #include -enum patty_ax25_obj_type { - PATTY_AX25_OBJ_UNKNOWN, - PATTY_AX25_OBJ_IF, - PATTY_AX25_OBJ_SOCKET, - PATTY_AX25_OBJ_PORT, - PATTY_AX25_OBJ_LINK -}; - typedef struct _patty_ax25_stats { size_t rx; size_t tx; diff --git a/include/patty/ax25/if.h b/include/patty/ax25/if.h index 61c1a5e..09cd200 100644 --- a/include/patty/ax25/if.h +++ b/include/patty/ax25/if.h @@ -21,9 +21,8 @@ enum patty_ax25_if_type { }; typedef struct _patty_ax25_if { - enum patty_ax25_obj_type type; - enum patty_ax25_if_type type_if; - patty_ax25_stats stats; + enum patty_ax25_if_type type; + patty_ax25_stats stats; char name[8]; diff --git a/src/ax25.c b/src/ax25.c index c223300..89917a8 100644 --- a/src/ax25.c +++ b/src/ax25.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -34,12 +33,46 @@ error_list_new_ifaces: return -1; } +static int destroy_if(patty_ax25_if *iface, void *ctx) { + patty_ax25_if_destroy(iface); + + return 0; +} + void patty_ax25_stop(patty_ax25 *ax25) { patty_dict_destroy(ax25->fd_lookup); patty_list_destroy(ax25->socks); + + patty_ax25_each_if(ax25, destroy_if, NULL); patty_list_destroy(ax25->ifaces); } +static void *fd_lookup(patty_ax25 *ax25, int fd) { + void *obj; + + if ((obj = patty_dict_get(ax25->fd_lookup, &fd, sizeof(fd))) == NULL) { + errno = EBADF; + + goto error_dict_get; + } + + return obj; + +error_dict_get: + return NULL; +} + +static int fd_store(patty_ax25 *ax25, void *obj) { + if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), obj) == NULL) { + goto error_dict_set; + } + + return ax25->fd++; + +error_dict_set: + return -1; +} + int patty_ax25_open(patty_ax25 *ax25, const char *ifname) { patty_ax25_if *iface; @@ -47,19 +80,15 @@ int patty_ax25_open(patty_ax25 *ax25, const char *ifname) { goto error_get_if; } - if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), iface) == NULL) { - goto error_dict_set; - } + return fd_store(ax25, iface); - return ax25->fd++; - -error_dict_set: error_get_if: return -1; } int patty_ax25_socket(patty_ax25 *ax25) { patty_ax25_sock *sock; + int fd; if ((sock = malloc(sizeof(*sock))) == NULL) { goto error_malloc_sock; @@ -67,13 +96,15 @@ int patty_ax25_socket(patty_ax25 *ax25) { memset(sock, '\0', sizeof(*sock)); - if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), sock) == NULL) { - goto error_dict_set; + sock->status = PATTY_AX25_SOCK_OPEN; + + if ((fd = fd_store(ax25, sock)) < 0) { + goto error_fd_store; } - return ax25->fd++; + return fd; -error_dict_set: +error_fd_store: free(sock); error_malloc_sock: @@ -83,10 +114,8 @@ error_malloc_sock: int patty_ax25_bind(patty_ax25 *ax25, int socket, const char *callsign, int ssid) { patty_ax25_sock *sock; - if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) { - errno = EBADF; - - goto error_dict_get; + if ((sock = fd_lookup(ax25, socket)) == NULL) { + goto error_fd_lookup; } /* @@ -107,7 +136,7 @@ int patty_ax25_bind(patty_ax25 *ax25, int socket, const char *callsign, int ssid error_address_create: error_exists: -error_dict_get: +error_fd_lookup: return -1; } @@ -121,10 +150,8 @@ int patty_ax25_listen(patty_ax25 *ax25, int socket, const char *callsign, int ss int patty_ax25_connect(patty_ax25 *ax25, int socket, const char *callsign, int ssid) { patty_ax25_sock *sock; - if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) { - errno = EBADF; - - goto error_dict_get; + if ((sock = fd_lookup(ax25, socket)) == NULL) { + goto error_fd_lookup; } if (sock->remote != NULL) { @@ -141,6 +168,6 @@ int patty_ax25_connect(patty_ax25 *ax25, int socket, const char *callsign, int s error_address_create: error_exists: -error_dict_get: +error_fd_lookup: return -1; } diff --git a/src/frame.c b/src/frame.c index bc0ca2a..ad149a1 100644 --- a/src/frame.c +++ b/src/frame.c @@ -11,7 +11,7 @@ static ssize_t frame_decode_station(patty_ax25_address *address, void *data, off * First, unpack each callsign octet, ensuring all are within the 7 bit * ASCII space and do not have the extended bit set to 1. */ - for (i=0; i<6; i++) { + for (i=0; i> 1; @@ -53,7 +53,7 @@ static ssize_t frame_decode_station(patty_ax25_address *address, void *data, off address->ssid = PATTY_AX25_ADDRESS_SSID_NUMBER(ssid); address->last = PATTY_AX25_ADDRESS_OCTET_LAST(ssid); - return 7; + return PATTY_AX25_ADDRESS_SIZE; error: return -1; diff --git a/src/hash.c b/src/hash.c index 5e42293..5898fc4 100644 --- a/src/hash.c +++ b/src/hash.c @@ -4,7 +4,7 @@ #include uint32_t patty_hash(void *key, size_t size) { - uint32_t hash = 0, i; + uint32_t hash = 0xffffffdf, i; for (i=0; itype_if = PATTY_AX25_IF_KISS_TNC_TTY; + iface->type = PATTY_AX25_IF_KISS_TNC_TTY; snprintf(iface->name, sizeof(iface->name), "%s%d", prefix, number++); @@ -63,8 +63,6 @@ patty_ax25_if *patty_ax25_if_create(int opts, void *info) { goto error; } - iface->type = PATTY_AX25_OBJ_IF; - return iface; error: diff --git a/src/test.c b/src/test.c index ffbfaab..f22c3ea 100644 --- a/src/test.c +++ b/src/test.c @@ -22,72 +22,68 @@ static void usage(int argc, char **argv, const char *message, ...) { exit(1); } -static int callsign_fprint(FILE *stream, const patty_ax25_address *address) { - fprintf(stream, "%s", address->callsign); - - if (address->ssid) { - fprintf(stream, "/%d", address->ssid); - } - - return 0; +int address_callback(patty_ax25_address *address, void *ctx) { + return printf(" %s/%d\n", address->callsign, address->ssid); } -static int address_fprint(FILE *stream, const patty_ax25_frame *frame) { - int i; +int iface_callback(patty_ax25_if *iface, void *ctx) { + printf("%s:\n", iface->name); - callsign_fprint(stream, &frame->src); - fprintf(stream, " > "); - callsign_fprint(stream, &frame->dest); - - for (i=0; ihops; i++) { - fprintf(stream, " < "); - callsign_fprint(stream, &frame->repeaters[i]); - } - - return 0; + return patty_ax25_if_each_address(iface, address_callback, ctx); } int main(int argc, char **argv) { - patty_kiss_tnc *tnc; - ssize_t len; - void *data; - int port; - + patty_ax25 ax25; + patty_ax25_if *iface; + int fd; + if (argc != 2) { usage(argc, argv, "No TNC device provided"); } - if ((tnc = patty_kiss_tnc_open(argv[1], 330)) == NULL) { - perror("Unable to open TNC"); + if (patty_ax25_init(&ax25) < 0) { + perror("Unable to initialize AX.25 stack"); exit(127); } - while ((len = patty_kiss_tnc_recv(tnc, &data, &port)) > 0) { - patty_ax25_frame frame; + if ((iface = patty_ax25_if_create(PATTY_AX25_IF_KISS_TNC_TTY, argv[1])) == NULL) { + perror("Unable to create interface"); - if (patty_ax25_frame_decode(&frame, data, len) < 0) { - perror("Unable to decode frame"); - - exit(127); - } - - address_fprint(stderr, &frame); - - if (frame.type == PATTY_AX25_FRAME_INFO) { - fprintf(stderr, " poll %d", PATTY_AX25_CONTROL_POLL(frame.control)); - fprintf(stderr, " ns %d", PATTY_AX25_CONTROL_SEQ_SEND(frame.control)); - fprintf(stderr, " nr %d", PATTY_AX25_CONTROL_SEQ_RECV(frame.control)); - } - - if (frame.payload) { - fprintf(stderr, " proto %02x", frame.proto); - } - - fprintf(stderr, " %ld bytes\n", len); + exit(127); } - patty_kiss_tnc_close(tnc); + if (patty_ax25_if_add_address(iface, "WX3RKR", 0) < 0) { + perror("Unable to add address"); + + exit(127); + } + + if (patty_ax25_add_if(&ax25, iface) < 0) { + perror("Unable to add interface"); + + exit(127); + } + + if ((fd = patty_ax25_socket(&ax25)) < 0) { + perror("Unable to create new socket"); + + exit(127); + } + + if (patty_ax25_bind(&ax25, fd, "WX3RKR", 0) < 0) { + perror("Unable to bind socket to local address"); + + exit(127); + } + + if (patty_ax25_connect(&ax25, fd, "N5TX", 0) < 0) { + perror("Unable to connect"); + + exit(127); + } + + patty_ax25_stop(&ax25); return 0; }