Initial commit of netdb library

This commit is contained in:
taubert 1998-01-24 07:05:48 +00:00
parent 418ae96ccb
commit 75831c8124
35 changed files with 4990 additions and 0 deletions

27
lib/netdb/Makefile Normal file
View File

@ -0,0 +1,27 @@
#
# Makefile for netdb
#
# $Id: Makefile,v 1.1 1998/01/24 07:05:09 taubert Exp $
#
.INCLUDE: ../../paths.mk
.INCLUDE: ../const.mk
OBJS= rcmd.o getnetbyname.o getnetbyaddr.o getnetent.o getprotoname.o \
getproto.o getprotoent.o getservbyname.o getservbyport.o \
getservent.o gethostnamadr.o sethostent.o inet_addr.o inet_lnaof.o \
inet_makeaddr.o inet_netof.o inet_network.o inet_ntoa.o res_comp.o \
res_debug.o res_init.o res_mkquery.o res_query.o res_send.o \
herror.o writev.o
CFLAGS +=-O79
libnetdb .PHONY: $(OBJS)
$(RM) $@
$(MAKELIB) $(MAKELIBFLAGS) -l $@ $(OBJS)
obj: $(OBJS)
%.o: %.c
$(CC) -o $@ -c $(__OFLAG) $(CFLAGS) $<

427
lib/netdb/gethostnamadr.c Normal file
View File

@ -0,0 +1,427 @@
/*
* Copyright (c) 1985, 1988 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)gethostnamadr.c 6.48 (Berkeley) 1/10/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <netdb.h>
#include <resolv.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include "protos.h"
#define MAXALIASES 35
#define MAXADDRS 35
static char *h_addr_ptrs[MAXADDRS + 1];
static struct hostent host;
static char *host_aliases[MAXALIASES];
static char hostbuf[BUFSIZ+1];
static struct in_addr host_addr;
static FILE *hostf = NULL;
static char hostaddr[MAXADDRS];
static char *host_addrs[2];
static int stayopen = 0;
#if PACKETSZ > 1024
#define MAXPACKET PACKETSZ
#else
#define MAXPACKET 1024
#endif
typedef union {
HEADER hdr;
u_char buf[MAXPACKET];
} querybuf;
typedef union {
long al;
char ac;
} align;
extern int h_errno;
static struct hostent *
getanswer(querybuf *answer, int anslen, int iquery)
{
register HEADER *hp;
register u_char *cp;
register int n;
u_char *eom;
char *bp, **ap;
int type, class, buflen, ancount, qdcount;
int haveanswer, getclass = C_ANY;
char **hap;
eom = answer->buf + anslen;
/*
* find first satisfactory answer
*/
hp = &answer->hdr;
ancount = ntohs(hp->ancount);
qdcount = ntohs(hp->qdcount);
bp = hostbuf;
buflen = sizeof(hostbuf);
cp = answer->buf + sizeof(HEADER);
if (qdcount) {
if (iquery) {
if ((n = dn_expand((u_char *)answer->buf,
(u_char *)eom, (u_char *)cp, (u_char *)bp,
buflen)) < 0) {
h_errno = NO_RECOVERY;
return ((struct hostent *) NULL);
}
cp += n + QFIXEDSZ;
host.h_name = bp;
n = strlen(bp) + 1;
bp += n;
buflen -= n;
} else
cp += __dn_skipname(cp, eom) + QFIXEDSZ;
while (--qdcount > 0)
cp += __dn_skipname(cp, eom) + QFIXEDSZ;
} else if (iquery) {
if (hp->aa)
h_errno = HOST_NOT_FOUND;
else
h_errno = TRY_AGAIN;
return ((struct hostent *) NULL);
}
ap = host_aliases;
*ap = NULL;
host.h_aliases = host_aliases;
hap = h_addr_ptrs;
*hap = NULL;
#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
host.h_addr_list = h_addr_ptrs;
#endif
haveanswer = 0;
while (--ancount >= 0 && cp < eom) {
if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom,
(u_char *)cp, (u_char *)bp, buflen)) < 0)
break;
cp += n;
type = _getshort(cp);
cp += sizeof(u_short);
class = _getshort(cp);
cp += sizeof(u_short) + sizeof(u_long);
n = _getshort(cp);
cp += sizeof(u_short);
if (type == T_CNAME) {
cp += n;
if (ap >= &host_aliases[MAXALIASES-1])
continue;
*ap++ = bp;
n = strlen(bp) + 1;
bp += n;
buflen -= n;
continue;
}
if (iquery && type == T_PTR) {
if ((n = dn_expand((u_char *)answer->buf,
(u_char *)eom, (u_char *)cp, (u_char *)bp,
buflen)) < 0)
break;
cp += n;
host.h_name = bp;
return(&host);
}
if (iquery || type != T_A) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("unexpected answer type %d, size %d\n",
type, n);
#endif
cp += n;
continue;
}
if (haveanswer) {
if (n != host.h_length) {
cp += n;
continue;
}
if (class != getclass) {
cp += n;
continue;
}
} else {
host.h_length = n;
getclass = class;
host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
if (!iquery) {
host.h_name = bp;
bp += strlen(bp) + 1;
}
}
bp += sizeof(align) - ((u_long)bp % sizeof(align));
if (bp + n >= &hostbuf[sizeof(hostbuf)]) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("size (%d) too big\n", n);
#endif
break;
}
bcopy(cp, *hap++ = bp, n);
bp +=n;
cp += n;
haveanswer++;
}
if (haveanswer) {
*ap = NULL;
#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
*hap = NULL;
#else
host.h_addr = h_addr_ptrs[0];
#endif
return (&host);
} else {
h_errno = TRY_AGAIN;
return ((struct hostent *) NULL);
}
}
struct hostent *
gethostbyname(const char *name)
{
static querybuf buf;
register const char *cp;
int n;
extern struct hostent *_gethtbyname(char *name);
struct hostent *local_ht;
/*
* disallow names consisting only of digits/dots, unless
* they end in a dot.
*/
if (isdigit(name[0]))
for (cp = name;; ++cp) {
if (!*cp) {
if (*--cp == '.')
break;
/*
* All-numeric, no dot at the end.
* Fake up a hostent as if we'd actually
* done a lookup.
*/
if (!inet_aton(name, &host_addr)) {
h_errno = HOST_NOT_FOUND;
return((struct hostent *) NULL);
}
host.h_name = (char *)name;
host.h_aliases = host_aliases;
host_aliases[0] = NULL;
host.h_addrtype = AF_INET;
host.h_length = sizeof(u_long);
h_addr_ptrs[0] = (char *)&host_addr;
h_addr_ptrs[1] = (char *)0;
#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
host.h_addr_list = h_addr_ptrs;
#else
host.h_addr = h_addr_ptrs[0];
#endif
return (&host);
}
if (!isdigit(*cp) && *cp != '.')
break;
}
/*
* GSTCP NOTE: I move this around so it checks /etc/hosts first.
*/
if (local_ht = _gethtbyname(name))
return local_ht;
if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_search failed\n");
#endif
return ((struct hostent *) NULL);
}
return (getanswer(&buf, n, 0));
}
struct hostent *
gethostbyaddr(const char *addr, int len, int type)
{
static char qbuf[MAXDNAME];
static querybuf buf;
int n;
register struct hostent *hp;
extern struct hostent *_gethtbyaddr(const char *addr, int len, int type);
if (type != AF_INET)
return ((struct hostent *) NULL);
(void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
((unsigned)addr[3] & 0xff),
((unsigned)addr[2] & 0xff),
((unsigned)addr[1] & 0xff),
((unsigned)addr[0] & 0xff));
n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf));
if (n < 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_query failed\n");
#endif
if (errno == ECONNREFUSED)
return (_gethtbyaddr(addr, len, type));
return ((struct hostent *) NULL);
}
hp = getanswer(&buf, n, 1);
if (hp == NULL)
return ((struct hostent *) NULL);
hp->h_addrtype = type;
hp->h_length = len;
h_addr_ptrs[0] = (char *)&host_addr;
h_addr_ptrs[1] = (char *)0;
host_addr = *(struct in_addr *)addr;
#if BSD < 43 && !defined(h_addr) /* new-style hostent structure */
hp->h_addr = h_addr_ptrs[0];
#endif
return(hp);
}
void _sethtent(int f)
{
if (hostf == NULL)
hostf = fopen(_PATH_HOSTS, "r" );
else
rewind(hostf);
stayopen |= f;
}
void _endhtent(void)
{
if (hostf && !stayopen) {
(void) fclose(hostf);
hostf = NULL;
}
}
struct hostent *
_gethtent(void)
{
char *p;
register char *cp, **q;
if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL)
return (NULL);
again:
if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL)
return (NULL);
if (*p == '#')
goto again;
cp = strpbrk(p, "#\n");
if (cp == NULL)
goto again;
*cp = '\0';
cp = strpbrk(p, " \t");
if (cp == NULL)
goto again;
*cp++ = '\0';
/* THIS STUFF IS INTERNET SPECIFIC */
#if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
host.h_addr_list = host_addrs;
#endif
host.h_addr = hostaddr;
*((u_long *)host.h_addr) = inet_addr(p);
host.h_length = sizeof (u_long);
host.h_addrtype = AF_INET;
while (*cp == ' ' || *cp == '\t')
cp++;
host.h_name = cp;
q = host.h_aliases = host_aliases;
cp = strpbrk(cp, " \t");
if (cp != NULL)
*cp++ = '\0';
while (cp && *cp) {
if (*cp == ' ' || *cp == '\t') {
cp++;
continue;
}
if (q < &host_aliases[MAXALIASES - 1])
*q++ = cp;
cp = strpbrk(cp, " \t");
if (cp != NULL)
*cp++ = '\0';
}
*q = NULL;
return (&host);
}
struct hostent *
_gethtbyname(char *name)
{
register struct hostent *p;
register char **cp;
_sethtent(0);
while (p = _gethtent()) {
if (strcasecmp(p->h_name, name) == 0)
break;
for (cp = p->h_aliases; *cp != 0; cp++)
if (strcasecmp(*cp, name) == 0)
goto found;
}
found:
_endhtent();
return (p);
}
struct hostent *
_gethtbyaddr(const char *addr, int len, int type)
{
register struct hostent *p;
#ifdef __GNO__
#define bcmp(__a, __b, __l) memcmp(__a, __b, __l)
#endif
_sethtent(0);
while (p = _gethtent())
if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))
break;
_endhtent();
return (p);
}

56
lib/netdb/getnetbyaddr.c Normal file
View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getnetbyaddr.c 5.7 (Berkeley) 6/1/90";
#endif /* LIBC_SCCS and not lint */
#include <netdb.h>
#include "protos.h"
extern int _net_stayopen;
struct netent *
getnetbyaddr(long net, 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);
}

63
lib/netdb/getnetbyname.c Normal file
View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getnetbyname.c 5.7 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <netdb.h>
#include <string.h>
#include "protos.h"
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);
}

122
lib/netdb/getnetent.c Normal file
View File

@ -0,0 +1,122 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getnetent.c 5.8 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include "protos.h"
#define MAXALIASES 35
static FILE *netf;
static char line[BUFSIZ+1];
static struct netent net;
static char *net_aliases[MAXALIASES];
int _net_stayopen;
void
setnetent(int f)
{
if (netf == NULL)
netf = fopen(_PATH_NETWORKS, "r" );
else
rewind(netf);
_net_stayopen |= f;
}
void
endnetent(void)
{
if (netf) {
fclose(netf);
netf = NULL;
}
_net_stayopen = 0;
}
struct netent *
getnetent(void)
{
char *p;
register char *cp, **q;
if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
return (NULL);
again:
p = fgets(line, BUFSIZ, netf);
if (p == NULL)
return (NULL);
if (*p == '#')
goto again;
cp = strpbrk(p, "#\n");
if (cp == NULL)
goto again;
*cp = '\0';
net.n_name = p;
cp = strpbrk(p, " \t");
if (cp == NULL)
goto again;
*cp++ = '\0';
while (*cp == ' ' || *cp == '\t')
cp++;
p = strpbrk(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 = strpbrk(cp, " \t");
if (cp != NULL)
*cp++ = '\0';
}
*q = NULL;
return (&net);
}

56
lib/netdb/getproto.c Normal file
View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getproto.c 5.6 (Berkeley) 6/1/90";
#endif /* LIBC_SCCS and not lint */
#include <netdb.h>
#include "protos.h"
extern int _proto_stayopen;
struct protoent *
getprotobynumber(int proto)
{
register struct protoent *p;
setprotoent(_proto_stayopen);
while (p = getprotoent())
if (p->p_proto == proto)
break;
if (!_proto_stayopen)
endprotoent();
return (p);
}

120
lib/netdb/getprotoent.c Normal file
View File

@ -0,0 +1,120 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getprotoent.c 5.8 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "protos.h"
#define MAXALIASES 35
static FILE *protof = NULL;
static char line[BUFSIZ+1];
static struct protoent proto;
static char *proto_aliases[MAXALIASES];
int _proto_stayopen;
void
setprotoent(int f)
{
if (protof == NULL)
protof = fopen(_PATH_PROTOCOLS, "r" );
else
rewind(protof);
_proto_stayopen |= f;
}
void
endprotoent(void)
{
if (protof) {
fclose(protof);
protof = NULL;
}
_proto_stayopen = 0;
}
struct protoent *
getprotoent(void)
{
char *p;
register char *cp, **q;
if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
return (NULL);
again:
if ((p = fgets(line, BUFSIZ, protof)) == NULL)
return (NULL);
if (*p == '#')
goto again;
cp = strpbrk(p, "#\n");
if (cp == NULL)
goto again;
*cp = '\0';
proto.p_name = p;
cp = strpbrk(p, " \t");
if (cp == NULL)
goto again;
*cp++ = '\0';
while (*cp == ' ' || *cp == '\t')
cp++;
p = strpbrk(cp, " \t");
if (p != NULL)
*p++ = '\0';
proto.p_proto = atoi(cp);
q = proto.p_aliases = proto_aliases;
if (p != NULL) {
cp = p;
while (cp && *cp) {
if (*cp == ' ' || *cp == '\t') {
cp++;
continue;
}
if (q < &proto_aliases[MAXALIASES - 1])
*q++ = cp;
cp = strpbrk(cp, " \t");
if (cp != NULL)
*cp++ = '\0';
}
}
*q = NULL;
return (&proto);
}

63
lib/netdb/getprotoname.c Normal file
View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getprotoname.c 5.7 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <netdb.h>
#include <string.h>
#include "protos.h"
extern int _proto_stayopen;
struct protoent *
getprotobyname(const char *name)
{
register struct protoent *p;
register char **cp;
setprotoent(_proto_stayopen);
while (p = getprotoent()) {
if (strcmp(p->p_name, name) == 0)
break;
for (cp = p->p_aliases; *cp != 0; cp++)
if (strcmp(*cp, name) == 0)
goto found;
}
found:
if (!_proto_stayopen)
endprotoent();
return (p);
}

66
lib/netdb/getservbyname.c Normal file
View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getservbyname.c 5.7 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <netdb.h>
#include <string.h>
#include "protos.h"
extern int _serv_stayopen;
struct servent *
getservbyname(const char *name, const char *proto)
{
register struct servent *p;
register char **cp;
setservent(_serv_stayopen);
while (p = getservent()) {
if (strcmp(name, p->s_name) == 0)
goto gotname;
for (cp = p->s_aliases; *cp; cp++)
if (strcmp(name, *cp) == 0)
goto gotname;
continue;
gotname:
if (proto == 0 || strcmp(p->s_proto, proto) == 0)
break;
}
if (!_serv_stayopen)
endservent();
return (p);
}

60
lib/netdb/getservbyport.c Normal file
View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getservbyport.c 5.7 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <netdb.h>
#include <string.h>
#include "protos.h"
extern int _serv_stayopen;
struct servent *
getservbyport(int port, const char *proto)
{
register struct servent *p;
setservent(_serv_stayopen);
while (p = getservent()) {
if (p->s_port != port)
continue;
if (proto == 0 || strcmp(p->s_proto, proto) == 0)
break;
}
if (!_serv_stayopen)
endservent();
return (p);
}

123
lib/netdb/getservent.c Normal file
View File

@ -0,0 +1,123 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getservent.c 5.9 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "protos.h"
#define MAXALIASES 35
static FILE *servf = NULL;
static char line[BUFSIZ+1];
static struct servent serv;
static char *serv_aliases[MAXALIASES];
int _serv_stayopen;
void
setservent(int f)
{
if (servf == NULL)
servf = fopen(_PATH_SERVICES, "r" );
else
rewind(servf);
_serv_stayopen |= f;
}
void
endservent(void)
{
if (servf) {
fclose(servf);
servf = NULL;
}
_serv_stayopen = 0;
}
struct servent *
getservent(void)
{
char *p;
register char *cp, **q;
if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
return (NULL);
again:
if ((p = fgets(line, BUFSIZ, servf)) == NULL)
return (NULL);
if (*p == '#')
goto again;
cp = strpbrk(p, "#\n");
if (cp == NULL)
goto again;
*cp = '\0';
serv.s_name = p;
p = strpbrk(p, " \t");
if (p == NULL)
goto again;
*p++ = '\0';
while (*p == ' ' || *p == '\t')
p++;
cp = strpbrk(p, ",/");
if (cp == NULL)
goto again;
*cp++ = '\0';
serv.s_port = htons((u_short)atoi(p));
serv.s_proto = cp;
q = serv.s_aliases = serv_aliases;
cp = strpbrk(cp, " \t");
if (cp != NULL)
*cp++ = '\0';
while (cp && *cp) {
if (*cp == ' ' || *cp == '\t') {
cp++;
continue;
}
if (q < &serv_aliases[MAXALIASES - 1])
*q++ = cp;
cp = strpbrk(cp, " \t");
if (cp != NULL)
*cp++ = '\0';
}
*q = NULL;
return (&serv);
}

95
lib/netdb/herror.c Normal file
View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 1987 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)herror.c 6.6 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include "protos.h"
char *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 */
};
int h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) };
extern int h_errno;
/*
* herror --
* print the error indicated by the h_errno value.
*/
void
herror(const char *s)
{
#if 1
struct iovec iov[4];
register struct iovec *v = iov;
if (s && *s) {
v->iov_base = (char *)s;
v->iov_len = strlen(s);
v++;
v->iov_base = ": ";
v->iov_len = 2;
v++;
}
v->iov_base = (u_int)h_errno < h_nerr ?
h_errlist[h_errno] : "Unknown error";
v->iov_len = strlen(v->iov_base);
v++;
v->iov_base = "\n";
v->iov_len = 1;
writev(STDERR_FILENO, iov, (v - iov) + 1);
#else
char *temp;
if (s && *s) {
write(STDERR_FILENO, s, strlen(s));
write(STDERR_FILENO, ": ", 2);
}
temp = ((u_int)h_errno < h_nerr) ? h_errlist[h_errno] : "Unknown error";
write(STDERR_FILENO, temp, strlen(temp));
write(STDERR_FILENO, "\n", 1);
#endif
}

150
lib/netdb/inet_addr.c Normal file
View File

@ -0,0 +1,150 @@
/*
* Copyright (c) 1983, 1990 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)inet_addr.c 5.11 (Berkeley) 12/9/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include "protos.h"
/*
* Check whether "cp" is a valid ascii representation
* of an Internet address and convert to a binary address.
* Returns 1 if the address is valid, 0 if not.
* This replaces inet_addr, the return value from which
* cannot distinguish between failure and a local broadcast address.
*/
int
inet_aton(const char *cp, struct in_addr *addr)
{
u_long val, base, n;
char c;
u_long parts[4], *pp = parts;
for (;;) {
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, other=decimal.
*/
val = 0; base = 10;
if (*cp == '0') {
if (*++cp == 'x' || *cp == 'X')
base = 16, cp++;
else
base = 8;
}
while ((c = *cp) != '\0') {
if (isascii(c) && isdigit(c)) {
val = (val * base) + (c - '0');
cp++;
continue;
}
if (base == 16 && isascii(c) && isxdigit(c)) {
val = (val << 4) +
(c + 10 - (islower(c) ? 'a' : 'A'));
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)
return (0);
*pp++ = val, cp++;
} else
break;
}
/*
* Check for trailing characters.
*/
if (*cp && (!isascii(*cp) || !isspace(*cp)))
return (0);
/*
* 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)
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;
}
if (addr)
addr->s_addr = htonl(val);
return (1);
}
/*
* Ascii internet address interpretation routine.
* The value returned is in network order.
*/
u_long
inet_addr(const char *cp)
{
struct in_addr val;
if (inet_aton(cp, &val))
return (val.s_addr);
return (INADDR_NONE);
}

60
lib/netdb/inet_lnaof.c Normal file
View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)inet_lnaof.c 5.7 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "protos.h"
/*
* Return the local network address portion of an
* internet address; handles class a/b/c network
* number formats.
*/
u_long
inet_lnaof(struct in_addr in)
{
register u_long i = ntohl(in.s_addr);
if (IN_CLASSA(i))
return ((i)&IN_CLASSA_HOST);
else if (IN_CLASSB(i))
return ((i)&IN_CLASSB_HOST);
else
return ((i)&IN_CLASSC_HOST);
}

63
lib/netdb/inet_makeaddr.c Normal file
View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)inet_makeaddr.c 5.6 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "protos.h"
/*
* Formulate an Internet address from network + host. Used in
* building addresses stored in the ifnet structure.
*/
struct in_addr
inet_makeaddr(u_long net, u_long host)
{
u_long addr;
if (net < 128)
addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
else if (net < 65536)
addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
else if (net < 16777216L)
addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
else
addr = net | host;
addr = htonl(addr);
return (*(struct in_addr *)&addr);
}

59
lib/netdb/inet_netof.c Normal file
View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)inet_netof.c 5.7 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "protos.h"
/*
* Return the network number from an internet
* address; handles class a/b/c network #'s.
*/
u_long
inet_netof(struct in_addr in)
{
register u_long i = ntohl(in.s_addr);
if (IN_CLASSA(i))
return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
else if (IN_CLASSB(i))
return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
else
return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
}

94
lib/netdb/inet_network.c Normal file
View File

@ -0,0 +1,94 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)inet_network.c 5.8 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include "protos.h"
/*
* Internet network address interpretation routine.
* The library routines call this routine to interpret
* network numbers.
*/
u_long
inet_network(const char *cp)
{
register u_long val, base, n;
register char c;
u_long parts[4], *pp = parts;
register int i;
again:
val = 0; base = 10;
if (*cp == '0')
base = 8, cp++;
if (*cp == 'x' || *cp == 'X')
base = 16, cp++;
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);
}

64
lib/netdb/inet_ntoa.c Normal file
View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)inet_ntoa.c 5.6 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
/*
* Convert network-format internet address
* to base 256 d.d.d.d representation.
*/
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include "protos.h"
char *
inet_ntoa(struct in_addr in)
{
static char b[18];
register char *p;
p = (char *)&in;
#define UC(b) (((int)b)&0xff)
#if 0
(void)snprintf(b, sizeof(b),
#else
(void)sprintf(b,
#endif
"%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
return (b);
}

121
lib/netdb/iso_addr.c Normal file
View File

@ -0,0 +1,121 @@
/*
* Copyright (c) 1989 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)iso_addr.c 5.4 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <netiso/iso.h>
#include <string.h>
#include "protos.h"
/* States*/
#define VIRGIN 0
#define GOTONE 1
#define GOTTWO 2
/* Inputs */
#define DIGIT (4*0)
#define END (4*1)
#define DELIM (4*2)
struct iso_addr *
iso_addr(addr)
register const char *addr;
{
static struct iso_addr out_addr;
register char *cp = out_addr.isoa_genaddr;
char *cplim = cp + sizeof(out_addr.isoa_genaddr);
register int byte = 0, state = VIRGIN, new;
bzero((char *)&out_addr, sizeof(out_addr));
do {
if ((*addr >= '0') && (*addr <= '9')) {
new = *addr - '0';
} else if ((*addr >= 'a') && (*addr <= 'f')) {
new = *addr - 'a' + 10;
} else if ((*addr >= 'A') && (*addr <= 'F')) {
new = *addr - 'A' + 10;
} else if (*addr == 0)
state |= END;
else
state |= DELIM;
addr++;
switch (state /* | INPUT */) {
case GOTTWO | DIGIT:
*cp++ = byte; /*FALLTHROUGH*/
case VIRGIN | DIGIT:
state = GOTONE; byte = new; continue;
case GOTONE | DIGIT:
state = GOTTWO; byte = new + (byte << 4); continue;
default: /* | DELIM */
state = VIRGIN; *cp++ = byte; byte = 0; continue;
case GOTONE | END:
case GOTTWO | END:
*cp++ = byte; /* FALLTHROUGH */
case VIRGIN | END:
break;
}
break;
} while (cp < cplim);
out_addr.isoa_len = cp - out_addr.isoa_genaddr;
return (&out_addr);
}
static char hexlist[] = "0123456789abcdef";
char *
iso_ntoa(isoa)
const struct iso_addr *isoa;
{
static char obuf[64];
register char *out = obuf;
register int i;
register u_char *in = (u_char *)isoa->isoa_genaddr;
u_char *inlim = in + isoa->isoa_len;
out[1] = 0;
while (in < inlim) {
i = *in++;
*out++ = '.';
if (i > 0xf) {
out[1] = hexlist[i & 0xf];
i >>= 4;
out[0] = hexlist[i];
out += 2;
} else
*out++ = hexlist[i];
}
*out = 0;
return(obuf + 1);
}

144
lib/netdb/linkaddr.c Normal file
View File

@ -0,0 +1,144 @@
/*-
* Copyright (c) 1990 The Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)linkaddr.c 5.2 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_dl.h>
#include <string.h>
#include "protos.h"
/* States*/
#define NAMING 0
#define GOTONE 1
#define GOTTWO 2
#define RESET 3
/* Inputs */
#define DIGIT (4*0)
#define END (4*1)
#define DELIM (4*2)
#define LETTER (4*3)
void
link_addr(addr, sdl)
register const char *addr;
register struct sockaddr_dl *sdl;
{
register char *cp = sdl->sdl_data;
char *cplim = sdl->sdl_len + (char *)sdl;
register int byte = 0, state = NAMING, new;
bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1);
sdl->sdl_family = AF_LINK;
do {
state &= ~LETTER;
if ((*addr >= '0') && (*addr <= '9')) {
new = *addr - '0';
} else if ((*addr >= 'a') && (*addr <= 'f')) {
new = *addr - 'a' + 10;
} else if ((*addr >= 'A') && (*addr <= 'F')) {
new = *addr - 'A' + 10;
} else if (*addr == 0) {
state |= END;
} else if (state == NAMING &&
(((*addr >= 'A') && (*addr <= 'Z')) ||
((*addr >= 'a') && (*addr <= 'z'))))
state |= LETTER;
else
state |= DELIM;
addr++;
switch (state /* | INPUT */) {
case NAMING | DIGIT:
case NAMING | LETTER:
*cp++ = addr[-1]; continue;
case NAMING | DELIM:
state = RESET; sdl->sdl_nlen = cp - sdl->sdl_data; continue;
case GOTTWO | DIGIT:
*cp++ = byte; /*FALLTHROUGH*/
case RESET | DIGIT:
state = GOTONE; byte = new; continue;
case GOTONE | DIGIT:
state = GOTTWO; byte = new + (byte << 4); continue;
default: /* | DELIM */
state = RESET; *cp++ = byte; byte = 0; continue;
case GOTONE | END:
case GOTTWO | END:
*cp++ = byte; /* FALLTHROUGH */
case RESET | END:
break;
}
break;
} while (cp < cplim);
sdl->sdl_alen = cp - LLADDR(sdl);
new = cp - (char *)sdl;
if (new > sizeof(*sdl))
sdl->sdl_len = new;
return;
}
static char hexlist[] = "0123456789abcdef";
char *
link_ntoa(sdl)
register const struct sockaddr_dl *sdl;
{
static char obuf[64];
register char *out = obuf;
register int i;
register u_char *in = (u_char *)LLADDR(sdl);
u_char *inlim = in + sdl->sdl_nlen;
int firsttime = 1;
if (sdl->sdl_nlen) {
bcopy(sdl->sdl_data, obuf, sdl->sdl_nlen);
out += sdl->sdl_nlen;
*out++ = ':';
}
while (in < inlim) {
if (firsttime) firsttime = 0; else *out++ = '.';
i = *in++;
if (i > 0xf) {
out[1] = hexlist[i & 0xf];
i >>= 4;
out[0] = hexlist[i];
out += 2;
} else
*out++ = hexlist[i];
}
*out = 0;
return(obuf);
}

229
lib/netdb/ns_addr.c Normal file
View File

@ -0,0 +1,229 @@
/*
* Copyright (c) 1986 Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* J.Q. Johnson.
*
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)ns_addr.c 6.7 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <netns/ns.h>
#include <stdio.h>
#include <string.h>
#include "protos.h"
static struct ns_addr addr, zero_addr;
static void Field(), cvtbase();
struct ns_addr
ns_addr(name)
const char *name;
{
char separator;
char *hostname, *socketname, *cp;
char buf[50];
(void)strncpy(buf, name, sizeof(buf - 1));
buf[sizeof(buf - 1)] = '\0';
/*
* First, figure out what he intends as a field separtor.
* Despite the way this routine is written, the prefered
* form 2-272.AA001234H.01777, i.e. XDE standard.
* Great efforts are made to insure backward compatability.
*/
if (hostname = index(buf, '#'))
separator = '#';
else {
hostname = index(buf, '.');
if ((cp = index(buf, ':')) &&
((hostname && cp < hostname) || (hostname == 0))) {
hostname = cp;
separator = ':';
} else
separator = '.';
}
if (hostname)
*hostname++ = 0;
addr = zero_addr;
Field(buf, addr.x_net.c_net, 4);
if (hostname == 0)
return (addr); /* No separator means net only */
socketname = index(hostname, separator);
if (socketname) {
*socketname++ = 0;
Field(socketname, (u_char *)&addr.x_port, 2);
}
Field(hostname, addr.x_host.c_host, 6);
return (addr);
}
static void
Field(buf, out, len)
char *buf;
u_char *out;
int len;
{
register char *bp = buf;
int i, ibase, base16 = 0, base10 = 0, clen = 0;
int hb[6], *hp;
char *fmt;
/*
* first try 2-273#2-852-151-014#socket
*/
if ((*buf != '-') &&
(1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
&hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
cvtbase(1000L, 256, hb, i, out, len);
return;
}
/*
* try form 8E1#0.0.AA.0.5E.E6#socket
*/
if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
&hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
cvtbase(256L, 256, hb, i, out, len);
return;
}
/*
* try form 8E1#0:0:AA:0:5E:E6#socket
*/
if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
&hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
cvtbase(256L, 256, hb, i, out, len);
return;
}
/*
* This is REALLY stretching it but there was a
* comma notation separting shorts -- definitely non standard
*/
if (1 < (i = sscanf(buf,"%x,%x,%x",
&hb[0], &hb[1], &hb[2]))) {
hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
hb[2] = htons(hb[2]);
cvtbase(65536L, 256, hb, i, out, len);
return;
}
/* Need to decide if base 10, 16 or 8 */
while (*bp) switch (*bp++) {
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '-':
break;
case '8': case '9':
base10 = 1;
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
base16 = 1;
break;
case 'x': case 'X':
*--bp = '0';
base16 = 1;
break;
case 'h': case 'H':
base16 = 1;
/* fall into */
default:
*--bp = 0; /* Ends Loop */
}
if (base16) {
fmt = "%3x";
ibase = 4096;
} else if (base10 == 0 && *buf == '0') {
fmt = "%3o";
ibase = 512;
} else {
fmt = "%3d";
ibase = 1000;
}
for (bp = buf; *bp++; ) clen++;
if (clen == 0) clen++;
if (clen > 18) clen = 18;
i = ((clen - 1) / 3) + 1;
bp = clen + buf - 3;
hp = hb + i - 1;
while (hp > hb) {
(void)sscanf(bp, fmt, hp);
bp[0] = 0;
hp--;
bp -= 3;
}
(void)sscanf(buf, fmt, hp);
cvtbase((long)ibase, 256, hb, i, out, len);
}
static void
cvtbase(oldbase,newbase,input,inlen,result,reslen)
long oldbase;
int newbase;
int input[];
int inlen;
unsigned char result[];
int reslen;
{
int d, e;
long sum;
e = 1;
while (e > 0 && reslen > 0) {
d = 0; e = 0; sum = 0;
/* long division: input=input/newbase */
while (d < inlen) {
sum = sum*oldbase + (long) input[d];
e += (sum > 0);
input[d++] = sum / newbase;
sum %= newbase;
}
result[--reslen] = sum; /* accumulate remainder */
}
for (d=0; d < reslen; d++)
result[d] = 0;
}

102
lib/netdb/ns_ntoa.c Normal file
View File

@ -0,0 +1,102 @@
/*
* Copyright (c) 1986 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)ns_ntoa.c 6.6 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <netns/ns.h>
#include <stdio.h>
#include "protos.h"
char *
ns_ntoa(addr)
struct ns_addr addr;
{
static char obuf[40];
union { union ns_net net_e; u_long long_e; } net;
u_short port = htons(addr.x_port);
register char *cp;
char *cp2;
register u_char *up = addr.x_host.c_host;
u_char *uplim = up + 6;
static char *spectHex();
net.net_e = addr.x_net;
sprintf(obuf, "%lx", ntohl(net.long_e));
cp = spectHex(obuf);
cp2 = cp + 1;
while (*up==0 && up < uplim) up++;
if (up == uplim) {
if (port) {
sprintf(cp, ".0");
cp += 2;
}
} else {
sprintf(cp, ".%x", *up++);
while (up < uplim) {
while (*cp) cp++;
sprintf(cp, "%02x", *up++);
}
cp = spectHex(cp2);
}
if (port) {
sprintf(cp, ".%x", port);
spectHex(cp + 1);
}
return (obuf);
}
static char *
spectHex(p0)
char *p0;
{
int ok = 0;
int nonzero = 0;
register char *p = p0;
for (; *p; p++) switch (*p) {
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
*p += ('A' - 'a');
/* fall into . . . */
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
ok = 1;
case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
nonzero = 1;
}
if (nonzero && !ok) { *p++ = 'H'; *p = 0; }
return (p);
}

41
lib/netdb/protos.h Normal file
View File

@ -0,0 +1,41 @@
segment "netdb ";
#include <sys/types.h>
#include <stdio.h>
/* herror.c */
void herror(const char *s);
/* rcmd.c */
int rresvport(int *alport);
int _validuser(FILE *hostf, char *rhost, char *luser, char *ruser, int baselen);
/* res_comp.c */
int dn_comp(const u_char *exp_dn, u_char *comp_dn, int length,
u_char **dnptrs, u_char **lastdnptr);
int dn_expand(const u_char *msg, const u_char *eomorig, const u_char *comp_dn,
u_char *exp_dn, int length);
void _res_close(void);
int __dn_skipname(const u_char *comp_dn, const u_char *eom);
void __putshort(register u_short s, register u_char *msgp);
void __putlong(register u_long l, register u_char *msgp);
/* res_debug.c */
void __p_query(char *msg);
/* res_init.c */
int res_init(void);
/* res_mkquery.c */
int res_mkquery(int op, const char *dname, int class, int type,
const char *data, int datalen, const struct rrec *newrr,
char *buf, int buflen);
/* res_query.c */
int res_query(char *name, int class, int type, u_char *answer, int anslen);
int res_querydomain(char *name, char *domain, int class, int type,
u_char *answer, int anslen);
int res_search(char *name, int class, int type, u_char *answer, int anslen);
/* res_send.c */
int res_send(const char *buf, int buflen, char *answer, int anslen);

359
lib/netdb/rcmd.c Normal file
View File

@ -0,0 +1,359 @@
/*
* Copyright (c) 1983 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)rcmd.c 5.24 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <fcntl.h>
#include <netdb.h>
#include <pwd.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include <gno/gno.h>
#include "protos.h"
int rcmd(char **ahost, u_short rport, const char *locuser, const char *remuser,
const char *cmd, int *fd2p)
{
int s, timo = 1, pid;
long oldmask;
static struct sockaddr_in sin, sin2, from;
char c;
int lport = IPPORT_RESERVED - 1;
struct hostent *hp;
fd_set reads;
pid = getpid();
hp = gethostbyname(*ahost);
if (hp == 0) {
herror(*ahost);
return (-1);
}
*ahost = hp->h_name;
oldmask = sigblock(sigmask(SIGURG));
for (;;) {
s = rresvport(&lport);
if (s < 0) {
if (errno == EAGAIN)
fprintf(stderr, "socket: All ports in use\n");
else
perror("rcmd: socket");
sigsetmask(oldmask);
return (-1);
}
/*
fcntl(s, F_SETOWN, pid);
*/
sin.sin_family = hp->h_addrtype;
bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr, hp->h_length);
sin.sin_port = rport;
if (connect(s, (struct __SOCKADDR *)&sin, sizeof(sin)) >= 0)
break;
#ifdef __ORCAC__
{
int e = errno;
#endif
(void) close(s);
#ifdef __ORCAC__
errno = e;
}
if (errno == ECONNRESET) {
lport--;
continue;
}
#endif
if (errno == EADDRINUSE) {
lport--;
continue;
}
if (errno == ECONNREFUSED && timo <= 16) {
sleep(timo);
timo *= 2;
continue;
}
if (hp->h_addr_list[1] != NULL) {
int oerrno = errno;
fprintf(stderr,
"connect to address %s: ", inet_ntoa(sin.sin_addr));
errno = oerrno;
perror(0);
hp->h_addr_list++;
bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr,
hp->h_length);
fprintf(stderr, "Trying %s...\n",
inet_ntoa(sin.sin_addr));
continue;
}
perror(hp->h_name);
sigsetmask(oldmask);
return (-1);
}
lport--;
if (fd2p == 0) {
write(s, "", 1);
lport = 0;
} else {
char num[8];
int s2 = rresvport(&lport), s3;
int len = sizeof (from);
if (s2 < 0)
goto bad;
listen(s2, 1);
(void) sprintf(num, "%d", lport);
if (write(s, num, strlen(num)+1) != strlen(num)+1) {
perror("write: setting up stderr");
(void) close(s2);
goto bad;
}
FD_ZERO(&reads);
FD_SET(s, &reads);
FD_SET(s2, &reads);
errno = 0;
if (select(32, &reads, 0, 0, 0) < 1 ||
!FD_ISSET(s2, &reads)) {
if (errno != 0)
perror("select: setting up stderr");
else
fprintf(stderr,
"select: protocol failure in circuit setup.\n");
(void) close(s2);
goto bad;
}
s3 = accept(s2, (struct __SOCKADDR *)&from, &len);
(void) close(s2);
if (s3 < 0) {
perror("accept");
lport = 0;
goto bad;
}
*fd2p = s3;
from.sin_port = ntohs((u_short)from.sin_port);
if (from.sin_family != AF_INET ||
from.sin_port >= IPPORT_RESERVED ||
from.sin_port < IPPORT_RESERVED / 2) {
fprintf(stderr,
"socket: protocol failure in circuit setup.\n");
goto bad2;
}
}
(void) write(s, locuser, strlen(locuser)+1);
(void) write(s, remuser, strlen(remuser)+1);
(void) write(s, cmd, strlen(cmd)+1);
if (read(s, &c, 1) != 1) {
perror(*ahost);
goto bad2;
}
if (c != 0) {
while (read(s, &c, 1) == 1) {
(void) write(2, &c, 1);
if (c == '\n')
break;
}
goto bad2;
}
sigsetmask(oldmask);
return (s);
bad2:
if (lport)
(void) close(*fd2p);
bad:
(void) close(s);
sigsetmask(oldmask);
return (-1);
}
int rresvport(int *alport)
{
struct sockaddr_in sin;
int s;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
return (-1);
for (;;) {
sin.sin_port = htons((u_short)*alport);
if (bind(s, (struct __SOCKADDR *)&sin, sizeof (sin)) >= 0)
return (s);
if (errno != EADDRINUSE) {
(void) close(s);
return (-1);
}
(*alport)--;
if (*alport == IPPORT_RESERVED/2) {
(void) close(s);
errno = EAGAIN; /* close */
return (-1);
}
}
}
int _check_rhosts_file = 1;
int ruserok(const char *rhost, int superuser, const char *ruser,
const char *luser)
{
FILE *hostf;
static char fhost[MAXHOSTNAMELEN];
int first = 1;
register char *sp, *p;
int baselen = -1;
sp = (char *)rhost;
p = fhost;
while (*sp) {
if (*sp == '.') {
if (baselen == -1)
baselen = sp - rhost;
*p++ = *sp++;
} else {
*p++ = isupper(*sp) ? tolower(*sp++) : *sp++;
}
}
*p = '\0';
hostf = superuser ? (FILE *)0 : fopen(_PATH_HEQUIV, "r");
again:
if (hostf) {
if (!_validuser(hostf, fhost, luser, ruser, baselen)) {
(void) fclose(hostf);
return(0);
}
(void) fclose(hostf);
}
if (first == 1 && (_check_rhosts_file || superuser)) {
struct stat sbuf;
struct passwd *pwd;
static char pbuf[MAXPATHLEN];
first = 0;
if ((pwd = getpwnam(luser)) == NULL)
return(-1);
(void)strcpy(pbuf, pwd->pw_dir);
(void)strcat(pbuf, "/.rhosts");
if ((hostf = fopen(pbuf, "r")) == NULL)
return(-1);
/*
* if owned by someone other than user or root or if
* writeable by anyone but the owner, quit
*/
if (fstat(fileno(hostf), &sbuf) ||
sbuf.st_uid && sbuf.st_uid != pwd->pw_uid ||
sbuf.st_mode&022) {
fclose(hostf);
return(-1);
}
goto again;
}
return (-1);
}
/* don't make static, used by lpd(8) */
int _validuser(FILE *hostf, char *rhost, char *luser, char *ruser, int baselen)
{
register char *p;
char *user;
static char ahost[MAXHOSTNAMELEN];
static int _checkhost(char *rhost, char *lhost, int len);
while (fgets(ahost, sizeof (ahost), hostf)) {
p = ahost;
while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
*p = isupper(*p) ? tolower(*p) : *p;
p++;
}
if (*p == ' ' || *p == '\t') {
*p++ = '\0';
while (*p == ' ' || *p == '\t')
p++;
user = p;
while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0')
p++;
} else
user = p;
*p = '\0';
if (_checkhost(rhost, ahost, baselen) &&
!strcmp(ruser, *user ? user : luser)) {
return (0);
}
}
return (-1);
}
static int
_checkhost(char *rhost, char *lhost, int len)
{
static char ldomain[MAXHOSTNAMELEN + 1];
static char *domainp = NULL;
static int nodomain = 0;
register char *cp;
if (len == -1)
return(!strcmp(rhost, lhost));
if (strncmp(rhost, lhost, len))
return(0);
if (!strcmp(rhost, lhost))
return(1);
if (*(lhost + len) != '\0')
return(0);
if (nodomain)
return(0);
if (!domainp) {
if (gethostname(ldomain, sizeof(ldomain)) == -1) {
nodomain = 1;
return(0);
}
ldomain[MAXHOSTNAMELEN] = 0; /* NULL; */
if ((domainp = index(ldomain, '.')) == (char *)NULL) {
nodomain = 1;
return(0);
}
for (cp = ++domainp; *cp; ++cp)
if (isupper(*cp))
*cp = tolower(*cp);
}
return(!strcmp(domainp, rhost + len +1));
}

49
lib/netdb/recv.c Normal file
View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 1988 The Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)recv.c 5.3 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include "protos.h"
recv(s, buf, len, flags)
int s, len, flags;
void *buf;
{
return(recvfrom(s, buf, len, flags, (struct __SOCKADDR *)NULL, 0));
}

341
lib/netdb/res_comp.c Normal file
View File

@ -0,0 +1,341 @@
/*
* Copyright (c) 1985 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)res_comp.c 6.22 (Berkeley) 3/19/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <arpa/nameser.h>
#include <netinet/in.h>
#include <resolv.h>
#include <stdio.h>
#include "protos.h"
static dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, u_char **lastdnptr);
/*
* Expand compressed domain name 'comp_dn' to full domain name.
* 'msg' is a pointer to the begining of the message,
* 'eomorig' points to the first location after the message,
* 'exp_dn' is a pointer to a buffer of size 'length' for the result.
* Return size of compressed name or -1 if there was an error.
*/
int dn_expand(const u_char *msg, const u_char *eomorig, const u_char *comp_dn,
u_char *exp_dn, int length)
{
register u_char *cp, *dn;
register int n, c;
u_char *eom;
int len = -1, checked = 0;
dn = exp_dn;
cp = (u_char *)comp_dn;
eom = exp_dn + length;
/*
* fetch next label in domain name
*/
while (n = *cp++) {
/*
* Check for indirection
*/
switch (n & INDIR_MASK) {
case 0:
if (dn != exp_dn) {
if (dn >= eom)
return (-1);
*dn++ = '.';
}
if (dn+n >= eom)
return (-1);
checked += n + 1;
while (--n >= 0) {
if ((c = *cp++) == '.') {
if (dn + n + 2 >= eom)
return (-1);
*dn++ = '\\';
}
*dn++ = c;
if (cp >= eomorig) /* out of range */
return(-1);
}
break;
case INDIR_MASK:
if (len < 0)
len = cp - comp_dn + 1;
cp = (u_char *)msg + (((n & 0x3f) << 8) | (*cp & 0xff));
if (cp < msg || cp >= eomorig) /* out of range */
return(-1);
checked += 2;
/*
* Check for loops in the compressed name;
* if we've looked at the whole message,
* there must be a loop.
*/
if (checked >= eomorig - msg)
return (-1);
break;
default:
return (-1); /* flag error */
}
}
*dn = '\0';
if (len < 0)
len = cp - comp_dn;
return (len);
}
/*
* Compress domain name 'exp_dn' into 'comp_dn'.
* Return the size of the compressed name or -1.
* 'length' is the size of the array pointed to by 'comp_dn'.
* 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
* is a pointer to the beginning of the message. The list ends with NULL.
* 'lastdnptr' is a pointer to the end of the arrary pointed to
* by 'dnptrs'. Side effect is to update the list of pointers for
* labels inserted into the message as we compress the name.
* If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
* is NULL, we don't update the list.
*/
int dn_comp(const u_char *exp_dn, u_char *comp_dn, int length,
u_char **dnptrs, u_char **lastdnptr)
{
register u_char *cp, *dn;
register int c, l;
u_char **cpp, **lpp, *sp, *eob;
u_char *msg;
dn = (u_char *)exp_dn;
cp = comp_dn;
eob = cp + length;
if (dnptrs != NULL) {
if ((msg = *dnptrs++) != NULL) {
for (cpp = dnptrs; *cpp != NULL; cpp++)
;
lpp = cpp; /* end of list to search */
}
} else
msg = NULL;
for (c = *dn++; c != '\0'; ) {
/* look to see if we can use pointers */
if (msg != NULL) {
if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
if (cp+1 >= eob)
return (-1);
*cp++ = (l >> 8) | INDIR_MASK;
*cp++ = l % 256;
return (cp - comp_dn);
}
/* not found, save it */
if (lastdnptr != NULL && cpp < lastdnptr-1) {
*cpp++ = cp;
*cpp = NULL;
}
}
sp = cp++; /* save ptr to length byte */
do {
if (c == '.') {
c = *dn++;
break;
}
if (c == '\\') {
if ((c = *dn++) == '\0')
break;
}
if (cp >= eob) {
if (msg != NULL)
*lpp = NULL;
return (-1);
}
*cp++ = c;
} while ((c = *dn++) != '\0');
/* catch trailing '.'s but not '..' */
if ((l = cp - sp - 1) == 0 && c == '\0') {
cp--;
break;
}
if (l <= 0 || l > MAXLABEL) {
if (msg != NULL)
*lpp = NULL;
return (-1);
}
*sp = l;
}
if (cp >= eob) {
if (msg != NULL)
*lpp = NULL;
return (-1);
}
*cp++ = '\0';
return (cp - comp_dn);
}
/*
* Skip over a compressed domain name. Return the size or -1.
*/
int __dn_skipname(const u_char *comp_dn, const u_char *eom)
{
register u_char *cp;
register int n;
cp = (u_char *)comp_dn;
while (cp < eom && (n = *cp++)) {
/*
* check for indirection
*/
switch (n & INDIR_MASK) {
case 0: /* normal case, n == len */
cp += n;
continue;
default: /* illegal type */
return (-1);
case INDIR_MASK: /* indirection */
cp++;
}
break;
}
return (cp - comp_dn);
}
/*
* Search for expanded name from a list of previously compressed names.
* Return the offset from msg if found or -1.
* dnptrs is the pointer to the first name on the list,
* not the pointer to the start of the message.
*/
static
dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, u_char **lastdnptr)
{
register u_char *dn, *cp, **cpp;
register int n;
u_char *sp;
for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
dn = exp_dn;
sp = cp = *cpp;
while (n = *cp++) {
/*
* check for indirection
*/
switch (n & INDIR_MASK) {
case 0: /* normal case, n == len */
while (--n >= 0) {
if (*dn == '.')
goto next;
if (*dn == '\\')
dn++;
if (*dn++ != *cp++)
goto next;
}
if ((n = *dn++) == '\0' && *cp == '\0')
return (sp - msg);
if (n == '.')
continue;
goto next;
default: /* illegal type */
return (-1);
case INDIR_MASK: /* indirection */
cp = msg + (((n & 0x3f) << 8) | *cp);
}
}
if (*dn == '\0')
return (sp - msg);
next: ;
}
return (-1);
}
/*
* Routines to insert/extract short/long's. Must account for byte
* order and non-alignment problems. This code at least has the
* advantage of being portable.
*
* used by sendmail.
*/
u_short
_getshort(u_char *msgp)
{
register u_char *p = (u_char *) msgp;
#ifdef vax
/*
* vax compiler doesn't put shorts in registers
*/
register u_long u;
#else
register u_short u;
#endif
u = *p++ << 8;
return ((u_short)(u | *p));
}
u_long
_getlong(u_char *msgp)
{
register u_char *p = (u_char *) msgp;
register u_long u;
u = *p++; u <<= 8;
u |= *p++; u <<= 8;
u |= *p++; u <<= 8;
return (u | *p);
}
void
#ifdef __STDC__
__putshort(register u_short s, register u_char *msgp)
#else
__putshort(s, msgp)
register u_short s;
register u_char *msgp;
#endif
{
msgp[1] = s;
msgp[0] = s >> 8;
}
void
__putlong(register u_long l, register u_char *msgp)
{
msgp[3] = l;
msgp[2] = (l >>= 8);
msgp[1] = (l >>= 8);
msgp[0] = l >> 8;
}

511
lib/netdb/res_debug.c Normal file
View File

@ -0,0 +1,511 @@
/*-
* Copyright (c) 1985, 1990 Regents of the University of California.
* 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.
*
* @(#)res_debug.c 5.36 (Berkeley) 3/6/91
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)res_debug.c 5.36 (Berkeley) 3/6/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <stdio.h>
#include <string.h>
#include "protos.h"
void __fp_query(char *msg, FILE *file);
char *__p_class(int class), *__p_time(u_long value), *__p_type(int type);
static char *p_cdname(char *cp, char *msg, FILE *file),
*p_rr(char *cp, char *msg, FILE *file);
char *_res_opcodes[] = {
"QUERY",
"IQUERY",
"CQUERYM",
"CQUERYU",
"4",
"5",
"6",
"7",
"8",
"UPDATEA",
"UPDATED",
"UPDATEDA",
"UPDATEM",
"UPDATEMA",
"ZONEINIT",
"ZONEREF",
};
char *_res_resultcodes[] = {
"NOERROR",
"FORMERR",
"SERVFAIL",
"NXDOMAIN",
"NOTIMP",
"REFUSED",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"NOCHANGE",
};
void __p_query(char *msg)
{
__fp_query(msg,stdout);
}
/*
* Print the contents of a query.
* This is intended to be primarily a debugging routine.
*/
void
__fp_query(char *msg, FILE *file)
{
register char *cp;
register HEADER *hp;
register int n;
/*
* Print header fields.
*/
hp = (HEADER *)msg;
cp = msg + sizeof(HEADER);
fprintf(file,"HEADER:\n");
fprintf(file,"\topcode = %s", _res_opcodes[hp->opcode]);
fprintf(file,", id = %d", ntohs(hp->id));
fprintf(file,", rcode = %s\n", _res_resultcodes[hp->rcode]);
fprintf(file,"\theader flags: ");
if (hp->qr)
fprintf(file," qr");
if (hp->aa)
fprintf(file," aa");
if (hp->tc)
fprintf(file," tc");
if (hp->rd)
fprintf(file," rd");
if (hp->ra)
fprintf(file," ra");
if (hp->pr)
fprintf(file," pr");
fprintf(file,"\n\tqdcount = %d", ntohs(hp->qdcount));
fprintf(file,", ancount = %d", ntohs(hp->ancount));
fprintf(file,", nscount = %d", ntohs(hp->nscount));
fprintf(file,", arcount = %d\n\n", ntohs(hp->arcount));
/*
* Print question records.
*/
if (n = ntohs(hp->qdcount)) {
fprintf(file,"QUESTIONS:\n");
while (--n >= 0) {
fprintf(file,"\t");
cp = p_cdname(cp, msg, file);
if (cp == NULL)
return;
fprintf(file,", type = %s", __p_type(_getshort(cp)));
cp += sizeof(u_short);
fprintf(file,
", class = %s\n\n", __p_class(_getshort(cp)));
cp += sizeof(u_short);
}
}
/*
* Print authoritative answer records
*/
if (n = ntohs(hp->ancount)) {
fprintf(file,"ANSWERS:\n");
while (--n >= 0) {
fprintf(file,"\t");
cp = p_rr(cp, msg, file);
if (cp == NULL)
return;
}
}
/*
* print name server records
*/
if (n = ntohs(hp->nscount)) {
fprintf(file,"NAME SERVERS:\n");
while (--n >= 0) {
fprintf(file,"\t");
cp = p_rr(cp, msg, file);
if (cp == NULL)
return;
}
}
/*
* print additional records
*/
if (n = ntohs(hp->arcount)) {
fprintf(file,"ADDITIONAL RECORDS:\n");
while (--n >= 0) {
fprintf(file,"\t");
cp = p_rr(cp, msg, file);
if (cp == NULL)
return;
}
}
}
static char *
p_cdname(char *cp, char *msg, FILE *file)
{
static char name[MAXDNAME];
int n;
if ((n = dn_expand((u_char *)msg, (u_char *)msg + 512, (u_char *)cp,
(u_char *)name, sizeof(name))) < 0)
return (NULL);
if (name[0] == '\0') {
name[0] = '.';
name[1] = '\0';
}
fputs(name, file);
return (cp + n);
}
/*
* Print resource record fields in human readable form.
*/
static char *
p_rr(char *cp, char *msg, FILE *file)
{
int type, class, dlen, n, c;
struct in_addr inaddr;
char *cp1, *cp2;
if ((cp = p_cdname(cp, msg, file)) == NULL)
return (NULL); /* compression error */
fprintf(file,"\n\ttype = %s", __p_type(type = _getshort(cp)));
cp += sizeof(u_short);
fprintf(file,", class = %s", __p_class(class = _getshort(cp)));
cp += sizeof(u_short);
fprintf(file,", ttl = %s", __p_time(_getlong(cp)));
cp += sizeof(u_long);
fprintf(file,", dlen = %d\n", dlen = _getshort(cp));
cp += sizeof(u_short);
cp1 = cp;
/*
* Print type specific data, if appropriate
*/
switch (type) {
case T_A:
switch (class) {
case C_IN:
case C_HS:
bcopy(cp, (char *)&inaddr, sizeof(inaddr));
if (dlen == 4) {
fprintf(file,"\tinternet address = %s\n",
inet_ntoa(inaddr));
cp += dlen;
} else if (dlen == 7) {
fprintf(file,"\tinternet address = %s",
inet_ntoa(inaddr));
fprintf(file,", protocol = %d", cp[4]);
fprintf(file,", port = %d\n",
(cp[5] << 8) + cp[6]);
cp += dlen;
}
break;
default:
cp += dlen;
}
break;
case T_CNAME:
case T_MB:
case T_MG:
case T_MR:
case T_NS:
case T_PTR:
fprintf(file,"\tdomain name = ");
cp = p_cdname(cp, msg, file);
fprintf(file,"\n");
break;
case T_HINFO:
if (n = *cp++) {
fprintf(file,"\tCPU=%.*s\n", n, cp);
cp += n;
}
if (n = *cp++) {
fprintf(file,"\tOS=%.*s\n", n, cp);
cp += n;
}
break;
case T_SOA:
fprintf(file,"\torigin = ");
cp = p_cdname(cp, msg, file);
fprintf(file,"\n\tmail addr = ");
cp = p_cdname(cp, msg, file);
fprintf(file,"\n\tserial = %ld", _getlong(cp));
cp += sizeof(u_long);
fprintf(file,"\n\trefresh = %s", __p_time(_getlong(cp)));
cp += sizeof(u_long);
fprintf(file,"\n\tretry = %s", __p_time(_getlong(cp)));
cp += sizeof(u_long);
fprintf(file,"\n\texpire = %s", __p_time(_getlong(cp)));
cp += sizeof(u_long);
fprintf(file,"\n\tmin = %s\n", __p_time(_getlong(cp)));
cp += sizeof(u_long);
break;
case T_MX:
fprintf(file,"\tpreference = %ld,",_getshort(cp));
cp += sizeof(u_short);
fprintf(file," name = ");
cp = p_cdname(cp, msg, file);
break;
case T_TXT:
(void) fputs("\t\"", file);
cp2 = cp1 + dlen;
while (cp < cp2) {
if (n = (unsigned char) *cp++) {
for (c = n; c > 0 && cp < cp2; c--)
if (*cp == '\n') {
(void) putc('\\', file);
(void) putc(*cp++, file);
} else
(void) putc(*cp++, file);
}
}
(void) fputs("\"\n", file);
break;
case T_MINFO:
fprintf(file,"\trequests = ");
cp = p_cdname(cp, msg, file);
fprintf(file,"\n\terrors = ");
cp = p_cdname(cp, msg, file);
break;
case T_UINFO:
fprintf(file,"\t%s\n", cp);
cp += dlen;
break;
case T_UID:
case T_GID:
if (dlen == 4) {
fprintf(file,"\t%ld\n", _getlong(cp));
cp += sizeof(int);
}
break;
case T_WKS:
if (dlen < sizeof(u_long) + 1)
break;
bcopy(cp, (char *)&inaddr, sizeof(inaddr));
cp += sizeof(u_long);
fprintf(file,"\tinternet address = %s, protocol = %d\n\t",
inet_ntoa(inaddr), *cp++);
n = 0;
while (cp < cp1 + dlen) {
c = *cp++;
do {
if (c & 0200)
fprintf(file," %d", n);
c <<= 1;
} while (++n & 07);
}
putc('\n',file);
break;
#ifdef ALLOW_T_UNSPEC
case T_UNSPEC:
{
int NumBytes = 8;
char *DataPtr;
int i;
if (dlen < NumBytes) NumBytes = dlen;
fprintf(file, "\tFirst %d bytes of hex data:",
NumBytes);
for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++)
fprintf(file, " %x", *DataPtr);
fputs("\n", file);
cp += dlen;
}
break;
#endif /* ALLOW_T_UNSPEC */
default:
fprintf(file,"\t???\n");
cp += dlen;
}
if (cp != cp1 + dlen) {
fprintf(file,"packet size error (%#x != %#x)\n", cp, cp1+dlen);
cp = NULL;
}
fprintf(file,"\n");
return (cp);
}
static char nbuf[40];
/*
* Return a string for the type
*/
char *
__p_type(int type)
{
switch (type) {
case T_A:
return("A");
case T_NS: /* authoritative server */
return("NS");
case T_CNAME: /* canonical name */
return("CNAME");
case T_SOA: /* start of authority zone */
return("SOA");
case T_MB: /* mailbox domain name */
return("MB");
case T_MG: /* mail group member */
return("MG");
case T_MR: /* mail rename name */
return("MR");
case T_NULL: /* null resource record */
return("NULL");
case T_WKS: /* well known service */
return("WKS");
case T_PTR: /* domain name pointer */
return("PTR");
case T_HINFO: /* host information */
return("HINFO");
case T_MINFO: /* mailbox information */
return("MINFO");
case T_MX: /* mail routing info */
return("MX");
case T_TXT: /* text */
return("TXT");
case T_AXFR: /* zone transfer */
return("AXFR");
case T_MAILB: /* mail box */
return("MAILB");
case T_MAILA: /* mail address */
return("MAILA");
case T_ANY: /* matches any type */
return("ANY");
case T_UINFO:
return("UINFO");
case T_UID:
return("UID");
case T_GID:
return("GID");
#ifdef ALLOW_T_UNSPEC
case T_UNSPEC:
return("UNSPEC");
#endif /* ALLOW_T_UNSPEC */
default:
(void)sprintf(nbuf, "%d", type);
return(nbuf);
}
}
/*
* Return a mnemonic for class
*/
char *
__p_class(int class)
{
switch (class) {
case C_IN: /* internet class */
return("IN");
case C_HS: /* hesiod class */
return("HS");
case C_ANY: /* matches any class */
return("ANY");
default:
(void)sprintf(nbuf, "%d", class);
return(nbuf);
}
}
/*
* Return a mnemonic for a time to live
*/
char *
__p_time(u_long value)
{
int secs, mins, hours;
register char *p;
if (value == 0) {
strcpy(nbuf, "0 secs");
return(nbuf);
}
secs = value % 60;
value /= 60;
mins = value % 60;
value /= 60;
hours = value % 24;
value /= 24;
#define PLURALIZE(x) x, (x == 1) ? "" : "s"
p = nbuf;
if (value) {
(void)sprintf(p, "%lu day%s", PLURALIZE(value));
while (*++p);
}
if (hours) {
if (value)
*p++ = ' ';
(void)sprintf(p, "%d hour%s", PLURALIZE(hours));
while (*++p);
}
if (mins) {
if (value || hours)
*p++ = ' ';
(void)sprintf(p, "%d min%s", PLURALIZE(mins));
while (*++p);
}
if (secs || ! (value || hours || mins)) {
if (value || hours || mins)
*p++ = ' ';
(void)sprintf(p, "%d sec%s", PLURALIZE(secs));
}
return(nbuf);
}

195
lib/netdb/res_init.c Normal file
View File

@ -0,0 +1,195 @@
/*-
* Copyright (c) 1985, 1989 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)res_init.c 6.15 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h> /* gethostname() is stuck in here temporarily */
#include "protos.h"
/*
* Resolver state default settings
*/
struct state _res = {
RES_TIMEOUT, /* retransmition time interval */
4, /* number of times to retransmit */
RES_DEFAULT, /* options flags */
1, /* number of name servers */
};
/*
* Set up default settings. If the configuration file exist, the values
* there will have precedence. Otherwise, the server address is set to
* INADDR_ANY and the default domain name comes from the gethostname().
*
* The configuration file should only be used if you want to redefine your
* domain or run without a server on your machine.
*
* Return 0 if completes successfully, -1 on error
*/
int res_init(void)
{
register FILE *fp;
register char *cp, **pp;
register int n;
static char buf[BUFSIZ];
int nserv = 0; /* number of nameserver records read from file */
int haveenv = 0;
int havesearch = 0;
_res.nsaddr.sin_addr.s_addr = INADDR_ANY;
_res.nsaddr.sin_family = AF_INET;
_res.nsaddr.sin_port = htons(NAMESERVER_PORT);
_res.nscount = 1;
/* Allow user to override the local domain definition */
if ((cp = getenv("LOCALDOMAIN")) != NULL) {
(void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
haveenv++;
}
if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
/* read the config file */
while (fgets(buf, sizeof(buf), fp) != NULL) {
/* read default domain name */
if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
if (haveenv) /* skip if have from environ */
continue;
cp = buf + sizeof("domain") - 1;
while (*cp == ' ' || *cp == '\t')
cp++;
if ((*cp == '\0') || (*cp == '\n'))
continue;
(void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
if ((cp = index(_res.defdname, '\n')) != NULL)
*cp = '\0';
havesearch = 0;
continue;
}
/* set search list */
if (!strncmp(buf, "search", sizeof("search") - 1)) {
if (haveenv) /* skip if have from environ */
continue;
cp = buf + sizeof("search") - 1;
while (*cp == ' ' || *cp == '\t')
cp++;
if ((*cp == '\0') || (*cp == '\n'))
continue;
(void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
if ((cp = index(_res.defdname, '\n')) != NULL)
*cp = '\0';
/*
* Set search list to be blank-separated strings
* on rest of line.
*/
cp = _res.defdname;
pp = _res.dnsrch;
*pp++ = cp;
for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
if (*cp == ' ' || *cp == '\t') {
*cp = 0;
n = 1;
} else if (n) {
*pp++ = cp;
n = 0;
}
}
/* null terminate last domain if there are excess */
while (*cp != '\0' && *cp != ' ' && *cp != '\t')
cp++;
*cp = '\0';
*pp++ = 0;
havesearch = 1;
continue;
}
/* read nameservers to query */
if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) &&
nserv < MAXNS) {
cp = buf + sizeof("nameserver") - 1;
while (*cp == ' ' || *cp == '\t')
cp++;
if ((*cp == '\0') || (*cp == '\n'))
continue;
if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
inet_addr(cp)) == (unsigned)-1) {
_res.nsaddr_list[nserv].sin_addr.s_addr
= INADDR_ANY;
continue;
}
_res.nsaddr_list[nserv].sin_family = AF_INET;
_res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
nserv++;
continue;
}
}
if (nserv > 1)
_res.nscount = nserv;
(void) fclose(fp);
}
if (_res.defdname[0] == 0) {
if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
(cp = index(buf, '.')))
(void)strcpy(_res.defdname, cp + 1);
}
/* find components of local domain that might be searched */
if (havesearch == 0) {
pp = _res.dnsrch;
*pp++ = _res.defdname;
for (cp = _res.defdname, n = 0; *cp; cp++)
if (*cp == '.')
n++;
cp = _res.defdname;
for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH;
n--) {
cp = index(cp, '.');
*pp++ = ++cp;
}
*pp++ = 0;
}
_res.options |= RES_INIT;
return (0);
}

204
lib/netdb/res_mkquery.c Normal file
View File

@ -0,0 +1,204 @@
/*
* Copyright (c) 1985 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)res_mkquery.c 6.16 (Berkeley) 3/6/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <stdio.h>
#include <string.h>
#include "protos.h"
/*
* Form all types of queries.
* Returns the size of the result or -1.
*/
int res_mkquery(int op, const char *dname, int class, int type,
const char *data, int datalen, const struct rrec *newrr,
char *buf, int buflen)
{
register HEADER *hp;
register char *cp;
register int n;
static char *dnptrs[10];
char **dpp, **lastdnptr;
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_mkquery(%d, %s, %d, %d)\n", op, dname, class, type);
#endif /* DEBUG */
/*
* Initialize header fields.
*/
if ((buf == NULL) || (buflen < sizeof(HEADER)))
return(-1);
bzero(buf, sizeof(HEADER));
hp = (HEADER *) buf;
hp->id = htons(++_res.id);
hp->opcode = op;
hp->pr = (_res.options & RES_PRIMARY) != 0;
hp->rd = (_res.options & RES_RECURSE) != 0;
hp->rcode = NOERROR;
cp = buf + sizeof(HEADER);
buflen -= sizeof(HEADER);
dpp = dnptrs;
*dpp++ = buf;
*dpp++ = NULL;
lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]);
/*
* perform opcode specific processing
*/
switch (op) {
case QUERY:
if ((buflen -= QFIXEDSZ) < 0)
return(-1);
if ((n = dn_comp((u_char *)dname, (u_char *)cp, buflen,
(u_char **)dnptrs, (u_char **)lastdnptr)) < 0)
return (-1);
cp += n;
buflen -= n;
__putshort(type, (u_char *)cp);
cp += sizeof(u_short);
__putshort(class, (u_char *)cp);
cp += sizeof(u_short);
hp->qdcount = htons(1);
if (op == QUERY || data == NULL)
break;
/*
* Make an additional record for completion domain.
*/
buflen -= RRFIXEDSZ;
if ((n = dn_comp((u_char *)data, (u_char *)cp, buflen,
(u_char **)dnptrs, (u_char **)lastdnptr)) < 0)
return (-1);
cp += n;
buflen -= n;
__putshort(T_NULL, (u_char *)cp);
cp += sizeof(u_short);
__putshort(class, (u_char *)cp);
cp += sizeof(u_short);
__putlong(0, (u_char *)cp);
cp += sizeof(u_long);
__putshort(0, (u_char *)cp);
cp += sizeof(u_short);
hp->arcount = htons(1);
break;
case IQUERY:
/*
* Initialize answer section
*/
if (buflen < 1 + RRFIXEDSZ + datalen)
return (-1);
*cp++ = '\0'; /* no domain name */
__putshort(type, (u_char *)cp);
cp += sizeof(u_short);
__putshort(class, (u_char *)cp);
cp += sizeof(u_short);
__putlong(0, (u_char *)cp);
cp += sizeof(u_long);
__putshort(datalen, (u_char *)cp);
cp += sizeof(u_short);
if (datalen) {
bcopy(data, cp, datalen);
cp += datalen;
}
hp->ancount = htons(1);
break;
#ifdef ALLOW_UPDATES
/*
* For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA
* (Record to be modified is followed by its replacement in msg.)
*/
case UPDATEM:
case UPDATEMA:
case UPDATED:
/*
* The res code for UPDATED and UPDATEDA is the same; user
* calls them differently: specifies data for UPDATED; server
* ignores data if specified for UPDATEDA.
*/
case UPDATEDA:
buflen -= RRFIXEDSZ + datalen;
if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
return (-1);
cp += n;
__putshort(type, cp);
cp += sizeof(u_short);
__putshort(class, cp);
cp += sizeof(u_short);
__putlong(0, cp);
cp += sizeof(u_long);
__putshort(datalen, cp);
cp += sizeof(u_short);
if (datalen) {
bcopy(data, cp, datalen);
cp += datalen;
}
if ( (op == UPDATED) || (op == UPDATEDA) ) {
hp->ancount = htons(0);
break;
}
/* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */
case UPDATEA: /* Add new resource record */
buflen -= RRFIXEDSZ + datalen;
if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
return (-1);
cp += n;
__putshort(newrr->r_type, cp);
cp += sizeof(u_short);
__putshort(newrr->r_class, cp);
cp += sizeof(u_short);
__putlong(0, cp);
cp += sizeof(u_long);
__putshort(newrr->r_size, cp);
cp += sizeof(u_short);
if (newrr->r_size) {
bcopy(newrr->r_data, cp, newrr->r_size);
cp += newrr->r_size;
}
hp->ancount = htons(0);
break;
#endif /* ALLOW_UPDATES */
}
return (cp - buf);
}

271
lib/netdb/res_query.c Normal file
View File

@ -0,0 +1,271 @@
/*
* Copyright (c) 1988 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)res_query.c 5.11 (Berkeley) 3/6/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <netdb.h>
#include <resolv.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "protos.h"
#if PACKETSZ > 1024
#define MAXPACKET PACKETSZ
#else
#define MAXPACKET 1024
#endif
int h_errno;
/*
* Formulate a normal query, send, and await answer.
* Returned answer is placed in supplied buffer "answer".
* Perform preliminary check of answer, returning success only
* if no error is indicated and the answer count is nonzero.
* Return the size of the response on success, -1 on error.
* Error number is left in h_errno.
* Caller must parse answer and determine whether it answers the question.
*/
int res_query(char *name, int class, int type, u_char *answer, int anslen)
{
static char buf[MAXPACKET];
HEADER *hp;
int n;
if ((_res.options & RES_INIT) == 0 && res_init() == -1)
return (-1);
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_query(%s, %d, %d)\n", name, class, type);
#endif
n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
buf, sizeof(buf));
if (n <= 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_query: mkquery failed\n");
#endif
h_errno = NO_RECOVERY;
return (n);
}
n = res_send(buf, n, (char *)answer, anslen);
if (n < 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_query: send error\n");
#endif
h_errno = TRY_AGAIN;
return(n);
}
hp = (HEADER *) answer;
if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("rcode = %d, ancount=%d\n", hp->rcode,
ntohs(hp->ancount));
#endif
switch (hp->rcode) {
case NXDOMAIN:
h_errno = HOST_NOT_FOUND;
break;
case SERVFAIL:
h_errno = TRY_AGAIN;
break;
case NOERROR:
h_errno = NO_DATA;
break;
case FORMERR:
case NOTIMP:
case REFUSED:
default:
h_errno = NO_RECOVERY;
break;
}
return (-1);
}
return(n);
}
/*
* Formulate a normal query, send, and retrieve answer in supplied buffer.
* Return the size of the response on success, -1 on error.
* If enabled, implement search rules until answer or unrecoverable failure
* is detected. Error number is left in h_errno.
* Only useful for queries in the same name hierarchy as the local host
* (not, for example, for host address-to-name lookups in domain in-addr.arpa).
*/
int res_search(char *name, int class, int type, u_char *answer, int anslen)
{
register char *cp, **domain;
int n, ret, got_nodata = 0;
char *__hostalias(register const char *name);
if ((_res.options & RES_INIT) == 0 && res_init() == -1)
return (-1);
errno = 0;
h_errno = HOST_NOT_FOUND; /* default, if we never query */
for (cp = name, n = 0; *cp; cp++)
if (*cp == '.')
n++;
if (n == 0 && (cp = __hostalias(name)))
return (res_query(cp, class, type, answer, anslen));
/*
* We do at least one level of search if
* - there is no dot and RES_DEFNAME is set, or
* - there is at least one dot, there is no trailing dot,
* and RES_DNSRCH is set.
*/
if ((n == 0 && _res.options & RES_DEFNAMES) ||
(n != 0 && *--cp != '.' && _res.options & RES_DNSRCH))
for (domain = _res.dnsrch; *domain; domain++) {
ret = res_querydomain(name, *domain, class, type,
answer, anslen);
if (ret > 0)
return (ret);
/*
* If no server present, give up.
* If name isn't found in this domain,
* keep trying higher domains in the search list
* (if that's enabled).
* On a NO_DATA error, keep trying, otherwise
* a wildcard entry of another type could keep us
* from finding this entry higher in the domain.
* If we get some other error (negative answer or
* server failure), then stop searching up,
* but try the input name below in case it's fully-qualified.
*/
if (errno == ECONNREFUSED) {
h_errno = TRY_AGAIN;
return (-1);
}
if (h_errno == NO_DATA)
got_nodata++;
if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
(_res.options & RES_DNSRCH) == 0)
break;
}
/*
* If the search/default failed, try the name as fully-qualified,
* but only if it contained at least one dot (even trailing).
* This is purely a heuristic; we assume that any reasonable query
* about a top-level domain (for servers, SOA, etc) will not use
* res_search.
*/
if (n && (ret = res_querydomain(name, (char *)NULL, class, type,
answer, anslen)) > 0)
return (ret);
if (got_nodata)
h_errno = NO_DATA;
return (-1);
}
/*
* Perform a call on res_query on the concatenation of name and domain,
* removing a trailing dot from name if domain is NULL.
*/
int res_querydomain(char *name, char *domain, int class, int type,
u_char *answer, int anslen)
{
static char nbuf[2*MAXDNAME+2];
char *longname = nbuf;
int n;
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("res_querydomain(%s, %s, %d, %d)\n",
name, domain, class, type);
#endif
if (domain == NULL) {
/*
* Check for trailing '.';
* copy without '.' if present.
*/
n = strlen(name) - 1;
if (name[n] == '.' && n < sizeof(nbuf) - 1) {
bcopy(name, nbuf, n);
nbuf[n] = '\0';
} else
longname = name;
} else
(void)sprintf(nbuf, "%.*s.%.*s",
MAXDNAME, name, MAXDNAME, domain);
return (res_query(longname, class, type, answer, anslen));
}
char *
__hostalias(register const char *name)
{
register char *C1, *C2;
FILE *fp;
char *file;
static char buf[BUFSIZ];
static char abuf[MAXDNAME];
file = getenv("HOSTALIASES");
if (file == NULL || (fp = fopen(file, "r")) == NULL)
return (NULL);
buf[sizeof(buf) - 1] = '\0';
while (fgets(buf, sizeof(buf), fp)) {
for (C1 = buf; *C1 && !isspace(*C1); ++C1);
if (!*C1)
break;
*C1 = '\0';
if (!strcasecmp(buf, name)) {
while (isspace(*++C1));
if (!*C1)
break;
for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
abuf[sizeof(abuf) - 1] = *C2 = '\0';
(void)strncpy(abuf, C1, sizeof(abuf) - 1);
fclose(fp);
return (abuf);
}
}
fclose(fp);
return (NULL);
}

421
lib/netdb/res_send.c Normal file
View File

@ -0,0 +1,421 @@
/*
* Copyright (c) 1985, 1989 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)res_send.c 6.27 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
/*
* Send query to name server and wait for reply.
*/
#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <stdio.h>
#include <errno.h>
#include <resolv.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include "protos.h"
static int s = -1; /* socket used for communications */
static struct __SOCKADDR no_addr;
int res_send(const char *buf, int buflen, char *answer, int anslen)
{
register int n;
int try, v_circuit, resplen, ns;
int gotsomewhere = 0, connected = 0;
int connreset = 0;
u_short id, len;
static struct iovec iov[2];
char *cp;
fd_set dsmask;
struct timeval timeout;
HEADER *hp = (HEADER *) buf;
HEADER *anhp = (HEADER *) answer;
int terrno = ETIMEDOUT;
static char junk[512];
#ifdef DEBUG
if (_res.options & RES_DEBUG) {
printf("res_send()\n");
__p_query(buf);
}
#endif /* DEBUG */
if (!(_res.options & RES_INIT))
if (res_init() == -1) {
return(-1);
}
v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;
id = hp->id;
/*
* Send request, RETRY times, or until successful
*/
for (try = 0; try < _res.retry; try++) {
for (ns = 0; ns < _res.nscount; ns++) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("Querying server (# %d) address = %s\n", ns+1,
inet_ntoa(_res.nsaddr_list[ns].sin_addr));
#endif /* DEBUG */
usevc:
if (v_circuit) {
int truncated = 0;
/*
* Use virtual circuit;
* at most one attempt per server.
*/
try = _res.retry;
if (s < 0) {
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
terrno = errno;
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("socket (vc) failed");
#endif /* DEBUG */
continue;
}
if (connect(s,
(struct __SOCKADDR *)&(_res.nsaddr_list[ns]),
sizeof(struct __SOCKADDR)) < 0) {
terrno = errno;
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("connect failed");
#endif /* DEBUG */
(void) close(s);
s = -1;
continue;
}
}
/*
* Send length & message
*/
len = htons((u_short)buflen);
iov[0].iov_base = (caddr_t)&len;
iov[0].iov_len = sizeof(len);
iov[1].iov_base = (char *)buf;
iov[1].iov_len = buflen;
if (writev(s, iov, 2) != sizeof(len) + buflen) {
terrno = errno;
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("write failed");
#endif /* DEBUG */
(void) close(s);
s = -1;
continue;
}
/*
* Receive length & response
*/
cp = answer;
len = sizeof(short);
while (len != 0 &&
(n = read(s, (char *)cp, (int)len)) > 0) {
cp += n;
len -= n;
}
if (n <= 0) {
terrno = errno;
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("read failed");
#endif /* DEBUG */
(void) close(s);
s = -1;
/*
* A long running process might get its TCP
* connection reset if the remote server was
* restarted. Requery the server instead of
* trying a new one. When there is only one
* server, this means that a query might work
* instead of failing. We only allow one reset
* per query to prevent looping.
*/
if (terrno == ECONNRESET && !connreset) {
connreset = 1;
ns--;
}
continue;
}
cp = answer;
if ((resplen = ntohs(*(u_short *)cp)) > anslen) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
fprintf(stderr, "response truncated\n");
#endif /* DEBUG */
len = anslen;
truncated = 1;
} else
len = resplen;
while (len != 0 &&
(n = read(s, (char *)cp, (int)len)) > 0) {
cp += n;
len -= n;
}
if (n <= 0) {
terrno = errno;
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("read failed");
#endif /* DEBUG */
(void) close(s);
s = -1;
continue;
}
if (truncated) {
/*
* Flush rest of answer
* so connection stays in synch.
*/
anhp->tc = 1;
len = resplen - anslen;
while (len != 0) {
n = (len > sizeof(junk) ?
sizeof(junk) : len);
if ((n = read(s, junk, n)) > 0)
len -= n;
else
break;
}
}
} else {
/*
* Use datagrams.
*/
if (s < 0) {
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) {
terrno = errno;
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("socket (dg) failed");
#endif /* DEBUG */
continue;
}
}
#if BSD >= 43
/*
* I'm tired of answering this question, so:
* On a 4.3BSD+ machine (client and server,
* actually), sending to a nameserver datagram
* port with no nameserver will cause an
* ICMP port unreachable message to be returned.
* If our datagram socket is "connected" to the
* server, we get an ECONNREFUSED error on the next
* socket operation, and select returns if the
* error message is received. We can thus detect
* the absence of a nameserver without timing out.
* If we have sent queries to at least two servers,
* however, we don't want to remain connected,
* as we wish to receive answers from the first
* server to respond.
*/
if (_res.nscount == 1 || (try == 0 && ns == 0)) {
/*
* Don't use connect if we might
* still receive a response
* from another server.
*/
if (connected == 0) {
if (connect(s, (struct __SOCKADDR *)&_res.nsaddr_list[ns],
sizeof(struct __SOCKADDR)) < 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("connect");
#endif /* DEBUG */
continue;
}
connected = 1;
}
if (send(s, buf, buflen, 0) != buflen) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("send");
#endif /* DEBUG */
continue;
}
} else {
/*
* Disconnect if we want to listen
* for responses from more than one server.
*/
if (connected) {
(void) connect(s, &no_addr,
sizeof(no_addr));
connected = 0;
}
#endif /* BSD */
if (sendto(s, buf, buflen, 0,
(struct __SOCKADDR *)&_res.nsaddr_list[ns],
sizeof(struct __SOCKADDR)) != buflen) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("sendto");
#endif /* DEBUG */
continue;
}
#if BSD >= 43
}
#endif
/*
* Wait for reply
*/
timeout.tv_sec = (_res.retrans << try);
if (try > 0)
timeout.tv_sec /= _res.nscount;
if (timeout.tv_sec <= 0)
timeout.tv_sec = 1;
timeout.tv_usec = 0;
wait:
FD_ZERO(&dsmask);
FD_SET(s, &dsmask);
n = select(s+1, &dsmask, (fd_set *)NULL,
(fd_set *)NULL, &timeout);
if (n < 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("select");
#endif /* DEBUG */
continue;
}
if (n == 0) {
/*
* timeout
*/
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("timeout\n");
#endif /* DEBUG */
#if BSD >= 43
gotsomewhere = 1;
#endif
continue;
}
if ((resplen = recv(s, answer, anslen, 0)) <= 0) {
#ifdef DEBUG
if (_res.options & RES_DEBUG)
perror("recvfrom");
#endif /* DEBUG */
continue;
}
gotsomewhere = 1;
if (id != anhp->id) {
/*
* response from old query, ignore it
*/
#ifdef DEBUG
if (_res.options & RES_DEBUG) {
printf("old answer:\n");
__p_query(answer);
}
#endif /* DEBUG */
goto wait;
}
if (!(_res.options & RES_IGNTC) && anhp->tc) {
/*
* get rest of answer;
* use TCP with same server.
*/
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf("truncated answer\n");
#endif /* DEBUG */
(void) close(s);
s = -1;
v_circuit = 1;
goto usevc;
}
}
#ifdef DEBUG
if (_res.options & RES_DEBUG) {
printf("got answer:\n");
__p_query(answer);
}
#endif /* DEBUG */
/*
* If using virtual circuits, we assume that the first server
* is preferred * over the rest (i.e. it is on the local
* machine) and only keep that one open.
* If we have temporarily opened a virtual circuit,
* or if we haven't been asked to keep a socket open,
* close the socket.
*/
if ((v_circuit &&
((_res.options & RES_USEVC) == 0 || ns != 0)) ||
(_res.options & RES_STAYOPEN) == 0) {
(void) close(s);
s = -1;
}
return (resplen);
}
}
if (s >= 0) {
(void) close(s);
s = -1;
}
if (v_circuit == 0)
if (gotsomewhere == 0)
errno = ECONNREFUSED; /* no nameservers found */
else
errno = ETIMEDOUT; /* no answer obtained */
else
errno = terrno;
return (-1);
}
/*
* This routine is for closing the socket if a virtual circuit is used and
* the program wants to close it. This provides support for endhostent()
* which expects to close the socket.
*
* This routine is not expected to be user visible.
*/
void _res_close(void)
{
if (s != -1) {
(void) close(s);
s = -1;
}
}

49
lib/netdb/send.c Normal file
View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 1988 The Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)send.c 5.3 (Berkeley) 2/24/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include "protos.h"
send(s, msg, len, flags)
int s, len, flags;
const void *msg;
{
return(sendto(s, msg, len, flags, (struct __SOCKADDR *)NULL, 0));
}

58
lib/netdb/sethostent.c Normal file
View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 1985 Regents of the University of California.
* 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.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)sethostent.c 6.9 (Berkeley) 3/19/91";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <netdb.h>
#include <resolv.h>
#include "protos.h"
void
sethostent(int stayopen)
{
if (stayopen)
_res.options |= RES_STAYOPEN | RES_USEVC;
}
void
endhostent(void)
{
_res.options &= ~(RES_STAYOPEN | RES_USEVC);
_res_close();
}

105
lib/netdb/tags Normal file
View File

@ -0,0 +1,105 @@
DELIM iso_addr.c /^#define DELIM (4\*2)$/
DIGIT iso_addr.c /^#define DIGIT (4\*0)$/
END iso_addr.c /^#define END (4\*1)$/
Field ns_addr.c /^Field(buf, out, len)$/
GOTONE iso_addr.c /^#define GOTONE 1$/
GOTTWO iso_addr.c /^#define GOTTWO 2$/
HOST_NOT_FOUND 2/orcacdefs/netdb.h /^#define HOST_NOT_FOUND 1 \/\* Authoritive Answer H/
LETTER linkaddr.c /^#define LETTER (4\*3)$/
MAXADDRS gethostnamadr.c /^#define MAXADDRS 35$/
MAXALIASES gethostnamadr.c /^#define MAXALIASES 35$/
MAXPACKET gethostnamadr.c /^#define MAXPACKET PACKETSZ$/
NAMING linkaddr.c /^#define NAMING 0$/
NO_ADDRESS 2/orcacdefs/netdb.h /^#define NO_ADDRESS NO_DATA \/\* no address, look /
NO_DATA 2/orcacdefs/netdb.h /^#define NO_DATA 4 \/\* Valid host name, no data r/
NO_RECOVERY 2/orcacdefs/netdb.h /^#define NO_RECOVERY 3 \/\* Non recoverable errors,/
PLURALIZE res_debug.c /^#define PLURALIZE(x) x, (x == 1) ? "" : "s"$/
RESET linkaddr.c /^#define RESET 3$/
TRY_AGAIN 2/orcacdefs/netdb.h /^#define TRY_AGAIN 2 \/\* Non-Authoritive Host not /
UC inet_ntoa.c /^#define UC(b) (((int)b)&0xff)$/
VIRGIN iso_addr.c /^#define VIRGIN 0$/
_PATH_HEQUIV 2/orcacdefs/netdb.h /^#define _PATH_HEQUIV "\/etc\/hosts\.equiv"$/
_PATH_HOSTS 2/orcacdefs/netdb.h /^#define _PATH_HOSTS "\/etc\/hosts"$/
_PATH_NETWORKS 2/orcacdefs/netdb.h /^#define _PATH_NETWORKS "\/etc\/networks"$/
_PATH_PROTOCOLS 2/orcacdefs/netdb.h /^#define _PATH_PROTOCOLS "\/etc\/protocols"$/
_PATH_SERVICES 2/orcacdefs/netdb.h /^#define _PATH_SERVICES "\/etc\/services"$/
__dn_skipname res_comp.c /^int __dn_skipname(const u_char \*comp_dn, const u_/
__fp_query res_debug.c /^__fp_query(char \*msg, FILE \*file)$/
__hostalias res_query.c /^__hostalias(register const char \*name)$/
__p_class res_debug.c /^__p_class(int class)$/
__p_query res_debug.c /^void __p_query(char \*msg)$/
__p_time res_debug.c /^__p_time(u_long value)$/
__p_type res_debug.c /^__p_type(int type)$/
__putlong res_comp.c /^__putlong(register u_long l, register u_char \*msg/
__putshort res_comp.c /^__putshort(s, msgp)$/
_checkhost rcmd.c /^_checkhost(char \*rhost, char \*lhost, int len)$/
_endhtent gethostnamadr.c /^void _endhtent(void)$/
_gethtbyaddr gethostnamadr.c /^_gethtbyaddr(const char \*addr, int len, int type)/
_gethtbyname gethostnamadr.c /^_gethtbyname(char \*name)$/
_gethtent gethostnamadr.c /^_gethtent(void)$/
_getlong res_comp.c /^_getlong(u_char \*msgp)$/
_getshort res_comp.c /^_getshort(u_char \*msgp)$/
_res_close res_send.c /^void _res_close(void)$/
_sethtent gethostnamadr.c /^void _sethtent(int f)$/
_validuser rcmd.c /^int _validuser(FILE \*hostf, char \*rhost, char \*/
align gethostnamadr.c /^} align;$/
cvtbase ns_addr.c /^cvtbase(oldbase,newbase,input,inlen,result,reslen)/
dn_comp res_comp.c /^int dn_comp(const u_char \*exp_dn, u_char \*comp_d/
dn_expand res_comp.c /^int dn_expand(const u_char \*msg, const u_char \*e/
dn_find res_comp.c /^dn_find(u_char \*exp_dn, u_char \*msg, u_char \*\*/
endhostent sethostent.c /^endhostent(void)$/
endnetent getnetent.c /^endnetent(void)$/
endprotoent getprotoent.c /^endprotoent(void)$/
endservent getservent.c /^endservent(void)$/
getanswer gethostnamadr.c /^getanswer(querybuf \*answer, int anslen, int iquer/
gethostbyaddr gethostnamadr.c /^gethostbyaddr(const char \*addr, int len, int type/
gethostbyname gethostnamadr.c /^gethostbyname(const char \*name)$/
getnetbyaddr getnetbyaddr.c /^getnetbyaddr(long net, int type)$/
getnetbyname getnetbyname.c /^getnetbyname(const char \*name)$/
getnetent getnetent.c /^getnetent(void)$/
getprotobyname getprotoname.c /^getprotobyname(const char \*name)$/
getprotobynumber getproto.c /^getprotobynumber(int proto)$/
getprotoent getprotoent.c /^getprotoent(void)$/
getservbyname getservbyname.c /^getservbyname(const char \*name, const char \*prot/
getservbyport getservbyport.c /^getservbyport(int port, const char \*proto)$/
getservent getservent.c /^getservent(void)$/
h_addr 2/orcacdefs/netdb.h /^#define h_addr h_addr_list\[0\] \/\* address, for /
herror herror.c /^herror(const char \*s)$/
hostent 2/orcacdefs/netdb.h /^struct hostent {$/
inet_addr inet_addr.c /^inet_addr(const char \*cp)$/
inet_aton inet_addr.c /^inet_aton(const char \*cp, struct in_addr \*addr)$/
inet_lnaof inet_lnaof.c /^inet_lnaof(struct in_addr in)$/
inet_makeaddr inet_makeaddr.c /^inet_makeaddr(u_long net, u_long host)$/
inet_netof inet_netof.c /^inet_netof(struct in_addr in)$/
inet_network inet_network.c /^inet_network(const char \*cp)$/
inet_ntoa inet_ntoa.c /^inet_ntoa(struct in_addr in)$/
iso_addr iso_addr.c /^iso_addr(addr)$/
iso_ntoa iso_addr.c /^iso_ntoa(isoa)$/
link_addr linkaddr.c /^link_addr(addr, sdl)$/
link_ntoa linkaddr.c /^link_ntoa(sdl)$/
netent 2/orcacdefs/netdb.h /^struct netent {$/
ns_addr ns_addr.c /^struct ns_addr $/
ns_ntoa ns_ntoa.c /^ns_ntoa(addr)$/
p_cdname res_debug.c /^p_cdname(char \*cp, char \*msg, FILE \*file)$/
p_rr res_debug.c /^p_rr(char \*cp, char \*msg, FILE \*file)$/
protoent 2/orcacdefs/netdb.h /^struct protoent {$/
querybuf gethostnamadr.c /^} querybuf;$/
rcmd rcmd.c /^int rcmd(char \*\*ahost, u_short rport, const char/
recv recv.c /^recv(s, buf, len, flags)$/
res_init res_init.c /^int res_init(void)$/
res_mkquery res_mkquery.c /^int res_mkquery(int op, const char \*dname, int cl/
res_query res_query.c /^int res_query(char \*name, int class, int type, u_/
res_querydomain res_query.c /^int res_querydomain(char \*name, char \*domain, in/
res_search res_query.c /^int res_search(char \*name, int class, int type, u/
res_send res_send.c /^int res_send(const char \*buf, int buflen, char \*/
rpcent 2/orcacdefs/netdb.h /^struct rpcent {$/
rresvport rcmd.c /^int rresvport(int \*alport)$/
ruserok rcmd.c /^int ruserok(const char \*rhost, int superuser, con/
send send.c /^send(s, msg, len, flags)$/
servent 2/orcacdefs/netdb.h /^struct servent {$/
sethostent sethostent.c /^sethostent(int stayopen)$/
setnetent getnetent.c /^setnetent(int f)$/
setprotoent getprotoent.c /^setprotoent(int f)$/
setservent getservent.c /^setservent(int f)$/
spectHex ns_ntoa.c /^spectHex(p0)$/
writev writev.c /^size_t writev(int fd, struct iovec \*iov, int iovc/

22
lib/netdb/writev.c Normal file
View File

@ -0,0 +1,22 @@
#include <sys/types.h>
#include <unistd.h>
#include <sys/uio.h>
#include "protos.h"
/* MAJOR HACK ALERT! This won't work right for packet based sockets! */
size_t writev(int fd, struct iovec *iov, int iovcnt)
{
int i;
size_t size, total_size = 0;
for (i=0; i<iovcnt; i++) {
size = write(fd, iov[i].iov_base, iov[i].iov_len);
total_size += size;
if (size != iov[i].iov_len) break;
}
return total_size;
}