Back to index

nagios-plugins  1.4.16
Defines | Functions
getaddrinfo.c File Reference
#include <config.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "gettext.h"

Go to the source code of this file.

Defines

#define _GL_ARG_NONNULL(params)
#define _(String)   gettext (String)
#define N_(String)   String
#define PF_INET   AF_INET
#define PF_UNSPEC   0

Functions

static bool validate_family (int family)
int getaddrinfo (const char *restrict nodename, const char *restrict servname, const struct addrinfo *restrict hints, struct addrinfo **restrict res)
void freeaddrinfo (struct addrinfo *ai)
int getnameinfo (const struct sockaddr *restrict sa, socklen_t salen, char *restrict node, socklen_t nodelen, char *restrict service, socklen_t servicelen, int flags)

Define Documentation

#define _ (   String)    gettext (String)

Definition at line 46 of file getaddrinfo.c.

#define _GL_ARG_NONNULL (   params)

Definition at line 23 of file getaddrinfo.c.

#define N_ (   String)    String

Definition at line 47 of file getaddrinfo.c.

#define PF_INET   AF_INET

Definition at line 51 of file getaddrinfo.c.

#define PF_UNSPEC   0

Definition at line 55 of file getaddrinfo.c.


Function Documentation

void freeaddrinfo ( struct addrinfo *  ai)

Definition at line 332 of file getaddrinfo.c.

{
#ifdef WIN32_NATIVE
  if (use_win32_p ())
    {
      freeaddrinfo_ptr (ai);
      return;
    }
#endif

  while (ai)
    {
      struct addrinfo *cur;

      cur = ai;
      ai = ai->ai_next;

      free (cur->ai_canonname);
      free (cur);
    }
}

Here is the caller graph for this function:

int getaddrinfo ( const char *restrict  nodename,
const char *restrict  servname,
const struct addrinfo *restrict  hints,
struct addrinfo **restrict  res 
)

Definition at line 128 of file getaddrinfo.c.

{
  struct addrinfo *tmp;
  int port = 0;
  struct hostent *he;
  void *storage;
  size_t size;
#if HAVE_IPV6
  struct v6_pair {
    struct addrinfo addrinfo;
    struct sockaddr_in6 sockaddr_in6;
  };
#endif
#if HAVE_IPV4
  struct v4_pair {
    struct addrinfo addrinfo;
    struct sockaddr_in sockaddr_in;
  };
#endif

#ifdef WIN32_NATIVE
  if (use_win32_p ())
    return getaddrinfo_ptr (nodename, servname, hints, res);
#endif

  if (hints && (hints->ai_flags & ~(AI_CANONNAME|AI_PASSIVE)))
    /* FIXME: Support more flags. */
    return EAI_BADFLAGS;

  if (hints && !validate_family (hints->ai_family))
    return EAI_FAMILY;

  if (hints &&
      hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
    /* FIXME: Support other socktype. */
    return EAI_SOCKTYPE; /* FIXME: Better return code? */

  if (!nodename)
    {
      if (!(hints->ai_flags & AI_PASSIVE))
        return EAI_NONAME;

#ifdef HAVE_IPV6
      nodename = (hints->ai_family == AF_INET6) ? "::" : "0.0.0.0";
#else
      nodename = "0.0.0.0";
#endif
    }

  if (servname)
    {
      struct servent *se = NULL;
      const char *proto =
        (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";

      if (hints == NULL || !(hints->ai_flags & AI_NUMERICSERV))
        /* FIXME: Use getservbyname_r if available. */
        se = getservbyname (servname, proto);

      if (!se)
        {
          char *c;
          if (!(*servname >= '0' && *servname <= '9'))
            return EAI_NONAME;
          port = strtoul (servname, &c, 10);
          if (*c || port > 0xffff)
            return EAI_NONAME;
          port = htons (port);
        }
      else
        port = se->s_port;
    }

  /* FIXME: Use gethostbyname_r if available. */
  he = gethostbyname (nodename);
  if (!he || he->h_addr_list[0] == NULL)
    return EAI_NONAME;

  switch (he->h_addrtype)
    {
#if HAVE_IPV6
    case PF_INET6:
      size = sizeof (struct v6_pair);
      break;
#endif

#if HAVE_IPV4
    case PF_INET:
      size = sizeof (struct v4_pair);
      break;
#endif

    default:
      return EAI_NODATA;
    }

  storage = calloc (1, size);
  if (!storage)
    return EAI_MEMORY;

  switch (he->h_addrtype)
    {
#if HAVE_IPV6
    case PF_INET6:
      {
        struct v6_pair *p = storage;
        struct sockaddr_in6 *sinp = &p->sockaddr_in6;
        tmp = &p->addrinfo;

        if (port)
          sinp->sin6_port = port;

        if (he->h_length != sizeof (sinp->sin6_addr))
          {
            free (storage);
            return EAI_SYSTEM; /* FIXME: Better return code?  Set errno? */
          }

        memcpy (&sinp->sin6_addr, he->h_addr_list[0], sizeof sinp->sin6_addr);

        tmp->ai_addr = (struct sockaddr *) sinp;
        tmp->ai_addrlen = sizeof *sinp;
      }
      break;
#endif

#if HAVE_IPV4
    case PF_INET:
      {
        struct v4_pair *p = storage;
        struct sockaddr_in *sinp = &p->sockaddr_in;
        tmp = &p->addrinfo;

        if (port)
          sinp->sin_port = port;

        if (he->h_length != sizeof (sinp->sin_addr))
          {
            free (storage);
            return EAI_SYSTEM; /* FIXME: Better return code?  Set errno? */
          }

        memcpy (&sinp->sin_addr, he->h_addr_list[0], sizeof sinp->sin_addr);

        tmp->ai_addr = (struct sockaddr *) sinp;
        tmp->ai_addrlen = sizeof *sinp;
      }
      break;
#endif

    default:
      free (storage);
      return EAI_NODATA;
    }

  if (hints && hints->ai_flags & AI_CANONNAME)
    {
      const char *cn;
      if (he->h_name)
        cn = he->h_name;
      else
        cn = nodename;

      tmp->ai_canonname = strdup (cn);
      if (!tmp->ai_canonname)
        {
          free (storage);
          return EAI_MEMORY;
        }
    }

  tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
  tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
  tmp->ai_addr->sa_family = he->h_addrtype;
  tmp->ai_family = he->h_addrtype;

#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
  switch (he->h_addrtype)
    {
#if HAVE_IPV4
    case AF_INET:
      tmp->ai_addr->sa_len = sizeof (struct sockaddr_in);
      break;
#endif
#if HAVE_IPV6
    case AF_INET6:
      tmp->ai_addr->sa_len = sizeof (struct sockaddr_in6);
      break;
#endif
    }
#endif

  /* FIXME: If more than one address, create linked list of addrinfo's. */

  *res = tmp;

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int getnameinfo ( const struct sockaddr *restrict  sa,
socklen_t  salen,
char *restrict  node,
socklen_t  nodelen,
char *restrict  service,
socklen_t  servicelen,
int  flags 
)

Definition at line 355 of file getaddrinfo.c.

{
#ifdef WIN32_NATIVE
  if (use_win32_p ())
    return getnameinfo_ptr (sa, salen, node, nodelen,
                            service, servicelen, flags);
#endif

  /* FIXME: Support other flags. */
  if ((node && nodelen > 0 && !(flags & NI_NUMERICHOST)) ||
      (service && servicelen > 0 && !(flags & NI_NUMERICHOST)) ||
      (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV)))
    return EAI_BADFLAGS;

  if (sa == NULL || salen < sizeof (sa->sa_family))
    return EAI_FAMILY;

  switch (sa->sa_family)
    {
#if HAVE_IPV4
    case AF_INET:
      if (salen < sizeof (struct sockaddr_in))
        return EAI_FAMILY;
      break;
#endif
#if HAVE_IPV6
    case AF_INET6:
      if (salen < sizeof (struct sockaddr_in6))
        return EAI_FAMILY;
      break;
#endif
    default:
      return EAI_FAMILY;
    }

  if (node && nodelen > 0 && flags & NI_NUMERICHOST)
    {
      switch (sa->sa_family)
        {
#if HAVE_IPV4
        case AF_INET:
          if (!inet_ntop (AF_INET,
                          &(((const struct sockaddr_in *) sa)->sin_addr),
                          node, nodelen))
            return EAI_SYSTEM;
          break;
#endif

#if HAVE_IPV6
        case AF_INET6:
          if (!inet_ntop (AF_INET6,
                          &(((const struct sockaddr_in6 *) sa)->sin6_addr),
                          node, nodelen))
            return EAI_SYSTEM;
          break;
#endif

        default:
          return EAI_FAMILY;
        }
    }

  if (service && servicelen > 0 && flags & NI_NUMERICSERV)
    switch (sa->sa_family)
      {
#if HAVE_IPV4
      case AF_INET:
#endif
#if HAVE_IPV6
      case AF_INET6:
#endif
        {
          unsigned short int port
            = ntohs (((const struct sockaddr_in *) sa)->sin_port);
          if (servicelen <= snprintf (service, servicelen, "%u", port))
            return EAI_OVERFLOW;
        }
        break;
      }

  return 0;
}

Here is the call graph for this function:

static bool validate_family ( int  family) [inline, static]

Definition at line 109 of file getaddrinfo.c.

{
  /* FIXME: Support more families. */
#if HAVE_IPV4
     if (family == PF_INET)
       return true;
#endif
#if HAVE_IPV6
     if (family == PF_INET6)
       return true;
#endif
     if (family == PF_UNSPEC)
       return true;
     return false;
}

Here is the caller graph for this function: