home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / nfs / nfswatch4.0 / netaddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-01  |  5.1 KB  |  261 lines

  1. #ifndef lint
  2. static char *RCSid = "$Header: /home/harbor/davy/system/nfswatch/RCS/netaddr.c,v 4.0 1993/03/01 19:59:00 davy Exp $";
  3. #endif
  4.  
  5. #include "os.h"
  6.  
  7. /*
  8.  * netaddr.c - routines for working with network addresses.
  9.  *
  10.  * David A. Curry                Jeffrey C. Mogul
  11.  * Purdue University                Digital Equipment Corporation
  12.  * Engineering Computer Network            Western Research Laboratory
  13.  * 1285 Electrical Engineering Building        250 University Avenue
  14.  * West Lafayette, IN 47907-1285        Palo Alto, CA 94301
  15.  * davy@ecn.purdue.edu                mogul@decwrl.dec.com
  16.  *
  17.  * $Log: netaddr.c,v $
  18.  * Revision 4.0  1993/03/01  19:59:00  davy
  19.  * NFSWATCH Version 4.0.
  20.  *
  21.  * Revision 3.4  1993/02/24  17:44:45  davy
  22.  * Added -auth mode, changes to -proc mode, -map option, -server option.
  23.  *
  24.  * Revision 3.3  1993/01/16  19:08:59  davy
  25.  * Corrected Jeff's address.
  26.  *
  27.  * Revision 3.2  1993/01/15  19:33:39  davy
  28.  * Miscellaneous cleanups.
  29.  *
  30.  * Revision 3.1  1993/01/13  20:18:17  davy
  31.  * Put in OS-specific define scheme, and merged in Tim Hudson's code for
  32.  * SGI systems (as yet untested).
  33.  *
  34.  * Revision 3.0  1991/01/23  08:23:06  davy
  35.  * NFSWATCH Version 3.0.
  36.  *
  37.  * Revision 1.2  90/08/17  15:47:24  davy
  38.  * NFSWATCH Version 2.0.
  39.  * 
  40.  * Revision 1.1  88/11/29  11:20:35  davy
  41.  * NFSWATCH Release 1.0
  42.  * 
  43.  */
  44. #include <sys/param.h>
  45. #include <netdb.h>
  46. #include <stdio.h>
  47.  
  48. #include "nfswatch.h"
  49. #include "externs.h"
  50.  
  51. /*
  52.  * get_net_addrs - get network addresses of source and destination
  53.  *           hosts, along with official host names.
  54.  */
  55. void
  56. get_net_addrs()
  57. {
  58.     register int n;
  59.     char *inet_ntoa();
  60.     register char **cp;
  61.     struct hostent *hp;
  62.  
  63.     /*
  64.      * Look up the local host.
  65.      */
  66.     if ((hp = gethostbyname(myhost)) == NULL) {
  67.         (void) fprintf(stderr, "%s: %s: unknown host.\n", pname,
  68.             myhost);
  69.         finish(-1);
  70.     }
  71.  
  72.     /*
  73.      * Save the official host name.
  74.      */
  75.     (void) strcpy(myhost, hp->h_name);
  76.  
  77.     /*
  78.      * If one was specified, look up the destination host.
  79.      * Otherwise, we can use what we have.
  80.      */
  81.     if (allflag) {
  82.         (void) sprintf(dsthost, "all hosts");
  83.     }
  84.     else if (dstflag) {
  85.         if ((hp = gethostbyname(dsthost)) == NULL) {
  86.             (void) fprintf(stderr, "%s: %s: unknown host.\n", pname,
  87.                 dsthost);
  88.             finish(-1);
  89.         }
  90.  
  91.         /*
  92.          * Save the official host name.
  93.          */
  94.         (void) strcpy(dsthost, hp->h_name);
  95.     }
  96.     else {
  97.         /*
  98.          * Host name is the same as the local
  99.          * host.
  100.          */
  101.         (void) strcpy(dsthost, myhost);
  102.     }
  103.  
  104.     /*
  105.      * Copy destination host's network addresses.
  106.      */
  107.     n = 0;
  108.     (void) bzero((char *) dstaddrs, MAXHOSTADDR * sizeof(u_long));
  109.  
  110.     for (cp = hp->h_addr_list; *cp != NULL; cp++) {
  111.         if (n >= MAXHOSTADDR)
  112.             break;
  113.  
  114.         (void) bcopy(*cp, (char *) &dstaddrs[n], hp->h_length);
  115.         n++;
  116.     }
  117.  
  118.     /*
  119.      * If they specified a server host, get its addresses.
  120.      */
  121.     if (serverflag) {
  122.         if ((hp = gethostbyname(serverhost)) == NULL) {
  123.             fprintf(stderr, "%s: %s: unknown host.\n", pname,
  124.                 serverhost);
  125.             finish(-1);
  126.         }
  127.  
  128.         /*
  129.          * Save the official host name.
  130.          */
  131.         (void) strcpy(serverhost, hp->h_name);
  132.  
  133.         /*
  134.          * Copy the server's network addresses.
  135.          */
  136.         n = 0;
  137.         (void) bzero((char *) serveraddrs, MAXHOSTADDR *
  138.                  sizeof(u_long));
  139.  
  140.         for (cp = hp->h_addr_list; *cp != NULL; cp++) {
  141.             if (n >= MAXHOSTADDR)
  142.                 break;
  143.  
  144.             (void) bcopy(*cp, (char *) &serveraddrs[n],
  145.                      hp->h_length);
  146.             n++;
  147.         }
  148.     }
  149.  
  150.     /*
  151.      * If they didn't specify a source host,
  152.      * we're done.
  153.      */
  154.     if (!srcflag)
  155.         return;
  156.  
  157.     /*
  158.      * Look up the source host.
  159.      */
  160.     if ((hp = gethostbyname(srchost)) == NULL) {
  161.         (void) fprintf(stderr, "%s: %s: unknown host.\n", pname,
  162.             srchost);
  163.         finish(-1);
  164.     }
  165.  
  166.     /*
  167.      * Save the official host name.
  168.      */
  169.     (void) strcpy(srchost, hp->h_name);
  170.  
  171.     /*
  172.      * Copy source host's network addresses.
  173.      */
  174.     n = 0;
  175.     (void) bzero((char *) srcaddrs, MAXHOSTADDR * sizeof(u_long));
  176.  
  177.     for (cp = hp->h_addr_list; *cp != NULL; cp++) {
  178.         if (n >= MAXHOSTADDR)
  179.             break;
  180.  
  181.         (void) bcopy(*cp, (char *) &srcaddrs[n], hp->h_length);
  182.         n++;
  183.     }
  184. }
  185.  
  186. /*
  187.  * want_packet - determine if we're interested in a packet by examining
  188.  *         its source and destination addresses.
  189.  */
  190. int
  191. want_packet(src, dst)
  192. u_long src, dst;
  193. {
  194.     register int i, want;
  195.  
  196.     want = FALSE;
  197.  
  198.     /*
  199.      * Check that the source or destination is the server.
  200.      */
  201.     if (serverflag) {
  202.         for (i=0; (serveraddrs[i] != 0) && (i < MAXHOSTADDR); i++) {
  203.             if (!bcmp((char *) &src, (char *) &serveraddrs[i],
  204.                   sizeof(u_long)) ||
  205.                 !bcmp((char *) &dst, (char *) &serveraddrs[i],
  206.                   sizeof(u_long))) {
  207.                 want = TRUE;
  208.                 break;
  209.             }
  210.         }
  211.  
  212.         if (want && allflag)
  213.             thisdst = dst;
  214.  
  215.         return(want);
  216.     }
  217.      
  218.     /*
  219.      * Any source or destination is okay.
  220.      */
  221.     if (allflag) {
  222.         thisdst = dst;
  223.         return(TRUE);
  224.     }
  225.  
  226.     /*
  227.      * Check source address first.
  228.      */
  229.     if (srcflag) {
  230.         for (i = 0; (srcaddrs[i] != 0) && (i < MAXHOSTADDR); i++) {
  231.             if (!bcmp((char *) &src, (char *) &srcaddrs[i],
  232.                 sizeof(u_long))) {
  233.                 want = TRUE;
  234.                 break;
  235.             }
  236.         }
  237.  
  238.         /*
  239.          * If it's not from our source, we
  240.          * don't even need to check the destination.
  241.          */
  242.         if (!want)
  243.             return(FALSE);
  244.     }
  245.  
  246.     want = FALSE;
  247.  
  248.     /*
  249.      * Check destination address.
  250.      */
  251.     for (i = 0; (dstaddrs[i] != 0) && (i < MAXHOSTADDR); i++) {
  252.         if (!bcmp((char *) &dst, (char *) &dstaddrs[i],
  253.             sizeof(u_long))) {
  254.             want = TRUE;
  255.             break;
  256.         }
  257.     }
  258.  
  259.     return(want);
  260. }
  261.