2019-06-30 23:12:34 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* libs/libc/netdb/lib_getnameinfo.c
|
2019-06-30 23:12:34 +02:00
|
|
|
*
|
2021-03-02 09:55:05 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-06-30 23:12:34 +02:00
|
|
|
*
|
2021-03-02 09:55:05 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-06-30 23:12:34 +02:00
|
|
|
*
|
2021-03-02 09:55:05 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-06-30 23:12:34 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "netdb/lib_netdb.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_LIBC_NETDB
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: getnameinfo
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int getnameinfo(FAR const struct sockaddr *addr, socklen_t addrlen,
|
|
|
|
FAR char *host, socklen_t hostlen,
|
|
|
|
FAR char *serv, socklen_t servlen, int flags)
|
|
|
|
{
|
|
|
|
FAR const void *saddr;
|
|
|
|
socklen_t saddr_len;
|
|
|
|
int port;
|
|
|
|
int ret;
|
|
|
|
|
2020-03-29 19:22:29 +02:00
|
|
|
if (addr && addr->sa_family == AF_INET &&
|
2019-06-30 23:12:34 +02:00
|
|
|
addrlen == sizeof(struct sockaddr_in))
|
|
|
|
{
|
2020-03-29 19:22:29 +02:00
|
|
|
FAR const struct sockaddr_in *sa_in;
|
2019-06-30 23:12:34 +02:00
|
|
|
|
2020-03-29 19:22:29 +02:00
|
|
|
sa_in = (FAR const struct sockaddr_in *)addr;
|
2022-01-18 08:38:00 +01:00
|
|
|
port = NTOHS(sa_in->sin_port);
|
2019-06-30 23:12:34 +02:00
|
|
|
saddr = &sa_in->sin_addr;
|
|
|
|
saddr_len = sizeof(sa_in->sin_addr);
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2020-03-29 19:22:29 +02:00
|
|
|
else if (addr && addr->sa_family == AF_INET6 &&
|
2019-06-30 23:12:34 +02:00
|
|
|
addrlen == sizeof(struct sockaddr_in6))
|
|
|
|
{
|
2020-03-29 19:22:29 +02:00
|
|
|
FAR const struct sockaddr_in6 *sa_in6;
|
2019-06-30 23:12:34 +02:00
|
|
|
|
2020-03-29 19:22:29 +02:00
|
|
|
sa_in6 = (FAR const struct sockaddr_in6 *)addr;
|
2022-01-18 08:38:00 +01:00
|
|
|
port = NTOHS(sa_in6->sin6_port);
|
2019-06-30 23:12:34 +02:00
|
|
|
saddr = &sa_in6->sin6_addr;
|
|
|
|
saddr_len = sizeof(sa_in6->sin6_addr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return EAI_FAMILY;
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:22:29 +02:00
|
|
|
if (host && !(flags & NI_NUMERICHOST))
|
2019-06-30 23:12:34 +02:00
|
|
|
{
|
|
|
|
struct hostent hostent;
|
2020-03-31 20:21:04 +02:00
|
|
|
FAR struct hostent *res;
|
2020-04-02 07:36:57 +02:00
|
|
|
int error;
|
2019-06-30 23:12:34 +02:00
|
|
|
|
2020-03-30 14:49:28 +02:00
|
|
|
ret = gethostbyaddr_r(saddr, saddr_len, addr->sa_family, &hostent,
|
2020-04-02 07:36:57 +02:00
|
|
|
host, hostlen, &res, &error);
|
2019-06-30 23:12:34 +02:00
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2020-03-31 20:21:04 +02:00
|
|
|
size_t sz = strlen(res->h_name) + 1;
|
2019-06-30 23:12:34 +02:00
|
|
|
|
|
|
|
if (sz <= hostlen)
|
|
|
|
{
|
2020-03-31 20:21:04 +02:00
|
|
|
memmove(host, res->h_name, sz);
|
2019-06-30 23:12:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-31 20:21:04 +02:00
|
|
|
memmove(host, res->h_name, hostlen);
|
2019-06-30 23:12:34 +02:00
|
|
|
host[hostlen - 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-02 07:36:57 +02:00
|
|
|
switch (error)
|
2019-06-30 23:12:34 +02:00
|
|
|
{
|
|
|
|
case HOST_NOT_FOUND:
|
|
|
|
{
|
|
|
|
if (flags & NI_NAMEREQD)
|
|
|
|
{
|
|
|
|
return EAI_NONAME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NO_RECOVERY:
|
|
|
|
{
|
|
|
|
if (flags & NI_NAMEREQD)
|
|
|
|
{
|
|
|
|
return EAI_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NO_DATA:
|
|
|
|
case TRY_AGAIN:
|
|
|
|
{
|
|
|
|
if (flags & NI_NAMEREQD)
|
|
|
|
{
|
|
|
|
return EAI_AGAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-07-13 22:48:02 +02:00
|
|
|
DEBUGPANIC();
|
2019-06-30 23:12:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fall-back to numeric for the host name. */
|
|
|
|
|
|
|
|
flags |= NI_NUMERICHOST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:22:29 +02:00
|
|
|
if (host && (flags & NI_NUMERICHOST))
|
2019-06-30 23:12:34 +02:00
|
|
|
{
|
|
|
|
if (!inet_ntop(addr->sa_family, saddr, host, hostlen))
|
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
switch (get_errno())
|
2019-06-30 23:12:34 +02:00
|
|
|
{
|
|
|
|
case ENOSPC:
|
|
|
|
return EAI_OVERFLOW;
|
|
|
|
|
|
|
|
default:
|
2022-07-13 22:48:02 +02:00
|
|
|
DEBUGPANIC();
|
2019-06-30 23:12:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:22:29 +02:00
|
|
|
if (serv && !(flags & NI_NUMERICSERV))
|
2019-06-30 23:12:34 +02:00
|
|
|
{
|
|
|
|
struct servent servent;
|
2020-03-29 19:22:29 +02:00
|
|
|
FAR struct servent *result;
|
2019-06-30 23:12:34 +02:00
|
|
|
|
2020-03-29 12:31:45 +02:00
|
|
|
ret = getservbyport_r(port, flags & NI_DGRAM ? "udp" : "tcp",
|
|
|
|
&servent, serv, servlen, &result);
|
2019-06-30 23:12:34 +02:00
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
size_t sz = strlen(servent.s_name) + 1;
|
|
|
|
|
|
|
|
if (sz <= servlen)
|
|
|
|
{
|
|
|
|
memmove(serv, servent.s_name, sz);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memmove(serv, servent.s_name, servlen);
|
|
|
|
serv[servlen - 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Fall-back to numeric for the host name. */
|
|
|
|
|
|
|
|
flags |= NI_NUMERICSERV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:22:29 +02:00
|
|
|
if (serv && (flags & NI_NUMERICSERV))
|
2019-06-30 23:12:34 +02:00
|
|
|
{
|
|
|
|
snprintf(serv, servlen, "%d", port);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_LIBC_NETDB */
|