diff options
-rw-r--r-- | pingu_burst.c | 12 | ||||
-rw-r--r-- | sockaddr_util.c | 45 | ||||
-rw-r--r-- | sockaddr_util.h | 7 |
3 files changed, 53 insertions, 11 deletions
diff --git a/pingu_burst.c b/pingu_burst.c index e09c1ce..a9f8e83 100644 --- a/pingu_burst.c +++ b/pingu_burst.c @@ -17,6 +17,7 @@ void ping_burst_start(struct ev_loop *loop, struct pingu_host *host) struct addrinfo hints; struct addrinfo *ai, *rp; int r; + char buf[64]; /* we bind to device every burst in case an iface disappears and comes back. e.g ppp0 */ @@ -38,16 +39,17 @@ void ping_burst_start(struct ev_loop *loop, struct pingu_host *host) } for (rp = ai; rp != NULL; rp = rp->ai_next) { - sockaddr_init(&host->burst.saddr, ai->ai_family, - ai->ai_addr); + sockaddr_from_addrinfo(&host->burst.saddr, ai); r = pingu_ping_send(loop, host, 0); if (r == 0) break; } - if (rp == NULL) + sockaddr_to_string(&host->burst.saddr, buf, sizeof(buf)); + if (rp == NULL) { + log_debug("%s: failed to send first ping to %s", host->label, buf); host->burst.active = 0; - + } } void pingu_burst_timeout_cb(struct ev_loop *loop, struct ev_timer *w, @@ -59,6 +61,6 @@ void pingu_burst_timeout_cb(struct ev_loop *loop, struct ev_timer *w, log_warning("%s: burst already active", host->host); return; } - log_debug("%s: new burst", host->host); + log_debug("%s: new burst to %s via %s", host->label, host->host, host->iface->name); ping_burst_start(loop, host); } diff --git a/sockaddr_util.c b/sockaddr_util.c index 8dbb30a..a9e2755 100644 --- a/sockaddr_util.c +++ b/sockaddr_util.c @@ -3,12 +3,14 @@ #include <netinet/in.h> #include <string.h> +#include "log.h" #include "sockaddr_util.h" int sockaddr_cmp(union sockaddr_any *a, union sockaddr_any *b) { if (a->sa.sa_family != b->sa.sa_family) return a->sa.sa_family - b->sa.sa_family; + switch (a->sa.sa_family) { case AF_INET: return a->sin.sin_addr.s_addr - b->sin.sin_addr.s_addr; @@ -22,23 +24,54 @@ int sockaddr_cmp(union sockaddr_any *a, union sockaddr_any *b) return -1; } +union sockaddr_any *sockaddr_set4(union sockaddr_any *sa, void *addr) +{ + sa->sa.sa_family = AF_INET; + sa->sin.sin_addr.s_addr = *(uint32_t *)addr; + return sa; +} + +union sockaddr_any *sockaddr_set6(union sockaddr_any *sa, void *addr) +{ + sa->sa.sa_family = AF_INET6; + memcpy(&sa->sin6.sin6_addr, addr, sizeof(sa->sin6.sin6_addr)); + return sa; +} + union sockaddr_any *sockaddr_init(union sockaddr_any *sa, int family, void *addr) { - memset(sa, 0, sizeof(sa)); + memset(sa, 0, sizeof(*sa)); if (addr == NULL) return sa; - sa->sa.sa_family = family; switch (family) { case AF_INET: - sa->sin.sin_addr.s_addr = *(uint32_t *)addr; + return sockaddr_set4(sa, addr); break; case AF_INET6: - memcpy(&sa->sin6.sin6_addr, addr, - sizeof(sa->sin6.sin6_addr)); + return sockaddr_set6(sa, addr); break; } - return sa; + return NULL; +} + +union sockaddr_any *sockaddr_from_addrinfo(union sockaddr_any *sa, + struct addrinfo *ai) +{ + struct sockaddr_in *in = (struct sockaddr_in *)ai->ai_addr; + struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)ai->ai_addr; + memset(sa, 0, sizeof(*sa)); + if (ai == NULL) + return sa; + switch (ai->ai_family) { + case AF_INET: + return sockaddr_set4(sa, &in->sin_addr); + break; + case AF_INET6: + return sockaddr_set6(sa, &in6->sin6_addr); + break; + } + return NULL; } char *sockaddr_to_string(union sockaddr_any *sa, char *str, size_t size) diff --git a/sockaddr_util.h b/sockaddr_util.h index 7ac766b..97ea182 100644 --- a/sockaddr_util.h +++ b/sockaddr_util.h @@ -4,6 +4,10 @@ #define SOCKADDR_UTIL_H #include <netinet/in.h> +#include <sys/types.h> +#include <sys/socket.h> + +#include <netdb.h> union sockaddr_any { struct sockaddr sa; @@ -14,6 +18,9 @@ union sockaddr_any { int sockaddr_cmp(union sockaddr_any *a, union sockaddr_any *b); union sockaddr_any *sockaddr_init(union sockaddr_any *sa, int family, void *addr); +union sockaddr_any *sockaddr_from_addrinfo(union sockaddr_any *sa, + struct addrinfo *ai); + char *sockaddr_to_string(union sockaddr_any *sa, char *str, size_t size); #endif /* SOCKADDR_UTIL_H */ |