ndb 7.5 KB

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