From 08b24cfdb220744c47ed7bfe5aa3ed8b4c0aa3c6 Mon Sep 17 00:00:00 2001 From: Baptiste Assmann Date: Sat, 23 Jan 2016 23:39:12 +0100 Subject: [PATCH] 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 5afaad20fb..bc1ab403d2 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 c2d16896d1..d85c72020c 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;