convDNS2M.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <ip.h>
  12. #include "dns.h"
  13. /*
  14. * a dictionary of domain names for packing messages
  15. */
  16. enum
  17. {
  18. Ndict= 64,
  19. };
  20. typedef struct Dict Dict;
  21. struct Dict
  22. {
  23. struct {
  24. uint16_t offset; /* pointer to packed name in message */
  25. char *name; /* pointer to unpacked name in buf */
  26. } x[Ndict];
  27. int n; /* size of dictionary */
  28. uint8_t *start; /* start of packed message */
  29. char buf[16*1024]; /* buffer for unpacked names (was 4k) */
  30. char *ep; /* first free char in buf */
  31. };
  32. #define NAME(x) p = pname(p, ep, x, dp)
  33. #define SYMBOL(x) p = psym(p, ep, x)
  34. #define STRING(x) p = pstr(p, ep, x)
  35. #define BYTES(x, n) p = pbytes(p, ep, x, n)
  36. #define USHORT(x) p = pushort(p, ep, x)
  37. #define UCHAR(x) p = puchar(p, ep, x)
  38. #define ULONG(x) p = pulong(p, ep, x)
  39. #define V4ADDR(x) p = pv4addr(p, ep, x)
  40. #define V6ADDR(x) p = pv6addr(p, ep, x)
  41. static uint8_t*
  42. psym(uint8_t *p, uint8_t *ep, char *np)
  43. {
  44. int n;
  45. n = strlen(np);
  46. if(n >= Strlen) /* DNS maximum length string */
  47. n = Strlen - 1;
  48. if(ep - p < n+1) /* see if it fits in the buffer */
  49. return ep+1;
  50. *p++ = n;
  51. memmove(p, np, n);
  52. return p + n;
  53. }
  54. static uint8_t*
  55. pstr(uint8_t *p, uint8_t *ep, char *np)
  56. {
  57. return psym(p, ep, np);
  58. }
  59. static uint8_t*
  60. pbytes(uint8_t *p, uint8_t *ep, uint8_t *np, int n)
  61. {
  62. if(ep - p < n)
  63. return ep+1;
  64. memmove(p, np, n);
  65. return p + n;
  66. }
  67. static uint8_t*
  68. puchar(uint8_t *p, uint8_t *ep, int val)
  69. {
  70. if(ep - p < 1)
  71. return ep+1;
  72. *p++ = val;
  73. return p;
  74. }
  75. static uint8_t*
  76. pushort(uint8_t *p, uint8_t *ep, int val)
  77. {
  78. if(ep - p < 2)
  79. return ep+1;
  80. *p++ = val>>8;
  81. *p++ = val;
  82. return p;
  83. }
  84. static uint8_t*
  85. pulong(uint8_t *p, uint8_t *ep, int val)
  86. {
  87. if(ep - p < 4)
  88. return ep+1;
  89. *p++ = val>>24;
  90. *p++ = val>>16;
  91. *p++ = val>>8;
  92. *p++ = val;
  93. return p;
  94. }
  95. static uint8_t*
  96. pv4addr(uint8_t *p, uint8_t *ep, char *name)
  97. {
  98. uint8_t ip[IPaddrlen];
  99. if(ep - p < 4)
  100. return ep+1;
  101. parseip(ip, name);
  102. v6tov4(p, ip);
  103. return p + 4;
  104. }
  105. static uint8_t*
  106. pv6addr(uint8_t *p, uint8_t *ep, char *name)
  107. {
  108. if(ep - p < IPaddrlen)
  109. return ep+1;
  110. parseip(p, name);
  111. return p + IPaddrlen;
  112. }
  113. static uint8_t*
  114. pname(uint8_t *p, uint8_t *ep, char *np, Dict *dp)
  115. {
  116. int i;
  117. char *cp;
  118. char *last; /* last component packed */
  119. if(strlen(np) >= Domlen) /* make sure we don't exceed DNS limits */
  120. return ep+1;
  121. last = 0;
  122. while(*np){
  123. /* look through every component in the dictionary for a match */
  124. for(i = 0; i < dp->n; i++)
  125. if(strcmp(np, dp->x[i].name) == 0){
  126. if(ep - p < 2)
  127. return ep+1;
  128. if ((dp->x[i].offset>>8) & 0xc0)
  129. dnslog("convDNS2M: offset too big for "
  130. "DNS packet format");
  131. *p++ = dp->x[i].offset>>8 | 0xc0;
  132. *p++ = dp->x[i].offset;
  133. return p;
  134. }
  135. /* if there's room, enter this name in dictionary */
  136. if(dp->n < Ndict)
  137. if(last){
  138. /* the whole name is already in dp->buf */
  139. last = strchr(last, '.') + 1;
  140. dp->x[dp->n].name = last;
  141. dp->x[dp->n].offset = p - dp->start;
  142. dp->n++;
  143. } else {
  144. /* add to dp->buf */
  145. i = strlen(np);
  146. if(dp->ep + i + 1 < &dp->buf[sizeof dp->buf]){
  147. strcpy(dp->ep, np);
  148. dp->x[dp->n].name = dp->ep;
  149. last = dp->ep;
  150. dp->x[dp->n].offset = p - dp->start;
  151. dp->ep += i + 1;
  152. dp->n++;
  153. }
  154. }
  155. /* put next component into message */
  156. cp = strchr(np, '.');
  157. if(cp == nil){
  158. i = strlen(np);
  159. cp = np + i; /* point to null terminator */
  160. } else {
  161. i = cp - np;
  162. cp++; /* point past '.' */
  163. }
  164. if(ep-p < i+1)
  165. return ep+1;
  166. if (i > Labellen)
  167. return ep+1;
  168. *p++ = i; /* count of chars in label */
  169. memmove(p, np, i);
  170. np = cp;
  171. p += i;
  172. }
  173. if(p >= ep)
  174. return ep+1;
  175. *p++ = 0; /* add top level domain */
  176. return p;
  177. }
  178. static uint8_t*
  179. convRR2M(RR *rp, uint8_t *p, uint8_t *ep, Dict *dp)
  180. {
  181. uint8_t *lp, *data;
  182. int len, ttl;
  183. Txt *t;
  184. NAME(rp->owner->name);
  185. USHORT(rp->type);
  186. USHORT(rp->owner->class);
  187. /* egregious overuse of ttl (it's absolute time in the cache) */
  188. if(rp->db)
  189. ttl = rp->ttl;
  190. else
  191. ttl = rp->ttl - now;
  192. if(ttl < 0)
  193. ttl = 0;
  194. ULONG(ttl);
  195. lp = p; /* leave room for the rdata length */
  196. p += 2;
  197. data = p;
  198. if(data >= ep)
  199. return p+1;
  200. switch(rp->type){
  201. case Thinfo:
  202. SYMBOL(rp->cpu->name);
  203. SYMBOL(rp->os->name);
  204. break;
  205. case Tcname:
  206. case Tmb:
  207. case Tmd:
  208. case Tmf:
  209. case Tns:
  210. NAME(rp->host->name);
  211. break;
  212. case Tmg:
  213. case Tmr:
  214. NAME(rp->mb->name);
  215. break;
  216. case Tminfo:
  217. NAME(rp->rmb->name);
  218. NAME(rp->mb->name);
  219. break;
  220. case Tmx:
  221. USHORT(rp->pref);
  222. NAME(rp->host->name);
  223. break;
  224. case Ta:
  225. V4ADDR(rp->ip->name);
  226. break;
  227. case Taaaa:
  228. V6ADDR(rp->ip->name);
  229. break;
  230. case Tptr:
  231. NAME(rp->ptr->name);
  232. break;
  233. case Tsoa:
  234. NAME(rp->host->name);
  235. NAME(rp->rmb->name);
  236. ULONG(rp->soa->serial);
  237. ULONG(rp->soa->refresh);
  238. ULONG(rp->soa->retry);
  239. ULONG(rp->soa->expire);
  240. ULONG(rp->soa->minttl);
  241. break;
  242. case Tsrv:
  243. USHORT(rp->srv->pri);
  244. USHORT(rp->srv->weight);
  245. USHORT(rp->port);
  246. STRING(rp->host->name); /* rfc2782 sez no name compression */
  247. break;
  248. case Ttxt:
  249. for(t = rp->txt; t != nil; t = t->next)
  250. STRING(t->p);
  251. break;
  252. case Tnull:
  253. BYTES(rp->null->data, rp->null->dlen);
  254. break;
  255. case Trp:
  256. NAME(rp->rmb->name);
  257. NAME(rp->rp->name);
  258. break;
  259. case Tkey:
  260. USHORT(rp->key->flags);
  261. UCHAR(rp->key->proto);
  262. UCHAR(rp->key->alg);
  263. BYTES(rp->key->data, rp->key->dlen);
  264. break;
  265. case Tsig:
  266. USHORT(rp->sig->type);
  267. UCHAR(rp->sig->alg);
  268. UCHAR(rp->sig->labels);
  269. ULONG(rp->sig->ttl);
  270. ULONG(rp->sig->exp);
  271. ULONG(rp->sig->incep);
  272. USHORT(rp->sig->tag);
  273. NAME(rp->sig->signer->name);
  274. BYTES(rp->sig->data, rp->sig->dlen);
  275. break;
  276. case Tcert:
  277. USHORT(rp->cert->type);
  278. USHORT(rp->cert->tag);
  279. UCHAR(rp->cert->alg);
  280. BYTES(rp->cert->data, rp->cert->dlen);
  281. break;
  282. }
  283. /* stuff in the rdata section length */
  284. len = p - data;
  285. *lp++ = len >> 8;
  286. *lp = len;
  287. return p;
  288. }
  289. static uint8_t*
  290. convQ2M(RR *rp, uint8_t *p, uint8_t *ep, Dict *dp)
  291. {
  292. NAME(rp->owner->name);
  293. USHORT(rp->type);
  294. USHORT(rp->owner->class);
  295. return p;
  296. }
  297. static uint8_t*
  298. rrloop(RR *rp, int *countp, uint8_t *p, uint8_t *ep, Dict *dp, int quest)
  299. {
  300. uint8_t *np;
  301. *countp = 0;
  302. for(; rp && p < ep; rp = rp->next){
  303. if(quest)
  304. np = convQ2M(rp, p, ep, dp);
  305. else
  306. np = convRR2M(rp, p, ep, dp);
  307. if(np > ep)
  308. break;
  309. p = np;
  310. (*countp)++;
  311. }
  312. return p;
  313. }
  314. /*
  315. * convert into a message
  316. */
  317. int
  318. convDNS2M(DNSmsg *m, uint8_t *buf, int len)
  319. {
  320. uint32_t trunc = 0;
  321. uint8_t *p, *ep, *np;
  322. Dict d;
  323. d.n = 0;
  324. d.start = buf;
  325. d.ep = d.buf;
  326. memset(buf, 0, len);
  327. m->qdcount = m->ancount = m->nscount = m->arcount = 0;
  328. /* first pack in the RR's so we can get real counts */
  329. p = buf + 12;
  330. ep = buf + len;
  331. p = rrloop(m->qd, &m->qdcount, p, ep, &d, 1);
  332. p = rrloop(m->an, &m->ancount, p, ep, &d, 0);
  333. p = rrloop(m->ns, &m->nscount, p, ep, &d, 0);
  334. p = rrloop(m->ar, &m->arcount, p, ep, &d, 0);
  335. if(p > ep) {
  336. trunc = Ftrunc;
  337. dnslog("udp packet full; truncating my reply");
  338. p = ep;
  339. }
  340. /* now pack the rest */
  341. np = p;
  342. p = buf;
  343. ep = buf + len;
  344. USHORT(m->id);
  345. USHORT(m->flags | trunc);
  346. USHORT(m->qdcount);
  347. USHORT(m->ancount);
  348. USHORT(m->nscount);
  349. USHORT(m->arcount);
  350. USED(p);
  351. return np - buf;
  352. }