1: /*
   2:  * Copyright (c) 1983 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if defined(LIBC_SCCS) && !defined(lint)
   8: static char sccsid[] = "@(#)inet_addr.c	5.2.1 (2.11BSD GTE) 1/1/94";
   9: #endif
  10: 
  11: #include <sys/types.h>
  12: #include <ctype.h>
  13: #include <netinet/in.h>
  14: 
  15: /*
  16:  * Internet address interpretation routine.
  17:  * All the network library routines call this
  18:  * routine to interpret entries in the data bases
  19:  * which are expected to be an address.
  20:  * The value returned is in network order.
  21:  */
  22: u_long
  23: inet_addr(cp)
  24:     register char *cp;
  25: {
  26:     register u_long val, base;
  27:     register u_int n; /* can't switch on longs - should be an int anyway */
  28:     register char c;
  29:     u_long parts[4], *pp = parts;
  30: 
  31: again:
  32:     /*
  33: 	 * Collect number up to ``.''.
  34: 	 * Values are specified as for C:
  35: 	 * 0x=hex, 0=octal, other=decimal.
  36: 	 */
  37:     val = 0; base = 10;
  38:     if (*cp == '0')
  39:         base = 8, cp++;
  40:     if (*cp == 'x' || *cp == 'X')
  41:         base = 16, cp++;
  42:     while (c = *cp) {
  43:         if (isdigit(c)) {
  44:             val = (val * base) + (c - '0');
  45:             cp++;
  46:             continue;
  47:         }
  48:         if (base == 16 && isxdigit(c)) {
  49:             val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
  50:             cp++;
  51:             continue;
  52:         }
  53:         break;
  54:     }
  55:     if (*cp == '.') {
  56:         /*
  57: 		 * Internet format:
  58: 		 *	a.b.c.d
  59: 		 *	a.b.c	(with c treated as 16-bits)
  60: 		 *	a.b	(with b treated as 24 bits)
  61: 		 */
  62:         if (pp >= parts + 4)
  63:             return (-1);
  64:         *pp++ = val, cp++;
  65:         goto again;
  66:     }
  67:     /*
  68: 	 * Check for trailing characters.
  69: 	 */
  70:     if (*cp && !isspace(*cp))
  71:         return (-1);
  72:     *pp++ = val;
  73:     /*
  74: 	 * Concoct the address according to
  75: 	 * the number of parts specified.
  76: 	 */
  77:     n = pp - parts;
  78:     switch (n) {
  79: 
  80:     case 1:             /* a -- 32 bits */
  81:         val = parts[0];
  82:         break;
  83: 
  84:     case 2:             /* a.b -- 8.24 bits */
  85:         val = (parts[0] << 24) | (parts[1] & 0xffffff);
  86:         break;
  87: 
  88:     case 3:             /* a.b.c -- 8.8.16 bits */
  89:         val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
  90:             (parts[2] & 0xffffL);
  91:         break;
  92: 
  93:     case 4:             /* a.b.c.d -- 8.8.8.8 bits */
  94:         val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
  95:               ((parts[2] & 0xff) << 8) | (parts[3] & 0xff);
  96:         break;
  97: 
  98:     default:
  99:         return (-1);
 100:     }
 101:     val = htonl(val);
 102:     return (val);
 103: }

Defined functions

inet_addr defined in line 22; used 62 times

Defined variables

sccsid defined in line 8; never used
Last modified: 1994-01-11
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3376
Valid CSS Valid XHTML 1.0 Strict