home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / NETUSER.C < prev    next >
C/C++ Source or Header  |  1991-01-27  |  2KB  |  89 lines

  1. /* Miscellaneous integer and IP address format conversion subroutines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #define LINELEN 256
  5. #include <ctype.h>
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "netuser.h"
  9.  
  10. int Net_error;
  11.  
  12. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  13.  * binary IP address
  14.  */
  15. int32
  16. aton(s)
  17. register char *s;
  18. {
  19.     int32 n;
  20.  
  21.     register int i;
  22.  
  23.     n = 0;
  24.     if(s == NULLCHAR)
  25.         return 0;
  26.     for(i=24;i>=0;i -= 8){
  27.         /* Skip any leading stuff (e.g., spaces, '[') */
  28.         while(*s != '\0' && !isdigit(*s))
  29.             s++;
  30.         if(*s == '\0')
  31.             break;
  32.         n |= (int32)atoi(s) << i;
  33.         if((s = strchr(s,'.')) == NULLCHAR)
  34.             break;
  35.         s++;
  36.     }
  37.     return n;
  38. }
  39. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  40.  * string, e.g., 255.255.255.255\0
  41.  */
  42. char *
  43. inet_ntoa(a)
  44. int32 a;
  45. {
  46.     static char buf[16];
  47.  
  48.     sprintf(buf,"%u.%u.%u.%u",
  49.         hibyte(hiword(a)),
  50.         lobyte(hiword(a)),
  51.         hibyte(loword(a)),
  52.         lobyte(loword(a)) );
  53.     return buf;
  54. }
  55. /* Convert hex-ascii string to long integer */
  56. long
  57. htol(s)
  58. char *s;
  59. {
  60.     long ret;
  61.     char c;
  62.  
  63.     ret = 0;
  64.     while((c = *s++) != '\0'){
  65.         c &= 0x7f;
  66.         if(c == 'x')
  67.             continue;    /* Ignore 'x', e.g., '0x' prefixes */
  68.         if(c >= '0' && c <= '9')
  69.             ret = ret*16 + (c - '0');
  70.         else if(c >= 'a' && c <= 'f')
  71.             ret = ret*16 + (10 + c - 'a');
  72.         else if(c >= 'A' && c <= 'F')
  73.             ret = ret*16 + (10 + c - 'A');
  74.         else
  75.             break;
  76.     }
  77.     return ret;
  78. }
  79. char *
  80. pinet(s)
  81. struct socket *s;
  82. {
  83.     static char buf[30];
  84.  
  85.     sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
  86.     return buf;
  87. }
  88.  
  89.