sock_util: introduce endpoint conversion aliases for TCP

This commit is contained in:
Martine S. Lenders 2020-06-24 17:21:29 +02:00
parent 78c816c6af
commit ae7944f115
No known key found for this signature in database
GPG Key ID: CCD317364F63286F
2 changed files with 117 additions and 8 deletions

View File

@ -29,22 +29,56 @@
#include <stdint.h> #include <stdint.h>
#include "net/sock/udp.h" #include "net/sock/udp.h"
#include "net/sock/tcp.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #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[in] endpoint endpoint to format
* @param[out] addr_str where to write address as string * @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 number of bytes written to @p addr_str on success
* @returns <0 otherwise * @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 * @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); 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 * @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 on success
* @returns <0 otherwise * @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 * @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 * @return false if given endpoint identifiers do not point to the same
* destination, or if the address family is unknown * 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 * @defgroup net_sock_util_conf SOCK utility functions compile configurations

View File

@ -36,7 +36,8 @@
#define PORT_STR_LEN (5) #define PORT_STR_LEN (5)
#define NETIF_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; void *addr_ptr;
*addr_str = '\0'; *addr_str = '\0';
@ -187,7 +188,7 @@ int _parse_netif(sock_udp_ep_t *ep_out, char *netifstart)
return (netifend - 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; unsigned brackets_flag;
char *hoststart = (char*)str; char *hoststart = (char*)str;
@ -253,7 +254,8 @@ int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str)
return -EINVAL; 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); assert(a && b);