From df298a725d5ef2c1aa5591775b316d0afecbf713 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 9 Jun 2014 22:34:30 +0200 Subject: [PATCH] store ip address as struct in_addr instead of a string Signed-off-by: Felix Fietkau --- interface.c | 43 +++++++++++++++++++------------------------ interface.h | 4 +++- service.c | 11 +---------- 3 files changed, 23 insertions(+), 35 deletions(-) diff --git a/interface.c b/interface.c index 01aeaa7..bcd6f13 100644 --- a/interface.c +++ b/interface.c @@ -118,17 +118,14 @@ interface_socket_setup(struct interface *iface) int yes = 1; int no = 0; struct sockaddr_in sa = { 0 }; - struct in_addr in; int fd = iface->fd.fd; - inet_aton(iface->ip, &in); - sa.sin_family = AF_INET; sa.sin_port = htons(MCAST_PORT); inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr); memset(&mreq, 0, sizeof(mreq)); - mreq.imr_address.s_addr = in.s_addr; + mreq.imr_address.s_addr = iface->v4_addr.s_addr; mreq.imr_multiaddr = sa.sin_addr; mreq.imr_ifindex = iface->ifindex; @@ -214,52 +211,50 @@ iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new, } } -static const char* -get_iface_ipv4(const char *ifname) +static int +get_iface_ipv4(struct interface *iface) { - static char buffer[INET_ADDRSTRLEN]; + struct sockaddr_in *sin; struct ifreq ir; - const char *ret; - int sock; + int sock, ret = -1; sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) - return NULL; + return -1; memset(&ir, 0, sizeof(struct ifreq)); - strncpy(ir.ifr_name, ifname, sizeof(ir.ifr_name)); + strncpy(ir.ifr_name, iface->name, sizeof(ir.ifr_name)); - if (ioctl(sock, SIOCGIFADDR, &ir) < 0) - return NULL; + ret = ioctl(sock, SIOCGIFADDR, &ir); + if (ret < 0) + goto out; - ret = inet_ntop(AF_INET, &((struct sockaddr_in *) &ir.ifr_addr)->sin_addr, buffer, sizeof(buffer)); - close(sock); + sin = (struct sockaddr_in *) &ir.ifr_addr; + memcpy(&iface->v4_addr, &sin->sin_addr, sizeof(iface->v4_addr)); +out: + close(sock); return ret; } int interface_add(const char *name) { struct interface *iface; - const char *ip_str; - char *name_buf, *ip_buf; - - ip_str = get_iface_ipv4(name); - if (!ip_str) - return -1; + char *name_buf; iface = calloc_a(sizeof(*iface), - &name_buf, strlen(name) + 1, - &ip_buf, strlen(ip_str) + 1); + &name_buf, strlen(name) + 1); iface->name = strcpy(name_buf, name); - iface->ip = strcpy(ip_buf, ip_str); iface->ifindex = if_nametoindex(name); iface->fd.fd = -1; if (iface->ifindex <= 0) goto error; + if (get_iface_ipv4(iface)) + goto error; + vlist_add(&interfaces, &iface->node, name); return 0; diff --git a/interface.h b/interface.h index 5250e96..8fffc7e 100644 --- a/interface.h +++ b/interface.h @@ -18,6 +18,8 @@ #include #include +#include + #include #include @@ -31,7 +33,7 @@ struct interface { struct uloop_timeout reconnect; int ifindex; - const char *ip; + struct in_addr v4_addr; struct uloop_timeout announce_timer; int announce_state; diff --git a/service.c b/service.c index 4a9ccc8..b8eb406 100644 --- a/service.c +++ b/service.c @@ -12,11 +12,8 @@ */ #include -#include #include #include -#include -#include #include #include @@ -116,17 +113,11 @@ service_send_a(struct interface *iface) unsigned char buffer[MAX_NAME_LEN]; char *host = service_name("local"); int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL); - struct in_addr in; - - if (!inet_aton(iface->ip, &in)) { - fprintf(stderr, "%s is not valid\n", iface->ip); - return; - } if (len < 1) return; - dns_add_answer(TYPE_A, (uint8_t *) &in.s_addr, 4); + dns_add_answer(TYPE_A, (uint8_t *) &iface->v4_addr.s_addr, 4); } static void -- 2.25.1