home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / tcpipsrc / h / if / NetMisc / c / NETUSER < prev    next >
Encoding:
Text File  |  1995-02-04  |  2.0 KB  |  96 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 "netdb.h"
  9. #include "netuser.h"
  10. #include "misc.h"
  11. #include "socket.h"
  12. #include "timer.h"
  13. #include "tcp.h"
  14.  
  15. int net_error;
  16. extern int lookupnames;
  17.  
  18. #define LINELEN 256
  19.  
  20. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  21.  * binary IP address
  22.  */
  23. int32 aton(register char *s)
  24. {
  25.         int32 n;
  26.         register int i;
  27.  
  28.         n = 0;
  29.         for(i=24;i>=0;i -= 8){
  30.                 n |= (int32)atoi(s) << i;
  31.                 if((s = strchr(s,'.')) == NULLCHAR)
  32.                 break;
  33.                 s++;
  34.         }
  35.         return n;
  36. }
  37.  
  38. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  39.  * string, e.g., 255.255.255.255\0
  40.  */
  41. char *ntoa(int32 a)
  42. {
  43.   static char buf[256];
  44.   sprintf(buf,"%u.%u.%u.%u",
  45.           hibyte(hiword(a)),
  46.           lobyte(hiword(a)),
  47.           hibyte(loword(a)),
  48.           lobyte(loword(a)) );
  49.   return buf;
  50. }
  51.  
  52.  
  53. char *inet_ntoa(int32 a)
  54. {
  55.   struct hostent *host;
  56.  
  57.   if (lookupnames && (a != 0) && ((host = gethostbyaddr(a, 4, AF_INET)) != NULL))
  58.     return host->h_name;
  59.   else
  60.     return ntoa(a);
  61.   return "";
  62. }
  63.  
  64.  
  65. /* Convert a socket (address + port) to an ascii string of the form
  66.  * aaa.aaa.aaa.aaa:ppppp
  67.  */
  68. char *psocket(struct socket *s)
  69. {
  70.         static char buf[256];
  71.  
  72.         sprintf(buf,"%s:%s",inet_ntoa(s->address),tcp_port(s->port));
  73.         return buf;
  74. }
  75. /* Convert hex-ascii string to long integer */
  76. long htol(char *s)
  77. {
  78.         long ret;
  79.         char c;
  80.  
  81.         ret = 0;
  82.         while ((c = *s++) != '\0')
  83.         {
  84.                 c &= 0x7f;
  85.                 if(c >= '0' && c <= '9')
  86.                         ret = ret*16 + (c - '0');
  87.                 else if(c >= 'a' && c <= 'f')
  88.                         ret = ret*16 + (10 + c - 'a');
  89.                 else if(c >= 'A' && c <= 'F')
  90.                         ret = ret*16 + (10 + c - 'A');
  91.                 else
  92.                         break;
  93.         }
  94.         return ret;
  95. }
  96.