From 08396c87d0058d5091ba6d4002b6559f1d41ae5b Mon Sep 17 00:00:00 2001 From: Baptiste Assmann Date: Sun, 31 Jan 2016 00:27:17 +0100 Subject: [PATCH] MINOR: standard.c: ipcpy() function to copy an IP address from a struct sockaddr_storage into an other one The function ipcpy() simply duplicates the IP address found in one struct sockaddr_storage into an other struct sockaddr_storage. It also update the family on the destination structure. Memory of destination structure must be allocated and cleared by the caller. --- include/common/standard.h | 6 ++++++ src/standard.c | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/common/standard.h b/include/common/standard.h index bc1ab403d..d4f2448ef 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -886,6 +886,12 @@ extern int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr); */ int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2); +/* copy ip from into + * the caller must clear before calling. + * Returns a pointer to the destination + */ +struct sockaddr_storage *ipcpy(struct sockaddr_storage *source, struct sockaddr_storage *dest); + char *human_time(int t, short hz_div); extern const char *monthname[]; diff --git a/src/standard.c b/src/standard.c index d85c72020..5937b482f 100644 --- a/src/standard.c +++ b/src/standard.c @@ -2588,6 +2588,27 @@ int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2) return 1; } +/* copy IP address from into + * the caller must allocate and clear before calling. + * Returns a pointer to the destination. + */ +struct sockaddr_storage *ipcpy(struct sockaddr_storage *source, struct sockaddr_storage *dest) +{ + dest->ss_family = source->ss_family; + + /* copy new addr and apply it */ + switch (source->ss_family) { + case AF_INET: + ((struct sockaddr_in *)dest)->sin_addr.s_addr = ((struct sockaddr_in *)source)->sin_addr.s_addr; + break; + case AF_INET6: + memcpy(((struct sockaddr_in6 *)dest)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)source)->sin6_addr.s6_addr, sizeof(struct in6_addr)); + break; + } + + return dest; +} + char *human_time(int t, short hz_div) { static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s" char *p = rv;