net: sockets: Implement getnameinfo()
This function is the opposite of getaddrinfo(), i.e. converts struct sockaddr into a textual address. Normally (or more specifically, based on the flags) it would perform reverse DNS lookup, but current implementation implements only subset of functionality, by converting to numeric textual address. Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
This commit is contained in:
parent
fcced0c489
commit
87b5eb9fce
|
@ -217,6 +217,16 @@ static inline void zsock_freeaddrinfo(struct zsock_addrinfo *ai)
|
||||||
free(ai);
|
free(ai);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define NI_NUMERICHOST 1
|
||||||
|
#define NI_NUMERICSERV 2
|
||||||
|
#define NI_NOFQDN 4
|
||||||
|
#define NI_NAMEREQD 8
|
||||||
|
#define NI_DGRAM 16
|
||||||
|
|
||||||
|
int zsock_getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
|
||||||
|
char *host, socklen_t hostlen,
|
||||||
|
char *serv, socklen_t servlen, int flags);
|
||||||
|
|
||||||
#if defined(CONFIG_NET_SOCKETS_POSIX_NAMES)
|
#if defined(CONFIG_NET_SOCKETS_POSIX_NAMES)
|
||||||
|
|
||||||
#define pollfd zsock_pollfd
|
#define pollfd zsock_pollfd
|
||||||
|
@ -343,6 +353,14 @@ static inline void freeaddrinfo(struct zsock_addrinfo *ai)
|
||||||
zsock_freeaddrinfo(ai);
|
zsock_freeaddrinfo(ai);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
|
||||||
|
char *host, socklen_t hostlen,
|
||||||
|
char *serv, socklen_t servlen, int flags)
|
||||||
|
{
|
||||||
|
return zsock_getnameinfo(addr, addrlen, host, hostlen,
|
||||||
|
serv, servlen, flags);
|
||||||
|
}
|
||||||
|
|
||||||
#define addrinfo zsock_addrinfo
|
#define addrinfo zsock_addrinfo
|
||||||
|
|
||||||
static inline int gethostname(char *buf, size_t len)
|
static inline int gethostname(char *buf, size_t len)
|
||||||
|
|
|
@ -3,6 +3,7 @@ zephyr_include_directories(.)
|
||||||
if(NOT CONFIG_NET_SOCKETS_OFFLOAD)
|
if(NOT CONFIG_NET_SOCKETS_OFFLOAD)
|
||||||
zephyr_sources(
|
zephyr_sources(
|
||||||
getaddrinfo.c
|
getaddrinfo.c
|
||||||
|
getnameinfo.c
|
||||||
sockets.c
|
sockets.c
|
||||||
sockets_select.c
|
sockets_select.c
|
||||||
sockets_misc.c
|
sockets_misc.c
|
||||||
|
|
32
subsys/net/lib/sockets/getnameinfo.c
Normal file
32
subsys/net/lib/sockets/getnameinfo.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2019 Linaro Limited
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <net/socket.h>
|
||||||
|
|
||||||
|
int zsock_getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
|
||||||
|
char *host, socklen_t hostlen,
|
||||||
|
char *serv, socklen_t servlen, int flags)
|
||||||
|
{
|
||||||
|
/* Both sockaddr_in & _in6 have same offsets for family and address. */
|
||||||
|
struct sockaddr_in *a = (struct sockaddr_in *)addr;
|
||||||
|
|
||||||
|
if (host != NULL) {
|
||||||
|
void *res = zsock_inet_ntop(a->sin_family, &a->sin_addr,
|
||||||
|
host, hostlen);
|
||||||
|
|
||||||
|
if (res == NULL) {
|
||||||
|
return DNS_EAI_SYSTEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serv != NULL) {
|
||||||
|
snprintf(serv, servlen, "%hu", ntohs(a->sin_port));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in a new issue