home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / ham / k5jb.zip / K5JB.K28 / netuser.c < prev   
C/C++ Source or Header  |  1992-09-05  |  4KB  |  195 lines

  1. /* Miscellaneous format conversion subroutines */
  2.  
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "sokname.h"    /* for SOKNAME - N5OWK */
  7. #include "global.h"
  8. #include "netuser.h"
  9.  
  10. #ifdef UNIX
  11. #include "unix.h"
  12. #endif /* UNIX */
  13.  
  14. int net_error;
  15.  
  16. #define LINELEN 80
  17.  
  18. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  19.  * binary IP address
  20.  */
  21. int32
  22. aton(s)
  23. register char *s;
  24. {
  25.     int32 n;
  26.     int atoi();
  27.     register int i;
  28.  
  29.     n = 0;
  30.     for(i=24;i>=0;i -= 8){
  31.         n |= (int32)atoi(s) << i;
  32.         if((s = index(s,'.')) == NULLCHAR)
  33.         break;
  34.         s++;
  35.     }
  36.     return n;
  37. }
  38.  
  39. /* Resolve a host name into an IP address. IP addresses in dotted-decimal
  40.  * notation are distinguished from domain names by enclosing them in
  41.  * brackets, e.g., [44.64.0.1]
  42.  */
  43. int32
  44. resolve(host)
  45. char *host;
  46. {
  47.     register char *cp;
  48.     int32 addr;
  49.     char hostent[LINELEN];
  50.     FILE *sfile;
  51.     static struct {
  52.         char *name;
  53.         int32 address;
  54.     } cache;
  55.     void rip(), free();
  56.  
  57.     if(*host == '['){
  58.         /* Brackets indicate IP address in dotted-decimal form */
  59.         return aton(host + 1);
  60.     }
  61.     if(isdigit(*host))
  62.         return aton(host);
  63.     if(cache.name != NULLCHAR && strcmp(cache.name,host) == 0)
  64.         return cache.address;
  65.  
  66.     /* Not a numerical IP address, search the host table */
  67.     if((sfile = fopen(hosts,"r")) == NULLFILE){
  68.         return 0;
  69.     }
  70.     while (!feof(sfile)){
  71.         fgets(hostent,LINELEN,sfile);
  72.         rip(hostent);
  73.         cp = hostent;
  74.         if(*cp == '#' || !isdigit(*cp))
  75.             continue;    /* Comment or invalid line */
  76.         cp = strtok(cp," \t");
  77.         addr = aton(cp);
  78.         while(cp != NULLCHAR){
  79.             cp = strtok(NULLCHAR," \t");
  80.             if(strcmp(host,cp) == 0){
  81.                 /* Found it, put in cache */
  82.                 fclose(sfile);
  83.                 if(cache.name != NULLCHAR)
  84.                     free(cache.name);
  85.                 cache.name = malloc((unsigned)strlen(host)+1);
  86.                 strcpy(cache.name,host);
  87.                 cache.address = addr;
  88.                 return cache.address;
  89.             }
  90.             /* That one didn't match, try the next one */
  91.         }
  92.     }
  93.     /* No address found */
  94.     fclose(sfile);
  95.     return 0;
  96. }
  97.  
  98. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  99.  * string, e.g., 255.255.255.255\0
  100.  */
  101. char *
  102. inet_ntoa(a)
  103. int32 a;
  104. {
  105.     static char buf[16];
  106.  
  107.     sprintf(buf,"%u.%u.%u.%u",
  108.         hibyte(hiword(a)),
  109.         lobyte(hiword(a)),
  110.         hibyte(loword(a)),
  111.         lobyte(loword(a)) );
  112.     return buf;
  113. }
  114. /* Convert a socket (address + port) to an ascii string of the form
  115.  * aaa.aaa.aaa.aaa:ppppp
  116.  */
  117. char *
  118. psocket(s)
  119. struct socket *s;
  120. {
  121.     static char buf[30];
  122.  
  123.     sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
  124.     return buf;
  125. }
  126.  
  127. #ifdef SOKNAME
  128. /* Convert a socket (address + port) to an ascii string of the form
  129.  * n5owk.okla.ampr:pppppppp
  130.  */
  131.  
  132. #define POSITION 2    /* position of returned label in hosts.net file */
  133. char *
  134. puname(s)
  135. struct socket *s;
  136. {
  137.     register int i;
  138.     static char buf[25];            /* printf in caller only will do 24 chars */
  139.     char    *cp, *cpt,*psocket();      /* but that would leave no space between */
  140.     char    hostent[LINELEN];        /* port and next field so shortened return */
  141.     FILE    *sfile;              /* string by one character below */
  142.     char *tcp_port();
  143.     extern int issok;
  144.     void rip();
  145.  
  146.     if(issok){    /* it may be turned off by sokname command */
  147.     strncpy(buf,inet_ntoa(s->address),16);
  148.     i = strlen(buf);
  149.     if ((sfile = fopen(hosts,"r")) == NULLFILE)
  150.         return NULLCHAR;
  151.  
  152.     while (!feof(sfile)){
  153.         fgets(hostent,LINELEN,sfile);
  154.  
  155.         if(hostent[0] == '#' || !isdigit(hostent[0]))
  156.             continue;    /* Comment or invalid line */
  157.         if(strncmp(buf,hostent,i) != 0)    /* this requires IP address be */
  158.             continue;            /* left justified in file */
  159.         fclose(sfile);
  160.         rip(hostent);
  161.         cp = hostent;
  162.         for(i = 0,cp = strtok(cp," \t");cp != NULLCHAR && i < POSITION;i++){
  163.             cpt = cp;
  164.             cp = strtok(NULLCHAR," \t");
  165.         }
  166.         if(cp == NULLCHAR)
  167.             cp = cpt;
  168.         cp[15] = '\0';    /* in case cp was gross - size restricted for */
  169.         sprintf(buf,"%s:%s", cp, tcp_port(s->port)); /* screen cosmetics */
  170.         return buf;        /* in tcpcmd.c, port is max of 8 chars */
  171.     }
  172.  
  173.     /* No address found */
  174.     fclose(sfile);
  175.     }
  176.     cp = psocket(s);    /* punt, never heard of em - or sokname may be off */
  177.     return cp;
  178. }
  179. #endif
  180.  
  181. /* Convert hex-ascii string to long integer */
  182. long
  183. htol(s)
  184. char *s;
  185. {
  186.     long ret;
  187.     char c;
  188.  
  189.     ret = 0;
  190.     while((c = *s++) != '\0'){
  191. #if    (!ATARI_ST && !LATTICE)    /* DG2KK "ik versta er heelemal niets van!" */
  192.         c &= 0x7f;
  193. #endif
  194.         if(c >= '0' && c <= '9')
  195.