hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame sunrpc/rpc_gethostbyname.c

Packit 6c4009
/* IPv4-only variant of gethostbyname.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public License as
Packit 6c4009
   published by the Free Software Foundation; either version 2.1 of the
Packit 6c4009
   License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <rpc/rpc.h>
Packit 6c4009
#include <scratch_buffer.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__libc_rpc_gethostbyname (const char *host, struct sockaddr_in *addr)
Packit 6c4009
{
Packit 6c4009
  struct hostent hostbuf;
Packit 6c4009
  struct hostent *hp = NULL;
Packit 6c4009
  int herr;
Packit 6c4009
  struct scratch_buffer tmpbuf;
Packit 6c4009
  scratch_buffer_init (&tmpbuf);
Packit 6c4009
Packit 6c4009
  while (__gethostbyname2_r (host, AF_INET,
Packit 6c4009
                             &hostbuf, tmpbuf.data, tmpbuf.length, &hp,
Packit 6c4009
                             &herr) != 0
Packit 6c4009
         || hp == NULL)
Packit 6c4009
    if (herr != NETDB_INTERNAL || errno != ERANGE)
Packit 6c4009
      {
Packit 6c4009
        struct rpc_createerr *ce = &get_rpc_createerr ();
Packit 6c4009
        ce->cf_stat = RPC_UNKNOWNHOST;
Packit 6c4009
        scratch_buffer_free (&tmpbuf);
Packit 6c4009
        return -1;
Packit 6c4009
      }
Packit 6c4009
    else
Packit 6c4009
      {
Packit 6c4009
        if (!scratch_buffer_grow (&tmpbuf))
Packit 6c4009
          {
Packit 6c4009
            /* If memory allocation failed, allocating the RPC error
Packit 6c4009
               structure might could as well, so this could lead to a
Packit 6c4009
               crash.  */
Packit 6c4009
            struct rpc_createerr *ce = &get_rpc_createerr ();
Packit 6c4009
            ce->cf_stat = RPC_SYSTEMERROR;
Packit 6c4009
            ce->cf_error.re_errno = ENOMEM;
Packit 6c4009
            return -1;
Packit 6c4009
          }
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  if (hp->h_addrtype != AF_INET || hp->h_length != sizeof (addr->sin_addr))
Packit 6c4009
    {
Packit 6c4009
      struct rpc_createerr *ce = &get_rpc_createerr ();
Packit 6c4009
      ce->cf_stat = RPC_SYSTEMERROR;
Packit 6c4009
      ce->cf_error.re_errno = EAFNOSUPPORT;
Packit 6c4009
      scratch_buffer_free (&tmpbuf);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  addr->sin_family = AF_INET;
Packit 6c4009
  addr->sin_port = htons (0);
Packit 6c4009
  memcpy (&addr->sin_addr, hp->h_addr, sizeof (addr->sin_addr));
Packit 6c4009
  scratch_buffer_free (&tmpbuf);
Packit 6c4009
  return 0;
Packit 6c4009
}