Decouple interface names from patty_ax25_if

Changes:

    * Remove interface name argument from patty_ax25_if_new()

    * Add interface name argument to patty_ax25_server_if_add()

    * Add interface name argument to patty_daemon_if_add()

    * In src/server.c, implement 'struct if_entry' to hold the interface
      name, file descriptor, and pointer to a patty_ax25_if object, to
      recouple names to their respective interfaces

    * Add a return argument to patty_bin_if_create() to store a pointer
      to a new interface name created from a declaration
This commit is contained in:
XANTRONIX Development 2020-10-01 00:22:37 -04:00 committed by XANTRONIX Industrial
parent 61c17f8026
commit 88743a5754
9 changed files with 88 additions and 62 deletions

View file

@ -115,8 +115,7 @@ static patty_ax25_if *create_kiss(struct context *ctx, int argc, char **argv) {
} }
} }
return patty_ax25_if_new( ctx->name, return patty_ax25_if_new(&ctx->addr,
&ctx->addr,
patty_kiss_tnc_driver(), patty_kiss_tnc_driver(),
&info); &info);
@ -215,8 +214,7 @@ static patty_ax25_if *create_aprs_is(struct context *ctx,
} }
} }
if ((iface = patty_ax25_if_new( ctx->name, if ((iface = patty_ax25_if_new(&ctx->addr,
&ctx->addr,
patty_ax25_aprs_is_driver(), patty_ax25_aprs_is_driver(),
&info)) == NULL) { &info)) == NULL) {
patty_error_fmt(ctx->err, "Unable to connect to APRS-IS service %s:%s: %s", patty_error_fmt(ctx->err, "Unable to connect to APRS-IS service %s:%s: %s",
@ -243,7 +241,10 @@ struct if_type if_types[] = {
{ NULL, NULL } { NULL, NULL }
}; };
patty_ax25_if *patty_bin_if_create(int argc, char **argv, patty_error *e) { patty_ax25_if *patty_bin_if_create(int argc,
char **argv,
char **ifname,
patty_error *e) {
enum mode mode = MODE_NONE; enum mode mode = MODE_NONE;
struct context ctx = { struct context ctx = {
@ -286,6 +287,8 @@ patty_ax25_if *patty_bin_if_create(int argc, char **argv, patty_error *e) {
for (t=0; if_types[t].name; t++) { for (t=0; if_types[t].name; t++) {
if (strcmp(if_types[t].name, argv[i]) == 0) { if (strcmp(if_types[t].name, argv[i]) == 0) {
*ifname = ctx.name;
return if_types[t].func(&ctx, return if_types[t].func(&ctx,
argc - i - 1, argc - i - 1,
argv + i + 1); argv + i + 1);

View file

@ -98,6 +98,7 @@ static int handle_if(struct context *ctx,
char **argv) { char **argv) {
patty_ax25_if *iface; patty_ax25_if *iface;
patty_error e; patty_error e;
char *ifname = NULL;
if (argc < 2) { if (argc < 2) {
patty_error_fmt(&ctx->e, "line %d: No interface name provided", patty_error_fmt(&ctx->e, "line %d: No interface name provided",
@ -111,7 +112,7 @@ static int handle_if(struct context *ctx,
goto error_invalid; goto error_invalid;
} }
if ((iface = patty_bin_if_create(argc, argv, &e)) == NULL) { if ((iface = patty_bin_if_create(argc, argv, &ifname, &e)) == NULL) {
patty_error_fmt(&ctx->e, "line %d: %s", patty_error_fmt(&ctx->e, "line %d: %s",
lineno, lineno,
patty_error_set(&e)? patty_error_string(&e): strerror(errno)); patty_error_set(&e)? patty_error_string(&e): strerror(errno));
@ -122,17 +123,16 @@ static int handle_if(struct context *ctx,
char *pty; char *pty;
if (isatty(fd) && (pty = ptsname(fd)) != NULL) { if (isatty(fd) && (pty = ptsname(fd)) != NULL) {
printf("if %s pty %s\n", printf("if %s pty %s\n", ifname, pty);
patty_ax25_if_name(iface), pty);
fflush(stdout); fflush(stdout);
} }
} }
if (patty_daemon_if_add(ctx->daemon, iface) < 0) { if (patty_daemon_if_add(ctx->daemon, iface, ifname) < 0) {
patty_error_fmt(&ctx->e, "line %d: Unable to create interface %s: %s", patty_error_fmt(&ctx->e, "line %d: Unable to create interface %s: %s",
lineno, lineno,
patty_ax25_if_name(iface), ifname,
strerror(errno)); strerror(errno));
goto error_daemon_if_add; goto error_daemon_if_add;
@ -303,8 +303,7 @@ static int handle_standalone(patty_daemon *daemon,
goto error_kiss_config; goto error_kiss_config;
} }
if ((iface = patty_ax25_if_new(DEFAULT_IFNAME, if ((iface = patty_ax25_if_new(&addr,
&addr,
patty_kiss_tnc_driver(), patty_kiss_tnc_driver(),
&info)) == NULL) { &info)) == NULL) {
fprintf(stderr, "%s: Unable to create network interface %s: %s\n", fprintf(stderr, "%s: Unable to create network interface %s: %s\n",
@ -320,7 +319,7 @@ static int handle_standalone(patty_daemon *daemon,
} }
} }
if (patty_daemon_if_add(daemon, iface) < 0) { if (patty_daemon_if_add(daemon, iface, DEFAULT_IFNAME) < 0) {
fprintf(stderr, "%s: Unable to add interface %s: %s\n", fprintf(stderr, "%s: Unable to add interface %s: %s\n",
argv0, DEFAULT_IFNAME, strerror(errno)); argv0, DEFAULT_IFNAME, strerror(errno));

View file

@ -65,8 +65,6 @@ typedef void patty_ax25_if_phy;
typedef void patty_ax25_if_info; typedef void patty_ax25_if_info;
typedef struct _patty_ax25_if { typedef struct _patty_ax25_if {
char name[8];
uint32_t flags_classes; uint32_t flags_classes;
void *rx_buf, void *rx_buf,
@ -83,15 +81,12 @@ typedef struct _patty_ax25_if {
patty_ax25_if_phy *phy; patty_ax25_if_phy *phy;
} patty_ax25_if; } patty_ax25_if;
patty_ax25_if *patty_ax25_if_new(const char *name, patty_ax25_if *patty_ax25_if_new(patty_ax25_addr *addr,
patty_ax25_addr *addr,
patty_ax25_if_driver *driver, patty_ax25_if_driver *driver,
patty_ax25_if_info *info); patty_ax25_if_info *info);
void patty_ax25_if_destroy(patty_ax25_if *iface); void patty_ax25_if_destroy(patty_ax25_if *iface);
char *patty_ax25_if_name(patty_ax25_if *iface);
int patty_ax25_if_addr_each(patty_ax25_if *iface, int patty_ax25_if_addr_each(patty_ax25_if *iface,
int (*callback)(patty_ax25_addr *, void *), int (*callback)(patty_ax25_addr *, void *),
void *ctx); void *ctx);

View file

@ -8,7 +8,8 @@ patty_ax25_server *patty_ax25_server_new();
void patty_ax25_server_destroy(patty_ax25_server *server); void patty_ax25_server_destroy(patty_ax25_server *server);
int patty_ax25_server_if_add(patty_ax25_server *server, int patty_ax25_server_if_add(patty_ax25_server *server,
patty_ax25_if *iface); patty_ax25_if *iface,
const char *ifname);
int patty_ax25_server_if_delete(patty_ax25_server *server, int patty_ax25_server_if_delete(patty_ax25_server *server,
const char *ifname); const char *ifname);
@ -17,7 +18,7 @@ patty_ax25_if *patty_ax25_server_if_get(patty_ax25_server *server,
const char *ifname); const char *ifname);
int patty_ax25_server_if_each(patty_ax25_server *server, int patty_ax25_server_if_each(patty_ax25_server *server,
int (*callback)(patty_ax25_if *, void *), int (*callback)(char *, patty_ax25_if *, void *),
void *ctx); void *ctx);
int patty_ax25_server_route_add(patty_ax25_server *server, int patty_ax25_server_route_add(patty_ax25_server *server,

View file

@ -6,6 +6,7 @@
patty_ax25_if *patty_bin_if_create(int argc, patty_ax25_if *patty_bin_if_create(int argc,
char **argv, char **argv,
char **ifname,
patty_error *e); patty_error *e);
#endif /* _PATTY_BIN_IF_H */ #endif /* _PATTY_BIN_IF_H */

View file

@ -17,7 +17,8 @@ int patty_daemon_set_sock_path(patty_daemon *daemon, const char *path);
int patty_daemon_set_pidfile(patty_daemon *daemon, const char *path); int patty_daemon_set_pidfile(patty_daemon *daemon, const char *path);
int patty_daemon_if_add(patty_daemon *daemon, int patty_daemon_if_add(patty_daemon *daemon,
patty_ax25_if *iface); patty_ax25_if *iface,
const char *ifname);
int patty_daemon_route_add(patty_daemon *daemon, int patty_daemon_route_add(patty_daemon *daemon,
const char *ifname, const char *ifname,

View file

@ -117,8 +117,9 @@ error_strdup:
} }
int patty_daemon_if_add(patty_daemon *daemon, int patty_daemon_if_add(patty_daemon *daemon,
patty_ax25_if *iface) { patty_ax25_if *iface,
return patty_ax25_server_if_add(daemon->server, iface); const char *ifname) {
return patty_ax25_server_if_add(daemon->server, iface, ifname);
} }
int patty_daemon_route_add(patty_daemon *daemon, int patty_daemon_route_add(patty_daemon *daemon,

View file

@ -7,8 +7,7 @@
#include <patty/ax25.h> #include <patty/ax25.h>
#include <patty/kiss.h> #include <patty/kiss.h>
patty_ax25_if *patty_ax25_if_new(const char *name, patty_ax25_if *patty_ax25_if_new(patty_ax25_addr *addr,
patty_ax25_addr *addr,
patty_ax25_if_driver *driver, patty_ax25_if_driver *driver,
patty_ax25_if_info *info) { patty_ax25_if_info *info) {
patty_ax25_if *iface; patty_ax25_if *iface;
@ -31,8 +30,6 @@ patty_ax25_if *patty_ax25_if_new(const char *name,
iface->driver = driver; iface->driver = driver;
strncpy(iface->name, name, sizeof(iface->name));
/* /*
* TODO: Eventually inherit the half/full-duplex flag from the PHY this * TODO: Eventually inherit the half/full-duplex flag from the PHY this
* interface is bound to * interface is bound to
@ -96,10 +93,6 @@ void patty_ax25_if_destroy(patty_ax25_if *iface) {
free(iface); free(iface);
} }
char *patty_ax25_if_name(patty_ax25_if *iface) {
return iface->name;
}
int patty_ax25_if_addr_each(patty_ax25_if *iface, int patty_ax25_if_addr_each(patty_ax25_if *iface,
int (*callback)(patty_ax25_addr *, void *), int (*callback)(patty_ax25_addr *, void *),
void *ctx) { void *ctx) {

View file

@ -15,6 +15,12 @@
typedef int (*patty_ax25_server_call)(patty_ax25_server *, int); typedef int (*patty_ax25_server_call)(patty_ax25_server *, int);
typedef struct if_entry {
int fd;
char name[10];
patty_ax25_if *iface;
} if_entry;
struct _patty_ax25_server { struct _patty_ax25_server {
int fd, /* fd of UNIX domain socket */ int fd, /* fd of UNIX domain socket */
fd_max; fd_max;
@ -107,10 +113,21 @@ error_malloc_server:
return NULL; return NULL;
} }
static int destroy_if(patty_ax25_if *iface, void *ctx) { static void destroy_ifaces(patty_list *ifaces) {
patty_ax25_if_destroy(iface); patty_list_item *item = ifaces->first;
return 0; while (item) {
patty_list_item *next = item->next;
struct if_entry *entry = item->value;
patty_ax25_if_destroy(entry->iface);
free(entry);
free(item);
item = next;
}
free(ifaces);
} }
static int destroy_socks_by_client_entry(uint32_t key, void *value, void *ctx) { static int destroy_socks_by_client_entry(uint32_t key, void *value, void *ctx) {
@ -128,9 +145,8 @@ void patty_ax25_server_destroy(patty_ax25_server *server) {
patty_dict_destroy(server->socks_by_client); patty_dict_destroy(server->socks_by_client);
patty_dict_destroy(server->socks_by_fd); patty_dict_destroy(server->socks_by_fd);
patty_ax25_server_if_each(server, destroy_if, NULL);
patty_ax25_route_table_destroy(server->routes); patty_ax25_route_table_destroy(server->routes);
patty_list_destroy(server->ifaces); destroy_ifaces(server->ifaces);
free(server); free(server);
} }
@ -429,20 +445,35 @@ static inline void sock_flow_start(patty_ax25_server *server,
} }
int patty_ax25_server_if_add(patty_ax25_server *server, int patty_ax25_server_if_add(patty_ax25_server *server,
patty_ax25_if *iface) { patty_ax25_if *iface,
int fd; const char *name) {
struct if_entry *entry;
if ((fd = patty_ax25_if_fd(iface)) >= 0) { if ((entry = malloc(sizeof(*entry))) == NULL) {
fd_watch(server, fd); goto error_malloc_entry;
} }
if (patty_list_append(server->ifaces, iface) == NULL) { if ((entry->fd = patty_ax25_if_fd(iface)) < 0) {
goto error_if_fd;
}
strncpy(entry->name, name, sizeof(entry->name));
entry->iface = iface;
if (patty_list_append(server->ifaces, entry) == NULL) {
goto error_list_append; goto error_list_append;
} }
fd_watch(server, entry->fd);
return 0; return 0;
error_list_append: error_list_append:
error_if_fd:
free(entry);
error_malloc_entry:
return -1; return -1;
} }
@ -450,20 +481,20 @@ int patty_ax25_server_if_delete(patty_ax25_server *server,
const char *ifname) { const char *ifname) {
patty_list_item *item = server->ifaces->first; patty_list_item *item = server->ifaces->first;
int fd, i = 0; int i = 0;
while (item) { while (item) {
patty_ax25_if *iface = item->value; struct if_entry *entry = item->value;
if (strncmp(iface->name, ifname, sizeof(iface->name)) == 0) { if (strncmp(entry->name, ifname, sizeof(entry->name)) == 0) {
if ((fd = patty_ax25_if_fd(iface)) >= 0) { fd_clear(server, entry->fd);
fd_clear(server, fd);
}
if (patty_list_splice(server->ifaces, i) == NULL) { if (patty_list_splice(server->ifaces, i) == NULL) {
goto error_list_splice; goto error_list_splice;
} }
free(entry);
return 0; return 0;
} }
@ -482,10 +513,10 @@ patty_ax25_if *patty_ax25_server_if_get(patty_ax25_server *server,
patty_list_item *item = server->ifaces->first; patty_list_item *item = server->ifaces->first;
while (item) { while (item) {
patty_ax25_if *iface = item->value; struct if_entry *entry = item->value;
if (strncmp(iface->name, name, sizeof(iface->name)) == 0) { if (strncmp(entry->name, name, sizeof(entry->name)) == 0) {
return iface; return entry->iface;
} }
item = item->next; item = item->next;
@ -495,14 +526,14 @@ patty_ax25_if *patty_ax25_server_if_get(patty_ax25_server *server,
} }
int patty_ax25_server_if_each(patty_ax25_server *server, int patty_ax25_server_if_each(patty_ax25_server *server,
int (*callback)(patty_ax25_if *, void *), int (*callback)(char *, patty_ax25_if *, void *),
void *ctx) { void *ctx) {
patty_list_item *item = server->ifaces->first; patty_list_item *item = server->ifaces->first;
while (item) { while (item) {
patty_ax25_if *iface = item->value; struct if_entry *entry = item->value;
if (callback(iface, ctx) < 0) { if (callback(entry->name, entry->iface, ctx) < 0) {
goto error_callback; goto error_callback;
} }
@ -2002,20 +2033,21 @@ error_decode:
return 0; return 0;
} }
static int handle_iface(patty_ax25_server *server, patty_ax25_if *iface) { static int handle_iface(patty_ax25_server *server, struct if_entry *entry) {
int fd = patty_ax25_if_fd(iface); patty_ax25_if *iface = entry->iface;
ssize_t len; ssize_t len;
if (!FD_ISSET(fd, &server->fds_r)) { if (!FD_ISSET(entry->fd, &server->fds_r)) {
goto done; goto done;
} }
if ((len = patty_ax25_if_fill(iface)) < 0) { if ((len = patty_ax25_if_fill(entry->iface)) < 0) {
goto error_io; goto error_io;
} else if (len == 0) { } else if (len == 0) {
close(fd); close(entry->fd);
fd_clear(server, fd); fd_clear(server, entry->fd);
goto done; goto done;
} }
@ -2052,9 +2084,9 @@ static int handle_ifaces(patty_ax25_server *server) {
patty_list_item *item = server->ifaces->first; patty_list_item *item = server->ifaces->first;
while (item) { while (item) {
patty_ax25_if *iface = item->value; struct if_entry *entry = item->value;
if (handle_iface(server, iface) < 0) { if (handle_iface(server, entry) < 0) {
goto error_io; goto error_io;
} }