From 1abc0b6c98e0db0af4284ef356d2e422148d4e2e Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sat, 25 Jul 2015 19:39:35 -0500 Subject: [PATCH] Implement patty_ax25_listen() --- include/patty/ax25.h | 8 ++++---- include/patty/ax25/port.h | 5 +++-- src/ax25.c | 39 +++++++++++++++++++++++++++++++++++++++ src/port.c | 20 ++++++++++++++++++-- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/include/patty/ax25.h b/include/patty/ax25.h index 23aacde..9e0aeda 100644 --- a/include/patty/ax25.h +++ b/include/patty/ax25.h @@ -28,10 +28,10 @@ enum patty_ax25_event { typedef struct _patty_ax25_sock { enum patty_ax25_obj_type type; - int iface; - int address; - int port; - int link; + patty_ax25_if * iface; + patty_ax25_address * address; + patty_ax25_port * port; + patty_ax25_link * link; } patty_ax25_sock; struct _patty_ax25 { diff --git a/include/patty/ax25/port.h b/include/patty/ax25/port.h index 0d3c4a9..f838574 100644 --- a/include/patty/ax25/port.h +++ b/include/patty/ax25/port.h @@ -10,11 +10,12 @@ typedef struct _patty_ax25_port { int ssid; } patty_ax25_port; -patty_ax25_port *patty_ax25_port_create(patty_ax25_if *iface, - const char *callsign, int ssid); +patty_ax25_port *patty_ax25_port_create(const char *callsign, int ssid); void patty_ax25_port_destroy(patty_ax25_port *port); +int patty_ax25_port_bind(patty_ax25_port *port, patty_ax25_if *iface); + int patty_ax25_each_port(patty_ax25 *ax25, int (*callback)(patty_ax25_port *, void *), void *ctx); diff --git a/src/ax25.c b/src/ax25.c index 4fdf85a..0ac717b 100644 --- a/src/ax25.c +++ b/src/ax25.c @@ -87,3 +87,42 @@ error_dict_set: error_malloc_sock: return -1; } + +int patty_ax25_listen(patty_ax25 *ax25, int socket, const char *callsign, int ssid) { + patty_ax25_port *port; + patty_ax25_sock *sock; + + if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) { + goto error_dict_get; + } + + /* + * If there is already a port associated with this socket, then that's a + * problem. + */ + if (sock->port != NULL) { + errno = EBUSY; + + goto error_busy; + } + + if ((port = patty_ax25_port_create(callsign, ssid)) == NULL) { + goto error_port_create; + } + + if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), port) == NULL) { + goto error_dict_set; + } + + sock->port = port; + + return ax25->fd++; + +error_dict_set: + patty_ax25_port_destroy(port); + +error_port_create: +error_busy: +error_dict_get: + return -1; +} diff --git a/src/port.c b/src/port.c index 587791e..2480d93 100644 --- a/src/port.c +++ b/src/port.c @@ -1,9 +1,10 @@ #include #include +#include #include -patty_ax25_port *patty_ax25_port_create(patty_ax25_if *iface, const char *callsign, int ssid) { +patty_ax25_port *patty_ax25_port_create(const char *callsign, int ssid) { patty_ax25_port *port; if ((port = malloc(sizeof(*port))) == NULL) { @@ -11,8 +12,8 @@ patty_ax25_port *patty_ax25_port_create(patty_ax25_if *iface, const char *callsi } port->type = PATTY_AX25_OBJ_PORT; - port->iface = iface; port->ssid = ssid; + port->iface = NULL; strncpy(port->callsign, callsign, sizeof(port->callsign)); @@ -26,6 +27,21 @@ void patty_ax25_port_destroy(patty_ax25_port *port) { free(port); } +int patty_ax25_port_bind(patty_ax25_port *port, patty_ax25_if *iface) { + if (port->iface != NULL) { + errno = EBUSY; + + goto error_busy; + } + + port->iface = iface; + + return 0; + +error_busy: + return -1; +} + int patty_ax25_each_port(patty_ax25 *ax25, int (*callback)(patty_ax25_port *, void *), void *ctx) { patty_list_iterator *iter; patty_ax25_port *port;