net/sock_udp: add sock_udp_ep_equal()

This commit is contained in:
Hauke Petersen 2018-06-29 17:22:59 +02:00
parent 534b0da9a5
commit bf8f8dfa67
2 changed files with 53 additions and 6 deletions

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de> * 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 * 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 * General Public License v2.1. See the file LICENSE in the top level
@ -15,19 +16,23 @@
* @{ * @{
* *
* @file * @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 #ifndef NET_SOCK_UTIL_H
#define NET_SOCK_UTIL_H #define NET_SOCK_UTIL_H
#include <stdbool.h>
#include "net/sock/udp.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/** /**
* @brief Format UDP endpoint to string and port * @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); 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 * @name helper definitions
* @{ * @{

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de> * 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 * 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 * 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 * @file
* @brief sock utility functions implementation * @brief sock utility functions implementation
* @author Kaspar Schleiser <kaspar@schleiser.de> *
* @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 #endif
return -EINVAL; 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;
}
}