From e85df9e29edf98b96ed072ee6115d1ef4648b90f Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sat, 25 Jul 2015 00:26:23 -0500 Subject: [PATCH] Well, it does compile --- include/patty/ax25/defs.h | 9 +- include/patty/ax25/if.h | 2 + include/patty/list.h | 2 + src/ax25.c | 9 -- src/if.c | 194 ++++++++++++++++++++++++++++++++++++++ src/list.c | 30 ++++++ 6 files changed, 236 insertions(+), 10 deletions(-) diff --git a/include/patty/ax25/defs.h b/include/patty/ax25/defs.h index e4b2b74..70dfdc5 100644 --- a/include/patty/ax25/defs.h +++ b/include/patty/ax25/defs.h @@ -18,6 +18,13 @@ typedef struct _patty_ax25_stats { size_t dropped; } patty_ax25_stats; -typedef struct _patty_ax25 patty_ax25; +typedef struct _patty_ax25 { + patty_list * ifaces; + patty_list * ports; + patty_list * links; + + patty_dict * fd_lookup; + int fd; +} patty_ax25; #endif /* _PATTY_AX25_DEFS_H */ diff --git a/include/patty/ax25/if.h b/include/patty/ax25/if.h index e22ccbf..09cd200 100644 --- a/include/patty/ax25/if.h +++ b/include/patty/ax25/if.h @@ -24,6 +24,8 @@ typedef struct _patty_ax25_if { enum patty_ax25_if_type type; patty_ax25_stats stats; + char name[8]; + patty_kiss_tnc * tnc; patty_list * addresses; } patty_ax25_if; diff --git a/include/patty/list.h b/include/patty/list.h index 6e67f72..8a6f82a 100644 --- a/include/patty/list.h +++ b/include/patty/list.h @@ -44,6 +44,8 @@ void *patty_list_last(patty_list *list); void *patty_list_pop(patty_list *list); +void *patty_list_splice(patty_list *list, off_t index); + void *patty_list_insert(patty_list *list, off_t index); void *patty_list_index(patty_list *list, off_t index); diff --git a/src/ax25.c b/src/ax25.c index 9f605c3..00273b6 100644 --- a/src/ax25.c +++ b/src/ax25.c @@ -5,15 +5,6 @@ #include -struct _patty_ax25 { - patty_list * ifaces; - patty_list * ports; - patty_list * links; - - patty_dict * fd_lookup; - int fd; -}; - int patty_ax25_init(patty_ax25 *ax25) { memset(ax25, '\0', sizeof(*ax25)); diff --git a/src/if.c b/src/if.c index 91ae4dd..204f63b 100644 --- a/src/if.c +++ b/src/if.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,6 +8,9 @@ static patty_ax25_if *create_tnc(const char *device) { patty_ax25_if *iface; + static char * prefix = "kiss"; + static int number = 0; + if ((iface = malloc(sizeof(*iface))) == NULL) { goto error_malloc_iface; } @@ -19,6 +23,8 @@ static patty_ax25_if *create_tnc(const char *device) { iface->type = PATTY_AX25_IF_KISS_TNC_TTY; + snprintf(iface->name, sizeof(iface->name), "%s%d", prefix, number++); + return iface; error_kiss_tnc_open: @@ -72,3 +78,191 @@ void patty_ax25_if_destroy(patty_ax25_if *iface) { default: break; } } + +int patty_ax25_if_each_address(patty_ax25_if *iface, int (*callback)(patty_ax25_address *, void *), void *ctx) { + patty_list_iterator *iter; + patty_ax25_address *address; + + if ((iter = patty_list_start(iface->addresses)) == NULL) { + goto error_list_start; + } + + while ((address = patty_list_next(iter)) != NULL) { + if (callback(address, ctx) < 0) { + goto error_callback; + } + } + + patty_list_finish(iter); + + return 0; + +error_callback: + patty_list_finish(iter); + +error_list_start: + return -1; +} + +static patty_ax25_address *find_address(patty_ax25_if *iface, const char *callsign, int ssid) { + patty_list_iterator *iter; + patty_ax25_address *address; + + if ((iter = patty_list_start(iface->addresses)) == NULL) { + goto error_list_start; + } + + while ((address = patty_list_next(iter)) != NULL) { + if (strncmp(address->callsign, callsign, sizeof(address->callsign)) != 0) { + continue; + } + + if (address->ssid != ssid) { + continue; + } + + patty_list_finish(iter); + + return address; + } + + patty_list_finish(iter); + +error_list_start: + return NULL; +} + +int patty_ax25_if_add_address(patty_ax25_if *iface, const char *callsign, int ssid) { + patty_ax25_address *address; + + if (find_address(iface, callsign, ssid) != NULL) { + errno = EEXIST; + + goto error_exists; + } + + if ((address = malloc(sizeof(*address))) == NULL) { + goto error_malloc_address; + } + + memset(address, '\0', sizeof(*address)); + + strncpy(address->callsign, callsign, sizeof(address->callsign)); + + address->ssid = ssid; + + if ((patty_list_append(iface->addresses, address)) == NULL) { + goto error_list_append; + } + + return 0; + +error_list_append: + free(address); + +error_malloc_address: +error_exists: + return -1; +} + +int patty_ax25_if_delete_address(patty_ax25_if *iface, const char *callsign, int ssid) { + patty_list_item *item = iface->addresses->first; + int i = 0; + + while (item) { + patty_ax25_address *address = item->value; + + if (strncmp(address->callsign, callsign, sizeof(address->callsign)) == 0 && address->ssid == ssid) { + if (patty_list_splice(iface->addresses, i) == NULL) { + goto error_list_splice; + } + } + + item = item->next; + i++; + } + + return 0; + +error_list_splice: + return -1; +} + +patty_ax25_if *patty_ax25_get_if(patty_ax25 *ax25, const char *name) { + patty_list_iterator *iter; + patty_ax25_if *iface; + + if ((iter = patty_list_start(ax25->ifaces)) == NULL) { + goto error_list_start; + } + + while ((iface = patty_list_next(iter)) != NULL) { + if (strncmp(iface->name, name, sizeof(iface->name)) == 0) { + patty_list_finish(iter); + + return iface; + } + } + + patty_list_finish(iter); + +error_list_start: + return NULL; +} + +int patty_ax25_each_if(patty_ax25 *ax25, int (*callback)(patty_ax25_if *, void *), void *ctx) { + patty_list_iterator *iter; + patty_ax25_if *iface; + + if ((iter = patty_list_start(ax25->ifaces)) == NULL) { + goto error_list_start; + } + + while ((iface = patty_list_next(iter)) != NULL) { + if (callback(iface, ctx) < 0) { + goto error_callback; + } + } + + patty_list_finish(iter); + + return 0; + +error_callback: + patty_list_finish(iter); + +error_list_start: + return -1; +} + +int patty_ax25_add_if(patty_ax25 *ax25, patty_ax25_if *iface) { + if (patty_list_append(ax25->ifaces, iface) == NULL) { + goto error_list_append; + } + + return 0; + +error_list_append: + return -1; +} + +int patty_ax25_delete_if(patty_ax25 *ax25, patty_ax25_if *iface) { + patty_list_item *item = ax25->ifaces->first; + int i = 0; + + while (item) { + if (item->value == iface) { + if (patty_list_splice(ax25->ifaces, i) == NULL) { + goto error_list_splice; + } + + return 0; + } + + item = item->next; + i++; + } + +error_list_splice: + return -1; +} diff --git a/src/list.c b/src/list.c index da7146b..605fc1a 100644 --- a/src/list.c +++ b/src/list.c @@ -117,6 +117,36 @@ void *patty_list_pop(patty_list *list) { return NULL; } +void *patty_list_splice(patty_list *list, off_t index) { + patty_list_item *item = list->first; + size_t i = 0; + + while (index < 0) { + index += list->length; + } + + while (item) { + if (i == index) { + if (item->prev && item->next) { + item->prev->next = item->next; + } else if (item->prev) { + list->last = item->prev; + item->prev->next = NULL; + } else if (item->next) { + list->first = item->next; + item->next->prev = NULL; + } + + return item->value; + } + + item = item->next; + i++; + } + + return NULL; +} + void *patty_list_index(patty_list *list, off_t index) { patty_list_item *item = list->first; size_t i = 0;