home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp-sdk / src / rpclib / get_myaddress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  2.4 KB  |  109 lines

  1. /*
  2.  *      $Id: get_myaddress.c,v 4.2 1994/09/29 23:48:50 jraja Exp $
  3.  *
  4.  *      Get client's IP address via gethostid().
  5.  *
  6.  *      Copyright © 1994 AmiTCP/IP Group,
  7.  *                       Network Solutions Development Inc.
  8.  *                       All rights reserved. 
  9.  */
  10.  
  11. /* @(#)get_myaddress.c    2.1 88/07/29 4.0 RPCSRC */
  12. #if !defined(lint) && defined(SCCSIDS)
  13. static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro";
  14. #endif
  15.  
  16. /*
  17.  * Copyright (C) 1984, Sun Microsystems, Inc.
  18.  */
  19.  
  20. #include <sys/param.h>
  21. #include <rpc/types.h>
  22. #include <rpc/xdr.h>
  23. #include <rpc/auth.h>
  24. #include <rpc/clnt.h>
  25. #include <rpc/pmap_prot.h>
  26. #include <sys/socket.h>
  27. #include <stdio.h>
  28. #include <net/if.h>
  29. #include <sys/ioctl.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32.  
  33. /* 
  34.  * don't use gethostbyname, which would invoke yellow pages
  35.  */
  36. void
  37. get_myaddress(struct sockaddr_in *addr)
  38. {
  39. #ifdef AMITCP
  40.   /*
  41.    * AmiTCP gethostid() is documented to return our primary IP address
  42.    * as the host id, use it.
  43.    */
  44.   static struct sockaddr_in saddr = { sizeof(struct sockaddr_in),
  45.                     AF_INET,
  46.                     htons(PMAPPORT),
  47.                     0 };
  48.   *addr = saddr;
  49.   addr->sin_addr.s_addr = gethostid();
  50. #else
  51.     int s;
  52.     char buf[BUFSIZ];
  53.     struct ifconf ifc;
  54.     struct ifreq ifreq, *ifr;
  55.     int len, slop;
  56.  
  57.     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  58.         perror("get_myaddress: socket");
  59.         exit(1);
  60.     }
  61.     ifc.ifc_len = sizeof (buf);
  62.     ifc.ifc_buf = buf;
  63. #ifdef AMITCP
  64. #define ioctl IoctlSocket
  65. #endif
  66.     if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
  67.         perror("get_myaddress: ioctl (get interface configuration)");
  68.         exit(1);
  69.     }
  70.     ifr = ifc.ifc_req;
  71.     for (len = ifc.ifc_len; len; len -= sizeof ifreq) {
  72.         ifreq = *ifr;
  73.  
  74.         if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
  75.             perror("get_myaddress: ioctl");
  76.             exit(1);
  77.         }
  78.         if ((ifreq.ifr_flags & IFF_UP) &&
  79.             ifr->ifr_addr.sa_family == AF_INET) {
  80.             *addr = *((struct sockaddr_in *)&ifr->ifr_addr);
  81.             addr->sin_port = htons(PMAPPORT);
  82.  
  83.             /*
  84.              * Break on first non-loopback address. 
  85.              * Loopback address will remain if no other suitable
  86.              * address is found.
  87.              */
  88.                 if (!(ifreq.ifr_flags & IFF_LOOPBACK))
  89.                     break;
  90.         }
  91.         /*
  92.          * Deal with variable length addresses
  93.          */
  94.         slop = ifr->ifr_addr.sa_len - sizeof (struct sockaddr);
  95.         if (slop) {
  96.             ifr = (struct ifreq *) ((caddr_t)ifr + slop);
  97.             len -= slop;
  98.         }
  99.         ifr++;
  100.     }
  101. #ifdef AMITCP
  102. #undef ioctl
  103.     (void) CloseSocket(s);
  104. #else
  105.     (void) close(s);
  106. #endif
  107. #endif /* !AMITCP */
  108. }
  109.