home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume25 / finger / part03 / undomain.c < prev   
C/C++ Source or Header  |  1992-04-03  |  6KB  |  226 lines

  1. /*
  2.  * undomain.c -- massage host names
  3.  *
  4.  * Copyright (C) 1986, 1990  Philip L. Budne
  5.  *
  6.  * This file is part of "Phil's Finger Program".
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 1, or (at your option)
  11.  * any later version.
  12.  *
  13.  */
  14.  
  15. # ifndef lint
  16. static char *rcsid = "$Id: undomain.c,v 3.0 90/07/06 13:12:06 budd Rel $";
  17. # endif /* lint not defined */
  18.  
  19. # include <stdio.h>            /* for NULL */
  20. # include <strings.h>            /* for (r)index */
  21. # include <ctype.h>            /* for isdigit() */
  22. # include <sys/types.h>            /* for socket.h */
  23. # include <sys/socket.h>        /* for AF_INET */
  24. # include <netdb.h>            /* for {host,net}ent structs */
  25. # include <arpa/inet.h>            /* for inet_.... */
  26. # include <netinet/in.h>        /* for in_addr */
  27. # include "finger.h"
  28. # include "upper.h"
  29.  
  30. # if 1
  31. # define SCMP(a,b) casecmp(a,b)
  32. # define CCMP(a,b) TOUP(a) == TOUP(b)
  33. # define TOUP(c) uppercase[c&0177]
  34. # else  /* not 1 */
  35. # define SCMP(a,b) (strcmp(a,b) == 0)
  36. # define CCMP(a,b) a == b
  37. # endif /* not 1 */
  38.  
  39. extern char localhost[];        /* from args.c */
  40. extern char OFFICIALhostname[];        /* from args.c */
  41.  
  42. extern char *conf_prefix();        /* from conf.c */
  43.  
  44. LOCAL char *prefixes[] = {        /* read from init file? */
  45.                     /* Sorted by length?? */
  46. # ifdef PREFIXES
  47.     PREFIXES ,
  48. # endif /* PREFIXES defined */
  49.     NULL
  50.     };
  51.  
  52. LOCAL BOOL tryprefix( hp, pp )
  53.     register char *pp, *hp;
  54. {
  55.     char *h;
  56. # ifdef DEBUG
  57.     printf("tryprefix(%s,%s)\n", hp, pp );
  58. # endif /* DEBUG defined */
  59.     h = hp;                /* save host buffer pointer */
  60.     while( CCMP(*hp,*pp) && *pp != EOS ) {
  61.     hp++;
  62.     pp++;
  63.     } /* while prefix matches */
  64.     if( *hp == EOS )            /* saw end of host first? */
  65.     return( FALSE );        /* ENTIRE hostname is a prefix?? */
  66.                     /* try next (shorter?) prefix */
  67.  
  68.     if( *pp == EOS )            /* saw end of prefix */
  69.     strcpy( h, hp );        /* slide back (backwards overlap?) */
  70.  
  71.     return( TRUE );
  72. } /* tryprefix */
  73.  
  74. LOCAL void unprefix( h )
  75. char *h;
  76. {
  77.     char **ppp, *pp;
  78.     int i;
  79.  
  80.     for( ppp = prefixes; *ppp != NULL; ppp++ )
  81.     if( tryprefix( h, *ppp ) )
  82.         return;
  83.  
  84.     for( i = 0; (pp = conf_prefix(i)) != NULL; i++ )
  85.     if( tryprefix( h, pp ) )
  86.         return;
  87. } /* unprefix */
  88.  
  89. GLOBAL void undomain( h, rem_prefix )
  90. char *h;
  91. int rem_prefix;
  92. {
  93.     int tries;
  94.     register char *p1, *p2;
  95.     char *op1;
  96.  
  97.     HZUP( h );
  98.     p1 = rindex(h, '.');        /* find last dot in name */
  99.     if( p1 != NULL ) {            /* hack for well known domains */
  100.     if( SCMP( p1, ".ARPA" ) ) {
  101.         *p1 = EOS;            /* zap it */
  102.         return;
  103.     } /* special case for .ARPA */
  104.     } /* got last dot */
  105.     else {                /* no dots at all */
  106. # ifdef UNPREFIX_NODOMAIN
  107.     if( rem_prefix )
  108.         unprefix( h );
  109. # endif /* UNPREFIX_NODOMAIN defined */
  110.     return;
  111.     } /* no dots at all */
  112.  
  113.     tries = 0;
  114.     p1 = index(h, '.');            /* find first dot in new name */
  115.     if( p1 == NULL )
  116.     return;
  117.     p1++;
  118.     op1 = p1;
  119.  
  120. # if 0
  121.     p2 = index( OFFICIALhostname, '.' ); /* find first dot in local name */
  122.     if( p2 == NULL )
  123.     return;
  124.     p2++;
  125. # else  /* not 0 */
  126.     p2 = OFFICIALhostname;        /* host may be a subdomain of ours */
  127. # endif /* not 0 */
  128.  
  129.     for( ; ; ) {            /* loop1 */
  130.     for( ; ; ) {            /* loop2 */
  131.         tries++;
  132.         if( SCMP( p1, p2 ) ) {    /* both suffixes match? */
  133.         p1[-1] = EOS;        /* yes remove common suffix */
  134.         if( rem_prefix && tries == 1 ) {
  135.             unprefix( h );
  136.             return;
  137.         } /* removing prefixes and full domain matches */
  138.         } /* suffixes match */
  139.  
  140.         p1 = index( p1, '.' );    /* find next dot in target string */
  141.         if( p1 == NULL )        /* no more? */
  142.         break;            /* loop2 */
  143.         p1++;
  144.     } /* loop2 */
  145.  
  146.     p2 = index( p2, '.' );        /* step to next higher local domain */
  147.     if( p2 == NULL )
  148.         break;            /* loop1 */
  149.     p2++;
  150.     p1 = op1;            /* go back to first target domain */
  151.     } /* loop1 */
  152. } /* undomain */
  153.  
  154. BOOL casecmp( a, b )
  155.     register char *a, *b;
  156. {
  157.     while( TOUP(*a) == TOUP(*b++) )
  158.     if( *a == EOS )
  159.         return( TRUE );
  160.     else
  161.         a++;
  162.     return( FALSE );
  163. } /* casecmp */
  164.  
  165. GLOBAL int checkhost( host, len )
  166.     char *host;
  167.     int len;
  168. {
  169.     int o1, o2, o3, o4;
  170.  
  171.     /* exactly four octets? */
  172.     if( isdigit( host[0] ) &&
  173.        sscanf( host, "%d.%d.%d.%d", &o1, &o2, &o3, &o4 ) == 4 ) {
  174.     char temp[ 256 ];        /* large */
  175.     struct in_addr in;
  176.     struct hostent *hp;
  177.  
  178.     /* make into an address */
  179.     in.s_addr = (o1 << 24) | (o2 << 16) | (o3 << 8) | o4;
  180.  
  181.     /* assume login was dumb. try to reverse address */
  182.     if( (hp = gethostbyaddr( &in, sizeof( in ), AF_INET )) != NULL ) {
  183.         strzcpy( host, hp->h_name, len );
  184.         return( TRUE );
  185.     }
  186.     else if( net_host( temp, in ) ) {
  187.         strzcpy( host, temp, len );
  188.         return( TRUE );
  189.     } /* getnetbyaddr */
  190.     } /* 4 dotted octets */
  191.     return( FALSE );
  192. } /* checkhost */
  193.  
  194. GLOBAL int
  195. net_host( buf, in )
  196.     char *buf;
  197.     struct in_addr in;
  198. {
  199.     char octet[ 5 ];
  200.     struct netent *np;
  201.     long lna;
  202.  
  203.     if( (np = getnetbyaddr( inet_netof( in ), AF_INET )) != NULL ) {
  204.     strcpy( buf, np->n_name );
  205.  
  206.     lna = inet_lnaof( in );        /* get "local net address" */
  207.     if( lna & 0xff0000 ) {        /* 2nd octet set */
  208.         sprintf( octet, ".%d", (lna >> 16) & 0xff );
  209.         strcat( buf, octet );
  210.     }
  211.     if( lna & 0xffff00 ) {    /* 2nd or 3rd */
  212.         sprintf( octet, ".%d", (lna >> 8) & 0xff );
  213.         strcat( buf, octet );
  214.     }
  215.     sprintf( octet, ".%d", lna & 0xff ); /* Class A B and C */
  216.     strcat( buf, octet );
  217.     return( TRUE );
  218.     }
  219.     return( FALSE );
  220. }
  221. /*
  222.  * Local variables:
  223.  * comment-column: 40
  224.  * End:
  225.  */
  226.