Home | History | Annotate | Line # | Download | only in lib
getaddrinfo.c revision 1.1.1.1.26.1
      1           1.1  christos /* Get address information (partial implementation).
      2           1.1  christos    Copyright (C) 1997, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
      3           1.1  christos    Contributed by Simon Josefsson <simon (at) josefsson.org>.
      4           1.1  christos 
      5           1.1  christos    This program is free software; you can redistribute it and/or modify
      6           1.1  christos    it under the terms of the GNU General Public License as published by
      7           1.1  christos    the Free Software Foundation; either version 2, or (at your option)
      8           1.1  christos    any later version.
      9           1.1  christos 
     10           1.1  christos    This program is distributed in the hope that it will be useful,
     11           1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12           1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13           1.1  christos    GNU General Public License for more details.
     14           1.1  christos 
     15           1.1  christos    You should have received a copy of the GNU General Public License
     16           1.1  christos    along with this program; if not, write to the Free Software Foundation,
     17           1.1  christos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
     18  1.1.1.1.26.1       snj #include <sys/cdefs.h>
     19  1.1.1.1.26.1       snj __RCSID("$NetBSD: getaddrinfo.c,v 1.1.1.1.26.1 2017/05/13 06:23:23 snj Exp $");
     20  1.1.1.1.26.1       snj 
     21           1.1  christos 
     22           1.1  christos #ifdef HAVE_CONFIG_H
     23           1.1  christos # include <config.h>
     24           1.1  christos #endif
     25           1.1  christos 
     26           1.1  christos #include "getaddrinfo.h"
     27           1.1  christos 
     28           1.1  christos /* Get calloc. */
     29           1.1  christos #include <stdlib.h>
     30           1.1  christos 
     31           1.1  christos /* Get memcpy. */
     32           1.1  christos #include <string.h>
     33           1.1  christos 
     34           1.1  christos #include <stdbool.h>
     35           1.1  christos 
     36           1.1  christos #include "gettext.h"
     37           1.1  christos #define _(String) gettext (String)
     38           1.1  christos #define N_(String) String
     39           1.1  christos 
     40           1.1  christos #include "strdup.h"
     41           1.1  christos 
     42           1.1  christos static inline bool
     43           1.1  christos validate_family (int family)
     44           1.1  christos {
     45           1.1  christos   /* FIXME: Support more families. */
     46           1.1  christos #if HAVE_IPV4
     47           1.1  christos      if (family == PF_INET)
     48           1.1  christos        return true;
     49           1.1  christos #endif
     50           1.1  christos #if HAVE_IPV6
     51           1.1  christos      if (family == PF_INET6)
     52           1.1  christos        return true;
     53           1.1  christos #endif
     54           1.1  christos      if (family == PF_UNSPEC)
     55           1.1  christos        return true;
     56           1.1  christos      return false;
     57           1.1  christos }
     58           1.1  christos 
     59           1.1  christos /* Translate name of a service location and/or a service name to set of
     60           1.1  christos    socket addresses. */
     61           1.1  christos int
     62           1.1  christos getaddrinfo (const char *restrict nodename,
     63           1.1  christos 	     const char *restrict servname,
     64           1.1  christos 	     const struct addrinfo *restrict hints,
     65           1.1  christos 	     struct addrinfo **restrict res)
     66           1.1  christos {
     67           1.1  christos   struct addrinfo *tmp;
     68           1.1  christos   struct servent *se;
     69           1.1  christos   struct hostent *he;
     70           1.1  christos   size_t sinlen;
     71           1.1  christos 
     72           1.1  christos   if (hints && (hints->ai_flags & ~AI_CANONNAME))
     73           1.1  christos     /* FIXME: Support more flags. */
     74           1.1  christos     return EAI_BADFLAGS;
     75           1.1  christos 
     76           1.1  christos   if (hints && !validate_family (hints->ai_family))
     77           1.1  christos     return EAI_FAMILY;
     78           1.1  christos 
     79           1.1  christos   if (hints &&
     80           1.1  christos       hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
     81           1.1  christos     /* FIXME: Support other socktype. */
     82           1.1  christos     return EAI_SOCKTYPE; /* FIXME: Better return code? */
     83           1.1  christos 
     84           1.1  christos   if (!nodename)
     85           1.1  christos     /* FIXME: Support server bind mode. */
     86           1.1  christos     return EAI_NONAME;
     87           1.1  christos 
     88           1.1  christos   if (servname)
     89           1.1  christos     {
     90           1.1  christos       const char *proto =
     91           1.1  christos 	(hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
     92           1.1  christos 
     93           1.1  christos       /* FIXME: Use getservbyname_r if available. */
     94           1.1  christos       se = getservbyname (servname, proto);
     95           1.1  christos 
     96           1.1  christos       if (!se)
     97           1.1  christos 	return EAI_SERVICE;
     98           1.1  christos     }
     99           1.1  christos 
    100           1.1  christos   /* FIXME: Use gethostbyname_r if available. */
    101           1.1  christos   he = gethostbyname (nodename);
    102           1.1  christos   if (!he || he->h_addr_list[0] == NULL)
    103           1.1  christos     return EAI_NONAME;
    104           1.1  christos 
    105           1.1  christos   switch (he->h_addrtype)
    106           1.1  christos     {
    107           1.1  christos #if HAVE_IPV6
    108           1.1  christos     case PF_INET6:
    109           1.1  christos       sinlen = sizeof (struct sockaddr_in6);
    110           1.1  christos       break;
    111           1.1  christos #endif
    112           1.1  christos 
    113           1.1  christos #if HAVE_IPV4
    114           1.1  christos     case PF_INET:
    115           1.1  christos       sinlen = sizeof (struct sockaddr_in);
    116           1.1  christos       break;
    117           1.1  christos #endif
    118           1.1  christos 
    119           1.1  christos     default:
    120           1.1  christos       return EAI_NODATA;
    121           1.1  christos     }
    122           1.1  christos 
    123           1.1  christos   tmp = calloc (1, sizeof (*tmp) + sinlen);
    124           1.1  christos   if (!tmp)
    125           1.1  christos     return EAI_MEMORY;
    126           1.1  christos 
    127           1.1  christos   switch (he->h_addrtype)
    128           1.1  christos     {
    129           1.1  christos #if HAVE_IPV6
    130           1.1  christos     case PF_INET6:
    131           1.1  christos       {
    132           1.1  christos 	struct sockaddr_in6 *sinp = (char *) tmp + sizeof (*tmp);
    133           1.1  christos 
    134           1.1  christos 	if (se)
    135           1.1  christos 	  sinp->sin6_port = se->s_port;
    136           1.1  christos 
    137           1.1  christos 	if (he->h_length != sizeof (sinp->sin6_addr))
    138           1.1  christos 	  return EAI_SYSTEM; /* FIXME: Better return code?  Set errno? */
    139           1.1  christos 
    140           1.1  christos 	memcpy (&sinp->sin6_addr, he->h_addr_list[0], he->h_length);
    141           1.1  christos 
    142           1.1  christos 	tmp->ai_addr = (struct sockaddr *) sinp;
    143           1.1  christos 	tmp->ai_addrlen = sinlen;
    144           1.1  christos       }
    145           1.1  christos       break;
    146           1.1  christos #endif
    147           1.1  christos 
    148           1.1  christos #if HAVE_IPV4
    149           1.1  christos     case PF_INET:
    150           1.1  christos       {
    151           1.1  christos 	struct sockaddr_in *sinp = (char *) tmp + sizeof (*tmp);
    152           1.1  christos 
    153           1.1  christos 	if (se)
    154           1.1  christos 	  sinp->sin_port = se->s_port;
    155           1.1  christos 
    156           1.1  christos 	if (he->h_length != sizeof (sinp->sin_addr))
    157           1.1  christos 	  return EAI_SYSTEM; /* FIXME: Better return code?  Set errno? */
    158           1.1  christos 
    159           1.1  christos 	memcpy (&sinp->sin_addr, he->h_addr_list[0], he->h_length);
    160           1.1  christos 
    161           1.1  christos 	tmp->ai_addr = (struct sockaddr *) sinp;
    162           1.1  christos 	tmp->ai_addrlen = sinlen;
    163           1.1  christos       }
    164           1.1  christos       break;
    165           1.1  christos #endif
    166           1.1  christos 
    167           1.1  christos     default:
    168           1.1  christos       free (tmp);
    169           1.1  christos       return EAI_NODATA;
    170           1.1  christos     }
    171           1.1  christos 
    172           1.1  christos   if (hints && hints->ai_flags & AI_CANONNAME)
    173           1.1  christos     {
    174           1.1  christos       const char *cn;
    175           1.1  christos       if (he->h_name)
    176           1.1  christos 	cn = he->h_name;
    177           1.1  christos       else
    178           1.1  christos 	cn = nodename;
    179           1.1  christos 
    180           1.1  christos       tmp->ai_canonname = strdup (cn);
    181           1.1  christos       if (!tmp->ai_canonname)
    182           1.1  christos 	{
    183           1.1  christos 	  free (tmp);
    184           1.1  christos 	  return EAI_MEMORY;
    185           1.1  christos 	}
    186           1.1  christos     }
    187           1.1  christos 
    188           1.1  christos   tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
    189           1.1  christos   tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
    190           1.1  christos   tmp->ai_addr->sa_family = he->h_addrtype;
    191           1.1  christos 
    192           1.1  christos   /* FIXME: If more than one address, create linked list of addrinfo's. */
    193           1.1  christos 
    194           1.1  christos   *res = tmp;
    195           1.1  christos 
    196           1.1  christos   return 0;
    197           1.1  christos }
    198           1.1  christos 
    199           1.1  christos /* Free `addrinfo' structure AI including associated storage.  */
    200           1.1  christos void
    201           1.1  christos freeaddrinfo (struct addrinfo *ai)
    202           1.1  christos {
    203           1.1  christos   while (ai)
    204           1.1  christos     {
    205           1.1  christos       struct addrinfo *cur;
    206           1.1  christos 
    207           1.1  christos       cur = ai;
    208           1.1  christos       ai = ai->ai_next;
    209           1.1  christos 
    210           1.1  christos       if (cur->ai_canonname) free (cur->ai_canonname);
    211           1.1  christos       free (cur);
    212           1.1  christos     }
    213           1.1  christos }
    214