dnsparser.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2010-2014, 2018 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/dnsparser.c
  18. * @brief helper library to parse DNS packets.
  19. * @author Philipp Toelke
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #if HAVE_LIBIDN2
  24. #if HAVE_IDN2_H
  25. #include <idn2.h>
  26. #elif HAVE_IDN2_IDN2_H
  27. #include <idn2/idn2.h>
  28. #endif
  29. #elif HAVE_LIBIDN
  30. #if HAVE_IDNA_H
  31. #include <idna.h>
  32. #elif HAVE_IDN_IDNA_H
  33. #include <idn/idna.h>
  34. #endif
  35. #endif
  36. #include "gnunet_util_lib.h"
  37. /**
  38. * Check if a label in UTF-8 format can be coded into valid IDNA.
  39. * This can fail if the ASCII-conversion becomes longer than 63 characters.
  40. *
  41. * @param label label to check (UTF-8 string)
  42. * @return #GNUNET_OK if the label can be converted to IDNA,
  43. * #GNUNET_SYSERR if the label is not valid for DNS names
  44. */
  45. int
  46. GNUNET_DNSPARSER_check_label (const char *label)
  47. {
  48. char *output;
  49. size_t slen;
  50. if (NULL != strchr (label, '.'))
  51. return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
  52. if (0 == strcmp (label, "@")) /* '@' is reserved for the empty label, see #GNUNET_GNS_EMPTY_LABEL_AT */
  53. return GNUNET_SYSERR;
  54. if (IDNA_SUCCESS != idna_to_ascii_8z (label, &output, IDNA_ALLOW_UNASSIGNED))
  55. return GNUNET_SYSERR;
  56. slen = strlen (output);
  57. free (output);
  58. return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
  59. }
  60. /**
  61. * Check if a label in UTF-8 format can be coded into valid IDNA.
  62. * This can fail if the ASCII-conversion becomes longer than 253 characters.
  63. *
  64. * @param name name to check (UTF-8 string)
  65. * @return #GNUNET_OK if the label can be converted to IDNA,
  66. * #GNUNET_SYSERR if the label is not valid for DNS names
  67. */
  68. int
  69. GNUNET_DNSPARSER_check_name (const char *name)
  70. {
  71. char *ldup;
  72. char *output;
  73. size_t slen;
  74. char *tok;
  75. ldup = GNUNET_strdup (name);
  76. for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
  77. if (GNUNET_OK != GNUNET_DNSPARSER_check_label (tok))
  78. {
  79. GNUNET_free (ldup);
  80. return GNUNET_SYSERR;
  81. }
  82. GNUNET_free (ldup);
  83. if (IDNA_SUCCESS != idna_to_ascii_8z (name, &output, IDNA_ALLOW_UNASSIGNED))
  84. return GNUNET_SYSERR;
  85. slen = strlen (output);
  86. free (output);
  87. return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
  88. }
  89. /**
  90. * Free SOA information record.
  91. *
  92. * @param soa record to free
  93. */
  94. void
  95. GNUNET_DNSPARSER_free_soa (struct GNUNET_DNSPARSER_SoaRecord *soa)
  96. {
  97. if (NULL == soa)
  98. return;
  99. GNUNET_free (soa->mname);
  100. GNUNET_free (soa->rname);
  101. GNUNET_free (soa);
  102. }
  103. /**
  104. * Free CERT information record.
  105. *
  106. * @param cert record to free
  107. */
  108. void
  109. GNUNET_DNSPARSER_free_cert (struct GNUNET_DNSPARSER_CertRecord *cert)
  110. {
  111. if (NULL == cert)
  112. return;
  113. GNUNET_free (cert->certificate_data);
  114. GNUNET_free (cert);
  115. }
  116. /**
  117. * Free SRV information record.
  118. *
  119. * @param srv record to free
  120. */
  121. void
  122. GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
  123. {
  124. if (NULL == srv)
  125. return;
  126. GNUNET_free (srv->target);
  127. GNUNET_free (srv);
  128. }
  129. /**
  130. * Free MX information record.
  131. *
  132. * @param mx record to free
  133. */
  134. void
  135. GNUNET_DNSPARSER_free_mx (struct GNUNET_DNSPARSER_MxRecord *mx)
  136. {
  137. if (NULL == mx)
  138. return;
  139. GNUNET_free (mx->mxhost);
  140. GNUNET_free (mx);
  141. }
  142. /**
  143. * Free the given DNS record.
  144. *
  145. * @param r record to free
  146. */
  147. void
  148. GNUNET_DNSPARSER_free_record (struct GNUNET_DNSPARSER_Record *r)
  149. {
  150. GNUNET_free (r->name);
  151. switch (r->type)
  152. {
  153. case GNUNET_DNSPARSER_TYPE_MX:
  154. GNUNET_DNSPARSER_free_mx (r->data.mx);
  155. break;
  156. case GNUNET_DNSPARSER_TYPE_SOA:
  157. GNUNET_DNSPARSER_free_soa (r->data.soa);
  158. break;
  159. case GNUNET_DNSPARSER_TYPE_SRV:
  160. GNUNET_DNSPARSER_free_srv (r->data.srv);
  161. break;
  162. case GNUNET_DNSPARSER_TYPE_CERT:
  163. GNUNET_DNSPARSER_free_cert (r->data.cert);
  164. break;
  165. case GNUNET_DNSPARSER_TYPE_NS:
  166. case GNUNET_DNSPARSER_TYPE_CNAME:
  167. case GNUNET_DNSPARSER_TYPE_PTR:
  168. GNUNET_free (r->data.hostname);
  169. break;
  170. default:
  171. GNUNET_free (r->data.raw.data);
  172. break;
  173. }
  174. }
  175. /**
  176. * Parse name inside of a DNS query or record.
  177. *
  178. * @param udp_payload entire UDP payload
  179. * @param udp_payload_length length of @a udp_payload
  180. * @param off pointer to the offset of the name to parse in the udp_payload (to be
  181. * incremented by the size of the name)
  182. * @param depth current depth of our recursion (to prevent stack overflow)
  183. * @return name as 0-terminated C string on success, NULL if the payload is malformed
  184. */
  185. static char *
  186. parse_name (const char *udp_payload,
  187. size_t udp_payload_length,
  188. size_t *off,
  189. unsigned int depth)
  190. {
  191. const uint8_t *input = (const uint8_t *) udp_payload;
  192. char *ret;
  193. char *tmp;
  194. char *xstr;
  195. uint8_t len;
  196. size_t xoff;
  197. char *utf8;
  198. Idna_rc rc;
  199. ret = GNUNET_strdup ("");
  200. while (1)
  201. {
  202. if (*off >= udp_payload_length)
  203. {
  204. GNUNET_break_op (0);
  205. goto error;
  206. }
  207. len = input[*off];
  208. if (0 == len)
  209. {
  210. (*off)++;
  211. break;
  212. }
  213. if (len < 64)
  214. {
  215. if (*off + 1 + len > udp_payload_length)
  216. {
  217. GNUNET_break_op (0);
  218. goto error;
  219. }
  220. GNUNET_asprintf (&tmp, "%.*s", (int) len, &udp_payload[*off + 1]);
  221. if (IDNA_SUCCESS !=
  222. (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_ALLOW_UNASSIGNED)))
  223. {
  224. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  225. _ ("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
  226. tmp,
  227. idna_strerror (rc));
  228. GNUNET_free (tmp);
  229. GNUNET_asprintf (&tmp,
  230. "%s%.*s.",
  231. ret,
  232. (int) len,
  233. &udp_payload[*off + 1]);
  234. }
  235. else
  236. {
  237. GNUNET_free (tmp);
  238. GNUNET_asprintf (&tmp, "%s%s.", ret, utf8);
  239. free (utf8);
  240. }
  241. GNUNET_free (ret);
  242. ret = tmp;
  243. *off += 1 + len;
  244. }
  245. else if ((64 | 128) == (len & (64 | 128)))
  246. {
  247. if (depth > 32)
  248. {
  249. GNUNET_break_op (0);
  250. goto error; /* hard bound on stack to prevent "infinite" recursion, disallow! */
  251. }
  252. /* pointer to string */
  253. if (*off + 1 > udp_payload_length)
  254. {
  255. GNUNET_break_op (0);
  256. goto error;
  257. }
  258. xoff = ((len - (64 | 128)) << 8) + input[*off + 1];
  259. xstr = parse_name (udp_payload, udp_payload_length, &xoff, depth + 1);
  260. if (NULL == xstr)
  261. {
  262. GNUNET_break_op (0);
  263. goto error;
  264. }
  265. GNUNET_asprintf (&tmp, "%s%s.", ret, xstr);
  266. GNUNET_free (ret);
  267. GNUNET_free (xstr);
  268. ret = tmp;
  269. if (strlen (ret) > udp_payload_length)
  270. {
  271. GNUNET_break_op (0);
  272. goto error; /* we are looping (building an infinite string) */
  273. }
  274. *off += 2;
  275. /* pointers always terminate names */
  276. break;
  277. }
  278. else
  279. {
  280. /* neither pointer nor inline string, not supported... */
  281. GNUNET_break_op (0);
  282. goto error;
  283. }
  284. }
  285. if (0 < strlen (ret))
  286. ret[strlen (ret) - 1] = '\0'; /* eat tailing '.' */
  287. return ret;
  288. error:
  289. GNUNET_break_op (0);
  290. GNUNET_free (ret);
  291. return NULL;
  292. }
  293. /**
  294. * Parse name inside of a DNS query or record.
  295. *
  296. * @param udp_payload entire UDP payload
  297. * @param udp_payload_length length of @a udp_payload
  298. * @param off pointer to the offset of the name to parse in the udp_payload (to be
  299. * incremented by the size of the name)
  300. * @return name as 0-terminated C string on success, NULL if the payload is malformed
  301. */
  302. char *
  303. GNUNET_DNSPARSER_parse_name (const char *udp_payload,
  304. size_t udp_payload_length,
  305. size_t *off)
  306. {
  307. return parse_name (udp_payload, udp_payload_length, off, 0);
  308. }
  309. /**
  310. * Parse a DNS query entry.
  311. *
  312. * @param udp_payload entire UDP payload
  313. * @param udp_payload_length length of @a udp_payload
  314. * @param off pointer to the offset of the query to parse in the udp_payload (to be
  315. * incremented by the size of the query)
  316. * @param q where to write the query information
  317. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the query is malformed
  318. */
  319. int
  320. GNUNET_DNSPARSER_parse_query (const char *udp_payload,
  321. size_t udp_payload_length,
  322. size_t *off,
  323. struct GNUNET_DNSPARSER_Query *q)
  324. {
  325. char *name;
  326. struct GNUNET_TUN_DnsQueryLine ql;
  327. name = GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
  328. if (NULL == name)
  329. {
  330. GNUNET_break_op (0);
  331. return GNUNET_SYSERR;
  332. }
  333. q->name = name;
  334. if (*off + sizeof(struct GNUNET_TUN_DnsQueryLine) > udp_payload_length)
  335. {
  336. GNUNET_break_op (0);
  337. return GNUNET_SYSERR;
  338. }
  339. GNUNET_memcpy (&ql, &udp_payload[*off], sizeof(ql));
  340. *off += sizeof(ql);
  341. q->type = ntohs (ql.type);
  342. q->dns_traffic_class = ntohs (ql.dns_traffic_class);
  343. return GNUNET_OK;
  344. }
  345. /**
  346. * Parse a DNS SOA record.
  347. *
  348. * @param udp_payload reference to UDP packet
  349. * @param udp_payload_length length of @a udp_payload
  350. * @param off pointer to the offset of the query to parse in the SOA record (to be
  351. * incremented by the size of the record), unchanged on error
  352. * @return the parsed SOA record, NULL on error
  353. */
  354. struct GNUNET_DNSPARSER_SoaRecord *
  355. GNUNET_DNSPARSER_parse_soa (const char *udp_payload,
  356. size_t udp_payload_length,
  357. size_t *off)
  358. {
  359. struct GNUNET_DNSPARSER_SoaRecord *soa;
  360. struct GNUNET_TUN_DnsSoaRecord soa_bin;
  361. size_t old_off;
  362. old_off = *off;
  363. soa = GNUNET_new (struct GNUNET_DNSPARSER_SoaRecord);
  364. soa->mname =
  365. GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
  366. soa->rname =
  367. GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
  368. if ((NULL == soa->mname) || (NULL == soa->rname) ||
  369. (*off + sizeof(struct GNUNET_TUN_DnsSoaRecord) > udp_payload_length))
  370. {
  371. GNUNET_break_op (0);
  372. GNUNET_DNSPARSER_free_soa (soa);
  373. *off = old_off;
  374. return NULL;
  375. }
  376. GNUNET_memcpy (&soa_bin,
  377. &udp_payload[*off],
  378. sizeof(struct GNUNET_TUN_DnsSoaRecord));
  379. soa->serial = ntohl (soa_bin.serial);
  380. soa->refresh = ntohl (soa_bin.refresh);
  381. soa->retry = ntohl (soa_bin.retry);
  382. soa->expire = ntohl (soa_bin.expire);
  383. soa->minimum_ttl = ntohl (soa_bin.minimum);
  384. (*off) += sizeof(struct GNUNET_TUN_DnsSoaRecord);
  385. return soa;
  386. }
  387. /**
  388. * Parse a DNS MX record.
  389. *
  390. * @param udp_payload reference to UDP packet
  391. * @param udp_payload_length length of @a udp_payload
  392. * @param off pointer to the offset of the query to parse in the MX record (to be
  393. * incremented by the size of the record), unchanged on error
  394. * @return the parsed MX record, NULL on error
  395. */
  396. struct GNUNET_DNSPARSER_MxRecord *
  397. GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
  398. size_t udp_payload_length,
  399. size_t *off)
  400. {
  401. struct GNUNET_DNSPARSER_MxRecord *mx;
  402. uint16_t mxpref;
  403. size_t old_off;
  404. old_off = *off;
  405. if (*off + sizeof(uint16_t) > udp_payload_length)
  406. {
  407. GNUNET_break_op (0);
  408. return NULL;
  409. }
  410. GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof(uint16_t));
  411. (*off) += sizeof(uint16_t);
  412. mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
  413. mx->preference = ntohs (mxpref);
  414. mx->mxhost =
  415. GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
  416. if (NULL == mx->mxhost)
  417. {
  418. GNUNET_break_op (0);
  419. GNUNET_DNSPARSER_free_mx (mx);
  420. *off = old_off;
  421. return NULL;
  422. }
  423. return mx;
  424. }
  425. /**
  426. * Parse a DNS SRV record.
  427. *
  428. * @param udp_payload reference to UDP packet
  429. * @param udp_payload_length length of @a udp_payload
  430. * @param off pointer to the offset of the query to parse in the SRV record (to be
  431. * incremented by the size of the record), unchanged on error
  432. * @return the parsed SRV record, NULL on error
  433. */
  434. struct GNUNET_DNSPARSER_SrvRecord *
  435. GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
  436. size_t udp_payload_length,
  437. size_t *off)
  438. {
  439. struct GNUNET_DNSPARSER_SrvRecord *srv;
  440. struct GNUNET_TUN_DnsSrvRecord srv_bin;
  441. size_t old_off;
  442. old_off = *off;
  443. if (*off + sizeof(struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
  444. return NULL;
  445. GNUNET_memcpy (&srv_bin,
  446. &udp_payload[*off],
  447. sizeof(struct GNUNET_TUN_DnsSrvRecord));
  448. (*off) += sizeof(struct GNUNET_TUN_DnsSrvRecord);
  449. srv = GNUNET_new (struct GNUNET_DNSPARSER_SrvRecord);
  450. srv->priority = ntohs (srv_bin.prio);
  451. srv->weight = ntohs (srv_bin.weight);
  452. srv->port = ntohs (srv_bin.port);
  453. srv->target =
  454. GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
  455. if (NULL == srv->target)
  456. {
  457. GNUNET_DNSPARSER_free_srv (srv);
  458. *off = old_off;
  459. return NULL;
  460. }
  461. return srv;
  462. }
  463. /**
  464. * Parse a DNS CERT record.
  465. *
  466. * @param udp_payload reference to UDP packet
  467. * @param udp_payload_length length of @a udp_payload
  468. * @param off pointer to the offset of the query to parse in the CERT record (to be
  469. * incremented by the size of the record), unchanged on error
  470. * @return the parsed CERT record, NULL on error
  471. */
  472. struct GNUNET_DNSPARSER_CertRecord *
  473. GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
  474. size_t udp_payload_length,
  475. size_t *off)
  476. {
  477. struct GNUNET_DNSPARSER_CertRecord *cert;
  478. struct GNUNET_TUN_DnsCertRecord dcert;
  479. if (*off + sizeof(struct GNUNET_TUN_DnsCertRecord) >= udp_payload_length)
  480. {
  481. GNUNET_break_op (0);
  482. return NULL;
  483. }
  484. GNUNET_memcpy (&dcert,
  485. &udp_payload[*off],
  486. sizeof(struct GNUNET_TUN_DnsCertRecord));
  487. (*off) += sizeof(struct GNUNET_TUN_DnsCertRecord);
  488. cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
  489. cert->cert_type = ntohs (dcert.cert_type);
  490. cert->cert_tag = ntohs (dcert.cert_tag);
  491. cert->algorithm = dcert.algorithm;
  492. cert->certificate_size = udp_payload_length - (*off);
  493. cert->certificate_data = GNUNET_malloc (cert->certificate_size);
  494. GNUNET_memcpy (cert->certificate_data,
  495. &udp_payload[*off],
  496. cert->certificate_size);
  497. (*off) += cert->certificate_size;
  498. return cert;
  499. }
  500. /**
  501. * Parse a DNS record entry.
  502. *
  503. * @param udp_payload entire UDP payload
  504. * @param udp_payload_length length of @a udp_payload
  505. * @param off pointer to the offset of the record to parse in the udp_payload (to be
  506. * incremented by the size of the record)
  507. * @param r where to write the record information
  508. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the record is malformed
  509. */
  510. int
  511. GNUNET_DNSPARSER_parse_record (const char *udp_payload,
  512. size_t udp_payload_length,
  513. size_t *off,
  514. struct GNUNET_DNSPARSER_Record *r)
  515. {
  516. char *name;
  517. struct GNUNET_TUN_DnsRecordLine rl;
  518. size_t old_off;
  519. uint16_t data_len;
  520. name = GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
  521. if (NULL == name)
  522. {
  523. GNUNET_break_op (0);
  524. return GNUNET_SYSERR;
  525. }
  526. r->name = name;
  527. if (*off + sizeof(struct GNUNET_TUN_DnsRecordLine) > udp_payload_length)
  528. {
  529. GNUNET_break_op (0);
  530. return GNUNET_SYSERR;
  531. }
  532. GNUNET_memcpy (&rl, &udp_payload[*off], sizeof(rl));
  533. (*off) += sizeof(rl);
  534. r->type = ntohs (rl.type);
  535. r->dns_traffic_class = ntohs (rl.dns_traffic_class);
  536. r->expiration_time = GNUNET_TIME_relative_to_absolute (
  537. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, ntohl (rl.ttl)));
  538. data_len = ntohs (rl.data_len);
  539. if (*off + data_len > udp_payload_length)
  540. {
  541. GNUNET_break_op (0);
  542. return GNUNET_SYSERR;
  543. }
  544. old_off = *off;
  545. switch (r->type)
  546. {
  547. case GNUNET_DNSPARSER_TYPE_NS:
  548. case GNUNET_DNSPARSER_TYPE_CNAME:
  549. case GNUNET_DNSPARSER_TYPE_DNAME:
  550. case GNUNET_DNSPARSER_TYPE_PTR:
  551. r->data.hostname =
  552. GNUNET_DNSPARSER_parse_name (udp_payload, udp_payload_length, off);
  553. if ((NULL == r->data.hostname) || (old_off + data_len != *off))
  554. return GNUNET_SYSERR;
  555. return GNUNET_OK;
  556. case GNUNET_DNSPARSER_TYPE_SOA:
  557. r->data.soa =
  558. GNUNET_DNSPARSER_parse_soa (udp_payload, udp_payload_length, off);
  559. if ((NULL == r->data.soa) || (old_off + data_len != *off))
  560. {
  561. GNUNET_break_op (0);
  562. return GNUNET_SYSERR;
  563. }
  564. return GNUNET_OK;
  565. case GNUNET_DNSPARSER_TYPE_MX:
  566. r->data.mx =
  567. GNUNET_DNSPARSER_parse_mx (udp_payload, udp_payload_length, off);
  568. if ((NULL == r->data.mx) || (old_off + data_len != *off))
  569. {
  570. GNUNET_break_op (0);
  571. return GNUNET_SYSERR;
  572. }
  573. return GNUNET_OK;
  574. case GNUNET_DNSPARSER_TYPE_SRV:
  575. r->data.srv =
  576. GNUNET_DNSPARSER_parse_srv (udp_payload, udp_payload_length, off);
  577. if ((NULL == r->data.srv) || (old_off + data_len != *off))
  578. {
  579. GNUNET_break_op (0);
  580. return GNUNET_SYSERR;
  581. }
  582. return GNUNET_OK;
  583. default:
  584. r->data.raw.data = GNUNET_malloc (data_len);
  585. r->data.raw.data_len = data_len;
  586. GNUNET_memcpy (r->data.raw.data, &udp_payload[*off], data_len);
  587. break;
  588. }
  589. (*off) += data_len;
  590. return GNUNET_OK;
  591. }
  592. /**
  593. * Parse a UDP payload of a DNS packet in to a nice struct for further
  594. * processing and manipulation.
  595. *
  596. * @param udp_payload wire-format of the DNS packet
  597. * @param udp_payload_length number of bytes in @a udp_payload
  598. * @return NULL on error, otherwise the parsed packet
  599. */
  600. struct GNUNET_DNSPARSER_Packet *
  601. GNUNET_DNSPARSER_parse (const char *udp_payload, size_t udp_payload_length)
  602. {
  603. struct GNUNET_DNSPARSER_Packet *p;
  604. const struct GNUNET_TUN_DnsHeader *dns;
  605. size_t off;
  606. unsigned int n;
  607. if (udp_payload_length < sizeof(struct GNUNET_TUN_DnsHeader))
  608. return NULL;
  609. dns = (const struct GNUNET_TUN_DnsHeader *) udp_payload;
  610. off = sizeof(struct GNUNET_TUN_DnsHeader);
  611. p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
  612. p->flags = dns->flags;
  613. p->id = dns->id;
  614. n = ntohs (dns->query_count);
  615. if (n > 0)
  616. {
  617. p->queries = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Query);
  618. p->num_queries = n;
  619. for (unsigned int i = 0; i < n; i++)
  620. if (GNUNET_OK != GNUNET_DNSPARSER_parse_query (udp_payload,
  621. udp_payload_length,
  622. &off,
  623. &p->queries[i]))
  624. goto error;
  625. }
  626. n = ntohs (dns->answer_rcount);
  627. if (n > 0)
  628. {
  629. p->answers = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
  630. p->num_answers = n;
  631. for (unsigned int i = 0; i < n; i++)
  632. if (GNUNET_OK != GNUNET_DNSPARSER_parse_record (udp_payload,
  633. udp_payload_length,
  634. &off,
  635. &p->answers[i]))
  636. goto error;
  637. }
  638. n = ntohs (dns->authority_rcount);
  639. if (n > 0)
  640. {
  641. p->authority_records = GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
  642. p->num_authority_records = n;
  643. for (unsigned int i = 0; i < n; i++)
  644. if (GNUNET_OK != GNUNET_DNSPARSER_parse_record (udp_payload,
  645. udp_payload_length,
  646. &off,
  647. &p->authority_records[i]))
  648. goto error;
  649. }
  650. n = ntohs (dns->additional_rcount);
  651. if (n > 0)
  652. {
  653. p->additional_records =
  654. GNUNET_new_array (n, struct GNUNET_DNSPARSER_Record);
  655. p->num_additional_records = n;
  656. for (unsigned int i = 0; i < n; i++)
  657. {
  658. if (GNUNET_OK !=
  659. GNUNET_DNSPARSER_parse_record (udp_payload,
  660. udp_payload_length,
  661. &off,
  662. &p->additional_records[i]))
  663. goto error;
  664. }
  665. }
  666. return p;
  667. error:
  668. GNUNET_break_op (0);
  669. GNUNET_DNSPARSER_free_packet (p);
  670. return NULL;
  671. }
  672. /**
  673. * Duplicate (deep-copy) the given DNS record
  674. *
  675. * @param r the record
  676. * @return the newly allocated record
  677. */
  678. struct GNUNET_DNSPARSER_Record *
  679. GNUNET_DNSPARSER_duplicate_record (const struct GNUNET_DNSPARSER_Record *r)
  680. {
  681. struct GNUNET_DNSPARSER_Record *dup = GNUNET_memdup (r, sizeof(*r));
  682. dup->name = GNUNET_strdup (r->name);
  683. switch (r->type)
  684. {
  685. case GNUNET_DNSPARSER_TYPE_NS:
  686. case GNUNET_DNSPARSER_TYPE_CNAME:
  687. case GNUNET_DNSPARSER_TYPE_PTR: {
  688. dup->data.hostname = GNUNET_strdup (r->data.hostname);
  689. break;
  690. }
  691. case GNUNET_DNSPARSER_TYPE_SOA: {
  692. dup->data.soa = GNUNET_DNSPARSER_duplicate_soa_record (r->data.soa);
  693. break;
  694. }
  695. case GNUNET_DNSPARSER_TYPE_CERT: {
  696. dup->data.cert = GNUNET_DNSPARSER_duplicate_cert_record (r->data.cert);
  697. break;
  698. }
  699. case GNUNET_DNSPARSER_TYPE_MX: {
  700. dup->data.mx = GNUNET_DNSPARSER_duplicate_mx_record (r->data.mx);
  701. break;
  702. }
  703. case GNUNET_DNSPARSER_TYPE_SRV: {
  704. dup->data.srv = GNUNET_DNSPARSER_duplicate_srv_record (r->data.srv);
  705. break;
  706. }
  707. default: {
  708. dup->data.raw.data = GNUNET_memdup (r->data.raw.data,
  709. r->data.raw.data_len);
  710. }
  711. }
  712. return dup;
  713. }
  714. /**
  715. * Duplicate (deep-copy) the given DNS record
  716. *
  717. * @param r the record
  718. * @return the newly allocated record
  719. */
  720. struct GNUNET_DNSPARSER_SoaRecord *
  721. GNUNET_DNSPARSER_duplicate_soa_record (
  722. const struct GNUNET_DNSPARSER_SoaRecord *r)
  723. {
  724. struct GNUNET_DNSPARSER_SoaRecord *dup = GNUNET_memdup (r, sizeof(*r));
  725. dup->mname = GNUNET_strdup (r->mname);
  726. dup->rname = GNUNET_strdup (r->rname);
  727. return dup;
  728. }
  729. /**
  730. * Duplicate (deep-copy) the given DNS record
  731. *
  732. * @param r the record
  733. * @return the newly allocated record
  734. */
  735. struct GNUNET_DNSPARSER_CertRecord *
  736. GNUNET_DNSPARSER_duplicate_cert_record (
  737. const struct GNUNET_DNSPARSER_CertRecord *r)
  738. {
  739. struct GNUNET_DNSPARSER_CertRecord *dup = GNUNET_memdup (r, sizeof(*r));
  740. dup->certificate_data = GNUNET_strdup (r->certificate_data);
  741. return dup;
  742. }
  743. /**
  744. * Duplicate (deep-copy) the given DNS record
  745. *
  746. * @param r the record
  747. * @return the newly allocated record
  748. */
  749. struct GNUNET_DNSPARSER_MxRecord *
  750. GNUNET_DNSPARSER_duplicate_mx_record (const struct GNUNET_DNSPARSER_MxRecord *r)
  751. {
  752. struct GNUNET_DNSPARSER_MxRecord *dup = GNUNET_memdup (r, sizeof(*r));
  753. dup->mxhost = GNUNET_strdup (r->mxhost);
  754. return dup;
  755. }
  756. /**
  757. * Duplicate (deep-copy) the given DNS record
  758. *
  759. * @param r the record
  760. * @return the newly allocated record
  761. */
  762. struct GNUNET_DNSPARSER_SrvRecord *
  763. GNUNET_DNSPARSER_duplicate_srv_record (
  764. const struct GNUNET_DNSPARSER_SrvRecord *r)
  765. {
  766. struct GNUNET_DNSPARSER_SrvRecord *dup = GNUNET_memdup (r, sizeof(*r));
  767. dup->target = GNUNET_strdup (r->target);
  768. return dup;
  769. }
  770. /**
  771. * Free memory taken by a packet.
  772. *
  773. * @param p packet to free
  774. */
  775. void
  776. GNUNET_DNSPARSER_free_packet (struct GNUNET_DNSPARSER_Packet *p)
  777. {
  778. for (unsigned int i = 0; i < p->num_queries; i++)
  779. GNUNET_free (p->queries[i].name);
  780. GNUNET_free (p->queries);
  781. for (unsigned int i = 0; i < p->num_answers; i++)
  782. GNUNET_DNSPARSER_free_record (&p->answers[i]);
  783. GNUNET_free (p->answers);
  784. for (unsigned int i = 0; i < p->num_authority_records; i++)
  785. GNUNET_DNSPARSER_free_record (&p->authority_records[i]);
  786. GNUNET_free (p->authority_records);
  787. for (unsigned int i = 0; i < p->num_additional_records; i++)
  788. GNUNET_DNSPARSER_free_record (&p->additional_records[i]);
  789. GNUNET_free (p->additional_records);
  790. GNUNET_free (p);
  791. }
  792. /* ********************** DNS packet assembly code **************** */
  793. /**
  794. * Add a DNS name to the UDP packet at the given location, converting
  795. * the name to IDNA notation as necessary.
  796. *
  797. * @param dst where to write the name (UDP packet)
  798. * @param dst_len number of bytes in @a dst
  799. * @param off pointer to offset where to write the name (increment by bytes used)
  800. * must not be changed if there is an error
  801. * @param name name to write
  802. * @return #GNUNET_SYSERR if @a name is invalid
  803. * #GNUNET_NO if @a name did not fit
  804. * #GNUNET_OK if @a name was added to @a dst
  805. */
  806. int
  807. GNUNET_DNSPARSER_builder_add_name (char *dst,
  808. size_t dst_len,
  809. size_t *off,
  810. const char *name)
  811. {
  812. const char *dot;
  813. const char *idna_name;
  814. char *idna_start;
  815. size_t start;
  816. size_t pos;
  817. size_t len;
  818. Idna_rc rc;
  819. if (NULL == name)
  820. return GNUNET_SYSERR;
  821. if (IDNA_SUCCESS !=
  822. (rc = idna_to_ascii_8z (name, &idna_start, IDNA_ALLOW_UNASSIGNED)))
  823. {
  824. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  825. _ (
  826. "Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
  827. name,
  828. idna_strerror (rc));
  829. return GNUNET_NO;
  830. }
  831. idna_name = idna_start;
  832. start = *off;
  833. if (start + strlen (idna_name) + 2 > dst_len)
  834. goto fail;
  835. pos = start;
  836. do
  837. {
  838. dot = strchr (idna_name, '.');
  839. if (NULL == dot)
  840. len = strlen (idna_name);
  841. else
  842. len = dot - idna_name;
  843. if ((len >= 64) || (0 == len))
  844. {
  845. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  846. "Invalid DNS name `%s': label with %u characters encountered\n",
  847. name,
  848. (unsigned int) len);
  849. goto fail; /* label too long or empty */
  850. }
  851. dst[pos++] = (char) (uint8_t) len;
  852. GNUNET_memcpy (&dst[pos], idna_name, len);
  853. pos += len;
  854. idna_name += len + 1; /* also skip dot */
  855. }
  856. while (NULL != dot);
  857. dst[pos++] = '\0'; /* terminator */
  858. *off = pos;
  859. free (idna_start);
  860. return GNUNET_OK;
  861. fail:
  862. free (idna_start);
  863. return GNUNET_NO;
  864. }
  865. /**
  866. * Add a DNS query to the UDP packet at the given location.
  867. *
  868. * @param dst where to write the query
  869. * @param dst_len number of bytes in @a dst
  870. * @param off pointer to offset where to write the query (increment by bytes used)
  871. * must not be changed if there is an error
  872. * @param query query to write
  873. * @return #GNUNET_SYSERR if @a query is invalid
  874. * #GNUNET_NO if @a query did not fit
  875. * #GNUNET_OK if @a query was added to @a dst
  876. */
  877. int
  878. GNUNET_DNSPARSER_builder_add_query (char *dst,
  879. size_t dst_len,
  880. size_t *off,
  881. const struct GNUNET_DNSPARSER_Query *query)
  882. {
  883. int ret;
  884. struct GNUNET_TUN_DnsQueryLine ql;
  885. ret = GNUNET_DNSPARSER_builder_add_name (dst,
  886. dst_len
  887. - sizeof(
  888. struct GNUNET_TUN_DnsQueryLine),
  889. off,
  890. query->name);
  891. if (ret != GNUNET_OK)
  892. return ret;
  893. ql.type = htons (query->type);
  894. ql.dns_traffic_class = htons (query->dns_traffic_class);
  895. GNUNET_memcpy (&dst[*off], &ql, sizeof(ql));
  896. (*off) += sizeof(ql);
  897. return GNUNET_OK;
  898. }
  899. /**
  900. * Add an MX record to the UDP packet at the given location.
  901. *
  902. * @param dst where to write the mx record
  903. * @param dst_len number of bytes in @a dst
  904. * @param off pointer to offset where to write the mx information (increment by bytes used);
  905. * can also change if there was an error
  906. * @param mx mx information to write
  907. * @return #GNUNET_SYSERR if @a mx is invalid
  908. * #GNUNET_NO if @a mx did not fit
  909. * #GNUNET_OK if @a mx was added to @a dst
  910. */
  911. int
  912. GNUNET_DNSPARSER_builder_add_mx (char *dst,
  913. size_t dst_len,
  914. size_t *off,
  915. const struct GNUNET_DNSPARSER_MxRecord *mx)
  916. {
  917. uint16_t mxpref;
  918. if (*off + sizeof(uint16_t) > dst_len)
  919. return GNUNET_NO;
  920. mxpref = htons (mx->preference);
  921. GNUNET_memcpy (&dst[*off], &mxpref, sizeof(mxpref));
  922. (*off) += sizeof(mxpref);
  923. return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
  924. }
  925. /**
  926. * Add a CERT record to the UDP packet at the given location.
  927. *
  928. * @param dst where to write the CERT record
  929. * @param dst_len number of bytes in @a dst
  930. * @param off pointer to offset where to write the CERT information (increment by bytes used);
  931. * can also change if there was an error
  932. * @param cert CERT information to write
  933. * @return #GNUNET_SYSERR if @a cert is invalid
  934. * #GNUNET_NO if @a cert did not fit
  935. * #GNUNET_OK if @a cert was added to @a dst
  936. */
  937. int
  938. GNUNET_DNSPARSER_builder_add_cert (
  939. char *dst,
  940. size_t dst_len,
  941. size_t *off,
  942. const struct GNUNET_DNSPARSER_CertRecord *cert)
  943. {
  944. struct GNUNET_TUN_DnsCertRecord dcert;
  945. #ifdef __clang__
  946. #pragma clang diagnostic push
  947. #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
  948. #endif
  949. if ((cert->cert_type > UINT16_MAX) || (cert->algorithm > UINT8_MAX))
  950. {
  951. GNUNET_break (0);
  952. return GNUNET_SYSERR;
  953. }
  954. #ifdef __clang__
  955. #pragma clang diagnostic pop
  956. #endif
  957. if (*off + sizeof(struct GNUNET_TUN_DnsCertRecord) + cert->certificate_size >
  958. dst_len)
  959. return GNUNET_NO;
  960. dcert.cert_type = htons ((uint16_t) cert->cert_type);
  961. dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
  962. dcert.algorithm = (uint8_t) cert->algorithm;
  963. GNUNET_memcpy (&dst[*off], &dcert, sizeof(dcert));
  964. (*off) += sizeof(dcert);
  965. GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
  966. (*off) += cert->certificate_size;
  967. return GNUNET_OK;
  968. }
  969. /**
  970. * Add an SOA record to the UDP packet at the given location.
  971. *
  972. * @param dst where to write the SOA record
  973. * @param dst_len number of bytes in @a dst
  974. * @param off pointer to offset where to write the SOA information (increment by bytes used)
  975. * can also change if there was an error
  976. * @param soa SOA information to write
  977. * @return #GNUNET_SYSERR if @a soa is invalid
  978. * #GNUNET_NO if @a soa did not fit
  979. * #GNUNET_OK if @a soa was added to @a dst
  980. */
  981. int
  982. GNUNET_DNSPARSER_builder_add_soa (char *dst,
  983. size_t dst_len,
  984. size_t *off,
  985. const struct GNUNET_DNSPARSER_SoaRecord *soa)
  986. {
  987. struct GNUNET_TUN_DnsSoaRecord sd;
  988. int ret;
  989. if ((GNUNET_OK !=
  990. (ret =
  991. GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, soa->mname))) ||
  992. (GNUNET_OK !=
  993. (ret =
  994. GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, soa->rname))))
  995. return ret;
  996. if (*off + sizeof(struct GNUNET_TUN_DnsSoaRecord) > dst_len)
  997. return GNUNET_NO;
  998. sd.serial = htonl (soa->serial);
  999. sd.refresh = htonl (soa->refresh);
  1000. sd.retry = htonl (soa->retry);
  1001. sd.expire = htonl (soa->expire);
  1002. sd.minimum = htonl (soa->minimum_ttl);
  1003. GNUNET_memcpy (&dst[*off], &sd, sizeof(sd));
  1004. (*off) += sizeof(sd);
  1005. return GNUNET_OK;
  1006. }
  1007. /**
  1008. * Add an SRV record to the UDP packet at the given location.
  1009. *
  1010. * @param dst where to write the SRV record
  1011. * @param dst_len number of bytes in @a dst
  1012. * @param off pointer to offset where to write the SRV information (increment by bytes used)
  1013. * can also change if there was an error
  1014. * @param srv SRV information to write
  1015. * @return #GNUNET_SYSERR if @a srv is invalid
  1016. * #GNUNET_NO if @a srv did not fit
  1017. * #GNUNET_OK if @a srv was added to @a dst
  1018. */
  1019. int
  1020. GNUNET_DNSPARSER_builder_add_srv (char *dst,
  1021. size_t dst_len,
  1022. size_t *off,
  1023. const struct GNUNET_DNSPARSER_SrvRecord *srv)
  1024. {
  1025. struct GNUNET_TUN_DnsSrvRecord sd;
  1026. int ret;
  1027. if (*off + sizeof(struct GNUNET_TUN_DnsSrvRecord) > dst_len)
  1028. return GNUNET_NO;
  1029. sd.prio = htons (srv->priority);
  1030. sd.weight = htons (srv->weight);
  1031. sd.port = htons (srv->port);
  1032. GNUNET_memcpy (&dst[*off], &sd, sizeof(sd));
  1033. (*off) += sizeof(sd);
  1034. if (GNUNET_OK !=
  1035. (ret =
  1036. GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, srv->target)))
  1037. return ret;
  1038. return GNUNET_OK;
  1039. }
  1040. /**
  1041. * Add a DNS record to the UDP packet at the given location.
  1042. *
  1043. * @param dst where to write the query
  1044. * @param dst_len number of bytes in @a dst
  1045. * @param off pointer to offset where to write the query (increment by bytes used)
  1046. * must not be changed if there is an error
  1047. * @param record record to write
  1048. * @return #GNUNET_SYSERR if @a record is invalid
  1049. * #GNUNET_NO if @a record did not fit
  1050. * #GNUNET_OK if @a record was added to @a dst
  1051. */
  1052. static int
  1053. add_record (char *dst,
  1054. size_t dst_len,
  1055. size_t *off,
  1056. const struct GNUNET_DNSPARSER_Record *record)
  1057. {
  1058. int ret;
  1059. size_t start;
  1060. size_t pos;
  1061. struct GNUNET_TUN_DnsRecordLine rl;
  1062. start = *off;
  1063. ret = GNUNET_DNSPARSER_builder_add_name (dst,
  1064. dst_len
  1065. - sizeof(
  1066. struct GNUNET_TUN_DnsRecordLine),
  1067. off,
  1068. record->name);
  1069. if (GNUNET_OK != ret)
  1070. return ret;
  1071. /* '*off' is now the position where we will need to write the record line */
  1072. pos = *off + sizeof(struct GNUNET_TUN_DnsRecordLine);
  1073. switch (record->type)
  1074. {
  1075. case GNUNET_DNSPARSER_TYPE_MX:
  1076. ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx);
  1077. break;
  1078. case GNUNET_DNSPARSER_TYPE_CERT:
  1079. ret =
  1080. GNUNET_DNSPARSER_builder_add_cert (dst, dst_len, &pos, record->data.cert);
  1081. break;
  1082. case GNUNET_DNSPARSER_TYPE_SOA:
  1083. ret =
  1084. GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa);
  1085. break;
  1086. case GNUNET_DNSPARSER_TYPE_NS:
  1087. case GNUNET_DNSPARSER_TYPE_CNAME:
  1088. case GNUNET_DNSPARSER_TYPE_PTR:
  1089. ret = GNUNET_DNSPARSER_builder_add_name (dst,
  1090. dst_len,
  1091. &pos,
  1092. record->data.hostname);
  1093. break;
  1094. case GNUNET_DNSPARSER_TYPE_SRV:
  1095. ret =
  1096. GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv);
  1097. break;
  1098. default:
  1099. if (pos + record->data.raw.data_len > dst_len)
  1100. {
  1101. ret = GNUNET_NO;
  1102. break;
  1103. }
  1104. GNUNET_memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
  1105. pos += record->data.raw.data_len;
  1106. ret = GNUNET_OK;
  1107. break;
  1108. }
  1109. if (GNUNET_OK != ret)
  1110. {
  1111. *off = start;
  1112. return GNUNET_NO;
  1113. }
  1114. if (pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine)) > UINT16_MAX)
  1115. {
  1116. /* record data too long */
  1117. *off = start;
  1118. return GNUNET_NO;
  1119. }
  1120. rl.type = htons (record->type);
  1121. rl.dns_traffic_class = htons (record->dns_traffic_class);
  1122. rl.ttl = htonl (
  1123. GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us
  1124. / 1000LL / 1000LL); /* in seconds */
  1125. rl.data_len = htons (
  1126. (uint16_t) (pos - (*off + sizeof(struct GNUNET_TUN_DnsRecordLine))));
  1127. GNUNET_memcpy (&dst[*off], &rl, sizeof(struct GNUNET_TUN_DnsRecordLine));
  1128. *off = pos;
  1129. return GNUNET_OK;
  1130. }
  1131. /**
  1132. * Given a DNS packet @a p, generate the corresponding UDP payload.
  1133. * Note that we do not attempt to pack the strings with pointers
  1134. * as this would complicate the code and this is about being
  1135. * simple and secure, not fast, fancy and broken like bind.
  1136. *
  1137. * @param p packet to pack
  1138. * @param max maximum allowed size for the resulting UDP payload
  1139. * @param buf set to a buffer with the packed message
  1140. * @param buf_length set to the length of @a buf
  1141. * @return #GNUNET_SYSERR if @a p is invalid
  1142. * #GNUNET_NO if @a p was truncated (but there is still a result in @a buf)
  1143. * #GNUNET_OK if @a p was packed completely into @a buf
  1144. */
  1145. int
  1146. GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
  1147. uint16_t max,
  1148. char **buf,
  1149. size_t *buf_length)
  1150. {
  1151. struct GNUNET_TUN_DnsHeader dns;
  1152. size_t off;
  1153. char tmp[max];
  1154. int ret;
  1155. int trc;
  1156. if ((p->num_queries > UINT16_MAX) || (p->num_answers > UINT16_MAX) ||
  1157. (p->num_authority_records > UINT16_MAX) ||
  1158. (p->num_additional_records > UINT16_MAX))
  1159. return GNUNET_SYSERR;
  1160. dns.id = p->id;
  1161. dns.flags = p->flags;
  1162. dns.query_count = htons (p->num_queries);
  1163. dns.answer_rcount = htons (p->num_answers);
  1164. dns.authority_rcount = htons (p->num_authority_records);
  1165. dns.additional_rcount = htons (p->num_additional_records);
  1166. off = sizeof(struct GNUNET_TUN_DnsHeader);
  1167. trc = GNUNET_NO;
  1168. for (unsigned int i = 0; i < p->num_queries; i++)
  1169. {
  1170. ret = GNUNET_DNSPARSER_builder_add_query (tmp,
  1171. sizeof(tmp),
  1172. &off,
  1173. &p->queries[i]);
  1174. if (GNUNET_SYSERR == ret)
  1175. return GNUNET_SYSERR;
  1176. if (GNUNET_NO == ret)
  1177. {
  1178. dns.query_count = htons ((uint16_t) (i - 1));
  1179. trc = GNUNET_YES;
  1180. break;
  1181. }
  1182. }
  1183. for (unsigned int i = 0; i < p->num_answers; i++)
  1184. {
  1185. ret = add_record (tmp, sizeof(tmp), &off, &p->answers[i]);
  1186. if (GNUNET_SYSERR == ret)
  1187. return GNUNET_SYSERR;
  1188. if (GNUNET_NO == ret)
  1189. {
  1190. dns.answer_rcount = htons ((uint16_t) (i - 1));
  1191. trc = GNUNET_YES;
  1192. break;
  1193. }
  1194. }
  1195. for (unsigned int i = 0; i < p->num_authority_records; i++)
  1196. {
  1197. ret = add_record (tmp, sizeof(tmp), &off, &p->authority_records[i]);
  1198. if (GNUNET_SYSERR == ret)
  1199. return GNUNET_SYSERR;
  1200. if (GNUNET_NO == ret)
  1201. {
  1202. dns.authority_rcount = htons ((uint16_t) (i - 1));
  1203. trc = GNUNET_YES;
  1204. break;
  1205. }
  1206. }
  1207. for (unsigned int i = 0; i < p->num_additional_records; i++)
  1208. {
  1209. ret = add_record (tmp, sizeof(tmp), &off, &p->additional_records[i]);
  1210. if (GNUNET_SYSERR == ret)
  1211. return GNUNET_SYSERR;
  1212. if (GNUNET_NO == ret)
  1213. {
  1214. dns.additional_rcount = htons (i - 1);
  1215. trc = GNUNET_YES;
  1216. break;
  1217. }
  1218. }
  1219. if (GNUNET_YES == trc)
  1220. dns.flags.message_truncated = 1;
  1221. GNUNET_memcpy (tmp, &dns, sizeof(struct GNUNET_TUN_DnsHeader));
  1222. *buf = GNUNET_malloc (off);
  1223. *buf_length = off;
  1224. GNUNET_memcpy (*buf, tmp, off);
  1225. if (GNUNET_YES == trc)
  1226. return GNUNET_NO;
  1227. return GNUNET_OK;
  1228. }
  1229. /**
  1230. * Convert a block of binary data to HEX.
  1231. *
  1232. * @param data binary data to convert
  1233. * @param data_size number of bytes in @a data
  1234. * @return HEX string (lower case)
  1235. */
  1236. char *
  1237. GNUNET_DNSPARSER_bin_to_hex (const void *data, size_t data_size)
  1238. {
  1239. char *ret;
  1240. size_t off;
  1241. const uint8_t *idata;
  1242. idata = data;
  1243. ret = GNUNET_malloc (data_size * 2 + 1);
  1244. for (off = 0; off < data_size; off++)
  1245. sprintf (&ret[off * 2], "%02x", idata[off]);
  1246. return ret;
  1247. }
  1248. /**
  1249. * Convert a HEX string to block of binary data.
  1250. *
  1251. * @param hex HEX string to convert (may contain mixed case)
  1252. * @param data where to write result, must be
  1253. * at least `strlen(hex)/2` bytes long
  1254. * @return number of bytes written to data
  1255. */
  1256. size_t
  1257. GNUNET_DNSPARSER_hex_to_bin (const char *hex, void *data)
  1258. {
  1259. size_t data_size;
  1260. size_t off;
  1261. uint8_t *idata;
  1262. unsigned int h;
  1263. char in[3];
  1264. data_size = strlen (hex) / 2;
  1265. idata = data;
  1266. in[2] = '\0';
  1267. for (off = 0; off < data_size; off++)
  1268. {
  1269. in[0] = tolower ((unsigned char) hex[off * 2]);
  1270. in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
  1271. if (1 != sscanf (in, "%x", &h))
  1272. return off;
  1273. idata[off] = (uint8_t) h;
  1274. }
  1275. return off;
  1276. }
  1277. /* end of dnsparser.c */