diff --git a/bin/if.c b/bin/if.c index 9d79ed7..b774716 100644 --- a/bin/if.c +++ b/bin/if.c @@ -44,6 +44,7 @@ static patty_ax25_if *create_kiss(struct context *ctx, int argc, char **argv) { enum mode_kiss mode = MODE_KISS_DEVICE; patty_kiss_tnc_info info; + patty_ax25_if *iface; int i; @@ -115,10 +116,21 @@ static patty_ax25_if *create_kiss(struct context *ctx, int argc, char **argv) { } } - return patty_ax25_if_new(&ctx->addr, - patty_kiss_tnc_driver(), - &info); + if ((iface = patty_ax25_if_new(patty_kiss_tnc_driver(), + &info)) == NULL) { + goto error_if_new; + } + if (patty_ax25_if_addr_set(iface, &ctx->addr) < 0) { + goto error_if_addr_set; + } + + return iface; + +error_if_addr_set: + patty_ax25_if_destroy(iface); + +error_if_new: error_invalid: return NULL; } @@ -214,8 +226,7 @@ static patty_ax25_if *create_aprs_is(struct context *ctx, } } - if ((iface = patty_ax25_if_new(&ctx->addr, - patty_ax25_aprs_is_driver(), + if ((iface = patty_ax25_if_new(patty_ax25_aprs_is_driver(), &info)) == NULL) { patty_error_fmt(ctx->err, "Unable to connect to APRS-IS service %s:%s: %s", info.host, info.port, strerror(errno)); @@ -223,8 +234,15 @@ static patty_ax25_if *create_aprs_is(struct context *ctx, goto error_if_new; } + if (patty_ax25_if_addr_set(iface, &ctx->addr) < 0) { + goto error_if_addr_set; + } + return iface; +error_if_addr_set: + patty_ax25_if_destroy(iface); + error_if_new: error_invalid: return NULL; diff --git a/bin/pattyd.c b/bin/pattyd.c index fd84558..083dc04 100644 --- a/bin/pattyd.c +++ b/bin/pattyd.c @@ -303,8 +303,7 @@ static int handle_standalone(patty_daemon *daemon, goto error_kiss_config; } - if ((iface = patty_ax25_if_new(&addr, - patty_kiss_tnc_driver(), + if ((iface = patty_ax25_if_new(patty_kiss_tnc_driver(), &info)) == NULL) { fprintf(stderr, "%s: Unable to create network interface %s: %s\n", argv0, DEFAULT_IFNAME, strerror(errno)); @@ -314,6 +313,13 @@ static int handle_standalone(patty_daemon *daemon, int fd = patty_ax25_if_fd(iface); char *pty; + if (patty_ax25_if_addr_set(iface, &addr) < 0) { + fprintf(stderr, "%s: Unable to set address for interface %s: %s\n", + argv[0], DEFAULT_IFNAME, strerror(errno)); + + goto error_if_addr_set; + } + if (isatty(fd) && (pty = ptsname(fd)) != NULL) { printf("if %s pty %s\n", DEFAULT_IFNAME, pty); } @@ -337,6 +343,9 @@ static int handle_standalone(patty_daemon *daemon, error_daemon_route_add_default: error_daemon_if_add: +error_if_addr_set: + patty_ax25_if_destroy(iface); + error_if_new: error_kiss_config: error_invalid_callsign: diff --git a/include/patty/ax25/if.h b/include/patty/ax25/if.h index 1ad5c00..2405bc7 100644 --- a/include/patty/ax25/if.h +++ b/include/patty/ax25/if.h @@ -81,12 +81,14 @@ typedef struct _patty_ax25_if { patty_ax25_if_phy *phy; } patty_ax25_if; -patty_ax25_if *patty_ax25_if_new(patty_ax25_addr *addr, - patty_ax25_if_driver *driver, +patty_ax25_if *patty_ax25_if_new(patty_ax25_if_driver *driver, patty_ax25_if_info *info); void patty_ax25_if_destroy(patty_ax25_if *iface); +int patty_ax25_if_addr_set(patty_ax25_if *iface, + patty_ax25_addr *addr); + int patty_ax25_if_addr_each(patty_ax25_if *iface, int (*callback)(patty_ax25_addr *, void *), void *ctx); diff --git a/src/if.c b/src/if.c index f34b1b1..a2f2dd4 100644 --- a/src/if.c +++ b/src/if.c @@ -7,17 +7,10 @@ #include #include -patty_ax25_if *patty_ax25_if_new(patty_ax25_addr *addr, - patty_ax25_if_driver *driver, +patty_ax25_if *patty_ax25_if_new(patty_ax25_if_driver *driver, patty_ax25_if_info *info) { patty_ax25_if *iface; - if (addr->callsign[0] == '\0') { - errno = EINVAL; - - goto error_invalid_address; - } - if ((iface = malloc(sizeof(*iface))) == NULL) { goto error_malloc_iface; } @@ -56,8 +49,6 @@ patty_ax25_if *patty_ax25_if_new(patty_ax25_addr *addr, goto error_dict_new_promisc_fds; } - memcpy(&iface->addr, addr, sizeof(iface->addr)); - return iface; error_dict_new_promisc_fds: @@ -76,10 +67,14 @@ error_phy_create: free(iface); error_malloc_iface: -error_invalid_address: return NULL; } +int patty_ax25_if_addr_set(patty_ax25_if *iface, + patty_ax25_addr *addr) { + return patty_ax25_addr_copy(&iface->addr, addr, 0); +} + void patty_ax25_if_destroy(patty_ax25_if *iface) { if (iface->driver->destroy) { iface->driver->destroy(iface->phy);