Blame sunrpc/rpc_gethostbyname.c

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