From bf8f8dfa679b6a48b7236ea378157218f9caf772 Mon Sep 17 00:00:00 2001 From: Hauke Petersen <hauke.petersen@fu-berlin.de> Date: Fri, 29 Jun 2018 17:22:59 +0200 Subject: [PATCH] net/sock_udp: add sock_udp_ep_equal() --- sys/include/net/sock/util.h | 26 +++++++++++++++++++++++--- sys/net/sock/sock_util.c | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/sys/include/net/sock/util.h b/sys/include/net/sock/util.h index 950039083a..cacee59912 100644 --- a/sys/include/net/sock/util.h +++ b/sys/include/net/sock/util.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de> + * 2018 Freie Universität Berlin * * This file is subject to the terms and conditions of the GNU Lesser * General Public License v2.1. See the file LICENSE in the top level @@ -15,19 +16,23 @@ * @{ * * @file - * @brief sock utility function definitions + * @brief sock utility function definitions * - * @author Kaspar Schleiser <kaspar@schleiser.de> + * @author Kaspar Schleiser <kaspar@schleiser.de> + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> */ #ifndef NET_SOCK_UTIL_H #define NET_SOCK_UTIL_H +#include <stdbool.h> + +#include "net/sock/udp.h" + #ifdef __cplusplus extern "C" { #endif - /** * @brief Format UDP endpoint to string and port * @@ -73,6 +78,21 @@ int sock_urlsplit(const char *url, char *hostport, char *urlpath); */ int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str); +/** + * @brief Compare the two given UDP endpoints + * + * The given endpoint identifiers are compared by checking their address family, + * their addresses, and their port value. + * + * @param[in] a Endpoint A + * @param[in] b Endpoint B + * + * @return true if given endpoint identifiers point to the same destination + * @return false if given endpoint identifiers do not point to the same + * destination, or if the address family is unknown + */ +bool sock_udp_ep_equal(const sock_udp_ep_t *a, const sock_udp_ep_t *b); + /** * @name helper definitions * @{ diff --git a/sys/net/sock/sock_util.c b/sys/net/sock/sock_util.c index e3dfa53d99..6f52a25ccb 100644 --- a/sys/net/sock/sock_util.c +++ b/sys/net/sock/sock_util.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de> + * 2018 Freie Universität Berlin * * This file is subject to the terms and conditions of the GNU Lesser * General Public License v2.1. See the file LICENSE in the top level @@ -7,11 +8,14 @@ */ /** - * @ingroup net_sock_util + * @ingroup net_sock_util * @{ + * * @file - * @brief sock utility functions implementation - * @author Kaspar Schleiser <kaspar@schleiser.de> + * @brief sock utility functions implementation + * + * @author Kaspar Schleiser <kaspar@schleiser.de> + * @author Hauke Petersen <hauke.petersen@fu-berlin.de> * @} */ @@ -181,3 +185,26 @@ int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str) #endif return -EINVAL; } + +bool sock_udp_ep_equal(const sock_udp_ep_t *a, const sock_udp_ep_t *b) +{ + assert(a && b); + + /* compare family and port */ + if ((a->family != b->family) || (a->port != b->port)) { + return false; + } + + /* compare addresses */ + switch (a->family) { +#ifdef SOCK_HAS_IPV6 + case AF_INET6: + return (memcmp(a->addr.ipv6, b->addr.ipv6, 16) == 0); + +#endif + case AF_INET: + return (memcmp(a->addr.ipv4, b->addr.ipv4, 4) == 0); + default: + return false; + } +} -- GitLab