1
0

ndb 8.7 KB

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