dns.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. typedef struct Ndbtuple Ndbtuple;
  2. enum
  3. {
  4. /* RR types; see: http://www.iana.org/assignments/dns-parameters */
  5. Ta= 1,
  6. Tns= 2,
  7. Tmd= 3,
  8. Tmf= 4,
  9. Tcname= 5,
  10. Tsoa= 6,
  11. Tmb= 7,
  12. Tmg= 8,
  13. Tmr= 9,
  14. Tnull= 10,
  15. Twks= 11,
  16. Tptr= 12,
  17. Thinfo= 13,
  18. Tminfo= 14,
  19. Tmx= 15,
  20. Ttxt= 16,
  21. Trp= 17,
  22. Tafsdb= 18,
  23. Tx25= 19,
  24. Tisdn= 20,
  25. Trt= 21,
  26. Tnsap= 22,
  27. Tnsapptr= 23,
  28. Tsig= 24,
  29. Tkey= 25,
  30. Tpx= 26,
  31. Tgpos= 27,
  32. Taaaa= 28,
  33. Tloc= 29,
  34. Tnxt= 30,
  35. Teid= 31,
  36. Tnimloc= 32,
  37. Tsrv= 33,
  38. Tatma= 34,
  39. Tnaptr= 35,
  40. Tkx= 36,
  41. Tcert= 37,
  42. Ta6= 38,
  43. Tdname= 39,
  44. Tsink= 40,
  45. Topt= 41,
  46. Tapl= 42,
  47. Tds= 43,
  48. Tsshfp= 44,
  49. Tipseckey= 45,
  50. Trrsig= 46,
  51. Tnsec= 47,
  52. Tdnskey= 48,
  53. Tspf= 99,
  54. Tuinfo= 100,
  55. Tuid= 101,
  56. Tgid= 102,
  57. Tunspec= 103,
  58. /* query types (all RR types are also queries) */
  59. Ttkey= 249, /* transaction key */
  60. Ttsig= 250, /* transaction signature */
  61. Tixfr= 251, /* incremental zone transfer */
  62. Taxfr= 252, /* zone transfer */
  63. Tmailb= 253, /* { Tmb, Tmg, Tmr } */
  64. Tmaila= 254, /* obsolete */
  65. Tall= 255, /* all records */
  66. /* classes */
  67. Csym= 0, /* internal symbols */
  68. Cin= 1, /* internet */
  69. Ccs, /* CSNET (obsolete) */
  70. Cch, /* Chaos net */
  71. Chs, /* Hesiod (?) */
  72. /* class queries (all class types are also queries) */
  73. Call= 255, /* all classes */
  74. /* opcodes */
  75. Oquery= 0<<11, /* normal query */
  76. Oinverse= 1<<11, /* inverse query (retired) */
  77. Ostatus= 2<<11, /* status request */
  78. Onotify= 4<<11, /* notify slaves of updates */
  79. Oupdate= 5<<11,
  80. Omask= 0xf<<11, /* mask for opcode */
  81. /* response codes */
  82. Rok= 0,
  83. Rformat= 1, /* format error */
  84. Rserver= 2, /* server failure (e.g. no answer from something) */
  85. Rname= 3, /* bad name */
  86. Runimplimented= 4, /* unimplemented */
  87. Rrefused= 5, /* we don't like you */
  88. Ryxdomain= 6, /* name exists when it should not */
  89. Ryxrrset= 7, /* rr set exists when it should not */
  90. Rnxrrset= 8, /* rr set that should exist does not */
  91. Rnotauth= 9, /* not authoritative */
  92. Rnotzone= 10, /* name not in zone */
  93. Rbadvers= 16, /* bad opt version */
  94. /* Rbadsig= 16, */ /* also tsig signature failure */
  95. Rbadkey= 17, /* key not recognized */
  96. Rbadtime= 18, /* signature out of time window */
  97. Rbadmode= 19, /* bad tkey mode */
  98. Rbadname= 20, /* duplicate key name */
  99. Rbadalg= 21, /* algorithm not supported */
  100. Rmask= 0x1f, /* mask for response */
  101. Rtimeout= 1<<5, /* timeout sending (for internal use only) */
  102. /* bits in flag word (other than opcode and response) */
  103. Fresp= 1<<15, /* message is a response */
  104. Fauth= 1<<10, /* true if an authoritative response */
  105. Ftrunc= 1<<9, /* truncated message */
  106. Frecurse= 1<<8, /* request recursion */
  107. Fcanrec= 1<<7, /* server can recurse */
  108. Domlen= 256, /* max domain name length (with NULL) */
  109. Labellen= 64, /* max domain label length (with NULL) */
  110. Strlen= 256, /* max string length (with NULL) */
  111. /* time to live values (in seconds) */
  112. Min= 60,
  113. Hour= 60*Min, /* */
  114. Day= 24*Hour, /* Ta, Tmx */
  115. Week= 7*Day, /* Tsoa, Tns */
  116. Year= 52*Week,
  117. DEFTTL= Day,
  118. /* reserved time (can't be timed out earlier) */
  119. Reserved= 5*Min,
  120. /* packet sizes */
  121. Maxudp= 512, /* maximum bytes per udp message */
  122. Maxudpin= 2048, /* maximum bytes per udp message */
  123. /* length of domain name hash table */
  124. HTLEN= 4*1024,
  125. Maxpath= 128, /* size of mntpt */
  126. RRmagic= 0xdeadbabe,
  127. DNmagic= 0xa110a110,
  128. /* parallelism: tune; was 32 */
  129. Maxactive= 64,
  130. /* tune; was 60 */
  131. Maxreqtm= 30, /* max. seconds to process a request */
  132. };
  133. typedef struct Area Area;
  134. typedef struct Block Block;
  135. typedef struct Cert Cert;
  136. typedef struct DN DN;
  137. typedef struct DNSmsg DNSmsg;
  138. typedef struct Key Key;
  139. typedef struct Null Null;
  140. typedef struct RR RR;
  141. typedef struct Request Request;
  142. typedef struct SOA SOA;
  143. typedef struct Server Server;
  144. typedef struct Sig Sig;
  145. typedef struct Srv Srv;
  146. typedef struct Txt Txt;
  147. /*
  148. * a structure to track a request and any slave process handling it
  149. */
  150. struct Request
  151. {
  152. int isslave; /* pid of slave */
  153. ulong aborttime; /* time at which we give up */
  154. jmp_buf mret; /* where master jumps to after starting a slave */
  155. int id;
  156. };
  157. /*
  158. * a domain name
  159. */
  160. struct DN
  161. {
  162. DN *next; /* hash collision list */
  163. ulong magic;
  164. char *name; /* owner */
  165. RR *rr; /* resource records off this name */
  166. ulong referenced; /* time last referenced */
  167. ulong lookuptime; /* last time we tried to get a better value */
  168. /* refs was `char' but we've seen refs > 120, so go whole hog */
  169. ulong refs; /* for mark and sweep */
  170. ushort class; /* RR class */
  171. uchar respcode; /* response code */
  172. /* was: char nonexistent; /* true if we get an authoritative nx for this domain */
  173. ulong ordinal;
  174. QLock querylck; /* permit only 1 query per domain name at a time */
  175. };
  176. /*
  177. * security info
  178. */
  179. struct Block
  180. {
  181. int dlen;
  182. uchar *data;
  183. };
  184. struct Key
  185. {
  186. int flags;
  187. int proto;
  188. int alg;
  189. Block;
  190. };
  191. struct Cert
  192. {
  193. int type;
  194. int tag;
  195. int alg;
  196. Block;
  197. };
  198. struct Sig
  199. {
  200. Cert;
  201. int labels;
  202. ulong ttl;
  203. ulong exp;
  204. ulong incep;
  205. DN *signer;
  206. };
  207. struct Null
  208. {
  209. Block;
  210. };
  211. /*
  212. * text strings
  213. */
  214. struct Txt
  215. {
  216. Txt *next;
  217. char *p;
  218. };
  219. /*
  220. * an unpacked resource record
  221. */
  222. struct RR
  223. {
  224. RR *next;
  225. ulong magic;
  226. DN *owner; /* domain that owns this resource record */
  227. uintptr pc;
  228. ulong ttl; /* time to live to be passed on */
  229. ulong expire; /* time this entry expires locally */
  230. ulong marker; /* used locally when scanning rrlists */
  231. ushort type; /* RR type */
  232. ushort query; /* query type is in response to */
  233. uchar auth; /* flag: authoritative */
  234. uchar db; /* flag: from database */
  235. uchar cached; /* flag: rr in cache */
  236. uchar negative; /* flag: this is a cached negative response */
  237. union { /* discriminated how? negative & type? */
  238. DN *negsoaowner; /* soa for cached negative response */
  239. DN *host; /* hostname - soa, cname, mb, md, mf, mx, ns */
  240. DN *cpu; /* cpu type - hinfo */
  241. DN *mb; /* mailbox - mg, minfo */
  242. DN *ip; /* ip address - a */
  243. DN *rp; /* rp arg - rp */
  244. uintptr arg0; /* arg[01] are compared to find dups in dn.c */
  245. };
  246. union { /* discriminated how? negative & type? */
  247. int negrcode; /* response code for cached negative resp. */
  248. DN *rmb; /* responsible maibox - minfo, soa, rp */
  249. DN *ptr; /* pointer to domain name - ptr */
  250. DN *os; /* operating system - hinfo */
  251. ulong pref; /* preference value - mx */
  252. ulong local; /* ns served from local database - ns */
  253. uintptr arg1; /* arg[01] are compared to find dups in dn.c */
  254. };
  255. union { /* discriminated by type */
  256. SOA *soa; /* soa timers - soa */
  257. Key *key;
  258. Cert *cert;
  259. Sig *sig;
  260. Null *null;
  261. Txt *txt;
  262. Srv *srv;
  263. };
  264. };
  265. /*
  266. * list of servers
  267. */
  268. struct Server
  269. {
  270. Server *next;
  271. char *name;
  272. };
  273. /*
  274. * timers for a start-of-authority record. all ulongs are in seconds.
  275. */
  276. struct SOA
  277. {
  278. ulong serial; /* zone serial # */
  279. ulong refresh; /* zone refresh interval */
  280. ulong retry; /* zone retry interval */
  281. ulong expire; /* time to expiration */
  282. ulong minttl; /* min. time to live for any entry */
  283. Server *slaves; /* slave servers */
  284. };
  285. /*
  286. * srv (service location) record (rfc2782):
  287. * _service._proto.name ttl class(IN) 'SRV' priority weight port target
  288. */
  289. struct Srv
  290. {
  291. ushort pri;
  292. ushort weight;
  293. ushort port;
  294. DN *target;
  295. };
  296. struct Rrlist
  297. {
  298. int count;
  299. RR *rrs;
  300. };
  301. /*
  302. * domain messages
  303. */
  304. struct DNSmsg
  305. {
  306. ushort id;
  307. int flags;
  308. int qdcount; /* questions */
  309. RR *qd;
  310. int ancount; /* answers */
  311. RR *an;
  312. int nscount; /* name servers */
  313. RR *ns;
  314. int arcount; /* hints */
  315. RR *ar;
  316. };
  317. /*
  318. * definition of local area for dblookup
  319. */
  320. struct Area
  321. {
  322. Area *next;
  323. int len; /* strlen(area->soarr->owner->name) */
  324. RR *soarr; /* soa defining this area */
  325. int neednotify;
  326. int needrefresh;
  327. };
  328. typedef struct Cfg Cfg;
  329. struct Cfg {
  330. int cachedb;
  331. int resolver;
  332. int serve;
  333. int inside;
  334. int straddle;
  335. };
  336. enum
  337. {
  338. Recurse,
  339. Dontrecurse,
  340. NOneg,
  341. OKneg,
  342. };
  343. extern Cfg cfg;
  344. extern char *dbfile;
  345. extern int debug;
  346. extern Area *delegated;
  347. extern char *logfile;
  348. extern int maxage; /* age of oldest entry in cache (secs) */
  349. extern char mntpt[];
  350. extern int needrefresh;
  351. extern int norecursion;
  352. extern ulong now; /* time base */
  353. extern Area *owned;
  354. extern int sendnotifies;
  355. extern ulong target;
  356. extern int testing; /* test cache whenever removing a DN */
  357. extern char *trace;
  358. extern int traceactivity;
  359. extern char *zonerefreshprogram;
  360. #pragma varargck type "R" RR*
  361. #pragma varargck type "Q" RR*
  362. /* dn.c */
  363. extern char *rrtname[];
  364. extern char *rname[];
  365. extern unsigned nrname;
  366. extern char *opname[];
  367. void abort(); /* char*, ... */;
  368. void addserver(Server**, char*);
  369. Server* copyserverlist(Server*);
  370. void db2cache(int);
  371. void dnage(DN*);
  372. void dnageall(int);
  373. void dnagedb(void);
  374. void dnauthdb(void);
  375. void dncheck(void*, int);
  376. void dndump(char*);
  377. void dnget(void);
  378. void dninit(void);
  379. DN* dnlookup(char*, int, int);
  380. void dnptr(uchar*, uchar*, char*, int, int);
  381. void dnpurge(void);
  382. void dnput(void);
  383. void dnslog(char*, ...);
  384. void* emalloc(int);
  385. char* estrdup(char*);
  386. void freeserverlist(Server*);
  387. int getactivity(Request*, int);
  388. Area* inmyarea(char*);
  389. void putactivity(int);
  390. RR* randomize(RR*);
  391. RR* rralloc(int);
  392. void rrattach(RR*, int);
  393. int rravfmt(Fmt*);
  394. RR* rrcat(RR**, RR*);
  395. RR** rrcopy(RR*, RR**);
  396. int rrfmt(Fmt*);
  397. void rrfree(RR*);
  398. void rrfreelist(RR*);
  399. RR* rrlookup(DN*, int, int);
  400. char* rrname(int, char*, int);
  401. RR* rrremneg(RR**);
  402. RR* rrremtype(RR**, int);
  403. int rrsupported(int);
  404. int rrtype(char*);
  405. void slave(Request*);
  406. int subsume(char*, char*);
  407. int tsame(int, int);
  408. void unique(RR*);
  409. void warning(char*, ...);
  410. /* dnarea.c */
  411. void refresh_areas(Area*);
  412. void freearea(Area**);
  413. void addarea(DN *dp, RR *rp, Ndbtuple *t);
  414. /* dblookup.c */
  415. int baddelegation(RR*, RR*, uchar*);
  416. RR* dbinaddr(DN*, int);
  417. RR* dblookup(char*, int, int, int, int);
  418. RR* dnsservers(int);
  419. RR* domainlist(int);
  420. int insideaddr(char *dom);
  421. int insidens(uchar *ip);
  422. int opendatabase(void);
  423. uchar* outsidens(int);
  424. /* dns.c */
  425. char* walkup(char*);
  426. RR* getdnsservers(int);
  427. void logreply(int, uchar*, DNSmsg*);
  428. void logsend(int, int, uchar*, char*, char*, int);
  429. void procsetname(char *fmt, ...);
  430. /* dnresolve.c */
  431. RR* dnresolve(char*, int, int, Request*, RR**, int, int, int, int*);
  432. int udpport(char *);
  433. int mkreq(DN *dp, int type, uchar *buf, int flags, ushort reqno);
  434. int seerootns(void);
  435. /* dnserver.c */
  436. void dnserver(DNSmsg*, DNSmsg*, Request*, uchar *, int);
  437. void dnudpserver(char*);
  438. void dntcpserver(char*);
  439. /* dnnotify.c */
  440. void dnnotify(DNSmsg*, DNSmsg*, Request*);
  441. void notifyproc(void);
  442. /* convDNS2M.c */
  443. int convDNS2M(DNSmsg*, uchar*, int);
  444. /* convM2DNS.c */
  445. char* convM2DNS(uchar*, int, DNSmsg*, int*);
  446. #pragma varargck argpos dnslog 1