ndb 9.4 KB

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