zephyr/subsys/net/lib/sockets/getnameinfo.c
Paul Sokolovsky 87b5eb9fce 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>
2019-02-19 17:34:57 -05:00

33 lines
691 B
C

/*
* 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;
}