home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / netlite / NET / c / NETUSER < prev    next >
Text File  |  1993-03-15  |  4KB  |  130 lines

  1. /* Miscellaneous format conversion subroutines */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include "global.h"
  8. #include "netuser.h"
  9. #include "misc.h"
  10.  
  11. int net_error;
  12.  
  13. #define LINELEN 256
  14.  
  15. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  16.  * binary IP address
  17.  */
  18. int32 aton(register char *s)
  19. {
  20.         int32 n;
  21.         register int i;
  22.  
  23.         n = 0;
  24.         for(i=24;i>=0;i -= 8){
  25.                 n |= (int32)atoi(s) << i;
  26.                 if((s = strchr(s,'.')) == NULLCHAR)
  27.                 break;
  28.                 s++;
  29.         }
  30.         return n;
  31. }
  32. /* Resolve a host name into an IP address. IP addresses in dotted-decimal
  33.  * notation are distinguished from domain names by enclosing them in
  34.  * brackets. e.g., [44.64.0.1]
  35.  */
  36. int32 resolve(char *host)
  37. {
  38.         int argc;
  39.         char *argv[10];
  40.         int i;
  41.         char *s;
  42.         char hostent[LINELEN];
  43.         FILE *sfile;
  44.         static struct {
  45.                 char *name;
  46.                 int32 address;
  47.         } cache;
  48.  
  49.         if(*host == '['){
  50.                 /* Brackets indicate IP address in dotted-decimal form */
  51.                 return aton(host + 1);
  52.         }
  53.         if(cache.name != NULLCHAR && strcmp(cache.name,host) == 0)
  54.                 return cache.address;
  55.  
  56.         /* Not a numerical IP address, search the host table */
  57.         if((sfile = fopen(hosts,"r")) == NULL){
  58.                 return 0;
  59.         }
  60.         while (fgets(hostent,LINELEN,sfile) != NULL){
  61.                 s = strtok(hostent,"\r\n\t ");
  62.                 if (s != NULL && *s == '#')
  63.                          continue;
  64.                 for (argc = 0, i = 0; i < 10 && s != NULL; i++){
  65.                          argv[argc++] = s;
  66.                          s = strtok(NULL,"\r\n\t ");
  67.                 }
  68.                 for (i = 1; i < argc; i++){
  69.                         if (argv[i][0] == '#')
  70.                                 break;
  71.                         if (strcmp(host,argv[i]) == 0){
  72.                                 /* Found it, put in cache */
  73.                                 fclose(sfile);
  74.                                 if(cache.name != NULL)
  75.                                         free(cache.name);
  76.                                 cache.name    = strdup(host);
  77.                                 cache.address = aton(argv[0]);
  78.                                 return cache.address;
  79.                         }
  80.                 }
  81.         }
  82.         /* No address found */
  83.         fclose(sfile);
  84.         return 0;
  85. }
  86. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  87.  * string, e.g., 255.255.255.255\0
  88.  */
  89. char *inet_ntoa(int32 a)
  90. {
  91.         static char buf[16];
  92.  
  93.         sprintf(buf,"%u.%u.%u.%u",
  94.                 hibyte(hiword(a)),
  95.                 lobyte(hiword(a)),
  96.                 hibyte(loword(a)),
  97.                 lobyte(loword(a)) );
  98.         return buf;
  99. }
  100. /* Convert a socket (address + port) to an ascii string of the form
  101.  * aaa.aaa.aaa.aaa:ppppp
  102.  */
  103. char *psocket(struct socket *s)
  104. {
  105.         static char buf[30];
  106.  
  107.         sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
  108.         return buf;
  109. }
  110. /* Convert hex-ascii string to long integer */
  111. long htol(char *s)
  112. {
  113.         long ret;
  114.         char c;
  115.  
  116.         ret = 0;
  117.         while((c = *s++) != '\0'){
  118.                 c &= 0x7f;
  119.                 if(c >= '0' && c <= '9')
  120.                         ret = ret*16 + (c - '0');
  121.                 else if(c >= 'a' && c <= 'f')
  122.                         ret = ret*16 + (10 + c - 'a');
  123.                 else if(c >= 'A' && c <= 'F')
  124.                         ret = ret*16 + (10 + c - 'A');
  125.                 else
  126.                         break;
  127.         }
  128.         return ret;
  129. }
  130.