You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dnslib.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /**************************************
  2. * AUTHOR: Federico Tomassini *
  3. * Copyright (C) Federico Tomassini *
  4. * Contact effetom@gmail.com *
  5. ***********************************************
  6. ******* BEGIN 3/2006 ********
  7. *************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, *
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  17. * GNU General Public License for more details. *
  18. * *
  19. ************************************************************************/
  20. #define _GNU_SOURCE
  21. #include <string.h>
  22. #include "dnslib.h"
  23. #include "err_errno.h"
  24. #include "log.h"
  25. #include "xmalloc.h"
  26. /*
  27. * Takes a label: is there a ptr?
  28. * Returns:
  29. * -1 is a malformed label is found
  30. * 0 if there's no pointer
  31. * <offset from start_pkt> if a pointer is found
  32. */
  33. int
  34. getlblptr(char *buf)
  35. {
  36. uint16_t dlbl;
  37. char c[2];
  38. memcpy(c, buf, 2);
  39. if (!LBL_PTR(*c)) /* No ptr */
  40. return 0;
  41. if (LBL_PTR(*c) != LBL_PTR_MASK) {
  42. debug(DBG_INSANE, "In getlblptr: invalid octet %02x",
  43. (unsigned char) c[0]);
  44. err_ret(ERR_DNSMLO, -1);
  45. }
  46. (*c) &= LBL_PTR_OFF_MASK;
  47. memcpy(&dlbl, c, 2);
  48. dlbl = ntohs(dlbl);
  49. return dlbl; /* offset */
  50. }
  51. /*
  52. * Reads a contiguous octet-sequence-label.
  53. * Writes on dst.
  54. * There are two limits:
  55. * the name has to be less than MAX_SEQ_LBL_LEN
  56. * we must stay in pkt_len
  57. * -limit- is the less limit
  58. *
  59. * Returns:
  60. * -1 On error
  61. * Bytes readed if OK
  62. */
  63. int
  64. read_label_octet(const char *src, char *dst, int limit)
  65. {
  66. int how;
  67. how = *src++;
  68. if (how > limit || how > DNS_MAX_LABELS) {
  69. error("In read_label_octet: got %d with limti %d\n", how, limit);
  70. err_ret(ERR_DNSMSL, -1);
  71. }
  72. memcpy(dst, src, how);
  73. return how;
  74. }
  75. /*
  76. * Converts a dns compliant sequence label name to string.
  77. * we start to read at -buf-
  78. * we need start_pkt for pointers
  79. * we need limit to remain under pktlen
  80. * Returns:
  81. * Bytes readed if OK
  82. * -1 on error
  83. */
  84. int
  85. lbltoname(char *buf, char *start_pkt, char *dst, int limit)
  86. {
  87. char *crow;
  88. int how, recursion = 0;
  89. int ptr;
  90. int writed = 0, readed = 0;
  91. int new_limit = limit;
  92. crow = buf;
  93. while (*crow) {
  94. ptr = getlblptr(crow);
  95. if (ptr) { /* Got a pointer.... or got an error */
  96. if (ptr == -1) {
  97. debug(DBG_INSANE, err_str);
  98. err_ret(ERR_DNSMSL, -1);
  99. }
  100. if (++recursion > MAX_RECURSION_PTR)
  101. err_ret(ERR_DNSTRP, -1);
  102. if (recursion == 1)
  103. readed += 2; /* we read the pointer */
  104. crow = start_pkt + ptr;
  105. new_limit = limit - (int) (crow - buf);
  106. if (new_limit <= 0
  107. || new_limit > (int) (buf - start_pkt) + limit)
  108. err_ret(ERR_DNSPLB, -1);
  109. if (getlblptr(crow))
  110. err_ret(ERR_DNSPTP, -1);
  111. }
  112. how =
  113. read_label_octet(crow, dst,
  114. min(new_limit, DNS_MAX_HNAME_LEN - writed));
  115. if (how == -1) {
  116. debug(DBG_INSANE, err_str);
  117. err_ret(ERR_DNSMSL, -1);
  118. }
  119. if (!recursion)
  120. readed += how + 1;
  121. writed += how + 1;
  122. dst += how;
  123. crow += how + 1;
  124. *dst++ = (*crow) ? '.' : 0;
  125. }
  126. if (!recursion)
  127. readed++;
  128. return readed;
  129. }
  130. /*
  131. * DNS PTR query ask for 4.3.2.1.in-addr.arpa to know
  132. * who is 1.2.3.4.
  133. * This function reads this type of query transalting it
  134. * in the second form.
  135. * Writes result on *dst.
  136. * -1 on error.
  137. */
  138. int
  139. swap_straddr(char *src, char *dst)
  140. {
  141. char a[3];
  142. int i, slen;
  143. char *crow, *tmp, *atom;
  144. int count = 0, offset = 0;
  145. slen = strlen(src);
  146. if (slen > DNS_MAX_HNAME_LEN)
  147. goto mlf_addr;
  148. tmp = src;
  149. for (i = 0; i < 4; i++) {
  150. count = 0;
  151. atom = a;
  152. while (*tmp && *tmp != '.') {
  153. if (count > 2)
  154. goto mlf_addr;
  155. *atom++ = *tmp++;
  156. count++;
  157. }
  158. if (!count)
  159. goto mlf_addr;
  160. crow = dst + slen - count - offset;
  161. strncpy(crow, a, count);
  162. offset += count;
  163. if (!(*tmp))
  164. break;
  165. else {
  166. if (i == 3)
  167. goto mlf_addr;
  168. *(crow - 1) = '.';
  169. offset++;
  170. tmp++;
  171. }
  172. }
  173. *(dst + slen) = 0;
  174. return 0;
  175. mlf_addr:
  176. debug(DBG_INSANE, "in swap_straddr: invalid address `%s`.\n", src);
  177. err_ret(ERR_DNSMDD, -1);
  178. }
  179. int
  180. swap_straddr6(char *src, char *dst)
  181. {
  182. int slen;
  183. char *tmp;
  184. slen = strlen(src);
  185. tmp = src + slen - 1;
  186. while (tmp != src)
  187. *dst++ = *tmp--;
  188. *dst++ = *tmp;
  189. *dst = 0;
  190. return 0;
  191. }
  192. int
  193. rm_inv_prefix(char *src, char *dst)
  194. {
  195. char *temp;
  196. int ret;
  197. if (!src) {
  198. debug(DBG_INSANE, "In rm_inv_prefix: NULL argument!");
  199. err_ret(ERR_DNSMDD, -1);
  200. }
  201. if (!
  202. ((temp = (char *) strcasestr(src, DNS_INV_PREFIX)) ||
  203. (temp = (char *) strcasestr(src, DNS_INV_PREFIX6)) ||
  204. (temp = (char *) strcasestr(src, OLD_DNS_INV_PREFIX6)))) {
  205. debug(DBG_INSANE, "In rm_inv_prefix(): no suffix for PTR query.");
  206. err_ret(ERR_DNSMDD, -1);
  207. }
  208. if (temp - src >= DNS_MAX_HNAME_LEN) {
  209. error("In rm_inv_prefix(): name too long.");
  210. err_ret(ERR_DNSMDD, -1);
  211. }
  212. ret = strstr(temp, "6") ? AF_INET6 : AF_INET;
  213. strncpy(dst, src, temp - src);
  214. dst[temp - src] = 0;
  215. return ret;
  216. }
  217. int
  218. add_inv_prefix(char *s, int family)
  219. {
  220. int len;
  221. len = strlen(s);
  222. if (family == AF_INET)
  223. strcat(s, DNS_INV_PREFIX);
  224. else
  225. strcat(s, DNS_INV_PREFIX6);
  226. return 0;
  227. }
  228. int
  229. swapped_straddr(char *src, char *dst)
  230. {
  231. char temp[DNS_MAX_HNAME_LEN];
  232. int res;
  233. res = rm_inv_prefix(src, temp);
  234. if (res == -1) {
  235. error(err_str);
  236. err_ret(ERR_DNSMDD, -1);
  237. }
  238. if (res == AF_INET)
  239. res = swap_straddr(temp, dst);
  240. else
  241. res = swap_straddr6(temp, dst);
  242. if (res == -1) {
  243. error(err_str);
  244. err_ret(ERR_DNSMDD, -1);
  245. }
  246. return 0;
  247. }
  248. int
  249. swapped_straddr_pref(char *src, char *dst, int family)
  250. {
  251. int res;
  252. if (family == AF_INET)
  253. res = swap_straddr(src, dst);
  254. else
  255. res = swap_straddr6(src, dst);
  256. if (res == -1) {
  257. error(err_str);
  258. err_ret(ERR_DNSMDD, -1);
  259. }
  260. add_inv_prefix(dst, family);
  261. return 0;
  262. }
  263. /*
  264. * Converts a domain_name_string into a sequence label format,
  265. * dns compliant. Writes on dst.
  266. * -1 on error, number of bytes writed on success
  267. */
  268. int
  269. nametolbl(char *name, char *dst)
  270. {
  271. char *crow;
  272. int offset = 0, res;
  273. if (strlen(name) > DNS_MAX_HNAME_LEN) {
  274. debug(DBG_INSANE, "Malformed name: %s.", name);
  275. err_ret(ERR_DNSMDA, -1);
  276. }
  277. while ((crow = strstr(name + 1, "."))) {
  278. res = crow - name;
  279. if (res > DNS_MAX_LABELS) {
  280. debug(DBG_INSANE, "Malformed name: %s.", name);
  281. err_ret(ERR_DNSMDA, -1);
  282. }
  283. *dst = (char) res; /* write the octet length */
  284. dst++;
  285. offset++;
  286. memcpy(dst, name, (size_t) res); /* write label */
  287. name += res + 1;
  288. dst += res;
  289. offset += res; /* shift ptrs */
  290. }
  291. if (!name)
  292. return offset;
  293. if ((res = (char) strlen(name)) > DNS_MAX_LABELS) {
  294. debug(DBG_INSANE, "Malformed name: %s", name);
  295. err_ret(ERR_DNSMDA, -1);
  296. }
  297. *dst++ = (char) res;
  298. strcpy(dst, name);
  299. offset += res + 2;
  300. return offset;
  301. }
  302. /*
  303. * Disassembles DNS packet headers, writing a yet allocated
  304. * dns_pkt_hdr struct.
  305. * No controls on len, bcz <<--the min_pkt_len is controlled
  306. * by recv.-->>
  307. * Returns the number of bytes readed (always DNS_HDR_SZ).
  308. */
  309. int
  310. d_hdr_u(char *buf, dns_pkt_hdr * dph)
  311. {
  312. uint8_t c;
  313. uint16_t s;
  314. // ROW 1
  315. memcpy(&s, buf, sizeof(uint16_t));
  316. dph->id = ntohs(s);
  317. // ROW 2
  318. buf += 2;
  319. memcpy(&c, buf, sizeof(uint8_t));
  320. dph->qr = (c >> 7) & 0x01;
  321. dph->opcode = (c >> 3) & 0x0f;
  322. dph->aa = (c >> 2) & 0x01;
  323. dph->tc = (c >> 1) & 0x01;
  324. dph->rd = c & 0x01;
  325. buf++;
  326. memcpy(&c, buf, sizeof(uint8_t));
  327. dph->ra = (c >> 7) & 0x01;
  328. dph->z = (c >> 4) & 0x07;
  329. dph->rcode = c & 0x0f;
  330. // ROW 3
  331. buf++;
  332. memcpy(&s, buf, sizeof(uint16_t));
  333. dph->qdcount = ntohs(s);
  334. // ROW 4
  335. buf += 2;
  336. memcpy(&s, buf, sizeof(uint16_t));
  337. dph->ancount = ntohs(s);
  338. // ROW 5
  339. buf += 2;
  340. memcpy(&s, buf, sizeof(uint16_t));
  341. dph->nscount = ntohs(s);
  342. // ROW 6
  343. buf += 2;
  344. memcpy(&s, buf, sizeof(uint16_t));
  345. dph->arcount = ntohs(s);
  346. buf += 2;
  347. return DNS_HDR_SZ; // i.e. 12 :)
  348. }
  349. /*
  350. * This function alloc a new dns_pkt_qst to store a dns_question_section.
  351. * The new dns_pkt_qst is also added to the principal dp-struct
  352. * Returns bytes readed if OK. -1 otherwise.
  353. */
  354. int
  355. d_qst_u(char *start_buf, char *buf, dns_pkt * dp, int limit_len)
  356. {
  357. int count;
  358. uint16_t s;
  359. dns_pkt_qst *dpq;
  360. dpq = dns_add_qst(dp);
  361. /* get name */
  362. if ((count = lbltoname(buf, start_buf, dpq->qname, limit_len)) == -1) {
  363. error(err_str);
  364. err_ret(ERR_DNSMDD, 1);
  365. }
  366. buf += count;
  367. /* Now we have to write 2+2 bytes */
  368. if (count + 4 > limit_len)
  369. err_ret(ERR_DNSPLB, 1);
  370. /* shift to type and class */
  371. memcpy(&s, buf, 2);
  372. dpq->qtype = ntohs(s);
  373. count += 2;
  374. buf += 2;
  375. memcpy(&s, buf, 2);
  376. dpq->qclass = ntohs(s);
  377. count += 2;
  378. return count;
  379. }
  380. /*
  381. * Disassembles a DNS qst_section_set.
  382. * Use the above function for each question section.
  383. * -1 on error. Number of bytes readed on success.
  384. * If -1 is returned, rcode ha sto be set to E_INTRPRT
  385. */
  386. int
  387. d_qsts_u(char *start_buf, char *buf, dns_pkt * dp, int limit_len)
  388. {
  389. int offset = 0, res;
  390. int i, count;
  391. if (!(count = DP_QDCOUNT(dp)))
  392. return 0; /* No questions. */
  393. for (i = 0; i < count; i++) {
  394. if ((res =
  395. d_qst_u(start_buf, buf + offset, dp,
  396. limit_len - offset)) == -1) {
  397. error(err_str);
  398. err_ret(ERR_DNSMDD, -1);
  399. }
  400. offset += res;
  401. }
  402. return offset;
  403. }
  404. /*
  405. * The behavior of this function is in all similar to dpkttoqst.
  406. * Returns -1 on error. Bytes readed otherwise.
  407. */
  408. int
  409. d_a_u(char *start_buf, char *buf, dns_pkt_a ** dpa_orig, int limit_len)
  410. {
  411. int count, rdlen;
  412. dns_pkt_a *dpa;
  413. uint16_t s;
  414. uint32_t ui;
  415. dpa = dns_add_a(dpa_orig);
  416. /* get name */
  417. if ((count = lbltoname(buf, start_buf, dpa->name, limit_len)) == -1) {
  418. error(err_str);
  419. err_ret(ERR_DNSMDD, -1);
  420. }
  421. buf += count;
  422. /* Now we have to write 2+2+4+2 bytes */
  423. if (count + 10 > limit_len)
  424. err_ret(ERR_DNSPLB, -1);
  425. memcpy(&s, buf, 2);
  426. dpa->type = ntohs(s);
  427. count += 2;
  428. buf += 2;
  429. memcpy(&s, buf, 2);
  430. dpa->cl = ntohs(s);
  431. count += 2;
  432. buf += 2;
  433. memcpy(&ui, buf, 4);
  434. dpa->ttl = ntohl(ui);
  435. count += 4;
  436. buf += 4;
  437. memcpy(&s, buf, 2);
  438. dpa->rdlength = ntohs(s);
  439. count += 2;
  440. buf += 2;
  441. rdlen = dpa->rdlength;
  442. if (rdlen > DNS_MAX_HNAME_LEN)
  443. err_ret(ERR_DNSMDD, -1);
  444. /* Now we have to write dpa->rdlength bytes */
  445. if (count + rdlen > limit_len)
  446. err_ret(ERR_DNSPLB, -1);
  447. if (dpa->type == T_A) {
  448. memcpy(dpa->rdata, buf, rdlen); /* 32bit address */
  449. count += rdlen;
  450. } else if (dpa->type == T_MX) {
  451. memcpy(dpa->rdata, buf, 2);
  452. if ((ui =
  453. lbltoname(buf + 2, start_buf, dpa->rdata + 2,
  454. rdlen - 2)) == -1) {
  455. error(err_str);
  456. err_ret(ERR_DNSMDD, -1);
  457. }
  458. if (rdlen != ui + 2) {
  459. debug(DBG_NORMAL,
  460. "In d_a_u(): rdlen (%d) differs from readed bytes (%d).",
  461. rdlen, ui + 2);
  462. err_ret(ERR_DNSMDD, -1);
  463. }
  464. count += 2 + ui;
  465. } else {
  466. if ((ui = lbltoname(buf, start_buf, dpa->rdata, rdlen)) == -1) {
  467. error(err_str);
  468. err_intret(ERR_DNSMDD);
  469. }
  470. if (rdlen != ui) {
  471. debug(DBG_NORMAL,
  472. "In d_a_u(): rdlen (%d) differs from readed bytes (%d).",
  473. rdlen, ui);
  474. err_ret(ERR_DNSMDD, -1);
  475. }
  476. count += ui;
  477. }
  478. return count;
  479. }
  480. /*
  481. * like d_qs_u. count is the number of section to read.
  482. * -1 on error. Bytes readed otherwise.
  483. */
  484. int
  485. d_as_u(char *start_buf, char *buf, dns_pkt_a ** dpa, int limit_len,
  486. int count)
  487. {
  488. int offset = 0, res;
  489. int i;
  490. if (!count)
  491. return 0;
  492. for (i = 0; i < count; i++) {
  493. if ((res =
  494. d_a_u(start_buf, buf + offset, dpa,
  495. limit_len - offset)) == -1) {
  496. error(err_str);
  497. err_intret(ERR_DNSMDD);
  498. }
  499. offset += res;
  500. }
  501. return offset;
  502. }
  503. /*
  504. * This is a main function: takes the pkt-buf and translate
  505. * it in structured data.
  506. * It cares about dns_pkt allocations.
  507. *
  508. * Returns:
  509. * -1 on E_INTRPRT
  510. * 0 if pkt must be discarded.
  511. * Number of bytes readed otherwise
  512. */
  513. int
  514. d_u(char *buf, int pktlen, dns_pkt ** dpp)
  515. {
  516. dns_pkt *dp;
  517. int offset = 0, res;
  518. char *crow;
  519. crow = buf;
  520. /* Controls pkt consistency: we must at least read pkt headers */
  521. if (pktlen < DNS_HDR_SZ)
  522. err_ret(ERR_DNSMDP, 0);
  523. *dpp = dp = create_dns_pkt();
  524. /* Writes headers */
  525. offset += d_hdr_u(buf, &(dp->pkt_hdr));
  526. if (pktlen > DNS_MAX_SZ) /* If pkt is too long: the headers are written,
  527. * so we can reply with E_INTRPRT
  528. */
  529. err_intret(ERR_DNSPLB);
  530. crow += offset;
  531. /* Writes qsts */
  532. if (dp->pkt_hdr.qdcount) {
  533. if ((res = d_qsts_u(buf, crow, dp, pktlen - offset)) == -1) {
  534. error(err_str);
  535. err_intret(ERR_DNSMDP);
  536. }
  537. offset += res;
  538. crow += res;
  539. }
  540. if (dp->pkt_hdr.ancount) {
  541. if ((res =
  542. d_as_u(buf, crow, &(dp->pkt_answ), pktlen - offset,
  543. DP_ANCOUNT(dp))) == -1) {
  544. error(err_str);
  545. err_intret(ERR_DNSMDP);
  546. }
  547. offset += res;
  548. }
  549. /*crow+=res;
  550. if ((res=dpkttoas(buf,crow,&(dp->pkt_auth),pktlen-offset,DP_NSCOUNT(dp)))==-1)
  551. return -1;
  552. offset+=res;
  553. crow+=res;
  554. if ((res=dpkttoas(buf,crow,&(dp->pkt_add),pktlen-offset,DP_ARCOUNT(dp)))==-1)
  555. return -1; */
  556. return offset;
  557. }
  558. /*
  559. * This function is the d_hdr_u inverse.
  560. * Takes a dns_pkt struct and builds the
  561. * header pkt-buffer
  562. * Returns the number of bytes writed.
  563. */
  564. int
  565. d_hdr_p(dns_pkt * dp, char *buf)
  566. {
  567. char *crow = buf;
  568. uint16_t u;
  569. dns_pkt_hdr *dph;
  570. dph = &(dp->pkt_hdr);
  571. u = htons(dph->id);
  572. memcpy(buf, &u, 2);
  573. buf += 2;
  574. if (dph->qr)
  575. *buf |= 0x80;
  576. *buf |= dph->opcode << 3;
  577. *buf |= dph->aa << 2;
  578. *buf |= dph->tc << 1;
  579. *buf |= dph->rd;
  580. buf++;
  581. *buf |= dph->ra << 7;
  582. *buf |= dph->z << 4;
  583. *buf |= dph->rcode;
  584. buf++;
  585. u = htons(dph->qdcount);
  586. memcpy(buf, &u, 2);
  587. buf += 2;
  588. u = htons(dph->ancount);
  589. memcpy(buf, &u, 2);
  590. buf += 2;
  591. u = htons(dph->nscount);
  592. memcpy(buf, &u, 2);
  593. buf += 2;
  594. u = htons(dph->arcount);
  595. memcpy(buf, &u, 2);
  596. buf += 2;
  597. return (int) (buf - crow);
  598. }
  599. /*
  600. * Translate a struct dns_pkt_qst in the dns-buffer buf.
  601. * Returns:
  602. * -1 On error
  603. * Bytes writed otherwise.
  604. */
  605. int
  606. d_qst_p(dns_pkt_qst * dpq, char *buf, int limitlen)
  607. {
  608. int offset;
  609. uint16_t u;
  610. if ((offset = nametolbl(dpq->qname, buf)) == -1) {
  611. error(err_str);
  612. err_ret(ERR_DNSMDA, -1);
  613. }
  614. if (offset + 4 > limitlen)
  615. err_ret(ERR_DNSPLB, -1);
  616. buf += offset;
  617. u = htons(dpq->qtype);
  618. memcpy(buf, &u, 2);
  619. buf += 2;
  620. offset += 2;
  621. u = htons(dpq->qclass);
  622. memcpy(buf, &u, 2);
  623. buf += 2;
  624. offset += 2;
  625. return offset;
  626. }
  627. /*
  628. * Translates the question sections of a struct dns_pkt
  629. * into buf.
  630. * Returns:
  631. * -1 on error.
  632. * Number of bytes writed otherwise,
  633. */
  634. int
  635. d_qsts_p(dns_pkt * dp, char *buf, int limitlen)
  636. {
  637. int offset = 0, res;
  638. int i;
  639. dns_pkt_qst *dpq;
  640. dpq = dp->pkt_qst;
  641. for (i = 0; dpq && i < DP_QDCOUNT(dp); i++) {
  642. if ((res = d_qst_p(dpq, buf + offset, limitlen - offset)) == -1) {
  643. error(err_str);
  644. err_ret(ERR_DNSMDA, -1);
  645. }
  646. offset += res;
  647. dpq = dpq->next;
  648. }
  649. return offset;
  650. }
  651. int
  652. d_a_p(dns_pkt_a * dpa, char *buf, int limitlen)
  653. {
  654. int offset, rdlen;
  655. uint16_t u;
  656. int i;
  657. if ((rdlen = nametolbl(dpa->name, buf)) == -1)
  658. return -1;
  659. offset = rdlen;
  660. if (offset + 10 > limitlen)
  661. err_intret(ERR_DNSPLB);
  662. buf += offset;
  663. u = htons(dpa->type);
  664. memcpy(buf, &u, 2);
  665. buf += 2;
  666. offset += 2;
  667. u = htons(dpa->cl);
  668. memcpy(buf, &u, 2);
  669. buf += 2;
  670. offset += 2;
  671. i = htonl(dpa->ttl);
  672. memcpy(buf, &i, 4);
  673. buf += 4;
  674. offset += 4;
  675. if (dpa->type == T_A) {
  676. if (offset + dpa->rdlength > limitlen)
  677. err_intret(ERR_DNSPLB);
  678. memcpy(buf + 2, dpa->rdata, dpa->rdlength);
  679. offset += dpa->rdlength;
  680. } else if (dpa->type == T_MX) {
  681. memcpy(buf + 2, dpa->rdata, 2);
  682. if ((rdlen = nametolbl(dpa->rdata + 2, buf + 4)) == -1) {
  683. error(err_str);
  684. err_ret(ERR_DNSMDA, -1);
  685. }
  686. offset += rdlen + 2;
  687. if (offset > limitlen)
  688. err_ret(ERR_DNSPLB, -1);
  689. dpa->rdlength = rdlen + 2;
  690. } else {
  691. if ((rdlen = nametolbl(dpa->rdata, buf + 2)) == -1) {
  692. error(err_str);
  693. err_ret(ERR_DNSMDA, -1);
  694. }
  695. offset += rdlen;
  696. if (offset > limitlen)
  697. err_ret(ERR_DNSPLB, -1);
  698. dpa->rdlength = rdlen;
  699. }
  700. u = htons(dpa->rdlength);
  701. memcpy(buf, &u, 2);
  702. offset += 2;
  703. return offset;
  704. }
  705. int
  706. d_as_p(dns_pkt_a * dpa, char *buf, int limitlen, int count)
  707. {
  708. int offset = 0, res;
  709. int i;
  710. for (i = 0; dpa && i < count; i++) {
  711. if ((res = d_a_p(dpa, buf + offset, limitlen - offset)) == -1) {
  712. error(err_str);
  713. err_ret(ERR_DNSMDA, -1);
  714. }
  715. offset += res;
  716. dpa = dpa->next;
  717. }
  718. return offset;
  719. }
  720. /*
  721. * Transform a dns_pkt structure in char stream.
  722. *
  723. * Returns:
  724. * -1 on error
  725. * len(stream) if OK
  726. *
  727. * The stream has at least the header section writed.
  728. * `buf' must be at least of DNS_MAX_SZ bytes.
  729. *
  730. * DANGER: This function realeses *ALWAYS* the dns_pkt *dp!!!!
  731. */
  732. int
  733. d_p(dns_pkt * dp, char *buf)
  734. {
  735. int offset, res;
  736. memset(buf, 0, DNS_MAX_SZ);
  737. offset = d_hdr_p(dp, buf);
  738. buf += offset;
  739. if ((res = d_qsts_p(dp, buf, DNS_MAX_SZ - offset)) == -1)
  740. goto server_fail;
  741. offset += res;
  742. buf += res;
  743. if ((res =
  744. d_as_p(dp->pkt_answ, buf, DNS_MAX_SZ - offset,
  745. DP_ANCOUNT(dp))) == -1)
  746. goto server_fail;
  747. offset += res;
  748. /*buf+=res;
  749. if ( (res=astodpkt(dp->pkt_auth,buf,DNS_MAX_SZ-offset,DP_NSCOUNT(dp)))==-1)
  750. goto server_fail;
  751. offset+=res;
  752. buf+=res; */
  753. /*if ( (res=astodpkt(dp->pkt_add,buf,DNS_MAX_SZ-offset,DP_ARCOUNT(dp)))==-1)
  754. goto server_fail;
  755. offset+=res; */
  756. destroy_dns_pkt(dp);
  757. return offset;
  758. server_fail:
  759. error(err_str);
  760. destroy_dns_pkt(dp);
  761. err_ret(ERR_DNSPDS, -1);
  762. }
  763. /* Memory Functions */
  764. dns_pkt *
  765. create_dns_pkt(void)
  766. {
  767. dns_pkt *dp;
  768. dp = xmalloc(DNS_PKT_SZ);
  769. memset(dp, 0, DNS_PKT_SZ);
  770. dp->pkt_qst = NULL;
  771. dp->pkt_answ = NULL;
  772. dp->pkt_add = NULL;
  773. dp->pkt_auth = NULL;
  774. return dp;
  775. }
  776. dns_pkt_qst *
  777. create_dns_pkt_qst(void)
  778. {
  779. dns_pkt_qst *dpq;
  780. dpq = xmalloc(DNS_PKT_QST_SZ);
  781. dpq->next = NULL;
  782. memset(dpq->qname, 0, DNS_MAX_HNAME_LEN);
  783. return dpq;
  784. }
  785. dns_pkt_a *
  786. create_dns_pkt_a(void)
  787. {
  788. dns_pkt_a *dpa;
  789. dpa = xmalloc(DNS_PKT_A_SZ);
  790. memset(dpa->name, 0, DNS_MAX_HNAME_LEN);
  791. memset(dpa->rdata, 0, DNS_MAX_HNAME_LEN);
  792. dpa->next = NULL;
  793. return dpa;
  794. }
  795. dns_pkt_qst *
  796. dns_add_qst(dns_pkt * dp)
  797. {
  798. dns_pkt_qst *dpq, *temp;
  799. dpq = create_dns_pkt_qst();
  800. temp = dp->pkt_qst;
  801. if (!temp) {
  802. dp->pkt_qst = dpq;
  803. return dpq;
  804. }
  805. while (temp->next)
  806. temp = temp->next;
  807. temp->next = dpq;
  808. return dpq;
  809. }
  810. void
  811. dns_del_last_qst(dns_pkt * dp)
  812. {
  813. dns_pkt_qst *dpq = dp->pkt_qst;
  814. if (!dpq)
  815. return;
  816. if (!(dpq->next)) {
  817. xfree(dpq);
  818. dp->pkt_qst = NULL;
  819. return;
  820. }
  821. while ((dpq->next)->next);
  822. xfree(dpq->next);
  823. dpq->next = NULL;
  824. return;
  825. }
  826. dns_pkt_a *
  827. dns_add_a(dns_pkt_a ** dpa)
  828. {
  829. dns_pkt_a *dpa_add, *a;
  830. int count = 0;
  831. a = *dpa;
  832. dpa_add = create_dns_pkt_a();
  833. if (!a) {
  834. (*dpa) = dpa_add;
  835. } else {
  836. while (a->next) {
  837. a = a->next;
  838. count++;
  839. }
  840. a->next = dpa_add;
  841. }
  842. return dpa_add;
  843. }
  844. void
  845. dns_a_default_fill(dns_pkt * dp, dns_pkt_a * dpa)
  846. {
  847. strcpy(dpa->name, dp->pkt_qst->qname);
  848. dpa->cl = C_IN;
  849. dpa->ttl = DNS_TTL;
  850. dpa->type = dp->pkt_qst->qtype;
  851. }
  852. void
  853. destroy_dns_pkt(dns_pkt * dp)
  854. {
  855. dns_pkt_a *dpa, *dpa_t;
  856. dns_pkt_qst *dpq, *dpq_t;
  857. if (dp->pkt_qst) {
  858. dpq = dp->pkt_qst;
  859. while (dpq) {
  860. dpq_t = dpq->next;
  861. xfree(dpq);
  862. dpq = dpq_t;
  863. }
  864. }
  865. if (dp->pkt_answ) {
  866. dpa = dp->pkt_answ;
  867. while (dpa) {
  868. dpa_t = dpa->next;
  869. xfree(dpa);
  870. dpa = dpa_t;
  871. }
  872. }
  873. if (dp->pkt_add) {
  874. dpa = dp->pkt_add;
  875. while (dpa) {
  876. dpa_t = dpa->next;
  877. xfree(dpa);
  878. dpa = dpa_t;
  879. }
  880. }
  881. if (dp->pkt_auth) {
  882. dpa = dp->pkt_auth;
  883. while (dpa) {
  884. dpa_t = dpa->next;
  885. xfree(dpa);
  886. dpa = dpa_t;
  887. }
  888. }
  889. xfree(dp);
  890. return;
  891. }