ndb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. .TH NDB 2
  2. .SH NAME
  3. ndbopen, ndbcat, ndbchanged, ndbclose, ndbreopen, ndbsearch, ndbsnext, ndbgetvalue, ndbfree, ipattr, ndbgetipaddr, ndbipinfo, csipinfo, ndbhash, ndbparse, csgetvalue, ndblookvalue, dnsquery, ndbdiscard, ndbconcatenate, ndbreorder, ndbsubstitute, ndbgetval, csgetval, ndblookval \- network database
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .br
  9. .B #include <bio.h>
  10. .br
  11. .B #include <ndb.h>
  12. .ta \w'\fLNdbtuplexx 'u
  13. .PP
  14. .B
  15. Ndb* ndbopen(char *file)
  16. .PP
  17. .B
  18. Ndb* ndbcat(Ndb *db1, Ndb *db2)
  19. .PP
  20. .B
  21. Ndb* ndbchanged(Ndb *db)
  22. .PP
  23. .B
  24. int ndbreopen(Ndb *db)
  25. .PP
  26. .B
  27. void ndbclose(Ndb *db)
  28. .PP
  29. .B
  30. Ndbtuple* ndbsearch(Ndb *db, Ndbs *s, char *attr, char *val)
  31. .PP
  32. .B
  33. Ndbtuple* ndbsnext(Ndbs *s, char *attr, char *val)
  34. .PP
  35. .B
  36. Ndbtuple* ndbgetvalue(Ndb *db, Ndbs *s, char *attr, char *val,
  37. .br
  38. .B
  39. char *rattr, char *buf, int buflen)
  40. .PP
  41. .B
  42. Ndbtuple* csgetvalue(char *netroot, char *attr, char *val, char *rattr,
  43. char *buf, int buflen)
  44. .PP
  45. .B
  46. char* ipattr(char *name)
  47. .PP
  48. .B
  49. Ndbtuple* ndbgetipaddr(Ndb *db, char *sys);
  50. .PP
  51. .B
  52. Ndbtuple* ndbipinfo(Ndb *db, char *attr, char *val, char **attrs,
  53. .br
  54. .B int nattr)
  55. .PP
  56. .B
  57. Ndbtuple* csipinfo(char *netroot, char *attr, char *val, char **attrs,
  58. .br
  59. .B int nattr)
  60. .PP
  61. .B
  62. ulong ndbhash(char *val, int hlen)
  63. .PP
  64. .B
  65. Ndbtuple* ndbparse(Ndb *db)
  66. .PP
  67. .B
  68. Ndbtuple* dnsquery(char *netroot, char *domainname, char *type)
  69. .PP
  70. .B
  71. Ndbtuple* ndblookvalue(Ndbtuple *entry, Ndbtuple *line, char *attr,
  72. char *to, int tolen)
  73. .PP
  74. .B
  75. void ndbfree(Ndbtuple *db)
  76. .PP
  77. .B
  78. Ndbtuple* ndbdiscard(Ndbtuple *t, Ndbtuple *a)
  79. .PP
  80. .B
  81. Ndbtuple* ndbconcatenate(Ndbtuple *a, Ndbtuple *b);
  82. .PP
  83. .B
  84. Ndbtuple* ndbreorder(Ndbtuple *t, Ndbtuple *a);
  85. .PP
  86. .B
  87. Ndbtuple* ndbsubstitute(Ndbtuple *t, Ndbtuple *from, Ndbtuple *to);
  88. .SH DESCRIPTION
  89. These routines are used by network administrative programs to search
  90. the network database.
  91. They operate on the database files described in
  92. .IR ndb (6).
  93. .PP
  94. .I Ndbopen
  95. opens the database
  96. .I file
  97. and calls
  98. .IR malloc (2)
  99. to allocate a buffer for it.
  100. If
  101. .I file
  102. is zero, all network database files are opened.
  103. .PP
  104. .I Ndbcat
  105. concatenates two open databases. Either argument may be
  106. nil.
  107. .PP
  108. .I Ndbreopen
  109. checks if the database files associated with
  110. .I db
  111. have changed and if so throws out any cached information and reopens
  112. the files.
  113. .PP
  114. .I Ndbclose
  115. closes any database files associated with
  116. .I db
  117. and frees all storage associated with them.
  118. .PP
  119. .I Ndbsearch
  120. and
  121. .I ndbsnext
  122. search a database for an entry containing the
  123. attribute/value pair,
  124. .IR attr = val .
  125. .I Ndbsearch
  126. is used to find the first match and
  127. .I ndbsnext
  128. is used to find each successive match.
  129. On a successful search both return a linked list of
  130. .I Ndbtuple
  131. structures acquired by
  132. .IR malloc (2)
  133. that represent the attribute/value pairs in the
  134. entry.
  135. On failure they return zero.
  136. .IP
  137. .EX
  138. typedef struct Ndbtuple Ndbtuple;
  139. struct Ndbtuple {
  140. char attr[Ndbalen];
  141. char *val;
  142. Ndbtuple *entry;
  143. Ndbtuple *line;
  144. ulong ptr; /* for the application; starts 0 */
  145. char valbuf[Ndbvlen]; /* initial allocation for val */
  146. };
  147. .EE
  148. .LP
  149. The
  150. .I entry
  151. pointers chain together all pairs in the entry in a null-terminated list.
  152. The
  153. .I line
  154. pointers chain together all pairs on the same line
  155. in a circular list.
  156. Thus, a program can implement 2 levels of binding for
  157. pairs in an entry.
  158. In general, pairs on the same line are bound tighter
  159. than pairs on different lines.
  160. .PP
  161. The argument
  162. .I s
  163. of
  164. .I ndbsearch
  165. has type
  166. .I Ndbs
  167. and should be pointed to valid storage before calling
  168. .IR ndbsearch ,
  169. which will fill it with information used by
  170. .I ndbsnext
  171. to link successive searches.
  172. The structure
  173. .I Ndbs
  174. looks like:
  175. .IP
  176. .EX
  177. typedef struct Ndbs Ndbs;
  178. struct Ndbs {
  179. Ndb *db; /* data base file being searched */
  180. ...
  181. Ndbtuple *t; /* last attribute value pair found */
  182. };
  183. .EE
  184. .LP
  185. The
  186. .I t
  187. field points to the pair within the entry matched by the
  188. .I ndbsearch
  189. or
  190. .IR ndbsnext .
  191. .PP
  192. .I Ndbgetvalue
  193. searches the database for an entry containing not only an
  194. attribute/value pair,
  195. .IR attr = val ,
  196. but also a pair with the attribute
  197. .IR rattr .
  198. If successful, it copies up to
  199. .I buflen
  200. bytes of the value associated with
  201. .I rattr
  202. into
  203. .IR buf .
  204. .I Csgetvalue
  205. is like
  206. .I ndbgetvalue
  207. but queries the connection server
  208. instead of looking directly at the database.
  209. Its first argument specifies the network root to use.
  210. If the argument is 0, it defaults to
  211. \f5"/net"\f1.
  212. .PP
  213. .I Ndbfree
  214. frees a list of tuples returned by one of the other
  215. routines.
  216. .PP
  217. .I Ipattr
  218. takes the name of an IP system and returns the attribute
  219. it corresponds to:
  220. .RS
  221. .TP
  222. .B dom
  223. domain name
  224. .TP
  225. .B ip
  226. Internet number
  227. .TP
  228. .B sys
  229. system name
  230. .RE
  231. .PP
  232. .I Ndbgetipaddr
  233. looks in
  234. .I db
  235. for an entry matching
  236. .I sys
  237. as the value of a
  238. .B sys=
  239. or
  240. .B dom=
  241. attribute/value pair and returns all IP addresses in the entry.
  242. If
  243. .I sys
  244. is already an IP address, a tuple containing just
  245. that address is returned.
  246. .PP
  247. .I Ndbipinfo
  248. looks up Internet protocol information about a system.
  249. This is an IP aware search. It looks first for information
  250. in the system's database entry and then in the database entries
  251. for any IP subnets or networks containing the system.
  252. The system is identified by the
  253. attribute/value pair,
  254. .IR attr = val .
  255. .I Ndbipinfo
  256. returns a list of tuples whose attributes match the
  257. attributes in the
  258. .I n
  259. element array
  260. .IR attrs .
  261. For example, consider the following database entries describing a network,
  262. a subnetwork, and a system.
  263. .PP
  264. .EX
  265. ipnet=big ip=10.0.0.0
  266. dns=dns.big.com
  267. smtp=smtp.big.com
  268. ipnet=dept ip=10.1.1.0 ipmask=255.255.255.0
  269. smtp=smtp1.big.com
  270. ip=10.1.1.4 dom=x.big.com
  271. bootf=/386/9pc
  272. .EE
  273. .PP
  274. Calling
  275. .PP
  276. .EX
  277. ndbipinfo(db, "dom", "x.big.com", ["bootf" "smtp" "dns"], 3)
  278. .EE
  279. .PP
  280. will return the tuples
  281. .BR bootf=/386/9pc ,
  282. .BR smtp=smtp1.big.com ,
  283. and
  284. .BR dns=dns.big.com .
  285. .PP
  286. .I Csipinfo
  287. is to
  288. .I ndbipinfo
  289. as
  290. .I csgetval
  291. is to
  292. .IR ndbgetval .
  293. .PP
  294. The next three routines are used by programs that create the
  295. hash tables and database files.
  296. .I Ndbhash
  297. computes a hash offset into a table of length
  298. .I hlen
  299. for the string
  300. .IR val .
  301. .I Ndbparse
  302. reads and parses the next entry from the database file.
  303. Multiple calls to
  304. .IR ndbparse
  305. parse sequential entries in the database file.
  306. A zero is returned at end of file.
  307. .PP
  308. .I Dnsquery
  309. submits a query about
  310. .I domainname
  311. to the
  312. .I ndb/dns
  313. mounted at
  314. .IB netroot /dns.
  315. It returns a linked list of
  316. .I Ndbtuple's
  317. representing a single database entry.
  318. The tuples are logicly arranged into lines using the
  319. .B line
  320. fieldin the structure.
  321. The possible
  322. .IR type 's
  323. of query are and the attributes on each returned tuple line is:
  324. .TP
  325. .B ip
  326. find the IP addresses. Returns
  327. domain name
  328. .RI ( dom )
  329. and ip address
  330. .RI ( ip )
  331. .TP
  332. .B mx
  333. look up the mail exchangers. Returns preference
  334. .RI ( pref )
  335. and exchanger
  336. .RI ( mx )
  337. .TP
  338. .B ptr
  339. do a reverse query. Here
  340. .I domainname
  341. must be an
  342. .SM ASCII
  343. IP address. Returns reverse name
  344. .RI ( ptr )
  345. and domain name
  346. .RI ( dom )
  347. .TP
  348. .B cname
  349. get the system that this name is a nickname for. Returns the nickname
  350. .RI ( dom )
  351. and the real name
  352. .RI ( cname )
  353. .TP
  354. .B soa
  355. return the start of area record for this field. Returns
  356. area name
  357. .RI ( dom ),
  358. primary name server
  359. .RI ( ns ),
  360. serial number
  361. .RI ( serial ),
  362. refresh time in seconds
  363. .RI ( refresh ),
  364. retry time in seconds
  365. .RI ( retry ),
  366. expiration time in seconds
  367. .RI ( expire ),
  368. and minimum time to lie
  369. .RI ( ttl ).
  370. .TP
  371. .B ns
  372. name servers. Returns domain name
  373. .RI ( dom )
  374. and name server
  375. .RI ( ns )
  376. .PP
  377. .I Ndblookvalue
  378. searches
  379. .I entry
  380. for the tuple
  381. with attribute
  382. .IR attr ,
  383. copies up to
  384. .I tolen
  385. bytes of the value into
  386. .IR to ,
  387. and returns a pointer to the tuple.
  388. If
  389. .I line
  390. points to a particular line in the entry, the
  391. search starts there and then wraps around to the beginning
  392. of the entry.
  393. .PP
  394. All of the routines provided to search the database
  395. provide an always consistent view of the relevant
  396. files. However, it may be advantageous for an application
  397. to read in the whole database using
  398. .I ndbopen
  399. and
  400. .I ndbparse
  401. and provide its own search routines. The
  402. .I ndbchanged
  403. routine can be used by the application to periodicly
  404. check for changes. It returns zero
  405. if none of the files comprising the database have
  406. changes and non-zero if they have.
  407. .PP
  408. Finally, a number of routines are provided for manipulating
  409. tuples.
  410. .PP
  411. .I Ndbdiscard
  412. removes attr/val pair
  413. .I a
  414. from tuple
  415. .I t
  416. and frees it.
  417. If
  418. .I a
  419. isn't in
  420. .I t
  421. it is just freed.
  422. .PP
  423. .I Ndbconcatenate
  424. concatenates two tuples and returns the result. Either
  425. or both tuples may be nil.
  426. .PP
  427. .I Ndbreorder
  428. reorders a tuple
  429. .IR t
  430. to make the line containing attr/val pair
  431. .I a
  432. first in the entry and making
  433. .I a
  434. first in its line.
  435. .PP
  436. .I Ndbsubstitute
  437. replaces a single att/val pair
  438. .I from
  439. in
  440. .I t
  441. with the tuple
  442. .IR to .
  443. All attr/val pairs in
  444. .I to
  445. end up on the same line.
  446. .I from
  447. is freed.
  448. .SH FILES
  449. .BR /lib/ndb " directory of network database files
  450. .SH SOURCE
  451. .B /sys/src/libndb
  452. .SH SEE ALSO
  453. .IR ndb (6)
  454. .IR ndb (8)
  455. .SH DIAGNOSTICS
  456. These routines set
  457. .IR errstr .
  458. .IR Ndbgetvalue ,
  459. .IR csgetvalue ,
  460. and
  461. .I ndblookvalue
  462. set
  463. .I errstr
  464. to
  465. .B "return value truncated"
  466. if the buffer provided isn't long enough for the
  467. returned value.
  468. .SH BUGS
  469. .IR Ndbgetval ,
  470. .IR csgetval ,
  471. and
  472. .I ndblookval
  473. are deprecated versions of
  474. .IR ndbgetvalue ,
  475. .IR csgetvalue ,
  476. and
  477. .IR ndblookvalue .
  478. They expect a fixed 64 byte long result
  479. buffer and existed when the values of a
  480. .I Ndbtuple
  481. structure where fixed length.