Skip to content

Commit c937969

Browse files
authored
Merge pull request #21419 from maribu/sys/net/sock_util
sys/net/sock_util: Do not depend on network stack
2 parents fd88c13 + 41c8f51 commit c937969

File tree

2 files changed

+117
-105
lines changed

2 files changed

+117
-105
lines changed

sys/include/net/sock/util.h

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* directory for more details.
88
*/
99

10+
#pragma once
11+
1012
/**
1113
* @defgroup net_sock_util sock utility functions
1214
* @ingroup net_sock
@@ -22,25 +24,63 @@
2224
* @author Hauke Petersen <[email protected]>
2325
*/
2426

25-
#ifndef NET_SOCK_UTIL_H
26-
#define NET_SOCK_UTIL_H
27-
2827
#include <stdbool.h>
2928
#include <stdint.h>
3029

31-
#include "net/sock/udp.h"
32-
#include "net/sock/tcp.h"
3330
#include "net/sock/config.h"
3431

32+
#if MODULE_GNRC_SOCK_UDP || MODULE_LWIP_SOCK_UDP || DOXYGEN
33+
# include "net/sock/udp.h"
34+
#endif
35+
#if MODULE_GNRC_SOCK_TCP || MODULE_LWIP_SOCK_TCP || DOXYGEN
36+
# include "net/sock/tcp.h"
37+
#endif
3538
#ifdef MODULE_SOCK_DTLS
36-
#include "net/credman.h"
37-
#include "net/sock/dtls.h"
39+
# include "net/credman.h"
40+
# include "net/sock/dtls.h"
41+
#endif
42+
43+
#if MODULE_GNRC_SOCK_UDP || MODULE_LWIP_SOCK_UDP ||MODULE_GNRC_SOCK_TCP || MODULE_LWIP_SOCK_TCP || DOXYGEN
44+
# define HAVE_SOCK_TL_EP 1 /**< Indicates presence of `struct _sock_tl_ep` */
3845
#endif
3946

4047
#ifdef __cplusplus
4148
extern "C" {
4249
#endif
4350

51+
/**
52+
* @brief Split url to host:port and url path
53+
*
54+
* Will split e.g., "https://host.name:1234/url/path" into
55+
* "host.name:1234" and "/url/path".
56+
*
57+
* @note Caller has to make sure hostport and urlpath can hold the results!
58+
* Make sure to provide space for @ref CONFIG_SOCK_HOSTPORT_MAXLEN respectively
59+
* @ref CONFIG_SOCK_URLPATH_MAXLEN bytes, if pointers are not NULL.
60+
* Scheme part of the URL is limited to @ref CONFIG_SOCK_SCHEME_MAXLEN length.
61+
*
62+
* @pre `url != NULL`
63+
*
64+
* @param[in] url URL to split. Must not be NULL.
65+
* @param[out] hostport where to write host:port. Can be NULL.
66+
* @param[out] urlpath where to write url path. Can be NULL.
67+
*
68+
* @returns 0 on success
69+
* @returns <0 otherwise
70+
*/
71+
int sock_urlsplit(const char *url, char *hostport, char *urlpath);
72+
73+
/**
74+
* @brief Returns a pointer to the path component in @p url
75+
*
76+
* @param[in] url URL to examine. Must not be NULL.
77+
*
78+
* @returns pointer to the start of the path component in @p url
79+
* @returns NULL if @p url is invalid
80+
*/
81+
const char *sock_urlpath(const char *url);
82+
83+
#if HAVE_SOCK_TL_EP
4484
/**
4585
* @brief Format common IP-based transport layer endpoint to string and port
4686
*
@@ -86,38 +126,6 @@ static inline int sock_udp_ep_fmt(const sock_udp_ep_t *endpoint,
86126
return sock_tl_ep_fmt(endpoint, addr_str, port);
87127
}
88128

89-
/**
90-
* @brief Split url to host:port and url path
91-
*
92-
* Will split e.g., "https://host.name:1234/url/path" into
93-
* "host.name:1234" and "/url/path".
94-
*
95-
* @note Caller has to make sure hostport and urlpath can hold the results!
96-
* Make sure to provide space for @ref CONFIG_SOCK_HOSTPORT_MAXLEN respectively
97-
* @ref CONFIG_SOCK_URLPATH_MAXLEN bytes, if pointers are not NULL.
98-
* Scheme part of the URL is limited to @ref CONFIG_SOCK_SCHEME_MAXLEN length.
99-
*
100-
* @pre `url != NULL`
101-
*
102-
* @param[in] url URL to split. Must not be NULL.
103-
* @param[out] hostport where to write host:port. Can be NULL.
104-
* @param[out] urlpath where to write url path. Can be NULL.
105-
*
106-
* @returns 0 on success
107-
* @returns <0 otherwise
108-
*/
109-
int sock_urlsplit(const char *url, char *hostport, char *urlpath);
110-
111-
/**
112-
* @brief Returns a pointer to the path component in @p url
113-
*
114-
* @param[in] url URL to examine. Must not be NULL.
115-
*
116-
* @returns pointer to the start of the path component in @p url
117-
* @returns NULL if @p url is invalid
118-
*/
119-
const char *sock_urlpath(const char *url);
120-
121129
/**
122130
* @brief Convert string to common IP-based transport layer endpoint
123131
*
@@ -273,6 +281,7 @@ static inline bool sock_udp_ep_equal(const sock_udp_ep_t *a,
273281
{
274282
return sock_tl_ep_equal(a, b);
275283
}
284+
#endif
276285

277286
#if defined(MODULE_SOCK_DTLS) || DOXYGEN
278287
/**
@@ -300,6 +309,4 @@ int sock_dtls_establish_session(sock_udp_t *sock_udp, sock_dtls_t *sock_dtls,
300309
#ifdef __cplusplus
301310
}
302311
#endif
303-
304-
#endif /* NET_SOCK_UTIL_H */
305312
/** @} */

sys/net/sock/sock_util.c

Lines changed: 69 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,23 @@
2626
#include <string.h>
2727
#include <assert.h>
2828

29-
#include "net/sock/udp.h"
3029
#include "net/sock/util.h"
3130
#include "net/iana/portrange.h"
3231

33-
#if defined(MODULE_DNS)
34-
#include "net/dns.h"
32+
#if MODULE_GNRC_SOCK_UDP || MODULE_LWIP_SOCK_UDP
33+
# include "net/sock/udp.h"
3534
#endif
3635

37-
#ifdef MODULE_RANDOM
38-
#include "random.h"
36+
#if MODULE_DNS
37+
# include "net/dns.h"
3938
#endif
4039

41-
#ifdef MODULE_FMT
42-
#include "fmt.h"
40+
#if MODULE_RANDOM
41+
# include "random.h"
42+
#endif
43+
44+
#if MODULE_FMT
45+
# include "fmt.h"
4346
#endif
4447

4548
#define ENABLE_DEBUG 0
@@ -48,49 +51,6 @@
4851
#define PORT_STR_LEN (5)
4952
#define NETIF_STR_LEN (5)
5053

51-
int sock_tl_ep_fmt(const struct _sock_tl_ep *endpoint,
52-
char *addr_str, uint16_t *port)
53-
{
54-
const void *addr_ptr;
55-
*addr_str = '\0';
56-
57-
switch (endpoint->family) {
58-
#ifdef SOCK_HAS_IPV4
59-
case AF_INET:
60-
addr_ptr = &endpoint->addr.ipv4;
61-
break;
62-
#endif
63-
#ifdef SOCK_HAS_IPV6
64-
case AF_INET6:
65-
addr_ptr = &endpoint->addr.ipv6;
66-
break;
67-
#endif
68-
default:
69-
return -ENOTSUP;
70-
}
71-
72-
if (!inet_ntop(endpoint->family, addr_ptr, addr_str, INET6_ADDRSTRLEN)) {
73-
return 0;
74-
}
75-
76-
if (IS_ACTIVE(SOCK_HAS_IPV6) && (endpoint->family == AF_INET6) && endpoint->netif) {
77-
#ifdef MODULE_FMT
78-
char *tmp = addr_str + strlen(addr_str);
79-
*tmp++ = '%';
80-
tmp += fmt_u16_dec(tmp, endpoint->netif);
81-
*tmp = '\0';
82-
#else
83-
sprintf(addr_str + strlen(addr_str), "%%%4u", endpoint->netif);
84-
#endif
85-
}
86-
87-
if (port) {
88-
*port = endpoint->port;
89-
}
90-
91-
return strlen(addr_str);
92-
}
93-
9454
static char* _find_hoststart(const char *url)
9555
{
9656
/* Increment CONFIG_SOCK_SCHEME_MAXLEN due to comparison with the colon after the
@@ -167,6 +127,50 @@ const char *sock_urlpath(const char *url)
167127
return _find_pathstart(hoststart);
168128
}
169129

130+
#if HAVE_SOCK_TL_EP
131+
int sock_tl_ep_fmt(const struct _sock_tl_ep *endpoint,
132+
char *addr_str, uint16_t *port)
133+
{
134+
const void *addr_ptr;
135+
*addr_str = '\0';
136+
137+
switch (endpoint->family) {
138+
#ifdef SOCK_HAS_IPV4
139+
case AF_INET:
140+
addr_ptr = &endpoint->addr.ipv4;
141+
break;
142+
#endif
143+
#ifdef SOCK_HAS_IPV6
144+
case AF_INET6:
145+
addr_ptr = &endpoint->addr.ipv6;
146+
break;
147+
#endif
148+
default:
149+
return -ENOTSUP;
150+
}
151+
152+
if (!inet_ntop(endpoint->family, addr_ptr, addr_str, INET6_ADDRSTRLEN)) {
153+
return 0;
154+
}
155+
156+
if (IS_ACTIVE(SOCK_HAS_IPV6) && (endpoint->family == AF_INET6) && endpoint->netif) {
157+
#ifdef MODULE_FMT
158+
char *tmp = addr_str + strlen(addr_str);
159+
*tmp++ = '%';
160+
tmp += fmt_u16_dec(tmp, endpoint->netif);
161+
*tmp = '\0';
162+
#else
163+
sprintf(addr_str + strlen(addr_str), "%%%4u", endpoint->netif);
164+
#endif
165+
}
166+
167+
if (port) {
168+
*port = endpoint->port;
169+
}
170+
171+
return strlen(addr_str);
172+
}
173+
170174
int _parse_port(sock_udp_ep_t *ep_out, const char *portstart)
171175
{
172176
int port_len = strlen(portstart);
@@ -258,20 +262,20 @@ int sock_tl_str2ep(struct _sock_tl_ep *ep_out, const char *str)
258262
hostbuf[hostlen] = '\0';
259263

260264
if (!brackets_flag) {
261-
#ifdef SOCK_HAS_IPV4
265+
# ifdef SOCK_HAS_IPV4
262266
if (inet_pton(AF_INET, hostbuf, &ep_out->addr.ipv4) == 1) {
263267
ep_out->family = AF_INET;
264268
return 0;
265269
}
266-
#endif
270+
# endif
267271
}
268272

269-
#ifdef SOCK_HAS_IPV6
273+
# ifdef SOCK_HAS_IPV6
270274
if (inet_pton(AF_INET6, hostbuf, ep_out->addr.ipv6) == 1) {
271275
ep_out->family = AF_INET6;
272276
return 0;
273277
}
274-
#endif
278+
# endif
275279

276280
return -EINVAL;
277281
}
@@ -283,7 +287,7 @@ int sock_tl_name2ep(struct _sock_tl_ep *ep_out, const char *str)
283287
return 0;
284288
}
285289

286-
#if defined(MODULE_SOCK_DNS) || defined(MODULE_SOCK_DNS_MOCK)
290+
# if MODULE_SOCK_DNS || MODULE_SOCK_DNS_MOCK
287291
int family;
288292
char hostbuf[CONFIG_SOCK_HOSTPORT_MAXLEN];
289293
const char *host;
@@ -314,20 +318,20 @@ int sock_tl_name2ep(struct _sock_tl_ep *ep_out, const char *str)
314318
}
315319

316320
switch (dns_query(host, &ep_out->addr, family)) {
317-
#ifdef SOCK_HAS_IPV4
321+
# ifdef SOCK_HAS_IPV4
318322
case 4:
319323
ep_out->family = AF_INET;
320324
return 0;
321-
#endif
322-
#ifdef SOCK_HAS_IPV6
325+
# endif
326+
# ifdef SOCK_HAS_IPV6
323327
case 16:
324328
ep_out->family = AF_INET6;
325329
return 0;
326-
#endif
330+
# endif
327331
default:
328332
return -EINVAL;
329333
}
330-
#endif
334+
# endif
331335
return res;
332336
}
333337

@@ -343,18 +347,19 @@ bool sock_tl_ep_equal(const struct _sock_tl_ep *a,
343347

344348
/* compare addresses */
345349
switch (a->family) {
346-
#ifdef SOCK_HAS_IPV4
350+
# ifdef SOCK_HAS_IPV4
347351
case AF_INET:
348352
return memcmp(a->addr.ipv4, b->addr.ipv4, 4) == 0;
349-
#endif
350-
#ifdef SOCK_HAS_IPV6
353+
# endif
354+
# ifdef SOCK_HAS_IPV6
351355
case AF_INET6:
352356
return memcmp(a->addr.ipv6, b->addr.ipv6, 16) == 0;
353-
#endif
357+
# endif
354358
default:
355359
return false;
356360
}
357361
}
362+
#endif
358363

359364
#if defined(MODULE_SOCK_DTLS)
360365
int sock_dtls_establish_session(sock_udp_t *sock_udp, sock_dtls_t *sock_dtls,

0 commit comments

Comments
 (0)