1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
.TH NDB 2
.SH NAME
ndbopen, ndbcat, ndbclose, ndbreopen, ndbsearch, ndbsnext, ndbgetval, ndbfree, ipattr, ndbipinfo, csipinfo, ndbhash, ndbparse, csgetval, ndblookval, dnsquery \- 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
.B
Ndb* ndbopen(char *file)
.PP
.B
Ndb* ndbcat(Ndb *db1, Ndb *db2)
.PP
.B
int ndbreopen(Ndb *db)
.PP
.B
void ndbclose(Ndb *db)
.PP
.B
Ndbtuple* ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val)
.PP
.B
Ndbtuple* ndbsnext(Ndbs *s, char *attr, char *val)
.PP
.B
Ndbtuple* ndbgetval(Ndb *db, Ndbs *s, char *attr, char *val,
.br
.B
char *rattr, char *buf)
.PP
.B
Ndbtuple* csgetval(char *netroot, char *attr, char *val, char *rattr, char *buf)
.PP
.B
void ndbfree(Ndbtuple *db)
.PP
.B
char* ipattr(char *name)
.PP
.B
Ndbtuple* ndbipinfo(Ndb *db, char *attr, char *val, char **attrs,
.br
.B int nattr)
.PP
.B
Ndbtuple* csipinfo(char *netroot, char *attr, char *val, char **attrs,
.br
.B int nattr)
.PP
.B
ulong ndbhash(char *val, int hlen)
.PP
.B
Ndbtuple* ndbparse(Ndb *db)
.PP
.B
Ndbtuple* dnsquery(char *netroot, char *domainname, char *type)
.PP
.B
Ndbtuple* ndblookval(Ndbtuple *entry, Ndbtuple *line, char *attr, char *to)
.SH DESCRIPTION
These routines are used by network administrative programs to search
the network database.
They operate on the database files described in
.IR ndb (6).
.PP
.I Ndbopen
opens the database
.I file
and calls
.IR malloc (2)
to allocate a buffer for it.
If
.I file
is zero, all network database files are opened.
.PP
.I Ndbcat
concatenates two open databases. Either argument may be
nil.
.PP
.I Ndbreopen
checks if the database files associated with
.I db
have changed and if so throws out any cached information and reopens
the files.
.PP
.I Ndbclose
closes any database files associated with
.I db
and frees all storage associated with them.
.PP
.I Ndbsearch
and
.I ndbsnext
search a database for an entry containing the
attribute/value pair,
.IR attr = val .
.I Ndbsearch
is used to find the first match and
.I ndbsnext
is used to find each successive match.
On a successful search both return a linked list of
.I Ndbtuple
structures acquired by
.IR malloc (2)
that represent the attribute/value pairs in the
entry.
On failure they return zero.
.IP
.EX
typedef struct Ndbtuple Ndbtuple;
struct Ndbtuple {
char attr[Ndbalen];
char val[Ndbvlen];
Ndbtuple *entry;
Ndbtuple *line;
ulong ptr; /* for the application; starts 0 */
};
.EE
.LP
The
.I entry
pointers chain together all pairs in the entry in a null-terminated list.
The
.I line
pointers chain together all pairs on the same line
in a circular list.
Thus, a program can implement 2 levels of binding for
pairs in an entry.
In general, pairs on the same line are bound tighter
than pairs on different lines.
.PP
The argument
.I s
of
.I ndbsearch
has type
.I Ndbs
and should be pointed to valid storage before calling
.IR ndbsearch ,
which will fill it with information used by
.I ndbsnext
to link successive searches.
The structure
.I Ndbs
looks like:
.IP
.EX
typedef struct Ndbs Ndbs;
struct Ndbs {
Ndb *db; /* data base file being searched */
...
Ndbtuple *t; /* last attribute value pair found */
};
.EE
.LP
The
.I t
field points to the pair within the entry matched by the
.I ndbsearch
or
.IR ndbsnext .
.PP
.I Ndbgetval
searches the database for an entry containing not only an
attribute/value pair,
.IR attr = val ,
but also a pair with the attribute
.IR rattr .
If successful, it copies the value associated with
.I rattr
into
.IR buf .
.I Buf
must point to an area at least
.I Ndbvlen
long.
.I Csgetval
is like
.I ndbgetval
but queries the connection server
instead of looking directly at the database.
It's first argument specifies the network root to use.
If the argument is 0, it defaults to
\f5"/net"\f1.
.PP
.I Ndbfree
frees a list of tuples returned by one of the other
routines.
.PP
.I Ipattr
takes the name of an IP system and returns the attribute
it corresponds to:
.RS
.TP
.B dom
domain name
.TP
.B ip
Internet number
.TP
.B sys
system name
.RE
.PP
.I Ndbipinfo
looks up Internet protocol information about a system.
This is an IP aware search. It looks first for information
in the system's database entry and then in the database entries
for any IP subnets or networks containing the system.
The system is identified by the
attribute/value pair,
.IR attr = val .
.I Ndbipinfo
returns a list of tuples whose attributes match the
attributes in the
.I n
element array
.IR attrs .
For example, consider the following database entries describing a network,
a subnetwork, and a system.
.PP
.EX
ipnet=big ip=10.0.0.0 ipsubmask=255.255.255.0
dns=dns.big.com
smtp=smtp.big.com
ipnet=dept ip=10.1.1.0 ipmask=255.255.255.0
smtp=smtp1.big.com
ip=10.1.1.4 dom=x.big.com
bootf=/386/9pc
.EE
.PP
Calling
.PP
.EX
ndbipinfo(db, "dom", "x.big.com", ["bootf" "smtp" "dns"], 3)
.EE
.PP
will return the tuples
.BR bootf=/386/9pc ,
.BR smtp=smtp1.big.com ,
and
.BR dns=dns.big.com .
.PP
.I Csipinfo
is to
.I ndbipinfo
as
.I csgetval
is to
.IR ndbgetval .
.PP
The last three calls are used by programs that create the
hash tables and database files.
.I Ndbhash
computes a hash offset into a table of length
.I hlen
for the string
.IR val .
.I Ndbparse
reads and parses the next entry from the database file.
Multiple calls to
.IR ndbparse
parse sequential entries in the database file.
A zero is returned at end of file.
.PP
.I Dnsquery
submits a query about
.I domainname
to the
.I ndb/dns
mounted at
.IB netroot /dns.
It returns a linked list of
.I Ndbtuple's
representing a single database entry.
The tuples are logicly arranged into lines using the
.B line
fieldin the structure.
The possible
.IR type 's
of query are and the attributes on each returned tuple line is:
.TP
.B ip
find the IP addresses. Returns
domain name
.RI ( dom )
and ip address
.RI ( ip )
.TP
.B mx
look up the mail exchangers. Returns preference
.RI ( pref )
and exchanger
.RI ( mx )
.TP
.B ptr
do a reverse query. Here
.I domainname
must be an
.SM ASCII
IP address. Returns reverse name
.RI ( ptr )
and domain name
.RI ( dom )
.TP
.B cname
get the system that this name is a nickname for. Returns the nickname
.RI ( dom )
and the real name
.RI ( cname )
.TP
.B soa
return the start of area record for this field. Returns
area 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 ns
name servers. Returns domain name
.RI ( dom )
and name server
.RI ( ns )
.PP
.I Ndblookval
searches
.I entry
for the tuple
with attribute
.IR attr ,
copies the value into
.IR to ,
and returns a pointer to the tuple.
If
.I line
points to a particular line in the entry, the
search starts there and then wraps around to the beginning
of the entry.
.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
These routines set
.IR errstr .
|