From: Baptiste Assmann Date: Sat, 30 Jan 2016 23:27:17 +0000 (+0100) Subject: MINOR: standard.c: ipcpy() function to copy an IP address from a struct sockaddr_stor... X-Git-Tag: v1.7-dev4~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=08396c87d0058d5091ba6d4002b6559f1d41ae5b;p=thirdparty%2Fhaproxy.git 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. --- diff --git a/include/common/standard.h b/include/common/standard.h index bc1ab403d2..d4f2448ef1 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 d85c72020c..5937b482f7 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;