home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / rpc / get_myaddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-01  |  3.8 KB  |  159 lines

  1. /* @(#)get_myaddress.c    2.1 88/07/29 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * get_myaddress.c
  36.  *
  37.  * Get client's IP address via ioctl.  This avoids using the yellowpages.
  38.  * Copyright (C) 1984, Sun Microsystems, Inc.
  39.  */
  40.  
  41. #include <rpc/rpc.h>
  42. #include <rpc/pmap_prot.h>
  43. #include <sys/socket.h>
  44. #include <stdio.h>
  45. #include <strings.h>
  46. #include <netdb.h>
  47. #include <sys/ioctl.h>
  48. #include <arpa/inet.h>
  49. #include <netinet/in.h>
  50. #include <net/if.h>
  51.  
  52. #if NLS
  53. #include "nl_types.h"
  54. #endif
  55.  
  56. /*
  57. #ifdef linux
  58. */
  59. #if 0
  60.  
  61. /*
  62. #ifndef USE_GETHOSTNAME
  63. #define USE_GETHOSTNAME
  64. #endif
  65. */
  66.  
  67. /* DO use gethostbyname because it's portable */
  68.  
  69. void get_myaddress(addr)
  70.     struct sockaddr_in *addr;
  71. {
  72. #ifdef USE_GETHOSTNAME
  73.     char localhost[256 + 1];
  74.     struct hostent *hp;
  75.  
  76. #if NLS
  77.     libc_nls_init();
  78. #endif
  79.     gethostname(localhost, 256);
  80.     if ((hp = gethostbyname(localhost)) == NULL) {
  81. #if NLS
  82.         perror(catgets(_libc_cat, RpcMiscSet, RpcMiscHostname,
  83.                    "get_myaddress: gethostbyname"));
  84. #else
  85.         perror("get_myaddress: gethostbyname");
  86. #endif
  87.         exit(1);
  88.     }
  89.     bcopy((char *) hp->h_addr, (char *) &addr->sin_addr, hp->h_length);
  90. #else
  91.     addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  92. #endif
  93.     addr->sin_family = AF_INET;
  94.     addr->sin_port = htons(PMAPPORT);
  95. }
  96.  
  97. #else
  98.  
  99. /* 
  100.  * don't use gethostbyname, which would invoke yellow pages
  101.  */
  102. void get_myaddress(addr)
  103.     struct sockaddr_in *addr;
  104. {
  105.     int s;
  106.     char buf[BUFSIZ];
  107.     struct ifconf ifc;
  108.     struct ifreq ifreq, *ifr;
  109.     int len;
  110.  
  111. #if NLS
  112.     libc_nls_init();
  113. #endif
  114.     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  115. #if NLS
  116.         perror(catgets(_libc_cat, RpcMiscSet, RpcMiscSocket,
  117.                    "get_myaddress: socket"));
  118. #else
  119.         perror("get_myaddress: socket");
  120. #endif
  121.         exit(1);
  122.     }
  123.     ifc.ifc_len = sizeof (buf);
  124.     ifc.ifc_buf = buf;
  125.     if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
  126. #if NLS
  127.         perror(catgets(_libc_cat, RpcMiscSet, RpcMiscIface,
  128.             "get_myaddress: ioctl (get interface configuration)"));
  129.  
  130. #else
  131.         perror("get_myaddress: ioctl (get interface configuration)");
  132. #endif
  133.         exit(1);
  134.     }
  135.     ifr = ifc.ifc_req;
  136.     for (len = ifc.ifc_len; len; len -= sizeof ifreq) {
  137.         ifreq = *ifr;
  138.         if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
  139. #if NLS
  140.         perror(catgets(_libc_cat, RpcMiscSet, RpcMiscIoctl,
  141.             "get_myaddress: ioctl"));
  142.  
  143. #else
  144.             perror("get_myaddress: ioctl");
  145. #endif
  146.             exit(1);
  147.         }
  148.         if ((ifreq.ifr_flags & IFF_UP) &&
  149.             ifr->ifr_addr.sa_family == AF_INET) {
  150.             *addr = *((struct sockaddr_in *)&ifr->ifr_addr);
  151.             addr->sin_port = htons(PMAPPORT);
  152.             break;
  153.         }
  154.         ifr++;
  155.     }
  156.     (void) close(s);
  157. }
  158. #endif
  159.