From 6fc85c49ab44929695cb2b120e8a680d5cab6729 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 5 Jun 2014 09:35:38 +0000 Subject: [PATCH] added function to compare struct sockaddr's git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@59 8ec27952-4edc-4aab-86aa-e87bb2611832 --- include/utils.h | 14 ++++++++++++++ src/utils.c | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/utils.h b/include/utils.h index 85c7b1611..12d9bce2e 100644 --- a/include/utils.h +++ b/include/utils.h @@ -15,6 +15,7 @@ struct config; struct sockaddr_in; +struct sockaddr; /// The log level which is passed as first argument to print() enum log_level @@ -55,6 +56,19 @@ int resolve_addr(const char *addr, struct sockaddr_in *sa, int flags); */ void init_realtime(struct config *g); +/** Compare two socket addresses based on their family and address. + * + * Only the family and the address is compared. + * Port numbers etc are ignored. + * + * @param a First address + * @param b Second address + * @return + * - 0 if the addresses are equal + * - otherwise they are not equal + */ +int sockaddr_cmp(struct sockaddr *a, struct sockaddr *b); + /// Check assertion and exit if failed. #define assert(exp) do { \ if (!(exp)) { \ diff --git a/src/utils.c b/src/utils.c index 72c4852ee..a753d1a93 100644 --- a/src/utils.c +++ b/src/utils.c @@ -90,6 +90,29 @@ int resolve_addr(const char *addr, struct sockaddr_in *sa, int flags) return 0; } +int sockaddr_cmp(struct sockaddr *a, struct sockaddr *b) +{ + if (a->sa_family == b->sa_family) { + switch (a->sa_family) { + case AF_INET: { + struct sockaddr_in *ai = (struct sockaddr_in *) a; + struct sockaddr_in *bi = (struct sockaddr_in *) b; + + return memcmp(&ai->sin_addr, &bi->sin_addr, sizeof(struct in_addr)); + } + + case AF_INET6: { + struct sockaddr_in6 *ai = (struct sockaddr_in6 *) a; + struct sockaddr_in6 *bi = (struct sockaddr_in6 *) b; + + return memcmp(&ai->sin6_addr, &bi->sin6_addr, sizeof(struct in6_addr)); + } + } + } + + return -1; +} + static cpu_set_t to_cpu_set_t(int set) { cpu_set_t cset;