Implement patty_ax25_listen()

This commit is contained in:
XANTRONIX Development 2015-07-25 19:39:35 -05:00 committed by XANTRONIX Industrial
parent c17abcf77a
commit 1abc0b6c98
4 changed files with 64 additions and 8 deletions

View file

@ -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 {

View file

@ -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);

View file

@ -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;
}

View file

@ -1,9 +1,10 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <patty/ax25.h>
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;