498 lines · 9.5 KB · Text
RawDownload
.TH NDB 2.SH NAMEndbopen, ndbcat, ndbchanged, ndbclose, ndbreopen, ndbsearch, ndbsnext, ndbgetvalue, ndbfree, ipattr, ndbgetipaddr, ndbipinfo, csipinfo, ndbhash, ndbparse, csgetvalue, ndbfindattr, dnsquery, ndbdiscard, ndbconcatenate, ndbreorder, ndbsubstitute, ndbgetval, csgetval, ndblookval \- network database.SH SYNOPSIS.B #include <u.h>.br.B #include <libc.h>.br.B #include <bio.h>.br.B #include <ndb.h>.ta \w'\fLNdbtuplexx 'u.PP.BNdb*	ndbopen(char *file).PP.BNdb*	ndbcat(Ndb *db1, Ndb *db2).PP.Bint	ndbchanged(Ndb *db).PP.Bint	ndbreopen(Ndb *db).PP.Bvoid	ndbclose(Ndb *db).PP.BNdbtuple*	ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val).PP.BNdbtuple*	ndbsnext(Ndbs *s, char *attr, char *val).PP.Bchar*	ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val,.br.B		char *rattr, Ndbtuple **tp).PP.Bchar*	csgetvalue(char *netroot, char *attr, char *val,.B		char *rattr, Ndbtuple **tp).PP.Bchar*	ipattr(char *name).PP.BNdbtuple*	ndbgetipaddr(Ndb *db, char *sys);.PP.BNdbtuple*	ndbipinfo(Ndb *db, char *attr, char *val, char **attrs,.br.B		int nattr).PP.BNdbtuple*	csipinfo(char *netroot, char *attr, char *val,.br.B		char **attrs, int nattr).PP.Bulong	ndbhash(char *val, int hlen).PP.BNdbtuple*	ndbparse(Ndb *db).PP.BNdbtuple*	dnsquery(char *netroot, char *domainname, char *type).PP.BNdbtuple*	ndbfindattr(Ndbtuple *entry, Ndbtuple *line, char *attr).PP.Bvoid	ndbfree(Ndbtuple *db).PP.BNdbtuple*	ndbdiscard(Ndbtuple  *t, Ndbtuple *a).PP.BNdbtuple*	ndbconcatenate(Ndbtuple *a, Ndbtuple *b).PP.BNdbtuple*	ndbreorder(Ndbtuple *t, Ndbtuple *a).PP.BNdbtuple*	ndbsubstitute(Ndbtuple *t, Ndbtuple *from, Ndbtuple *to).PP.Bvoid	ndbsetmalloctag(Ndbtuple *t, uintptr tag).SH DESCRIPTIONThese routines are used by network administrative programs to searchthe network database.They operate on the database files described in.IR ndb (6)..PP.I Ndbopenopens the database.I fileand calls.IR malloc (2)to allocate a buffer for it.If.I fileis zero, all network database files are opened..PP.I Ndbcatconcatenates two open databases.  Either argument may be nil..PP.I Ndbreopenthrows out any cached informationfor the database files associated with.I dband reopens the files..PP.I Ndbclosecloses any database files associated with.I dband frees all storage associated with them..PP.I Ndbsearchand.I ndbsnextsearch a database for an entry containing theattribute/value pair,.IR attr = val ..I Ndbsearchis used to find the first match and.I ndbsnextis used to find each successive match.On a successful search both return a linked list of.I Ndbtuplestructures acquired by.IR malloc (2)that represent the attribute/value pairs in theentry.On failure they return zero..IP.EXtypedef struct Ndbtuple Ndbtuple;struct Ndbtuple {        char      attr[Ndbalen];        char      *val;        Ndbtuple  *entry;        Ndbtuple  *line;        ulong     ptr;    /* for the application; starts 0 */        char      valbuf[Ndbvlen];  /* initial allocation for val */};.EE.LPThe.I entrypointers chain together all pairs in the entry in a null-terminated list.The.I linepointers chain together all pairs on the same linein a circular list.Thus, a program can implement 2 levels of binding forpairs in an entry.In general, pairs on the same line are bound tighterthan pairs on different lines..PPThe argument.I sof.I ndbsearchhas type.I Ndbsand should be pointed to valid storage before calling.IR ndbsearch ,which will fill it with information used by.I ndbsnextto link successive searches.The structure.I Ndbslooks like:.IP.EXtypedef struct Ndbs Ndbs;struct Ndbs {        Ndb      *db;   /* data base file being searched */        ...        Ndbtuple *t;    /* last attribute value pair found */};.EE.LPThe.I tfield points to the pair within the entry matched by the.I ndbsearchor.IR ndbsnext ..PP.I Ndbgetvaluesearches the database for an entry containing not only anattribute/value pair,.IR attr = val ,but also a pair with the attribute.IR rattr .If successful, it returns a malloced copy of the NUL-terminated value associated with.IR rattr .If.I tpis non nil,.I *tpwill point to the entry.  Otherwise the entry will be freed..PP.I Csgetvalueis like.I ndbgetvaluebut queries the connection serverinstead of looking directly at the database.Its first argument specifies the network root to use.If the argument is 0, it defaults to\f5"/net"\f1..PP.I Ndbfreefrees a list of tuples returned by one of the otherroutines..PP.I Ipattrtakes the name of an IP system and returns the attributeit corresponds to:.RS.TP.B domdomain name.TP.B ipInternet number.TP.B syssystem name.RE.PP.I Ndbgetipaddrlooks in.I dbfor an entry matching.I sysas the value of a.B sys=or.B dom=attribute/value pair and returns all IP addresses in the entry.If.I sysis already an IP address, a tuple containing justthat address is returned..PP.I Ndbipinfolooks up Internet protocol information about a system.This is an IP aware search.  It looks first for informationin the system's database entry and then in the database entriesfor any IP subnets or networks containing the system.The system is identified by theattribute/value pair,.IR attr = val ..I Ndbipinforeturns a list of tuples whose attributes match theattributes in the.I nelement array.IR attrs .If any.I attrsbegin with.LR @ ,the.L @is excluded from the attribute name,but causes any corresponding value returnedto be a resolved IP address(es), not a name.For example, consider the following database entries describing a network,a subnetwork, and a system..IP.EXipnet=big ip=10.0.0.0	dns=dns.big.com	smtp=smtp.big.comipnet=dept ip=10.1.1.0 ipmask=255.255.255.0	smtp=smtp1.big.comip=10.1.1.4 dom=x.big.com	bootf=/386/9pc.EE.PPCalling.IP.EXndbipinfo(db, "dom", "x.big.com", ["bootf" "smtp" "dns"], 3).EE.PPwill return the tuples.BR bootf=/386/9pc ,.BR smtp=smtp1.big.com ,and.BR dns=dns.big.com ..PP.I Csipinfois to.I ndbipinfoas.I csgetvalis to.IR ndbgetval ..PPThe next three routines are used by programs that create thehash tables and database files..I Ndbhashcomputes a hash offset into a table of length.I hlenfor the string.IR val ..I Ndbparsereads and parses the next entry from the database file.Multiple calls to.IR ndbparseparse sequential entries in the database file.A zero is returned at end of file..PP.I Dnsquerysubmits a query about.I domainnameto the.I ndb/dnsmounted at.IB netroot /dns.It returns a linked list of.I Ndbtuple'srepresenting a single database entry.The tuples are logically arranged into lines using the.B linefield in the structure.The possible.IR type 'sof query are and the attributes on each returned tuple line is:.TP.B ipfind the IP addresses.  Returnsdomain name.RI ( dom )and ip address.RI ( ip ).TP.B mxlook up the mail exchangers.  Returns preference.RI ( pref )and exchanger.RI ( mx ).TP.B ptrdo a reverse query.  Here.I domainnamemust be an.SM ASCIIIP address.  Returns reverse name.RI ( ptr )and domain name.RI ( dom ).TP.B cnameget the system that this name is a nickname for.  Returns the nickname.RI ( dom )and the real name.RI ( cname ).TP.B soareturn the start of area record for this field.  Returnsarea name.RI ( dom ),primary name server.RI ( ns ),serial number.RI ( serial ),refresh time in seconds.RI ( refresh ),retry time in seconds.RI ( retry ),expiration time in seconds.RI ( expire ),and minimum time to lie.RI ( ttl )..TP.B nsname servers.  Returns domain name.RI ( dom )and name server.RI ( ns ).PP.I Ndbfindattrsearches.I entryfor the tuplewith attribute.I attrand returns a pointer to the tuple.If.I linepoints to a particular line in the entry, thesearch starts there and then wraps around to the beginningof the entry..PPAll of the routines provided to search the databaseprovide an always consistent view of the relevantfiles.  However, it may be advantageous for an applicationto read in the whole database using.I ndbopenand.I ndbparseand provide its own search routines.  The.I ndbchangedroutine can be used by the application to periodicallycheck for changes.  It returns zeroif none of the files comprising the database havechanges and non-zero if they have..PPFinally, a number of routines are provided for manipulating tuples..PP.I Ndbdiscardremoves attr/val pair.I afrom tuple.I tand frees it.If.I aisn't in.I tit is just freed..PP.I Ndbconcatenateconcatenates two tuples and returns the result.  Eitheror both tuples may be nil..PP.I Ndbreorderreorders a tuple.IR tto make the line containing attr/val pair.I afirst in the entry and making.I afirst in its line..PP.I Ndbsubstitutereplaces a single att/val pair.I fromin.I twith the tuple.IR to .All attr/val pairs in.I toend up on the same line..I fromis freed..PP.I Ndbsetmalloctagsets the malloc tag(see.I setmalloctagin.IR malloc (2))of each tuple in the list.I tto.IR tag ..SH FILES.BR /lib/ndb "    directory of network database files.SH SOURCE.B /sys/src/libndb.SH SEE ALSO.IR ndb (6),.IR ndb (8).SH DIAGNOSTICS.IR Ndbgetvalue ,.IR csgetvalue ,and.I ndblookvalueset.I errstrto.L "buffer too short"if the buffer provided isn't long enough for thereturned value..SH BUGS.IR Ndbgetval ,.IR csgetval ,and.I ndblookvalare deprecated versions of.IR ndbgetvalue ,.IR csgetvalue ,and.IR ndblookvalue .They expect a fixed 64 byte long resultbuffer and existed when the values of a.I Ndbtuplestructure were fixed length.