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.
This commit is contained in:
Baptiste Assmann 2016-01-31 00:27:17 +01:00 committed by Willy Tarreau
parent 08b24cfdb2
commit 08396c87d0
2 changed files with 27 additions and 0 deletions

View File

@ -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 <source> into <dest>
* the caller must clear <dest> 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[];

View File

@ -2588,6 +2588,27 @@ int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2)
return 1;
}
/* copy IP address from <source> into <dest>
* the caller must allocate and clear <dest> 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;