Back to index

glibc  2.9
get_myaddr.c
Go to the documentation of this file.
00001 /* @(#)get_myaddress.c 2.1 88/07/29 4.0 RPCSRC */
00002 /*
00003  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00004  * unrestricted use provided that this legend is included on all tape
00005  * media and as a part of the software program in whole or part.  Users
00006  * may copy or modify Sun RPC without charge, but are not authorized
00007  * to license or distribute it to anyone else except as part of a product or
00008  * program developed by the user.
00009  *
00010  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00011  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00012  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00013  *
00014  * Sun RPC is provided with no support and without any obligation on the
00015  * part of Sun Microsystems, Inc. to assist in its use, correction,
00016  * modification or enhancement.
00017  *
00018  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00019  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00020  * OR ANY PART THEREOF.
00021  *
00022  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00023  * or profits or other special, indirect and consequential damages, even if
00024  * Sun has been advised of the possibility of such damages.
00025  *
00026  * Sun Microsystems, Inc.
00027  * 2550 Garcia Avenue
00028  * Mountain View, California  94043
00029  */
00030 #if !defined(lint) && defined(SCCSIDS)
00031 static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro";
00032 #endif
00033 
00034 /*
00035  * get_myaddress.c
00036  *
00037  * Get client's IP address via ioctl.  This avoids using the yellowpages.
00038  * Copyright (C) 1984, Sun Microsystems, Inc.
00039  */
00040 
00041 #include <rpc/types.h>
00042 #include <rpc/clnt.h>
00043 #include <rpc/pmap_prot.h>
00044 #include <sys/socket.h>
00045 #include <stdio.h>
00046 #include <unistd.h>
00047 #include <libintl.h>
00048 #include <net/if.h>
00049 #include <ifaddrs.h>
00050 #include <sys/ioctl.h>
00051 /* Order of following two #includes reversed by roland@gnu */
00052 #include <netinet/in.h>
00053 #include <arpa/inet.h>
00054 
00055 /*
00056  * don't use gethostbyname, which would invoke yellow pages
00057  *
00058  * Avoid loopback interfaces.  We return information from a loopback
00059  * interface only if there are no other possible interfaces.
00060  */
00061 void
00062 get_myaddress (struct sockaddr_in *addr)
00063 {
00064   struct ifaddrs *ifa;
00065 
00066   if (getifaddrs (&ifa) != 0)
00067     {
00068       perror ("get_myaddress: getifaddrs");
00069       exit (1);
00070     }
00071 
00072   int loopback = 0;
00073   struct ifaddrs *run;
00074 
00075  again:
00076   run = ifa;
00077   while (run != NULL)
00078     {
00079       if ((run->ifa_flags & IFF_UP)
00080          && run->ifa_addr != NULL
00081          && run->ifa_addr->sa_family == AF_INET
00082          && (!(run->ifa_flags & IFF_LOOPBACK)
00083              || (loopback == 1 && (run->ifa_flags & IFF_LOOPBACK))))
00084        {
00085          *addr = *((struct sockaddr_in *) run->ifa_addr);
00086          addr->sin_port = htons (PMAPPORT);
00087          goto out;
00088        }
00089 
00090       run = run->ifa_next;
00091     }
00092 
00093   if (loopback == 0)
00094     {
00095       loopback = 1;
00096       goto again;
00097     }
00098  out:
00099   freeifaddrs (ifa);
00100 
00101   /* The function is horribly specified.  It does not return any error
00102      if no interface is up.  Probably this won't happen (at least
00103      loopback is there) but still...  */
00104 }