Return to BSD News archive
Xref: sserve comp.protocols.tcp-ip.domains:3014 comp.unix.bsd:12072 Newsgroups: comp.protocols.tcp-ip.domains,comp.unix.bsd Path: sserve!newshost.anu.edu.au!munnari.oz.au!news.Hawaii.Edu!ames!elroy.jpl.nasa.gov!swrinde!cs.utexas.edu!utnut!torn!nott!bnrgate!bnr.co.uk!uknet!mcsun!news.inesc.pt!dec4pt.puug.pt!matuc2.mat.uc.pt!rps From: rps@matuc2.mat.uc.pt (Rui Pedro Mendes Salgueiro) Subject: An implementation of getnetby* using DNS (RFC1101) Message-ID: <1993Jun3.202843.13496@matuc2.mat.uc.pt> Organization: Dep. Matematica Universidade de Coimbra X-Newsreader: Tin 1.1 PL3 Date: Thu, 3 Jun 1993 20:28:43 GMT Lines: 946 This is a implementation of getnetby{addr,name}() and {set,end}hostent() that uses the DNS (RFC1101). If you want to make your network visible by the DNS (using these functions) you should add this lines to your DNS files: in the reverse mapping: 0.x.y.z.IN-ADDR.ARPA. IN PTR your-net.your.domain. and in the direct: your-net.your.domain. IN PTR 0.x.y.z.IN-ADDR.ARPA. This example is for a class C network (the minimum info necessary). See RFC1101 for more details. This has been tested in BSD/386 (0.9.4), in SunOS (4.0.3 and 4.1.3) and Ultrix 4.2. NOTE: These versions never use yellow pages (NIS). Authors: Carlos Leandro and Rui Salgueiro Dep. Matematica Universidade de Coimbra, Portugal, Europe 3 June 1993 See the README file for more details. # This is a shell archive. Save it in a file, remove anything before # this line, and then unpack it by entering "sh file". Note, it may # create directories; files and directories will be owned by you and # have default permissions. # # This archive contains: # # README # Makefile # main.c # getnetent.c # getnetbyname.c # getnetnamadr.c # getnetbyaddr.c # echo x - README sed 's/^X//' >README << 'END-of-README' XThis is a implementation of getnetby{addr,name}() and {set,end}hostent() Xthat uses the DNS (RFC1101). X XWe have moved the old versions of getnetby{addr,name}() and X{set,end}hostent() to _getnetby{addr,name}() and _{set,end}hostent(). X XOur implementation querys the DNS and falls back to the old functions in Xthe case of a negative reply. This is done since, at this moment, few people Xhave entered this information in the DNS. X XBecause of this we have modified the {set,end}netent() functions so now Xthey turn on or off the resolver options "RES_STAYOPEN | RES_USEVC" X(by calling {set,end}hostent() ) AND open the /etc/networks . X XWe have made a test program (called nettest) that looks for information Xon a network passed in the command line (either by name or by number). X X gnu% ./nettest 192.138.204 X Official name: net.mat.uc.pt X Net addr. type: 2 X Network : 12618444 ----> 192.138.204 X Aliases : X gnu% ./nettest 192.138.204.0 X Official name: net.mat.uc.pt X Net addr. type: 2 X Network : 3230321664 ----> 192.138.204.0 X Aliases : X gnu% ./nettest net.mat.uc.pt. X Official name: net.mat.uc.pt X Net addr. type: 2 X Network : 12618444 ----> 192.138.204 X Aliases : X gnu% ./nettest mat.uc.pt X Official name: mat.uc.pt X Net addr. type: 2 X Network : 12618444 ----> 192.138.204 X Aliases : X XThe program nettest.local uses the functions in libresolv, that usually Xread /etc/networks. X XThe Makefile included can also recompile netstat (if you have the Xsource to netstat) using these functions. X XAt this moment this netstat (option -r) is very slow. XIt will be better when named caches negative replys and/or Xmore people enter this information in the DNS. X XThis has been tested in BSD/386 (0.9.4), in SunOS (4.0.3 and 4.1.3) Xand Ultrix 4.2. XNOTE: These versions never use yellow pages (NIS). X XAuthors: X Carlos Leandro leandro@mat.uc.pt X Rui Salgueiro rps@mat.uc.pt X XSend bugs reports, bug fixes, suggestions, etc... to rfc1101@mat.uc.pt. END-of-README echo x - Makefile sed 's/^X//' >Makefile << 'END-of-Makefile' X# @(#)Makefile 1.6 (Coimbra) 93/06/03 X XPROG= nettest XSRCS= main.c getnetent.c getnetbyname.c getnetnamadr.c getnetbyaddr.c XOBJS= getnetent.o getnetbyname.o getnetnamadr.o getnetbyaddr.o XCFLAGS=-O -DLIBC_SCCS XLDFLAGS= XLIBS=-lresolv # SunOS needs this X X Xnettest: main.o $(OBJS) X cc $(LDFLAGS) main.o $(OBJS) $(LIBS) -o $@ X Xall: nettest netstat rfc1101.shar X Xnettest.local: main.o X cc $(LDFLAGS) main.o $(LIBS) -o $@ X Xrfc1101.shar: README Makefile $(SRCS) X shar README Makefile $(SRCS) > $@ X Xclean_all: clean X rm -f nettest nettest.local netstat rfc1101.shar X Xclean: X rm -f main.o netstat.o $(OBJS) X XNETSTAT_SOURCES= /usr/src/usr.bin/netstat/host.c \ X /usr/src/usr.bin/netstat/inet.c /usr/src/usr.bin/netstat/if.c \ X /usr/src/usr.bin/netstat/main.c /usr/src/usr.bin/netstat/mbuf.c \ X /usr/src/usr.bin/netstat/route.c /usr/src/usr.bin/netstat/unix.c \ X /usr/src/usr.bin/netstat/ns.c /usr/src/usr.bin/netstat/iso.c \ X /usr/src/sys/netiso/tp_astring.c X Xnetstat: netstat.o $(OBJS) X cc $(LDFLAGS) netstat.o $(OBJS) $(LIBS) -lkvm -o $@ X Xnetstat.o: $(NETSTAT_SOURCES) X -mkdir /tmp/netstat X cp $(NETSTAT_SOURCES) /usr/src/usr.bin/netstat/netstat.h /tmp/netstat X cd /tmp/netstat; cc -c $(CFLAGS) *.c; ld -r -o $@ *.o X mv /tmp/netstat/$@ . ; rm -r /tmp/netstat END-of-Makefile echo x - main.c sed 's/^X//' >main.c << 'END-of-main.c' X/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro X * Dep. Matematica Universidade de Coimbra, Portugal, Europe X */ X/* X * Copyright (c) 1983 Regents of the University of California. X * All rights reserved. X * X * Redistribution and use in source and binary forms, with or without X * modification, are permitted provided that the following conditions X * are met: X * 1. Redistributions of source code must retain the above copyright X * notice, this list of conditions and the following disclaimer. X * 2. Redistributions in binary form must reproduce the above copyright X * notice, this list of conditions and the following disclaimer in the X * documentation and/or other materials provided with the distribution. X * 3. All advertising materials mentioning features or use of this software X * must display the following acknowledgement: X * This product includes software developed by the University of X * California, Berkeley and its contributors. X * 4. Neither the name of the University nor the names of its contributors X * may be used to endorse or promote products derived from this software X * without specific prior written permission. X * X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE X * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF X * SUCH DAMAGE. X */ X X#if defined(LIBC_SCCS) && !defined(lint) Xstatic char sccsid[] = "@(#)main.c 1.4 (Coimbra) 93/06/03"; X#endif /* LIBC_SCCS and not lint */ X X#include <sys/types.h> X#include <netdb.h> X#include <stdio.h> X#include <string.h> X#include <sys/socket.h> X#include <netinet/in.h> X#include <arpa/inet.h> X#include <ctype.h> X X#include <arpa/nameser.h> X#include <resolv.h> X X#define ADDR 1 X#define NAME 2 X#define UNDEF -1 X Xint h_errno; Xvoid herror(); X Xchar *progname; Xchar *use = "%s [-d] [-a addr | -n name | string] \n"; Xint name_or_addr = UNDEF; X Xmain(argc, argv) Xint argc; Xchar *argv[]; X{ X char **ch, c, *net_name_or_addr, *s; X struct netent *net_entry; X register unsigned long net2, nn; X unsigned netbr[4]; X X progname = argv[0]; X net_name_or_addr = NULL; X X while (--argc > 0 && (*++argv)[0] == '-') { X for (s = argv[0] + 1; *s != '\0'; s++) { X switch (*s) { X case 'd': X _res.options |= RES_DEBUG; X break; X case 'a': X if (--argc > 0) { X name_or_addr = ADDR; X net_name_or_addr = *++argv; X } else X terminate(); X break; X case 'n': X if (--argc > 0) { X name_or_addr = NAME; X net_name_or_addr = *++argv; X } else X terminate(); X break; X default: X terminate(); X break; X } X } X } X X if (name_or_addr == UNDEF) { X if (argc > 0) { X c = **argv; X if (isalpha(c)) { X name_or_addr = NAME; X } else if (isdigit(c)) { X name_or_addr = ADDR; X } else { X terminate(); X } X net_name_or_addr = *argv++; X --argc; X } else X terminate(); X } X X if (argc > 0) { X fprintf(stderr, "extra arguments ignored:"); X while (argc-- > 0 ) X fprintf(stderr, " %s", *argv++); X fprintf(stderr, "\n"); X } X X/* fprintf(stderr, "name_or_addr = %d, net_name_or_addr = %s\n", X name_or_addr , net_name_or_addr); X*/ X switch (name_or_addr) { X case ADDR: X net_entry = getnetbyaddr(inet_network(net_name_or_addr ), X AF_INET); X break; X case NAME: X net_entry = getnetbyname(net_name_or_addr ); X break; X default: /* can't happen */ X terminate(); X } X X if (net_entry) { X printf(" Official name: %s \n", net_entry->n_name); X printf(" Net addr. type: %d \n", net_entry->n_addrtype); X printf(" Network : %u ----> ", net2 = (unsigned long)net_entry->n_net); X for (nn = 4; net2; netbr[--nn] = net2 & 0xff, net2 >>= 8) X ; X printf(" "); X switch (nn) { X case 0: X printf("%u.%u.%u.%u", netbr[0], netbr[1], netbr[2], netbr[3]); X break; X case 1: X printf("%u.%u.%u", netbr[1], netbr[2], netbr[3]); X break; X case 2: X printf("%u.%u", netbr[2], netbr[3]); X break; X case 3: X printf("%u", netbr[3]); X break; X } X printf(" \n"); X printf(" Aliases : \n"); X for (ch = net_entry->n_aliases; *ch != NULL; ch++) X printf(" %s \n", *ch); X return 0; X } else { X herror(progname); X return 2; X } X} X X Xterminate() X{ X fprintf(stderr, use, progname); X exit (1); X} X X X#ifdef sun Xchar *h_errlist[] = { X "Error 0", X "Unknown host", /* 1 HOST_NOT_FOUND */ X "Host name lookup failure", /* 2 TRY_AGAIN */ X "Unknown server error", /* 3 NO_RECOVERY */ X "No address associated with name", /* 4 NO_ADDRESS */ X}; X Xint h_nerr = { X sizeof(h_errlist) / sizeof(h_errlist[0]) }; X Xextern int h_errno; X X/* X * herror -- X * print the error indicated by the h_errno value. X */ Xvoid Xherror(s) Xchar *s; X{ X fprintf(stderr, "%s: %s\n", s, X (u_int) h_errno < h_nerr ? h_errlist[h_errno] : "Unknown error"); X} X X#endif END-of-main.c echo x - getnetent.c sed 's/^X//' >getnetent.c << 'END-of-getnetent.c' X/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro X * Dep. Matematica Universidade de Coimbra, Portugal, Europe X */ X/* X * Copyright (c) 1983 Regents of the University of California. X * All rights reserved. X * X * Redistribution and use in source and binary forms, with or without X * modification, are permitted provided that the following conditions X * are met: X * 1. Redistributions of source code must retain the above copyright X * notice, this list of conditions and the following disclaimer. X * 2. Redistributions in binary form must reproduce the above copyright X * notice, this list of conditions and the following disclaimer in the X * documentation and/or other materials provided with the distribution. X * 3. All advertising materials mentioning features or use of this software X * must display the following acknowledgement: X * This product includes software developed by the University of X * California, Berkeley and its contributors. X * 4. Neither the name of the University nor the names of its contributors X * may be used to endorse or promote products derived from this software X * without specific prior written permission. X * X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE X * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF X * SUCH DAMAGE. X */ X X#if defined(LIBC_SCCS) && !defined(lint) Xstatic char sccsid[] = "@(#)getnetent.c 1.1 (Coimbra) 93/06/02"; X#endif /* LIBC_SCCS and not lint */ X X#include <sys/types.h> X#include <sys/socket.h> X#include <sys/param.h> X#include <netinet/in.h> X#include <arpa/inet.h> X#include <arpa/nameser.h> X#include <resolv.h> X#include <netdb.h> X#include <stdio.h> X#include <string.h> X X#ifndef _PATH_NETWORKS X#define _PATH_NETWORKS "/etc/networks" X#endif X X#define MAXALIASES 35 X Xstatic FILE *netf; Xstatic char line[BUFSIZ+1]; Xstatic struct netent net; Xstatic char *net_aliases[MAXALIASES]; Xint _net_stayopen; X Xvoid _setnetent(), _endnetent(); X Xvoid Xsetnetent(stayopen) Xint stayopen; X{ X sethostent(stayopen); X _setnetent(stayopen); X} X Xvoid Xendnetent() X{ X endhostent(); X _endnetent(); X} X Xvoid X_setnetent(f) Xint f; X{ X if (netf == NULL) X netf = fopen(_PATH_NETWORKS, "r" ); X else X rewind(netf); X _net_stayopen |= f; X} X Xvoid X_endnetent() X{ X if (netf) { X fclose(netf); X netf = NULL; X } X _net_stayopen = 0; X} X Xstruct netent * Xgetnetent() X{ X char *p; X register char *cp, **q; X X if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) X return (NULL); Xagain: X p = fgets(line, BUFSIZ, netf); X if (p == NULL) X return (NULL); X if (*p == '#') X goto again; X cp = strpbrk(p, "#\n"); X if (cp == NULL) X goto again; X *cp = '\0'; X net.n_name = p; X cp = strpbrk(p, " \t"); X if (cp == NULL) X goto again; X *cp++ = '\0'; X while (*cp == ' ' || *cp == '\t') X cp++; X p = strpbrk(cp, " \t"); X if (p != NULL) X *p++ = '\0'; X net.n_net = inet_network(cp); X net.n_addrtype = AF_INET; X q = net.n_aliases = net_aliases; X if (p != NULL) X cp = p; X while (cp && *cp) { X if (*cp == ' ' || *cp == '\t') { X cp++; X continue; X } X if (q < &net_aliases[MAXALIASES - 1]) X *q++ = cp; X cp = strpbrk(cp, " \t"); X if (cp != NULL) X *cp++ = '\0'; X } X *q = NULL; X return (&net); X} END-of-getnetent.c echo x - getnetbyname.c sed 's/^X//' >getnetbyname.c << 'END-of-getnetbyname.c' X/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro X * Dep. Matematica Universidade de Coimbra, Portugal, Europe X */ X/* X * Copyright (c) 1983 Regents of the University of California. X * All rights reserved. X * X * Redistribution and use in source and binary forms, with or without X * modification, are permitted provided that the following conditions X * are met: X * 1. Redistributions of source code must retain the above copyright X * notice, this list of conditions and the following disclaimer. X * 2. Redistributions in binary form must reproduce the above copyright X * notice, this list of conditions and the following disclaimer in the X * documentation and/or other materials provided with the distribution. X * 3. All advertising materials mentioning features or use of this software X * must display the following acknowledgement: X * This product includes software developed by the University of X * California, Berkeley and its contributors. X * 4. Neither the name of the University nor the names of its contributors X * may be used to endorse or promote products derived from this software X * without specific prior written permission. X * X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE X * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF X * SUCH DAMAGE. X */ X X#if defined(LIBC_SCCS) && !defined(lint) Xstatic char sccsid[] = "@(#)getnetbyname.c 1.1 (Coimbra) 93/06/02"; X#endif /* LIBC_SCCS and not lint */ X X#include <netdb.h> X#include <string.h> X Xextern int _net_stayopen; X Xstruct netent * X_getnetbyname(name) X#ifdef sun X register char *name; X#else X register const char *name; X#endif X{ X register struct netent *p; X register char **cp; X X setnetent(_net_stayopen); X while (p = getnetent()) { X if (strcmp(p->n_name, name) == 0) X break; X for (cp = p->n_aliases; *cp != 0; cp++){ X if (strcmp(*cp, name) == 0) X goto found; X } X } Xfound: X if (!_net_stayopen) X endnetent(); X return (p); X} END-of-getnetbyname.c echo x - getnetnamadr.c sed 's/^X//' >getnetnamadr.c << 'END-of-getnetnamadr.c' X/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro X * Dep. Matematica Universidade de Coimbra, Portugal, Europe X */ X/* X * Copyright (c) 1983 Regents of the University of California. X * All rights reserved. X * X * Redistribution and use in source and binary forms, with or without X * modification, are permitted provided that the following conditions X * are met: X * 1. Redistributions of source code must retain the above copyright X * notice, this list of conditions and the following disclaimer. X * 2. Redistributions in binary form must reproduce the above copyright X * notice, this list of conditions and the following disclaimer in the X * documentation and/or other materials provided with the distribution. X * 3. All advertising materials mentioning features or use of this software X * must display the following acknowledgement: X * This product includes software developed by the University of X * California, Berkeley and its contributors. X * 4. Neither the name of the University nor the names of its contributors X * may be used to endorse or promote products derived from this software X * without specific prior written permission. X * X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE X * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF X * SUCH DAMAGE. X */ X X#if defined(LIBC_SCCS) && !defined(lint) Xstatic char sccsid[] = "@(#)getnetnamadr.c 1.4 (Coimbra) 93/06/03"; X#endif /* LIBC_SCCS and not lint */ X X#include <sys/param.h> X#include <sys/socket.h> X#include <netinet/in.h> X#include <arpa/inet.h> X#include <arpa/nameser.h> X#include <netdb.h> X#include <resolv.h> X#include <stdio.h> X#include <ctype.h> X#include <errno.h> X#include <string.h> X X#define BYADDR 0 X#define BYNAME 1 X#define MAXALIASES 35 X#define GetShort(cp) _getshort(cp); cp += sizeof(unsigned short); X Xstatic struct netent net_entry; Xstatic char *net_aliases[MAXALIASES]; Xstatic char netbuf[BUFSIZ+1]; Xstatic char *net_addrs[2]; X X#if PACKETSZ > 1024 X#define MAXPACKET PACKETSZ X#else X#define MAXPACKET 1024 X#endif X Xtypedef union { X HEADER hdr; X u_char buf[MAXPACKET]; X} querybuf; X Xtypedef union { X long al; X char ac; X} align; X Xint h_errno; X Xstatic struct netent * Xgetnetanswer(answer, anslen, net_i) X querybuf *answer; X int anslen; X int net_i; X{ X X register HEADER *hp; X register u_char *cp; X register int n; X u_char *eom; X char *bp, **ap; X int type, class, buflen, ancount, qdcount; X int haveanswer, getclass = C_ANY; X int net_length = 0; X int i, nchar; X X char aux1[30],aux2[30],ans[30]; X char *in,*st,*pauxt,*paux1= &aux1[0],*paux2= &aux2[0],flag=0; X /* X * find first satisfactory answer X X * answer --> +------------+ ( MESSAGE ) X | Header | X +------------+ X | Question | the question for the name server X +------------+ X | Answer | RRs answering the question X +------------+ X | Authority | RRs pointing toward an authority X | Additional | RRs holding additional information X +------------+ X */ X eom=answer->buf+anslen; X hp = &answer->hdr; X ancount= ntohs(hp->ancount); /*num. of records in the answer section*/ X qdcount= ntohs(hp->qdcount); /*num. of entries in the question section*/ X bp = netbuf; X buflen = sizeof(netbuf); X cp = answer->buf + sizeof(HEADER); X if (!qdcount) { X if (hp->aa) X h_errno = HOST_NOT_FOUND; X else X h_errno = TRY_AGAIN; X X return ((struct netent *) NULL); X } X while (qdcount-- > 0){ X#ifndef bsdi X cp += dn_skipname(cp, eom) + QFIXEDSZ; X#else X cp += __dn_skipname(cp, eom) + QFIXEDSZ; X#endif X } X ap = net_aliases; X *ap= NULL; X net_entry.n_aliases = net_aliases; X haveanswer = 0; X while (--ancount >= 0 && cp < eom) { X if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom, X (u_char *)cp, (u_char *)bp, buflen)) < 0) X break; X cp += n; X ans[0]='\0'; X (void)strcpy(&ans[0],bp); X type = GetShort(cp); X class = GetShort(cp); X cp += sizeof(u_long); X n = GetShort(cp); X if (class==C_IN && type == T_PTR) { X if ((n = dn_expand((u_char *)answer->buf, X (u_char *)eom, (u_char *)cp, (u_char *)bp, X buflen)) < 0) { X cp += n; X return ((struct netent *) NULL); X } X cp += n; X *ap++ = bp; X bp += strlen(bp)+1; X net_entry.n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; X haveanswer++; X } X } X if (haveanswer) { X *ap = NULL; X switch(net_i){ X case BYADDR : X net_entry.n_name = *net_entry.n_aliases; X net_entry.n_net=NULL; X break; X case BYNAME : X in= *net_entry.n_aliases; X net_entry.n_name= &ans[0]; X aux2[0]='\0'; X for (i=0;i<4;i++){ X for(st=in,nchar=0;*st!='.';st++,nchar++); X if (nchar!=1 || *in!='0' || flag){ X flag=1; X (void)strncpy(paux1,(i==0) ? in:in-1, X (i==0) ? nchar:nchar+1); X pauxt=paux2; X paux2=strcat(paux1,paux2); X paux1=pauxt; X } X in= ++st; X } X net_entry.n_net=inet_network(paux2); X } X net_entry.n_aliases++; X return (&net_entry); X } else { X h_errno = TRY_AGAIN; X return ((struct netent *) NULL); X } X} X Xstruct netent * Xgetnetbyaddr(net, net_type) X register long net; X register int net_type; X{ X extern struct netent * _getnetbyaddr(); X unsigned netbr[4]; X int nn,anslen; X querybuf buf; X char qbuf[MAXDNAME]; X register unsigned long net2 = net; X X X static struct netent *net_entry; X X if (net_type != AF_INET) X return _getnetbyaddr(net, net_type); X X/* net2 has to be unsigned or the loop may never stop */ X for (nn = 4; net2;){ X netbr[--nn] = net2 & 0xff; X net2 >>= 8; X } X switch (nn) { X case 3: /* Class A */ X (void)sprintf(qbuf, "0.0.0.%u.in-addr.arpa", X netbr[3]); X break; X case 2: /* Class B */ X (void)sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", X netbr[3], netbr[2]); X break; X case 1: /* Class C */ X (void)sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", X netbr[3], netbr[2], netbr[1]); X break; X case 0: /* Class D - E */ X (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", X netbr[3], netbr[2], netbr[1], netbr[0]); X break; X } X anslen = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf)); X if (anslen < 0) { X#ifdef DEBUG X if (_res.options & RES_DEBUG) X printf("res_query failed\n"); X#endif X if (errno == ECONNREFUSED) X return _getnetbyaddr(net, net_type); X return _getnetbyaddr(net, net_type); X } X net_entry= getnetanswer(&buf,anslen,BYADDR); X if (net_entry){ X unsigned u_net = net; /* maybe net should be unsigned ? */ X /* Strip trailing zeros */ X while ((u_net & 0xff) == 0 && u_net != 0) { X u_net >>= 8; X } X net_entry->n_net=u_net; X return net_entry; X } else X return _getnetbyaddr(net, net_type); X} X Xstruct netent * Xgetnetbyname(net) X#ifdef sun X register char *net; X#else X register const char *net; X#endif X{ X extern struct netent * _getnetbyname(); X unsigned netbr[4]; X int anslen; X querybuf buf; X char qbuf[MAXDNAME]; X X X struct netent *net_entry; X X (void)strcpy(&qbuf[0],net); X anslen = res_search(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf)); X if (anslen < 0) { X#ifdef DEBUG X if (_res.options & RES_DEBUG) X printf("res_query failed\n"); X#endif X if (errno == ECONNREFUSED) X return _getnetbyname(net); X return _getnetbyname(net); X } X net_entry= getnetanswer(&buf,anslen,BYNAME); X if (net_entry) X return net_entry; X else X return _getnetbyname(net); X X} END-of-getnetnamadr.c echo x - getnetbyaddr.c sed 's/^X//' >getnetbyaddr.c << 'END-of-getnetbyaddr.c' X/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro X * Dep. Matematica Universidade de Coimbra, Portugal, Europe X */ X/* X * Copyright (c) 1983 Regents of the University of California. X * All rights reserved. X * X * Redistribution and use in source and binary forms, with or without X * modification, are permitted provided that the following conditions X * are met: X * 1. Redistributions of source code must retain the above copyright X * notice, this list of conditions and the following disclaimer. X * 2. Redistributions in binary form must reproduce the above copyright X * notice, this list of conditions and the following disclaimer in the X * documentation and/or other materials provided with the distribution. X * 3. All advertising materials mentioning features or use of this software X * must display the following acknowledgement: X * This product includes software developed by the University of X * California, Berkeley and its contributors. X * 4. Neither the name of the University nor the names of its contributors X * may be used to endorse or promote products derived from this software X * without specific prior written permission. X * X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE X * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF X * SUCH DAMAGE. X */ X X#if defined(LIBC_SCCS) && !defined(lint) Xstatic char sccsid[] = "@(#)getnetbyaddr.c 1.1 (Coimbra) 93/06/02"; X#endif /* LIBC_SCCS and not lint */ X X#include <netdb.h> X Xextern int _net_stayopen; X Xstruct netent * X_getnetbyaddr(net, type) X register long net; X register int type; X{ X register struct netent *p; X X setnetent(_net_stayopen); X while (p = getnetent()) X if (p->n_addrtype == type && p->n_net == net) X break; X if (!_net_stayopen) X endnetent(); X return (p); X} END-of-getnetbyaddr.c exit -- Rui Salgueiro | Dpt. de Matematica |"Why do I smile at people rps@matuc2.mat.uc.pt | Universidade de Coimbra | I much rather kick in the eye" rps@inescc.pt | Portugal - Europe | Morrissey