Blame sunrpc/get_myaddr.c

Packit Service 82fcde
/*
Packit Service 82fcde
 * get_myaddress.c
Packit Service 82fcde
 *
Packit Service 82fcde
 * Get client's IP address via ioctl.  This avoids using the yellowpages.
Packit Service 82fcde
 * Copyright (c) 2010, Oracle America, Inc.
Packit Service 82fcde
 *
Packit Service 82fcde
 * Redistribution and use in source and binary forms, with or without
Packit Service 82fcde
 * modification, are permitted provided that the following conditions are
Packit Service 82fcde
 * met:
Packit Service 82fcde
 *
Packit Service 82fcde
 *     * Redistributions of source code must retain the above copyright
Packit Service 82fcde
 *       notice, this list of conditions and the following disclaimer.
Packit Service 82fcde
 *     * Redistributions in binary form must reproduce the above
Packit Service 82fcde
 *       copyright notice, this list of conditions and the following
Packit Service 82fcde
 *       disclaimer in the documentation and/or other materials
Packit Service 82fcde
 *       provided with the distribution.
Packit Service 82fcde
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit Service 82fcde
 *       contributors may be used to endorse or promote products derived
Packit Service 82fcde
 *       from this software without specific prior written permission.
Packit Service 82fcde
 *
Packit Service 82fcde
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 82fcde
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 82fcde
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit Service 82fcde
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit Service 82fcde
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit Service 82fcde
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service 82fcde
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit Service 82fcde
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit Service 82fcde
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit Service 82fcde
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit Service 82fcde
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 82fcde
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 82fcde
 */
Packit Service 82fcde
Packit Service 82fcde
#include <rpc/types.h>
Packit Service 82fcde
#include <rpc/clnt.h>
Packit Service 82fcde
#include <rpc/pmap_prot.h>
Packit Service 82fcde
#include <sys/socket.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
#include <libintl.h>
Packit Service 82fcde
#include <net/if.h>
Packit Service 82fcde
#include <ifaddrs.h>
Packit Service 82fcde
#include <sys/ioctl.h>
Packit Service 82fcde
#include <netinet/in.h>
Packit Service 82fcde
#include <arpa/inet.h>
Packit Service 82fcde
#include <shlib-compat.h>
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * don't use gethostbyname, which would invoke yellow pages
Packit Service 82fcde
 *
Packit Service 82fcde
 * Avoid loopback interfaces.  We return information from a loopback
Packit Service 82fcde
 * interface only if there are no other possible interfaces.
Packit Service 82fcde
 */
Packit Service 82fcde
void
Packit Service 82fcde
get_myaddress (struct sockaddr_in *addr)
Packit Service 82fcde
{
Packit Service 82fcde
  struct ifaddrs *ifa;
Packit Service 82fcde
Packit Service 82fcde
  if (getifaddrs (&ifa) != 0)
Packit Service 82fcde
    {
Packit Service 82fcde
      perror ("get_myaddress: getifaddrs");
Packit Service 82fcde
      exit (1);
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  int loopback = 0;
Packit Service 82fcde
  struct ifaddrs *run;
Packit Service 82fcde
Packit Service 82fcde
 again:
Packit Service 82fcde
  run = ifa;
Packit Service 82fcde
  while (run != NULL)
Packit Service 82fcde
    {
Packit Service 82fcde
      if ((run->ifa_flags & IFF_UP)
Packit Service 82fcde
	  && run->ifa_addr != NULL
Packit Service 82fcde
	  && run->ifa_addr->sa_family == AF_INET
Packit Service 82fcde
	  && (!(run->ifa_flags & IFF_LOOPBACK)
Packit Service 82fcde
	      || (loopback == 1 && (run->ifa_flags & IFF_LOOPBACK))))
Packit Service 82fcde
	{
Packit Service 82fcde
	  *addr = *((struct sockaddr_in *) run->ifa_addr);
Packit Service 82fcde
	  addr->sin_port = htons (PMAPPORT);
Packit Service 82fcde
	  goto out;
Packit Service 82fcde
	}
Packit Service 82fcde
Packit Service 82fcde
      run = run->ifa_next;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  if (loopback == 0)
Packit Service 82fcde
    {
Packit Service 82fcde
      loopback = 1;
Packit Service 82fcde
      goto again;
Packit Service 82fcde
    }
Packit Service 82fcde
 out:
Packit Service 82fcde
  freeifaddrs (ifa);
Packit Service 82fcde
Packit Service 82fcde
  /* The function is horribly specified.  It does not return any error
Packit Service 82fcde
     if no interface is up.  Probably this won't happen (at least
Packit Service 82fcde
     loopback is there) but still...  */
Packit Service 82fcde
}
Packit Service 82fcde
#ifdef EXPORT_RPC_SYMBOLS
Packit Service 82fcde
libc_hidden_def (get_myaddress)
Packit Service 82fcde
#else
Packit Service 82fcde
libc_hidden_nolink_sunrpc (get_myaddress, GLIBC_2_0)
Packit Service 82fcde
#endif