mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
netutils: add netutils_get_ipv6()
This commit is contained in:
parent
934c875aba
commit
3081eeb556
@ -113,6 +113,9 @@ endif
|
||||
ifneq (,$(filter netstats_neighbor,$(USEMODULE)))
|
||||
DIRS += net/netstats
|
||||
endif
|
||||
ifneq (,$(filter netutils,$(USEMODULE)))
|
||||
DIRS += net/netutils
|
||||
endif
|
||||
ifneq (,$(filter sema,$(USEMODULE)))
|
||||
DIRS += sema
|
||||
endif
|
||||
|
@ -185,6 +185,10 @@ ifneq (,$(filter od_string,$(USEMODULE)))
|
||||
USEMODULE += od
|
||||
endif
|
||||
|
||||
ifneq (,$(filter netutils,$(USEMODULE)))
|
||||
USEMODULE += ipv6_addr
|
||||
endif
|
||||
|
||||
ifneq (,$(filter newlib_gnu_source,$(USEMODULE)))
|
||||
FEATURES_REQUIRED += newlib
|
||||
endif
|
||||
|
52
sys/include/net/utils.h
Normal file
52
sys/include/net/utils.h
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (C) 2021 ML!PA Consulting GmbH
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup net_utils Network helper functions
|
||||
* @ingroup net
|
||||
* @brief Common network helper functions
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Common network interface API definitions
|
||||
*
|
||||
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
|
||||
*/
|
||||
|
||||
#ifndef NET_UTILS_H
|
||||
#define NET_UTILS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "net/ipv6/addr.h"
|
||||
#include "net/netif.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Parse an IPv6 address / hostname string.
|
||||
* If the @ref net_sock_dns module is used, this will
|
||||
* attempt to resolve hostnames via DNS to IPv6 addresses.
|
||||
*
|
||||
* @param[out] addr IPv6 address of the host
|
||||
* @param[out] netif Interface if address is link-local
|
||||
* @param[in] hostname IPv6 address string or hostname
|
||||
*
|
||||
* @return 0 on success, error otherwise
|
||||
*/
|
||||
int netutils_get_ipv6(ipv6_addr_t *addr, netif_t **netif, const char *hostname);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* NET_UTILS_H */
|
||||
/** @} */
|
3
sys/net/netutils/Makefile
Normal file
3
sys/net/netutils/Makefile
Normal file
@ -0,0 +1,3 @@
|
||||
MODULE = netutils
|
||||
|
||||
include $(RIOTBASE)/Makefile.base
|
78
sys/net/netutils/util.c
Normal file
78
sys/net/netutils/util.c
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (C) 2021 ML!PA Consulting GmbH
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "net/utils.h"
|
||||
#ifdef MODULE_SOCK_DNS
|
||||
#include "net/af.h"
|
||||
#include "net/sock/dns.h"
|
||||
#endif
|
||||
|
||||
/* get the next netif, returns true if there are more */
|
||||
static bool _netif_get(netif_t **current_netif)
|
||||
{
|
||||
netif_t *netif = *current_netif;
|
||||
netif = netif_iter(netif);
|
||||
|
||||
*current_netif = netif;
|
||||
return netif_iter(netif);
|
||||
}
|
||||
|
||||
int netutils_get_ipv6(ipv6_addr_t *addr, netif_t **netif, const char *hostname)
|
||||
{
|
||||
*netif = NULL;
|
||||
|
||||
if (hostname == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
#ifdef MODULE_SOCK_DNS
|
||||
/* hostname is not an IPv6 address */
|
||||
if (strchr(hostname, ':') == NULL) {
|
||||
int res = sock_dns_query(hostname, addr, AF_INET6);
|
||||
if (res < 0) {
|
||||
return res;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* search for interface ID */
|
||||
size_t len = strlen(hostname);
|
||||
char *iface = strchr(hostname, '%');
|
||||
if (iface) {
|
||||
*netif = netif_get_by_id(atoi(iface + 1));
|
||||
len -= strlen(iface);
|
||||
|
||||
if (*netif == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
/* preliminary select the first interface */
|
||||
else if (_netif_get(netif)) {
|
||||
/* don't take it if there is more than one interface */
|
||||
*netif = NULL;
|
||||
}
|
||||
|
||||
if (ipv6_addr_from_buf(addr, hostname, len) == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
/** @} */
|
Loading…
Reference in New Issue
Block a user