/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â This is free and unencumbered software released into the public domain.      â
â                                                                              â
â Anyone is free to copy, modify, publish, use, compile, sell, or              â
â distribute this software, either in source code form or as a compiled        â
â binary, for any purpose, commercial or non-commercial, and by any            â
â means.                                                                       â
â                                                                              â
â In jurisdictions that recognize copyright laws, the author or authors        â
â of this software dedicate any and all copyright interest in the              â
â software to the public domain. We make this dedication for the benefit       â
â of the public at large and to the detriment of our heirs and                 â
â successors. We intend this dedication to be an overt act of                  â
â relinquishment in perpetuity of all present and future rights to this        â
â software under copyright law.                                                â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,              â
â EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF           â
â MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       â
â IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR            â
â OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,        â
â ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR        â
â OTHER DEALINGS IN THE SOFTWARE.                                              â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/calls/calls.h"
#include "libc/dns/consts.h"
#include "libc/dns/dns.h"
#include "libc/dns/hoststxt.h"
#include "libc/dns/resolvconf.h"
#include "libc/dns/servicestxt.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/mem/mem.h"
#include "libc/sock/sock.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/inaddr.h"
#include "libc/sysv/errfuns.h"

/**
 * Resolves name/service for socket address.
 *
 * @param addr
 * @param addrlen
 * @param name
 * @param namelen
 * @param service
 * @param servicelen
 * @param flags
 *
 * @return 0 on success or EAI_xxx value
 */
int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *name,
                socklen_t namelen, char *service, socklen_t servicelen,
                int flags) {
  char *p, rdomain[1 + sizeof "255.255.255.255.in-addr.arpa"];
  char info[NI_MAXHOST + 1];
  int rc, port;
  uint8_t *ip;
  unsigned int valid_flags;

  valid_flags =
      (NI_NAMEREQD | NI_NUMERICHOST | NI_NUMERICSERV | NI_NOFQDN | NI_DGRAM);

  if (flags & ~(valid_flags)) return EAI_BADFLAGS;
  if (!name && !service) return EAI_NONAME;
  if (addr->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
    return EAI_FAMILY;

  ip = (uint8_t *)&(((struct sockaddr_in *)addr)->sin_addr);
  p = rdomain;
  p = FormatUint32(p, ip[3]), *p++ = '.';
  p = FormatUint32(p, ip[2]), *p++ = '.';
  p = FormatUint32(p, ip[1]), *p++ = '.';
  p = FormatUint32(p, ip[0]), stpcpy(p, ".in-addr.arpa");
  info[0] = '\0';
  if (name != NULL && namelen != 0) {
    if ((flags & NI_NUMERICHOST) && (flags & NI_NAMEREQD)) return EAI_NONAME;

    if ((flags & NI_NUMERICHOST) &&
        inet_ntop(AF_INET, ip, info, sizeof(info)) == NULL)
      return EAI_SYSTEM;
    else if (!info[0] && ResolveHostsReverse(GetHostsTxt(), AF_INET, ip, info,
                                             sizeof(info)) < 0)
      return EAI_SYSTEM;
    else if (!info[0] && ResolveDnsReverse(GetResolvConf(), AF_INET, rdomain,
                                           info, sizeof(info)) < 0)
      return EAI_SYSTEM;
    else if (!info[0] && (flags & NI_NAMEREQD))
      return EAI_NONAME;
    else if (!info[0] && inet_ntop(AF_INET, ip, info, sizeof(info)) == NULL)
      return EAI_SYSTEM;

    if (strlen(info) + 1 > namelen) return EAI_OVERFLOW;
    strcpy(name, info);
  }

  port = ntohs(((struct sockaddr_in *)addr)->sin_port);
  info[0] = '\0';
  if (service != NULL && servicelen != 0) {
    if ((flags & NI_NUMERICSERV) ||
        LookupServicesByPort(port, ((flags & NI_DGRAM) ? "udp" : "tcp"), 4,
                             info, sizeof(info), NULL) == -1)
      itoa(port, info, 10);
    if (strlen(info) + 1 > servicelen) return EAI_OVERFLOW;
    strcpy(service, info);
  }

  return 0;
}
