1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-25 06:23:53 +01:00

sock_util: add interface descriptor parsing to str2ep

This commit is contained in:
Martine S. Lenders 2020-05-12 17:42:33 +02:00
parent fcd36f7b77
commit a603504a6e
No known key found for this signature in database
GPG Key ID: CCD317364F63286F

View File

@ -33,6 +33,9 @@
#include "fmt.h"
#endif
#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)
{
void *addr_ptr;
@ -147,6 +150,45 @@ int sock_urlsplit(const char *url, char *hostport, char *urlpath)
return 0;
}
int _parse_port(sock_udp_ep_t *ep_out, const char *portstart)
{
int port_len = strlen(portstart);
/* Checks here verify that the supplied port number is up to 5 (random)
* chars in size and result is smaller or equal to UINT16_MAX. */
if (port_len > PORT_STR_LEN) {
return -EINVAL;
}
uint32_t port = atol(portstart);
if (port > UINT16_MAX) {
return -EINVAL;
}
ep_out->port = (uint16_t)port;
return port_len;
}
int _parse_netif(sock_udp_ep_t *ep_out, char *netifstart)
{
char *netifend;
size_t netiflen;
char netifbuf[NETIF_STR_LEN + 1];
for (netifend = netifstart; *netifend && *netifend != ']';
netifend++);
netiflen = netifend - netifstart;
if (!*netifend || (netiflen >= NETIF_STR_LEN) || (netiflen == 0)) {
/* no netif found, bail out */
return -EINVAL;
}
strncpy(netifbuf, netifstart, netiflen);
int netif = strtol(netifbuf, NULL, 10);
if ((netif < 0) || (((unsigned)netif) > UINT16_MAX)) {
return -EINVAL;
}
ep_out->netif = (uint16_t)netif;
return (netifend - netifstart);
}
int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str)
{
unsigned brackets_flag;
@ -159,8 +201,9 @@ int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str)
if (*hoststart == '[') {
brackets_flag = 1;
for (hostend = ++hoststart; *hostend && *hostend != ']';
hostend++);
for (hostend = ++hoststart;
*hostend && *hostend != ']' && *hostend != '%';
hostend++);
if (! *hostend || ((size_t)(hostend - hoststart) >= sizeof(hostbuf))) {
/* none found, bail out */
return -EINVAL;
@ -171,26 +214,28 @@ int sock_udp_str2ep(sock_udp_ep_t *ep_out, const char *str)
for (hostend = hoststart; *hostend && (*hostend != ':') && \
((size_t)(hostend - hoststart) < sizeof(hostbuf)); hostend++) {}
}
size_t hostlen = hostend - hoststart;
if (*(hostend + brackets_flag) == ':') {
char *portstart = hostend + brackets_flag + 1;
/* Checks here verify that the supplied port number is up to 5 (random)
* chars in size and result is smaller or equal to UINT16_MAX. */
if (strlen(portstart) > 5) {
return -EINVAL;
int res = _parse_port(ep_out, hostend + brackets_flag + 1);
if (res < 0) {
return res;
}
uint32_t port = atol(portstart);
if (port > UINT16_MAX) {
return -EINVAL;
}
else if (brackets_flag && (*hostend == '%')) {
int res = _parse_netif(ep_out, hostend + 1);
if (res < 0) {
return res;
}
char *colon_ptr = hostend + res + brackets_flag + 1;
if ((*colon_ptr == ':') &&
((res = _parse_port(ep_out, colon_ptr + 1)) < 0)) {
return res;
}
ep_out->port = (uint16_t)port;
}
if (hostlen >= sizeof(hostbuf)) {
return -EINVAL;
}
memcpy(hostbuf, hoststart, hostlen);
hostbuf[hostlen] = '\0';