1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/net/sock/sock_util.c

350 lines
8.3 KiB
C
Raw Normal View History

2017-03-05 23:53:51 +01:00
/*
* Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de>
2018-06-29 17:22:59 +02:00
* 2018 Freie Universität Berlin
2017-03-05 23:53:51 +01:00
*
* 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
* directory for more details.
*/
/**
2018-06-29 17:22:59 +02:00
* @ingroup net_sock_util
2017-03-05 23:53:51 +01:00
* @{
2018-06-29 17:22:59 +02:00
*
2017-03-05 23:53:51 +01:00
* @file
2018-06-29 17:22:59 +02:00
* @brief sock utility functions implementation
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
2017-03-05 23:53:51 +01:00
* @}
*/
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
2018-07-18 15:28:01 +02:00
#include <stdlib.h>
2017-03-05 23:53:51 +01:00
#include <string.h>
#include <assert.h>
2017-03-05 23:53:51 +01:00
#include "net/sock/udp.h"
#include "net/sock/util.h"
2022-07-27 14:32:20 +02:00
#if defined(MODULE_DNS)
#include "net/dns.h"
#endif
2017-03-05 23:53:51 +01:00
#ifdef MODULE_FMT
2017-03-05 23:53:51 +01:00
#include "fmt.h"
#endif
#define PORT_STR_LEN (5)
#define NETIF_STR_LEN (5)
int sock_tl_ep_fmt(const struct _sock_tl_ep *endpoint,
char *addr_str, uint16_t *port)
2017-03-05 23:53:51 +01:00
{
const void *addr_ptr;
2017-03-05 23:53:51 +01:00
*addr_str = '\0';
switch (endpoint->family) {
#ifdef SOCK_HAS_IPV4
case AF_INET:
addr_ptr = &endpoint->addr.ipv4;
break;
#endif
#ifdef SOCK_HAS_IPV6
case AF_INET6:
addr_ptr = &endpoint->addr.ipv6;
break;
#endif
default:
return -ENOTSUP;
2017-03-05 23:53:51 +01:00
}
if (!inet_ntop(endpoint->family, addr_ptr, addr_str, INET6_ADDRSTRLEN)) {
return 0;
}
if (IS_ACTIVE(SOCK_HAS_IPV6) && (endpoint->family == AF_INET6) && endpoint->netif) {
#ifdef MODULE_FMT
2017-03-05 23:53:51 +01:00
char *tmp = addr_str + strlen(addr_str);
*tmp++ = '%';
tmp += fmt_u16_dec(tmp, endpoint->netif);
*tmp = '\0';
2017-03-05 23:53:51 +01:00
#else
sprintf(addr_str + strlen(addr_str), "%%%4u", endpoint->netif);
#endif
}
if (port) {
*port = endpoint->port;
}
return strlen(addr_str);
}
static char* _find_hoststart(const char *url)
{
/* Increment CONFIG_SOCK_SCHEME_MAXLEN due to comparison with the colon after the
2018-07-18 14:33:35 +02:00
* scheme part
*/
size_t remaining = CONFIG_SOCK_SCHEME_MAXLEN + 1;
2017-03-05 23:53:51 +01:00
char *urlpos = (char*)url;
2022-10-10 20:31:15 +02:00
while (*urlpos && remaining) {
2018-07-18 14:33:35 +02:00
remaining--;
2017-03-05 23:53:51 +01:00
if (*urlpos++ == ':') {
if (strncmp(urlpos, "//", 2) == 0) {
return urlpos + 2;
}
break;
}
}
return NULL;
}
static char* _find_pathstart(const char *url)
{
size_t remaining = CONFIG_SOCK_HOSTPORT_MAXLEN;
2017-03-05 23:53:51 +01:00
char *urlpos = (char*)url;
2022-10-10 20:31:15 +02:00
while (*urlpos && remaining) {
remaining--;
2017-03-05 23:53:51 +01:00
if (*urlpos == '/') {
return urlpos;
}
urlpos++;
}
return urlpos;
2017-03-05 23:53:51 +01:00
}
int sock_urlsplit(const char *url, char *hostport, char *urlpath)
{
assert(url);
2017-03-05 23:53:51 +01:00
char *hoststart = _find_hoststart(url);
if (!hoststart) {
return -EINVAL;
}
char *pathstart = _find_pathstart(hoststart);
if (hostport) {
size_t hostlen = pathstart - hoststart;
/* hostlen must be smaller CONFIG_SOCK_HOSTPORT_MAXLEN to have space for the null
* terminator */
if (hostlen > CONFIG_SOCK_HOSTPORT_MAXLEN - 1) {
return -EOVERFLOW;
}
memcpy(hostport, hoststart, hostlen);
hostport[hostlen] = '\0';
}
2017-03-05 23:53:51 +01:00
if (urlpath) {
size_t pathlen = strlen(pathstart);
if (pathlen > CONFIG_SOCK_URLPATH_MAXLEN - 1) {
return -EOVERFLOW;
}
2017-03-05 23:53:51 +01:00
memcpy(urlpath, pathstart, pathlen);
urlpath[pathlen] = '\0';
2017-03-05 23:53:51 +01:00
}
return 0;
}
2022-04-18 01:31:15 +02:00
const char *sock_urlpath(const char *url)
{
assert(url);
char *hoststart = _find_hoststart(url);
if (!hoststart) {
return NULL;
}
return _find_pathstart(hoststart);
}
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] = {0};
for (netifend = netifstart; *netifend && *netifend != ']';
2022-10-10 20:31:15 +02:00
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_tl_str2ep(struct _sock_tl_ep *ep_out, const char *str)
2017-03-05 23:53:51 +01:00
{
unsigned brackets_flag;
char *hoststart = (char*)str;
char *hostend;
2017-03-05 23:53:51 +01:00
char hostbuf[CONFIG_SOCK_HOSTPORT_MAXLEN];
2017-03-05 23:53:51 +01:00
memset(ep_out, 0, sizeof(sock_udp_ep_t));
if (*hoststart == '[') {
brackets_flag = 1;
for (hostend = ++hoststart;
*hostend && *hostend != ']' && *hostend != '%';
2022-10-10 20:31:15 +02:00
hostend++) {}
2018-07-18 15:27:31 +02:00
if (! *hostend || ((size_t)(hostend - hoststart) >= sizeof(hostbuf))) {
2017-03-05 23:53:51 +01:00
/* none found, bail out */
return -EINVAL;
}
}
else {
brackets_flag = 0;
2018-07-18 15:27:31 +02:00
for (hostend = hoststart; *hostend && (*hostend != ':') && \
((size_t)(hostend - hoststart) < sizeof(hostbuf)); hostend++) {}
2017-03-05 23:53:51 +01:00
}
size_t hostlen = hostend - hoststart;
2017-03-05 23:53:51 +01:00
if (*(hostend + brackets_flag) == ':') {
int res = _parse_port(ep_out, hostend + brackets_flag + 1);
if (res < 0) {
return res;
}
}
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;
}
2017-03-05 23:53:51 +01:00
}
if (hostlen >= sizeof(hostbuf)) {
return -EINVAL;
}
memcpy(hostbuf, hoststart, hostlen);
hostbuf[hostlen] = '\0';
if (!brackets_flag) {
#ifdef SOCK_HAS_IPV4
2017-03-05 23:53:51 +01:00
if (inet_pton(AF_INET, hostbuf, &ep_out->addr.ipv4) == 1) {
ep_out->family = AF_INET;
return 0;
}
#endif
2017-03-05 23:53:51 +01:00
}
#ifdef SOCK_HAS_IPV6
if (inet_pton(AF_INET6, hostbuf, ep_out->addr.ipv6) == 1) {
ep_out->family = AF_INET6;
return 0;
2017-03-05 23:53:51 +01:00
}
#endif
2017-03-05 23:53:51 +01:00
return -EINVAL;
}
2018-06-29 17:22:59 +02:00
int sock_tl_name2ep(struct _sock_tl_ep *ep_out, const char *str)
{
int res = sock_tl_str2ep(ep_out, str);
if (res == 0) {
return 0;
}
#if defined(MODULE_SOCK_DNS) || defined(MODULE_SOCK_DNS_MOCK)
int family;
char hostbuf[CONFIG_SOCK_HOSTPORT_MAXLEN];
const char *host;
char *hostend = strchr(str, ':');
if (hostend == NULL) {
host = str;
ep_out->port = 0;
} else {
size_t host_len = hostend - str;
if (host_len >= sizeof(hostbuf)) {
return -EINVAL;
}
memcpy(hostbuf, str, host_len);
hostbuf[host_len] = 0;
host = hostbuf;
ep_out->port = atoi(hostend + 1);;
}
if (IS_ACTIVE(SOCK_HAS_IPV4) && IS_ACTIVE(SOCK_HAS_IPV6)) {
family = AF_UNSPEC;
} else if (IS_ACTIVE(SOCK_HAS_IPV4)) {
family = AF_INET;
} else if (IS_ACTIVE(SOCK_HAS_IPV6)) {
family = AF_INET6;
} else {
assert(0);
return -EINVAL;
}
2022-07-27 14:32:20 +02:00
switch (dns_query(host, &ep_out->addr, family)) {
#ifdef SOCK_HAS_IPV4
case 4:
ep_out->family = AF_INET;
return 0;
#endif
#ifdef SOCK_HAS_IPV6
case 16:
ep_out->family = AF_INET6;
return 0;
#endif
default:
return -EINVAL;
}
#endif
return res;
}
bool sock_tl_ep_equal(const struct _sock_tl_ep *a,
const struct _sock_tl_ep *b)
2018-06-29 17:22:59 +02:00
{
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_IPV4
case AF_INET:
return memcmp(a->addr.ipv4, b->addr.ipv4, 4) == 0;
#endif
#ifdef SOCK_HAS_IPV6
case AF_INET6:
return memcmp(a->addr.ipv6, b->addr.ipv6, 16) == 0;
#endif
default:
return false;
}
2018-06-29 17:22:59 +02:00
}