Refactor around the simplified concept of patty_ax25_sock
This commit is contained in:
parent
1abc0b6c98
commit
0f639cc350
6 changed files with 31 additions and 142 deletions
|
@ -14,8 +14,6 @@
|
||||||
#include <patty/ax25/address.h>
|
#include <patty/ax25/address.h>
|
||||||
#include <patty/ax25/frame.h>
|
#include <patty/ax25/frame.h>
|
||||||
#include <patty/ax25/if.h>
|
#include <patty/ax25/if.h>
|
||||||
#include <patty/ax25/port.h>
|
|
||||||
#include <patty/ax25/link.h>
|
|
||||||
|
|
||||||
enum patty_ax25_event {
|
enum patty_ax25_event {
|
||||||
PATTY_AX25_IO_UNKNOWN,
|
PATTY_AX25_IO_UNKNOWN,
|
||||||
|
@ -29,15 +27,13 @@ typedef struct _patty_ax25_sock {
|
||||||
enum patty_ax25_obj_type type;
|
enum patty_ax25_obj_type type;
|
||||||
|
|
||||||
patty_ax25_if * iface;
|
patty_ax25_if * iface;
|
||||||
patty_ax25_address * address;
|
patty_ax25_address * local;
|
||||||
patty_ax25_port * port;
|
patty_ax25_address * remote;
|
||||||
patty_ax25_link * link;
|
|
||||||
} patty_ax25_sock;
|
} patty_ax25_sock;
|
||||||
|
|
||||||
struct _patty_ax25 {
|
struct _patty_ax25 {
|
||||||
patty_list * ifaces;
|
patty_list * ifaces;
|
||||||
patty_list * ports;
|
patty_list * socks;
|
||||||
patty_list * links;
|
|
||||||
|
|
||||||
patty_dict * fd_lookup;
|
patty_dict * fd_lookup;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
#ifndef _PATTY_AX25_LINK_H
|
|
||||||
#define _PATTY_AX25_LINK_H
|
|
||||||
|
|
||||||
typedef struct _patty_ax25_link {
|
|
||||||
enum patty_ax25_if_type type;
|
|
||||||
patty_ax25_stats stats;
|
|
||||||
|
|
||||||
patty_ax25_if * iface;
|
|
||||||
patty_ax25_port * local;
|
|
||||||
patty_ax25_port * remote;
|
|
||||||
} patty_ax25_link;
|
|
||||||
|
|
||||||
#endif /* _PATTY_AX25_LINK_H */
|
|
|
@ -1,22 +0,0 @@
|
||||||
#ifndef _PATTY_AX25_PORT_H
|
|
||||||
#define _PATTY_AX25_PORT_H
|
|
||||||
|
|
||||||
typedef struct _patty_ax25_port {
|
|
||||||
enum patty_ax25_obj_type type;
|
|
||||||
|
|
||||||
patty_ax25_if *iface;
|
|
||||||
|
|
||||||
char callsign[7];
|
|
||||||
int ssid;
|
|
||||||
} patty_ax25_port;
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
#endif /* _PATTY_AX25_PORT_H */
|
|
|
@ -10,7 +10,7 @@ LDFLAGS =
|
||||||
HEADERS = kiss.h ax25.h ax25/if.h ax25/macros.h ax25/proto.h \
|
HEADERS = kiss.h ax25.h ax25/if.h ax25/macros.h ax25/proto.h \
|
||||||
ax25/frame.h list.h hash.h dict.h
|
ax25/frame.h list.h hash.h dict.h
|
||||||
|
|
||||||
OBJS = kiss.o ax25.o if.o port.o frame.o list.o hash.o dict.o test.o
|
OBJS = kiss.o ax25.o if.o frame.o list.o hash.o dict.o test.o
|
||||||
|
|
||||||
PROGRAM = test
|
PROGRAM = test
|
||||||
|
|
||||||
|
|
58
src/ax25.c
58
src/ax25.c
|
@ -12,12 +12,8 @@ int patty_ax25_init(patty_ax25 *ax25) {
|
||||||
goto error_list_new_ifaces;
|
goto error_list_new_ifaces;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((ax25->ports = patty_list_new()) == NULL) {
|
if ((ax25->socks = patty_list_new()) == NULL) {
|
||||||
goto error_list_new_ports;
|
goto error_list_new_socks;
|
||||||
}
|
|
||||||
|
|
||||||
if ((ax25->links = patty_list_new()) == NULL) {
|
|
||||||
goto error_list_new_links;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((ax25->fd_lookup = patty_dict_new()) == NULL) {
|
if ((ax25->fd_lookup = patty_dict_new()) == NULL) {
|
||||||
|
@ -29,12 +25,9 @@ int patty_ax25_init(patty_ax25 *ax25) {
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_dict_new_fd_lookup:
|
error_dict_new_fd_lookup:
|
||||||
patty_list_destroy(ax25->links);
|
patty_list_destroy(ax25->socks);
|
||||||
|
|
||||||
error_list_new_links:
|
error_list_new_socks:
|
||||||
patty_list_destroy(ax25->ports);
|
|
||||||
|
|
||||||
error_list_new_ports:
|
|
||||||
patty_list_destroy(ax25->ifaces);
|
patty_list_destroy(ax25->ifaces);
|
||||||
|
|
||||||
error_list_new_ifaces:
|
error_list_new_ifaces:
|
||||||
|
@ -43,8 +36,7 @@ error_list_new_ifaces:
|
||||||
|
|
||||||
void patty_ax25_stop(patty_ax25 *ax25) {
|
void patty_ax25_stop(patty_ax25 *ax25) {
|
||||||
patty_dict_destroy(ax25->fd_lookup);
|
patty_dict_destroy(ax25->fd_lookup);
|
||||||
patty_list_destroy(ax25->links);
|
patty_list_destroy(ax25->socks);
|
||||||
patty_list_destroy(ax25->ports);
|
|
||||||
patty_list_destroy(ax25->ifaces);
|
patty_list_destroy(ax25->ifaces);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,40 +81,44 @@ error_malloc_sock:
|
||||||
}
|
}
|
||||||
|
|
||||||
int patty_ax25_listen(patty_ax25 *ax25, int socket, const char *callsign, int ssid) {
|
int patty_ax25_listen(patty_ax25 *ax25, int socket, const char *callsign, int ssid) {
|
||||||
patty_ax25_port *port;
|
|
||||||
patty_ax25_sock *sock;
|
patty_ax25_sock *sock;
|
||||||
|
patty_ax25_address *addr;
|
||||||
|
|
||||||
if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) {
|
if ((sock = patty_dict_get(ax25->fd_lookup, &socket, sizeof(socket))) == NULL) {
|
||||||
goto error_dict_get;
|
goto error_dict_get;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If there is already a port associated with this socket, then that's a
|
* If there is already a local address associated with this socket, then
|
||||||
* problem.
|
* that's a problem.
|
||||||
*/
|
*/
|
||||||
if (sock->port != NULL) {
|
if (sock->local != NULL) {
|
||||||
errno = EBUSY;
|
errno = EEXIST;
|
||||||
|
|
||||||
goto error_busy;
|
goto error_exists;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((port = patty_ax25_port_create(callsign, ssid)) == NULL) {
|
if ((addr = malloc(sizeof(*addr))) == NULL) {
|
||||||
goto error_port_create;
|
goto error_malloc_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (patty_dict_set(ax25->fd_lookup, &ax25->fd, sizeof(ax25->fd), port) == NULL) {
|
strncpy(addr->callsign, callsign, sizeof(addr->callsign));
|
||||||
goto error_dict_set;
|
|
||||||
}
|
|
||||||
|
|
||||||
sock->port = port;
|
addr->ssid = ssid;
|
||||||
|
addr->last = 0;
|
||||||
|
addr->c = 0;
|
||||||
|
|
||||||
return ax25->fd++;
|
return 0;
|
||||||
|
|
||||||
error_dict_set:
|
error_malloc_addr:
|
||||||
patty_ax25_port_destroy(port);
|
error_exists:
|
||||||
|
|
||||||
error_port_create:
|
|
||||||
error_busy:
|
|
||||||
error_dict_get:
|
error_dict_get:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int patty_ax25_connect(patty_ax25 *ax25, int socket, const char *callsign, int ssid) {
|
||||||
|
/*
|
||||||
|
* Stub
|
||||||
|
*/
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
68
src/port.c
68
src/port.c
|
@ -1,68 +0,0 @@
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#include <patty/ax25.h>
|
|
||||||
|
|
||||||
patty_ax25_port *patty_ax25_port_create(const char *callsign, int ssid) {
|
|
||||||
patty_ax25_port *port;
|
|
||||||
|
|
||||||
if ((port = malloc(sizeof(*port))) == NULL) {
|
|
||||||
goto error_malloc_port;
|
|
||||||
}
|
|
||||||
|
|
||||||
port->type = PATTY_AX25_OBJ_PORT;
|
|
||||||
port->ssid = ssid;
|
|
||||||
port->iface = NULL;
|
|
||||||
|
|
||||||
strncpy(port->callsign, callsign, sizeof(port->callsign));
|
|
||||||
|
|
||||||
return port;
|
|
||||||
|
|
||||||
error_malloc_port:
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if ((iter = patty_list_start(ax25->ports)) == NULL) {
|
|
||||||
goto error_list_start;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((port = patty_list_next(iter)) != NULL) {
|
|
||||||
if (callback(port, ctx) < 0) {
|
|
||||||
goto error_callback;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
patty_list_finish(iter);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
error_callback:
|
|
||||||
patty_list_finish(iter);
|
|
||||||
|
|
||||||
error_list_start:
|
|
||||||
return -1;
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue