From df8c694f7eba95ac3b292a7bfeb081678ced00aa Mon Sep 17 00:00:00 2001 From: thorpej Date: Sat, 23 Sep 1995 17:14:40 +0000 Subject: [PATCH] Add an inet_addr() function; return network-order numeric representation of an IP address from `aa.bb.cc.dd'-style text input. --- sys/lib/libsa/net.c | 84 ++++++++++++++++++++++++++++++++++++++++++++- sys/lib/libsa/net.h | 5 +-- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/sys/lib/libsa/net.c b/sys/lib/libsa/net.c index 33b3f02976b6..eeda4018673e 100644 --- a/sys/lib/libsa/net.c +++ b/sys/lib/libsa/net.c @@ -1,4 +1,4 @@ -/* $NetBSD: net.c,v 1.8 1995/09/18 21:19:30 pk Exp $ */ +/* $NetBSD: net.c,v 1.9 1995/09/23 17:14:40 thorpej Exp $ */ /* * Copyright (c) 1992 Regents of the University of California. @@ -331,6 +331,88 @@ sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize) } } +/* + * Like inet_addr() in the C library, but we only accept base-10. + * Return values are in network order. + */ +n_long +inet_addr(cp) + char *cp; +{ + register u_long val; + register int n; + register char c; + u_int parts[4]; + register u_int *pp = parts; + + for (;;) { + /* + * Collect number up to ``.''. + * Values are specified as for C: + * 0x=hex, 0=octal, other=decimal. + */ + val = 0; + while ((c = *cp) != '\0') { + if (c >= '0' && c <= '9') { + val = (val * 10) + (c - '0'); + cp++; + continue; + } + break; + } + if (*cp == '.') { + /* + * Internet format: + * a.b.c.d + * a.b.c (with c treated as 16-bits) + * a.b (with b treated as 24 bits) + */ + if (pp >= parts + 3 || val > 0xff) + goto bad; + *pp++ = val, cp++; + } else + break; + } + /* + * Check for trailing characters. + */ + if (*cp != '\0') + goto bad; + + /* + * Concoct the address according to + * the number of parts specified. + */ + n = pp - parts + 1; + switch (n) { + + case 1: /* a -- 32 bits */ + break; + + case 2: /* a.b -- 8.24 bits */ + if (val > 0xffffff) + goto bad; + val |= parts[0] << 24; + break; + + case 3: /* a.b.c -- 8.8.16 bits */ + if (val > 0xffff) + goto bad; + val |= (parts[0] << 24) | (parts[1] << 16); + break; + + case 4: /* a.b.c.d -- 8.8.8.8 bits */ + if (val > 0xff) + goto bad; + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); + break; + } + + return (htonl(val)); + bad: + return (htonl(INADDR_NONE)); +} + char * inet_ntoa(ia) struct in_addr ia; diff --git a/sys/lib/libsa/net.h b/sys/lib/libsa/net.h index d46eef4505d6..01beb6461d9b 100644 --- a/sys/lib/libsa/net.h +++ b/sys/lib/libsa/net.h @@ -1,4 +1,4 @@ -/* $NetBSD: net.h,v 1.8 1995/09/23 03:31:51 gwr Exp $ */ +/* $NetBSD: net.h,v 1.9 1995/09/23 17:14:41 thorpej Exp $ */ /* * Copyright (c) 1993 Adam Glass @@ -106,7 +106,7 @@ ssize_t readudp __P((struct iodesc *, void *, size_t, time_t)); ssize_t sendrecv __P((struct iodesc *, ssize_t (*)(struct iodesc *, void *, size_t), void *, size_t, - ssize_t (*)(struct iodesc *, void *, size_t, time_t), + ssize_t (*)(struct iodesc *, void *, size_t, time_t), void *, size_t)); /* Utilities: */ @@ -114,6 +114,7 @@ char *ether_sprintf __P((u_char *)); int in_cksum __P((void *, int)); char *inet_ntoa __P((struct in_addr)); char *intoa __P((n_long)); /* similar to inet_ntoa */ +n_long inet_addr __P((char *)); /* Machine-dependent functions: */ time_t getsecs __P((void));