From 4ca714aa6a1ece8b78f13440dbb5271b8e9c21d6 Mon Sep 17 00:00:00 2001 From: Manuel Novoa III Date: Mon, 12 Mar 2001 10:04:34 +0000 Subject: Add some missing network functionality; grabbed from libc-5.3.12. --- libc/inet/Makefile | 9 ++-- libc/inet/getnetbyad.c | 36 +++++++++++++++ libc/inet/getnetbynm.c | 41 +++++++++++++++++ libc/inet/getnetent.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++ libc/inet/herror.c | 51 ++++++++++++++++++++++ libc/inet/inet_net.c | 98 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 347 insertions(+), 4 deletions(-) create mode 100644 libc/inet/getnetbyad.c create mode 100644 libc/inet/getnetbynm.c create mode 100644 libc/inet/getnetent.c create mode 100644 libc/inet/herror.c create mode 100644 libc/inet/inet_net.c (limited to 'libc/inet') diff --git a/libc/inet/Makefile b/libc/inet/Makefile index ff366f82e..303a81789 100644 --- a/libc/inet/Makefile +++ b/libc/inet/Makefile @@ -33,16 +33,17 @@ MSRC2=resolv.c MOBJ2=encodeh.o decodeh.o encoded.o decoded.o lengthd.o encodeq.o \ decodeq.o lengthq.o encodea.o decodea.o encodep.o decodep.o \ formquery.o dnslookup.o resolveaddress.o resolvemailbox.o \ - opennameservers.o closenameservers.o resolvename.o gethostbyname.o \ - gethostbyaddr.o res_init.o res_query.o getnetbyname.o \ - read_etc_hosts.o get_hosts_byname.o get_hosts_byaddr.o + opennameservers.o closenameservers.o resolvename.o gethostbyname.o\ + gethostbyaddr.o get_hosts_byname.o get_hosts_byaddr.o read_etc_hosts.o MSRC3=socketcalls.c MOBJ3= accept.o bind.o connect.o getpeername.o getsockname.o getsockopt.o \ listen.o recv.o recvfrom.o recvmsg.o send.o sendmsg.o sendto.o \ setsockopt.o shutdown.o socket.o socketpair.o -CSRC =getservice.c getproto.c hostid.c +CSRC =getservice.c getproto.c hostid.c getnetent.c getnetbynm.c getnetbyad.c \ + herror.c inet_net.c + COBJS=$(patsubst %.c,%.o, $(CSRC)) OBJS=$(MOBJ) $(MOBJ2) $(MOBJ3) $(COBJS) diff --git a/libc/inet/getnetbyad.c b/libc/inet/getnetbyad.c new file mode 100644 index 000000000..f1c2328f2 --- /dev/null +++ b/libc/inet/getnetbyad.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 1983 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +#include + +extern int _net_stayopen; + +struct netent * +getnetbyaddr(net, type) + register long net; + register int type; +{ + register struct netent *p; + + setnetent(_net_stayopen); + while ((p = getnetent())) + if (p->n_addrtype == type && p->n_net == net) + break; + if (!_net_stayopen) + endnetent(); + return (p); +} diff --git a/libc/inet/getnetbynm.c b/libc/inet/getnetbynm.c new file mode 100644 index 000000000..8d60964b0 --- /dev/null +++ b/libc/inet/getnetbynm.c @@ -0,0 +1,41 @@ +/* + * Copyright (c) 1983 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +#include +#include + +extern int _net_stayopen; + +struct netent * +getnetbyname(const char *name) +{ + register struct netent *p; + register char **cp; + + setnetent(_net_stayopen); + while ((p = getnetent())) { + if (strcmp(p->n_name, name) == 0) + break; + for (cp = p->n_aliases; *cp != 0; cp++) + if (strcmp(*cp, name) == 0) + goto found; + } +found: + if (!_net_stayopen) + endnetent(); + return (p); +} diff --git a/libc/inet/getnetent.c b/libc/inet/getnetent.c new file mode 100644 index 000000000..a4b9f602c --- /dev/null +++ b/libc/inet/getnetent.c @@ -0,0 +1,116 @@ +/* + * Copyright (c) 1983 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +#include +#include +#include + +#define MAXALIASES 35 + +static const char NETDB[] = _PATH_NETWORKS; +static FILE *netf = NULL; +static char line[BUFSIZ+1]; +static struct netent net; +static char *net_aliases[MAXALIASES]; +static char *any(char *, char *); + +int _net_stayopen; + +void +setnetent(f) + int f; +{ + if (netf == NULL) + netf = fopen(NETDB, "r" ); + else + rewind(netf); + _net_stayopen |= f; +} + +void +endnetent() +{ + if (netf) { + fclose(netf); + netf = NULL; + } + _net_stayopen = 0; +} + +struct netent * +getnetent() +{ + char *p; + register char *cp, **q; + + if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) + return (NULL); +again: + p = fgets(line, BUFSIZ, netf); + if (p == NULL) + return (NULL); + if (*p == '#') + goto again; + cp = any(p, "#\n"); + if (cp == NULL) + goto again; + *cp = '\0'; + net.n_name = p; + cp = any(p, " \t"); + if (cp == NULL) + goto again; + *cp++ = '\0'; + while (*cp == ' ' || *cp == '\t') + cp++; + p = any(cp, " \t"); + if (p != NULL) + *p++ = '\0'; + net.n_net = inet_network(cp); + net.n_addrtype = AF_INET; + q = net.n_aliases = net_aliases; + if (p != NULL) + cp = p; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &net_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = any(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + *q = NULL; + return (&net); +} + +static char * +any(cp, match) + register char *cp; + char *match; +{ + register char *mp, c; + + while ((c = *cp)) { + for (mp = match; *mp; mp++) + if (*mp == c) + return (cp); + cp++; + } + return ((char *)0); +} diff --git a/libc/inet/herror.c b/libc/inet/herror.c new file mode 100644 index 000000000..e24d93b3e --- /dev/null +++ b/libc/inet/herror.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 1987 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that: (1) source distributions retain this entire copyright + * notice and comment, and (2) distributions including binaries display + * the following acknowledgement: ``This product includes software + * developed by the University of California, Berkeley and its contributors'' + * in the documentation or other materials provided with the distribution + * and in all advertising materials mentioning features or use of this + * software. 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +#include +#include +#include + +static const char *const h_errlist[] = { + "Error 0", + "Unknown host", /* 1 HOST_NOT_FOUND */ + "Host name lookup failure", /* 2 TRY_AGAIN */ + "Unknown server error", /* 3 NO_RECOVERY */ + "No address associated with name", /* 4 NO_ADDRESS */ +}; +static const int h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) }; + +/* + * herror -- print the error indicated by the h_errno value. + */ +void herror(const char *s) +{ + static const char colon_space[] = ": "; + const char *p; + const char *c; + + c = colon_space; + if (!s || !*s) { + c += 2; + } + p = "Unknown error"; + if ((h_errno >= 0) && (h_errno < h_nerr)) { + p = h_errlist[h_errno]; + } + fprintf(stderr, "%s%s%s\n", s, c, p); +} diff --git a/libc/inet/inet_net.c b/libc/inet/inet_net.c new file mode 100644 index 000000000..fd57fe1ac --- /dev/null +++ b/libc/inet/inet_net.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * All rights reserved. + * + * 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, Berkeley 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. + */ + +#include +#include + +/* + * Internet network address interpretation routine. + * The library routines call this routine to interpret + * network numbers. + */ +u_int32_t +inet_network(const char *cp) +{ + register u_long val, base, n; + register char c; + u_long parts[4], *pp = parts; + register int i; + +again: + /* + * Collect number up to ``.''. + * Values are specified as for C: + * 0x=hex, 0=octal, other=decimal. + */ + val = 0; base = 10; + /* + * The 4.4BSD version of this file also accepts 'x__' as a hexa + * number. I don't think this is correct. -- Uli + */ + if (*cp == '0') { + if (*++cp == 'x' || *cp == 'X') + base = 16, cp++; + else + base = 8; + } + while ((c = *cp)) { + if (isdigit(c)) { + val = (val * base) + (c - '0'); + cp++; + continue; + } + if (base == 16 && isxdigit(c)) { + val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); + cp++; + continue; + } + break; + } + if (*cp == '.') { + if (pp >= parts + 4) + return (INADDR_NONE); + *pp++ = val, cp++; + goto again; + } + if (*cp && !isspace(*cp)) + return (INADDR_NONE); + *pp++ = val; + n = pp - parts; + if (n > 4) + return (INADDR_NONE); + for (val = 0, i = 0; i < n; i++) { + val <<= 8; + val |= parts[i] & 0xff; + } + return (val); +} -- cgit v1.2.3