diff --git a/sys/include/net/sock/util.h b/sys/include/net/sock/util.h index f403f0d3dc..1bb32a797f 100644 --- a/sys/include/net/sock/util.h +++ b/sys/include/net/sock/util.h @@ -29,22 +29,56 @@ #include #include "net/sock/udp.h" +#include "net/sock/tcp.h" #ifdef __cplusplus extern "C" { #endif /** - * @brief Format UDP endpoint to string and port + * @brief Format common IP-based transport layer endpoint to string and port * * @param[in] endpoint endpoint to format * @param[out] addr_str where to write address as string - * @param[out] port where to write prt number as uint16_t + * @param[out] port where to write port number as uint16_t * * @returns number of bytes written to @p addr_str on success * @returns <0 otherwise */ -int sock_udp_ep_fmt(const sock_udp_ep_t *endpoint, char *addr_str, uint16_t *port); +int sock_tl_ep_fmt(const struct _sock_tl_ep *endpoint, + char *addr_str, uint16_t *port); + +/** + * @brief Format TCP endpoint to string and port + * + * @param[in] endpoint endpoint to format + * @param[out] addr_str where to write address as string + * @param[out] port where to write port number as uint16_t + * + * @returns number of bytes written to @p addr_str on success + * @returns <0 otherwise + */ +static inline int sock_tcp_ep_fmt(const sock_tcp_ep_t *endpoint, + char *addr_str, uint16_t *port) +{ + return sock_tl_ep_fmt(endpoint, addr_str, port); +} + +/** + * @brief Format UDP endpoint to string and port + * + * @param[in] endpoint endpoint to format + * @param[out] addr_str where to write address as string + * @param[out] port where to write port number as uint16_t + * + * @returns number of bytes written to @p addr_str on success + * @returns <0 otherwise + */ +static inline int sock_udp_ep_fmt(const sock_udp_ep_t *endpoint, + char *addr_str, uint16_t *port) +{ + return sock_tl_ep_fmt(endpoint, addr_str, port); +} /** * @brief Split url to host:port and url path @@ -68,6 +102,37 @@ int sock_udp_ep_fmt(const sock_udp_ep_t *endpoint, char *addr_str, uint16_t *por */ int sock_urlsplit(const char *url, char *hostport, char *urlpath); +/** + * @brief Convert string to common IP-based transport layer endpoint + * + * Takes eg., "[2001:db8::1]:1234" and converts it into the corresponding UDP + * endpoint structure. + * + * @param[out] ep_out endpoint structure to fill + * @param[in] str string to read from + * + * @returns 0 on success + * @returns <0 otherwise + */ +int sock_tl_str2ep(struct _sock_tl_ep *ep_out, const char *str); + +/** + * @brief Convert string to TCP endpoint + * + * Takes eg., "[2001:db8::1]:1234" and converts it into the corresponding UDP + * endpoint structure. + * + * @param[out] ep_out endpoint structure to fill + * @param[in] str string to read from + * + * @returns 0 on success + * @returns <0 otherwise + */ +static inline int sock_tcp_str2ep(sock_tcp_ep_t *ep_out, const char *str) +{ + return sock_tl_str2ep(ep_out, str); +} + /** * @brief Convert string to UDP endpoint * @@ -80,7 +145,45 @@ int sock_urlsplit(const char *url, char *hostport, char *urlpath); * @returns 0 on success * @returns <0 otherwise */ -int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str); +static inline int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str) +{ + return sock_tl_str2ep(ep_out, str); +} + +/** + * @brief Compare the two given common IP-based transport layer 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_tl_ep_equal(const struct _sock_tl_ep *a, + const struct _sock_tl_ep *b); + +/** + * @brief Compare the two given TCP 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 + */ +static inline bool sock_tcp_ep_equal(const sock_tcp_ep_t *a, + const sock_tcp_ep_t *b) +{ + return sock_tl_ep_equal(a, b); +} /** * @brief Compare the two given UDP endpoints @@ -95,7 +198,11 @@ int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str); * @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); +static inline bool sock_udp_ep_equal(const sock_udp_ep_t *a, + const sock_udp_ep_t *b) +{ + return sock_tl_ep_equal(a, b); +} /** * @defgroup net_sock_util_conf SOCK utility functions compile configurations diff --git a/sys/net/sock/sock_util.c b/sys/net/sock/sock_util.c index 91730967d4..d02f1d53af 100644 --- a/sys/net/sock/sock_util.c +++ b/sys/net/sock/sock_util.c @@ -36,7 +36,8 @@ #define PORT_STR_LEN (5) #define NETIF_STR_LEN (5) -int sock_udp_ep_fmt(const sock_udp_ep_t *endpoint, char *addr_str, uint16_t *port) +int sock_tl_ep_fmt(const struct _sock_tl_ep *endpoint, + char *addr_str, uint16_t *port) { void *addr_ptr; *addr_str = '\0'; @@ -187,7 +188,7 @@ int _parse_netif(sock_udp_ep_t *ep_out, char *netifstart) return (netifend - netifstart); } -int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str) +int sock_tl_str2ep(struct _sock_tl_ep *ep_out, const char *str) { unsigned brackets_flag; char *hoststart = (char*)str; @@ -253,7 +254,8 @@ int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str) return -EINVAL; } -bool sock_udp_ep_equal(const sock_udp_ep_t *a, const sock_udp_ep_t *b) +bool sock_tl_ep_equal(const struct _sock_tl_ep *a, + const struct _sock_tl_ep *b) { assert(a && b);