Blame sunrpc/get_myaddr.c

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