From 88743a57549d2b26e9fab997a59a348578ce4ce3 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Thu, 1 Oct 2020 00:22:37 -0400 Subject: [PATCH] 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 --- bin/if.c | 13 ++++-- bin/pattyd.c | 15 +++--- include/patty/ax25/if.h | 7 +-- include/patty/ax25/server.h | 5 +- include/patty/bin/if.h | 1 + include/patty/daemon.h | 3 +- src/daemon.c | 5 +- src/if.c | 9 +--- src/server.c | 92 +++++++++++++++++++++++++------------ 9 files changed, 88 insertions(+), 62 deletions(-) diff --git a/bin/if.c b/bin/if.c index f313922..9d79ed7 100644 --- a/bin/if.c +++ b/bin/if.c @@ -115,8 +115,7 @@ static patty_ax25_if *create_kiss(struct context *ctx, int argc, char **argv) { } } - return patty_ax25_if_new( ctx->name, - &ctx->addr, + return patty_ax25_if_new(&ctx->addr, patty_kiss_tnc_driver(), &info); @@ -215,8 +214,7 @@ static patty_ax25_if *create_aprs_is(struct context *ctx, } } - if ((iface = patty_ax25_if_new( ctx->name, - &ctx->addr, + if ((iface = patty_ax25_if_new(&ctx->addr, patty_ax25_aprs_is_driver(), &info)) == NULL) { 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 } }; -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; 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++) { if (strcmp(if_types[t].name, argv[i]) == 0) { + *ifname = ctx.name; + return if_types[t].func(&ctx, argc - i - 1, argv + i + 1); diff --git a/bin/pattyd.c b/bin/pattyd.c index dfeb53d..fd84558 100644 --- a/bin/pattyd.c +++ b/bin/pattyd.c @@ -98,6 +98,7 @@ static int handle_if(struct context *ctx, char **argv) { patty_ax25_if *iface; patty_error e; + char *ifname = NULL; if (argc < 2) { 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; } - 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", lineno, patty_error_set(&e)? patty_error_string(&e): strerror(errno)); @@ -122,17 +123,16 @@ static int handle_if(struct context *ctx, char *pty; if (isatty(fd) && (pty = ptsname(fd)) != NULL) { - printf("if %s pty %s\n", - patty_ax25_if_name(iface), pty); + printf("if %s pty %s\n", ifname, pty); 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", lineno, - patty_ax25_if_name(iface), + ifname, strerror(errno)); goto error_daemon_if_add; @@ -303,8 +303,7 @@ static int handle_standalone(patty_daemon *daemon, goto error_kiss_config; } - if ((iface = patty_ax25_if_new(DEFAULT_IFNAME, - &addr, + if ((iface = patty_ax25_if_new(&addr, patty_kiss_tnc_driver(), &info)) == NULL) { 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", argv0, DEFAULT_IFNAME, strerror(errno)); diff --git a/include/patty/ax25/if.h b/include/patty/ax25/if.h index 11cf199..7eddf6a 100644 --- a/include/patty/ax25/if.h +++ b/include/patty/ax25/if.h @@ -65,8 +65,6 @@ typedef void patty_ax25_if_phy; typedef void patty_ax25_if_info; typedef struct _patty_ax25_if { - char name[8]; - uint32_t flags_classes; void *rx_buf, @@ -83,15 +81,12 @@ typedef struct _patty_ax25_if { patty_ax25_if_phy *phy; } patty_ax25_if; -patty_ax25_if *patty_ax25_if_new(const char *name, - patty_ax25_addr *addr, +patty_ax25_if *patty_ax25_if_new(patty_ax25_addr *addr, patty_ax25_if_driver *driver, patty_ax25_if_info *info); 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 (*callback)(patty_ax25_addr *, void *), void *ctx); diff --git a/include/patty/ax25/server.h b/include/patty/ax25/server.h index 4d28342..159e73a 100644 --- a/include/patty/ax25/server.h +++ b/include/patty/ax25/server.h @@ -8,7 +8,8 @@ patty_ax25_server *patty_ax25_server_new(); void patty_ax25_server_destroy(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, const char *ifname); @@ -17,7 +18,7 @@ patty_ax25_if *patty_ax25_server_if_get(patty_ax25_server *server, const char *ifname); 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); int patty_ax25_server_route_add(patty_ax25_server *server, diff --git a/include/patty/bin/if.h b/include/patty/bin/if.h index 36fb584..75cd254 100644 --- a/include/patty/bin/if.h +++ b/include/patty/bin/if.h @@ -6,6 +6,7 @@ patty_ax25_if *patty_bin_if_create(int argc, char **argv, + char **ifname, patty_error *e); #endif /* _PATTY_BIN_IF_H */ diff --git a/include/patty/daemon.h b/include/patty/daemon.h index 9ed2be2..2fb37e9 100644 --- a/include/patty/daemon.h +++ b/include/patty/daemon.h @@ -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_if_add(patty_daemon *daemon, - patty_ax25_if *iface); + patty_ax25_if *iface, + const char *ifname); int patty_daemon_route_add(patty_daemon *daemon, const char *ifname, diff --git a/src/daemon.c b/src/daemon.c index 2e5a793..14045ca 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -117,8 +117,9 @@ error_strdup: } int patty_daemon_if_add(patty_daemon *daemon, - patty_ax25_if *iface) { - return patty_ax25_server_if_add(daemon->server, iface); + patty_ax25_if *iface, + const char *ifname) { + return patty_ax25_server_if_add(daemon->server, iface, ifname); } int patty_daemon_route_add(patty_daemon *daemon, diff --git a/src/if.c b/src/if.c index 0494ab1..97acf34 100644 --- a/src/if.c +++ b/src/if.c @@ -7,8 +7,7 @@ #include #include -patty_ax25_if *patty_ax25_if_new(const char *name, - patty_ax25_addr *addr, +patty_ax25_if *patty_ax25_if_new(patty_ax25_addr *addr, patty_ax25_if_driver *driver, patty_ax25_if_info *info) { patty_ax25_if *iface; @@ -31,8 +30,6 @@ patty_ax25_if *patty_ax25_if_new(const char *name, iface->driver = driver; - strncpy(iface->name, name, sizeof(iface->name)); - /* * TODO: Eventually inherit the half/full-duplex flag from the PHY this * interface is bound to @@ -96,10 +93,6 @@ void patty_ax25_if_destroy(patty_ax25_if *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 (*callback)(patty_ax25_addr *, void *), void *ctx) { diff --git a/src/server.c b/src/server.c index 270e4fb..314b945 100644 --- a/src/server.c +++ b/src/server.c @@ -15,6 +15,12 @@ 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 { int fd, /* fd of UNIX domain socket */ fd_max; @@ -107,10 +113,21 @@ error_malloc_server: return NULL; } -static int destroy_if(patty_ax25_if *iface, void *ctx) { - patty_ax25_if_destroy(iface); +static void destroy_ifaces(patty_list *ifaces) { + 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) { @@ -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_fd); - patty_ax25_server_if_each(server, destroy_if, NULL); patty_ax25_route_table_destroy(server->routes); - patty_list_destroy(server->ifaces); + destroy_ifaces(server->ifaces); 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, - patty_ax25_if *iface) { - int fd; + patty_ax25_if *iface, + const char *name) { + struct if_entry *entry; - if ((fd = patty_ax25_if_fd(iface)) >= 0) { - fd_watch(server, fd); + if ((entry = malloc(sizeof(*entry))) == NULL) { + 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; } + fd_watch(server, entry->fd); + return 0; error_list_append: +error_if_fd: + free(entry); + +error_malloc_entry: return -1; } @@ -450,20 +481,20 @@ int patty_ax25_server_if_delete(patty_ax25_server *server, const char *ifname) { patty_list_item *item = server->ifaces->first; - int fd, i = 0; + int i = 0; while (item) { - patty_ax25_if *iface = item->value; + struct if_entry *entry = item->value; - if (strncmp(iface->name, ifname, sizeof(iface->name)) == 0) { - if ((fd = patty_ax25_if_fd(iface)) >= 0) { - fd_clear(server, fd); - } + if (strncmp(entry->name, ifname, sizeof(entry->name)) == 0) { + fd_clear(server, entry->fd); if (patty_list_splice(server->ifaces, i) == NULL) { goto error_list_splice; } + free(entry); + 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; while (item) { - patty_ax25_if *iface = item->value; + struct if_entry *entry = item->value; - if (strncmp(iface->name, name, sizeof(iface->name)) == 0) { - return iface; + if (strncmp(entry->name, name, sizeof(entry->name)) == 0) { + return entry->iface; } 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 (*callback)(patty_ax25_if *, void *), + int (*callback)(char *, patty_ax25_if *, void *), void *ctx) { patty_list_item *item = server->ifaces->first; 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; } @@ -2002,20 +2033,21 @@ error_decode: return 0; } -static int handle_iface(patty_ax25_server *server, patty_ax25_if *iface) { - int fd = patty_ax25_if_fd(iface); +static int handle_iface(patty_ax25_server *server, struct if_entry *entry) { + patty_ax25_if *iface = entry->iface; + ssize_t len; - if (!FD_ISSET(fd, &server->fds_r)) { + if (!FD_ISSET(entry->fd, &server->fds_r)) { goto done; } - if ((len = patty_ax25_if_fill(iface)) < 0) { + if ((len = patty_ax25_if_fill(entry->iface)) < 0) { goto error_io; } else if (len == 0) { - close(fd); + close(entry->fd); - fd_clear(server, fd); + fd_clear(server, entry->fd); goto done; } @@ -2052,9 +2084,9 @@ static int handle_ifaces(patty_ax25_server *server) { patty_list_item *item = server->ifaces->first; 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; }