Hi the list, Willy, Please find in attachment a couple of patches to add a couple of IP related functions: - ipcmp to compare 2 ipcmp, à la strcmp - ipcpy to copy an IP address, à la strcpy
Baptiste
From 85868161bd3ee2b60a8964645dde48b891315e73 Mon Sep 17 00:00:00 2001 From: Baptiste Assmann <[email protected]> Date: Sat, 23 Jan 2016 23:39:12 +0100 Subject: [PATCH 01/11] MINOR: standard.c: ipcmp() function to compare 2 IP addresses stored in 2 struct sockaddr_storage new ipcmp() function to compare 2 IP addresses stored in struct sockaddr_storage. Returns 0 if both addresses doesn't match and 1 if they do. --- include/common/standard.h | 6 ++++++ src/standard.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/common/standard.h b/include/common/standard.h index 5afaad2..bc1ab40 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -880,6 +880,12 @@ extern void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr); */ extern int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr); +/* compare two struct sockaddr_storage and return: + * 0 (true) if the addr is the same in both + * 1 (false) if the addr is not the same in both + */ +int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2); + char *human_time(int t, short hz_div); extern const char *monthname[]; diff --git a/src/standard.c b/src/standard.c index c2d1689..d85c720 100644 --- a/src/standard.c +++ b/src/standard.c @@ -2558,6 +2558,36 @@ int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr) return 0; } +/* compare two struct sockaddr_storage and return: + * 0 (true) if the addr is the same in both + * 1 (false) if the addr is not the same in both + * -1 (unable) if one of the addr is not AF_INET* + */ +int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2) +{ + if ((ss1->ss_family != AF_INET) && (ss1->ss_family != AF_INET6)) + return -1; + + if ((ss2->ss_family != AF_INET) && (ss2->ss_family != AF_INET6)) + return -1; + + if (ss1->ss_family != ss2->ss_family) + return 1; + + switch (ss1->ss_family) { + case AF_INET: + return memcmp(&((struct sockaddr_in *)ss1)->sin_addr, + &((struct sockaddr_in *)ss2)->sin_addr, + sizeof(struct in_addr)) != 0; + case AF_INET6: + return memcmp(&((struct sockaddr_in6 *)ss1)->sin6_addr, + &((struct sockaddr_in6 *)ss2)->sin6_addr, + sizeof(struct in6_addr)) != 0; + } + + return 1; +} + char *human_time(int t, short hz_div) { static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s" char *p = rv; -- 1.9.1
From bcb154ac7126019f1fd9358d777f460164b0c771 Mon Sep 17 00:00:00 2001 From: Baptiste Assmann <[email protected]> Date: Sun, 31 Jan 2016 00:27:17 +0100 Subject: [PATCH 02/11] 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 bc1ab40..d4f2448 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 <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[]; diff --git a/src/standard.c b/src/standard.c index d85c720..5937b48 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 <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; -- 1.9.1

