diff options
| author | thorpej <thorpej@NetBSD.org> | 1999-11-11 20:21:59 +0000 |
|---|---|---|
| committer | thorpej <thorpej@NetBSD.org> | 1999-11-11 20:21:59 +0000 |
| commit | 38200dd3e4bbc2000d0c5a4c01f86fa7f6b2e739 (patch) | |
| tree | 061920b5ed48b00f071473265d104521d1a41bf1 /sys/lib | |
| parent | c29af30c3cb3d96735312635e2894e4997a30146 (diff) | |
Networking routines needed for libsa to be self-contained.
Diffstat (limited to 'sys/lib')
| -rw-r--r-- | sys/lib/libsa/Makefile | 12 | ||||
| -rw-r--r-- | sys/lib/libsa/htonl.c | 22 | ||||
| -rw-r--r-- | sys/lib/libsa/htons.c | 22 | ||||
| -rw-r--r-- | sys/lib/libsa/inet_addr.c | 121 | ||||
| -rw-r--r-- | sys/lib/libsa/intoa.c | 74 | ||||
| -rw-r--r-- | sys/lib/libsa/ntohl.c | 22 | ||||
| -rw-r--r-- | sys/lib/libsa/ntohs.c | 22 | ||||
| -rw-r--r-- | sys/lib/libsa/stand.h | 26 |
8 files changed, 315 insertions, 6 deletions
diff --git a/sys/lib/libsa/Makefile b/sys/lib/libsa/Makefile index fb6f1b88d17..35d7f8df378 100644 --- a/sys/lib/libsa/Makefile +++ b/sys/lib/libsa/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.35 1999/11/11 18:11:25 thorpej Exp $ +# $NetBSD: Makefile,v 1.36 1999/11/11 20:22:00 thorpej Exp $ LIB= sa MKPIC= no @@ -18,9 +18,10 @@ CPPFLAGS= -I${SADIR} ${SACPPFLAGS} ${SAMISCCPPFLAGS} \ # stand routines SRCS+= alloc.c bcmp.c bcopy.c bzero.c errno.c exit.c exec.c getfile.c gets.c \ - globals.c memcmp.c memcpy.c memset.c panic.c printf.c \ - snprintf.c sprintf.c strlen.c strcmp.c strncmp.c strerror.c subr_prf.c \ - twiddle.c vsprintf.c checkpasswd.c + globals.c htonl.c htons.c inet_addr.c intoa.c memcmp.c memcpy.c \ + memset.c ntohs.c ntohl.c panic.c printf.c snprintf.c sprintf.c \ + strlen.c strcmp.c strncmp.c strerror.c subr_prf.c twiddle.c \ + vsprintf.c checkpasswd.c # io routines SRCS+= closeall.c dev.c disklabel.c dkcksum.c ioctl.c nullfs.c stat.c fstat.c @@ -45,6 +46,9 @@ SRCS+= nfs.c tftp.c .endif SRCS+= lfs.c ufs.c ufs_ls.c cd9660.c ustarfs.c +# quad routines +SRCS+= ashrdi3.c + # only needed during build libinstall:: diff --git a/sys/lib/libsa/htonl.c b/sys/lib/libsa/htonl.c new file mode 100644 index 00000000000..715a48944b7 --- /dev/null +++ b/sys/lib/libsa/htonl.c @@ -0,0 +1,22 @@ +/* $NetBSD: htonl.c,v 1.1 1999/11/11 20:21:59 thorpej Exp $ */ + +/* + * Written by J.T. Conklin <jtc@netbsd.org>. + * Public domain. + */ + +#include "stand.h" + +#undef htonl + +u_int32_t +htonl(x) + u_int32_t x; +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *)&x; + return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); +#else + return x; +#endif +} diff --git a/sys/lib/libsa/htons.c b/sys/lib/libsa/htons.c new file mode 100644 index 00000000000..5f702d04419 --- /dev/null +++ b/sys/lib/libsa/htons.c @@ -0,0 +1,22 @@ +/* $NetBSD: htons.c,v 1.1 1999/11/11 20:21:59 thorpej Exp $ */ + +/* + * Written by J.T. Conklin <jtc@netbsd.org>. + * Public domain. + */ + +#include "stand.h" + +#undef htons + +u_int16_t +htons(x) + u_int16_t x; +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *) &x; + return (u_int16_t)(s[0] << 8 | s[1]); +#else + return x; +#endif +} diff --git a/sys/lib/libsa/inet_addr.c b/sys/lib/libsa/inet_addr.c new file mode 100644 index 00000000000..a6e3960aa49 --- /dev/null +++ b/sys/lib/libsa/inet_addr.c @@ -0,0 +1,121 @@ +/* $NetBSD: inet_addr.c,v 1.1 1999/11/11 20:21:59 thorpej Exp $ */ + +/* Copyright (c) 1996 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +/* + * Derived from: + * src/lib/libc/net/inet_pton.c + */ + +#include "stand.h" + +/* + * Ascii internet address interpretation routine. + * The value returned is in network order. + */ +u_int32_t +inet_addr(src) + const char *src; +{ + u_int32_t val; + int base, n; + unsigned char c; + u_int parts[4]; + register u_int *pp = parts; + + c = *src; + for (;;) { + /* + * Collect number up to ``.''. + * Values are specified as for C: + * 0x=hex, 0=octal, isdigit=decimal. + */ + if (!isdigit(c)) + return (0); + val = 0; base = 10; + if (c == '0') { + c = *++src; + if (toupper(c) == 'X') + base = 16, c = *++src; + else + base = 8; + } + for (;;) { + if (isdigit(c)) { + val = (val * base) + (c - '0'); + c = *++src; + } else if (base == 16 && isxdigit(toupper(c))) { + val = (val << 4) | + (toupper(c) + 10 - 'A'); + c = *++src; + } else + break; + } + if (c == '.') { + /* + * 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) + return (0); + *pp++ = val; + c = *++src; + } else + break; + } + /* + * Check for trailing characters. + */ + if (c != '\0' && !isspace(c)) + return (0); + /* + * Concoct the address according to + * the number of parts specified. + */ + n = pp - parts + 1; + switch (n) { + + case 0: + return (0); /* initial nondigit */ + + case 1: /* a -- 32 bits */ + break; + + case 2: /* a.b -- 8.24 bits */ + if (val > 0xffffff) + return (0); + val |= parts[0] << 24; + break; + + case 3: /* a.b.c -- 8.8.16 bits */ + if (val > 0xffff) + return (0); + val |= (parts[0] << 24) | (parts[1] << 16); + break; + + case 4: /* a.b.c.d -- 8.8.8.8 bits */ + if (val > 0xff) + return (0); + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); + break; + } + + val = htonl(val); + return (val); +} diff --git a/sys/lib/libsa/intoa.c b/sys/lib/libsa/intoa.c new file mode 100644 index 00000000000..4e757a339c3 --- /dev/null +++ b/sys/lib/libsa/intoa.c @@ -0,0 +1,74 @@ +/* $NetBSD: intoa.c,v 1.1 1999/11/11 20:21:59 thorpej Exp $ */ + +/* + * Copyright (c) 1992 Regents of the University of California. + * All rights reserved. + * + * This software was developed by the Computer Systems Engineering group + * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and + * contributed to Berkeley. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Lawrence Berkeley Laboratory and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL) + */ + +#include "stand.h" + +/* Similar to inet_ntoa() */ +const char * +intoa(addr) + u_int32_t addr; +{ + char *cp; + u_int byte; + int n; + static char buf[17]; /* strlen(".255.255.255.255") + 1 */ + + addr = ntohl(addr); + cp = &buf[sizeof buf]; + *--cp = '\0'; + + n = 4; + do { + byte = addr & 0xff; + *--cp = byte % 10 + '0'; + byte /= 10; + if (byte > 0) { + *--cp = byte % 10 + '0'; + byte /= 10; + if (byte > 0) + *--cp = byte + '0'; + } + *--cp = '.'; + addr >>= 8; + } while (--n > 0); + + return (cp+1); +} diff --git a/sys/lib/libsa/ntohl.c b/sys/lib/libsa/ntohl.c new file mode 100644 index 00000000000..4db55e25430 --- /dev/null +++ b/sys/lib/libsa/ntohl.c @@ -0,0 +1,22 @@ +/* $NetBSD: ntohl.c,v 1.1 1999/11/11 20:21:59 thorpej Exp $ */ + +/* + * Written by J.T. Conklin <jtc@netbsd.org>. + * Public domain. + */ + +#include "stand.h" + +#undef ntohl + +u_int32_t +ntohl(x) + u_int32_t x; +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *)&x; + return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); +#else + return x; +#endif +} diff --git a/sys/lib/libsa/ntohs.c b/sys/lib/libsa/ntohs.c new file mode 100644 index 00000000000..89ad3c7dbc7 --- /dev/null +++ b/sys/lib/libsa/ntohs.c @@ -0,0 +1,22 @@ +/* $NetBSD: ntohs.c,v 1.1 1999/11/11 20:22:00 thorpej Exp $ */ + +/* + * Written by J.T. Conklin <jtc@netbsd.org>. + * Public domain. + */ + +#include "stand.h" + +#undef ntohs + +u_int16_t +ntohs(x) + u_int16_t x; +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *) &x; + return (u_int16_t)(s[0] << 8 | s[1]); +#else + return x; +#endif +} diff --git a/sys/lib/libsa/stand.h b/sys/lib/libsa/stand.h index 1320997837d..63983f04e13 100644 --- a/sys/lib/libsa/stand.h +++ b/sys/lib/libsa/stand.h @@ -1,4 +1,4 @@ -/* $NetBSD: stand.h,v 1.34 1999/11/11 18:11:25 thorpej Exp $ */ +/* $NetBSD: stand.h,v 1.35 1999/11/11 20:22:00 thorpej Exp $ */ /* * Copyright (c) 1999 Christopher G. Demetriou. All rights reserved. @@ -201,8 +201,10 @@ extern struct open_file files[]; #define isupper(c) ((c) >= 'A' && (c) <= 'Z') #define tolower(c) ((c) - 'A' + 'a') -#define isspace(c) ((c) == ' ' || (c) == '\t') +#define toupper(c) ((c) >= 'a' && (c) <= 'z' ? (c) - 'a' + 'A' : (c)) +#define isspace(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n')) #define isdigit(c) ((c) >= '0' && (c) <= '9') +#define isxdigit(c) (((c) >= 'A') && ((c) <= 'F')) int devopen __P((struct open_file *, const char *, char **)); #ifdef HEAP_VARIABLE @@ -242,6 +244,16 @@ size_t strlen __P((const char *)); int strcmp __P((const char *, const char *)); int strncmp __P((const char *, const char *, size_t)); +u_int32_t inet_addr __P((const char *)); +const char *intoa __P((u_int32_t)); +#define inet_ntoa(a) intoa((a).s_addr) + +u_int32_t htonl __P((u_int32_t)); +u_int16_t htons __P((u_int16_t)); + +u_int32_t ntohl __P((u_int32_t)); +u_int16_t ntohs __P((u_int16_t)); + extern int opterr, optind, optopt, optreset; extern char *optarg; int getopt __P((int, char * const *, const char *)); @@ -273,3 +285,13 @@ int oclose __P((int)); ssize_t oread __P((int, void *, size_t)); off_t olseek __P((int, off_t, int)); #endif + +static __inline u_int max __P((u_int, u_int)) __attribute__((__unused__)); + +static __inline u_int +max(a, b) + u_int a, b; +{ + + return (a > b ? a : b); +} |
