home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / unix / portmap.sha / from_local.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-11  |  4.0 KB  |  150 lines

  1.  /*
  2.   * Check if an address belongs to the local system. Adapted from:
  3.   * 
  4.   * @(#)pmap_svc.c 1.32 91/03/11 Copyright 1984,1990 Sun Microsystems, Inc.
  5.   * @(#)get_myaddress.c  2.1 88/07/29 4.0 RPCSRC.
  6.   */
  7.  
  8. /*
  9.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  10.  * unrestricted use provided that this legend is included on all tape
  11.  * media and as a part of the software program in whole or part.  Users
  12.  * may copy or modify Sun RPC without charge, but are not authorized
  13.  * to license or distribute it to anyone else except as part of a product or
  14.  * program developed by the user or with the express written consent of
  15.  * Sun Microsystems, Inc.
  16.  *
  17.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  18.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  19.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  20.  *
  21.  * Sun RPC is provided with no support and without any obligation on the
  22.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  23.  * modification or enhancement.
  24.  *
  25.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  26.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  27.  * OR ANY PART THEREOF.
  28.  *
  29.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  30.  * or profits or other special, indirect and consequential damages, even if
  31.  * Sun has been advised of the possibility of such damages.
  32.  *
  33.  * Sun Microsystems, Inc.
  34.  * 2550 Garcia Avenue
  35.  * Mountain View, California  94043
  36.  */
  37.  
  38. #ifndef lint
  39. static char sccsid[] = "@(#) from_local.c 1.1 92/06/11 22:53:17";
  40. #endif
  41.  
  42. #ifdef TEST
  43. #undef perror
  44. #endif
  45.  
  46. #include <sys/types.h>
  47. #include <sys/socket.h>
  48. #include <netdb.h>
  49. #include <netinet/in.h>
  50. #include <net/if.h>
  51. #include <sys/ioctl.h>
  52. #include <syslog.h>
  53.  
  54. #ifndef TRUE
  55. #define    TRUE    1
  56. #define FALSE    0
  57. #endif
  58.  
  59. /* How many interfaces could there be on a computer? */
  60.  
  61. #define    MAX_LOCAL 16
  62. static int num_local = -1;
  63. static struct in_addr addrs[MAX_LOCAL];
  64.  
  65. /* find_local - find all IP addresses for this host */
  66.  
  67. find_local()
  68. {
  69.     struct ifconf ifc;
  70.     struct ifreq ifreq;
  71.     struct ifreq *ifr;
  72.     struct ifreq *the_end;
  73.     int     sock;
  74.     char    buf[MAX_LOCAL * sizeof(struct ifreq)];
  75.  
  76.     /* Get list of network interfaces. */
  77.  
  78.     if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
  79.     perror("socket");
  80.     return (0);
  81.     }
  82.     ifc.ifc_len = sizeof(buf);
  83.     ifc.ifc_buf = buf;
  84.     if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
  85.     perror("SIOCGIFCONF");
  86.     (void) close(sock);
  87.     return (0);
  88.     }
  89.     /* Get IP address of each active IP network interface. */
  90.  
  91.     the_end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
  92.     num_local = 0;
  93.     for (ifr = ifc.ifc_req; ifr < the_end; ifr++) {
  94.     if (ifr->ifr_addr.sa_family == AF_INET) {    /* IP net interface */
  95.         ifreq = *ifr;
  96.         if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
  97.         perror("SIOCGIFFLAGS");
  98.         } else if (ifreq.ifr_flags & IFF_UP) {    /* active interface */
  99.         if (ioctl(sock, SIOCGIFADDR, (char *) &ifreq) < 0) {
  100.             perror("SIOCGIFADDR");
  101.         } else {
  102.             addrs[num_local++] = ((struct sockaddr_in *)
  103.                       & ifreq.ifr_addr)->sin_addr;
  104.         }
  105.         }
  106. #ifdef TIRPC
  107.         /* Support for variable-length addresses. */
  108.         if (num_local >= MAX_LOCAL)
  109.         break;
  110.         ifr = (struct ifreq *) ((caddr_t) ifr
  111.               + ifr->ifr_addr.sa_len - sizeof(struct sockaddr));
  112. #endif
  113.     }
  114.     }
  115.     (void) close(sock);
  116.     return (num_local);
  117. }
  118.  
  119. /* from_local - determine whether request comes from the local system */
  120.  
  121. from_local(addr)
  122. struct sockaddr_in *addr;
  123. {
  124.     int     i;
  125.  
  126.     if (num_local == -1 && find_local() == 0)
  127.     syslog(LOG_ERR, "cannot find any active local network interfaces");
  128.  
  129.     for (i = 0; i < num_local; i++) {
  130.     if (memcmp((char *) &(addr->sin_addr), (char *) &(addrs[i]),
  131.            sizeof(struct in_addr)) == 0)
  132.         return (TRUE);
  133.     }
  134.     return (FALSE);
  135. }
  136.  
  137. #ifdef TEST
  138.  
  139. main()
  140. {
  141.     char   *inet_ntoa();
  142.     int     i;
  143.  
  144.     find_local();
  145.     for (i = 0; i < num_local; i++)
  146.     printf("%s\n", inet_ntoa(addrs[i]));
  147. }
  148.  
  149. #endif
  150.