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.

packet.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copyright (c) 2014, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "rdns.h"
  24. #include "dns_private.h"
  25. #include "punycode.h"
  26. #include "packet.h"
  27. #include "util.h"
  28. #include "logger.h"
  29. #include "compression.h"
  30. void
  31. rdns_allocate_packet (struct rdns_request* req, unsigned int namelen)
  32. {
  33. namelen += 96 + 2 + 4 + 11; /* EDNS0 RR */
  34. req->packet = malloc (namelen);
  35. req->pos = 0;
  36. req->packet_len = namelen;
  37. }
  38. void
  39. rdns_make_dns_header (struct rdns_request *req, unsigned int qcount)
  40. {
  41. struct dns_header *header;
  42. /* Set DNS header values */
  43. header = (struct dns_header *)req->packet;
  44. memset (header, 0 , sizeof (struct dns_header));
  45. header->qid = rdns_permutor_generate_id ();
  46. header->rd = 1;
  47. header->qdcount = htons (qcount);
  48. header->arcount = htons (1);
  49. req->pos += sizeof (struct dns_header);
  50. req->id = header->qid;
  51. }
  52. static bool
  53. rdns_maybe_punycode_label (const uint8_t *begin,
  54. uint8_t const **dot, size_t *label_len)
  55. {
  56. bool ret = false;
  57. const uint8_t *p = begin;
  58. *dot = NULL;
  59. while (*p) {
  60. if (*p == '.') {
  61. *dot = p;
  62. break;
  63. }
  64. else if ((*p) & 0x80) {
  65. ret = true;
  66. }
  67. p ++;
  68. }
  69. if (label_len) {
  70. *label_len = p - begin;
  71. }
  72. return ret;
  73. }
  74. bool
  75. rdns_format_dns_name (struct rdns_resolver *resolver, const char *in,
  76. size_t inlen,
  77. char **out, size_t *outlen)
  78. {
  79. const uint8_t *dot;
  80. const uint8_t *p = in, *end = in + inlen;
  81. char *o;
  82. int labels = 0;
  83. size_t label_len, olen, remain;
  84. uint32_t *uclabel = NULL;
  85. size_t punylabel_len, uclabel_len;
  86. char tmp_label[DNS_D_MAXLABEL];
  87. bool need_encode = false;
  88. if (inlen == 0) {
  89. inlen = strlen (in);
  90. }
  91. /* Check for non-ascii characters */
  92. if (!(resolver->flags & RDNS_RESOLVER_NOIDN)) {
  93. while (p != end) {
  94. if (*p >= 0x80) {
  95. need_encode = true;
  96. }
  97. else if (*p == '.') {
  98. labels++;
  99. }
  100. p++;
  101. }
  102. }
  103. if (!need_encode) {
  104. *out = malloc (inlen + 1);
  105. if (*out == NULL) {
  106. return false;
  107. }
  108. o = *out;
  109. memcpy (o, in, inlen);
  110. o[inlen] = '\0';
  111. *outlen = inlen;
  112. return true;
  113. }
  114. /* We need to encode */
  115. p = in;
  116. /* We allocate 4 times more memory as we cannot guarantee encoding bounds */
  117. olen = inlen * sizeof (int32_t) + 1 + sizeof ("xn--") * labels;
  118. *out = malloc (olen + 1);
  119. if (*out == NULL) {
  120. return false;
  121. }
  122. o = *out;
  123. remain = olen;
  124. while (p != end) {
  125. /* Check label for unicode characters */
  126. if (rdns_maybe_punycode_label (p, &dot, &label_len)) {
  127. /* Convert to ucs4 */
  128. if (rdns_utf8_to_ucs4 (p, label_len, &uclabel, &uclabel_len) == 0) {
  129. punylabel_len = DNS_D_MAXLABEL;
  130. rdns_punycode_label_toascii (uclabel, uclabel_len,
  131. tmp_label, &punylabel_len);
  132. if (remain >= punylabel_len + 1) {
  133. memcpy (o, tmp_label, punylabel_len);
  134. o += punylabel_len;
  135. *o++ = '.';
  136. remain -= punylabel_len + 1;
  137. }
  138. else {
  139. rdns_info ("no buffer remain for punycoding query");
  140. goto err;
  141. }
  142. free (uclabel);
  143. uclabel = NULL;
  144. if (dot) {
  145. p = dot + 1;
  146. }
  147. else {
  148. break;
  149. }
  150. }
  151. else {
  152. break;
  153. }
  154. }
  155. else {
  156. if (dot) {
  157. if (label_len > DNS_D_MAXLABEL) {
  158. rdns_info ("dns name component is longer than 63 bytes, should be stripped");
  159. label_len = DNS_D_MAXLABEL;
  160. }
  161. if (remain < label_len + 1) {
  162. rdns_info ("no buffer remain for punycoding query");
  163. goto err;
  164. }
  165. if (label_len == 0) {
  166. /* Two dots in order, skip this */
  167. rdns_info ("name contains two or more dots in a row, replace it with one dot");
  168. p = dot + 1;
  169. continue;
  170. }
  171. memcpy (o, p, label_len);
  172. o += label_len;
  173. *o++ = '.';
  174. remain -= label_len + 1;
  175. p = dot + 1;
  176. }
  177. else {
  178. if (label_len == 0) {
  179. /* If name is ended with dot */
  180. break;
  181. }
  182. if (label_len > DNS_D_MAXLABEL) {
  183. rdns_info ("dns name component is longer than 63 bytes, should be stripped");
  184. label_len = DNS_D_MAXLABEL;
  185. }
  186. if (remain < label_len + 1) {
  187. rdns_info ("no buffer remain for punycoding query");
  188. goto err;
  189. }
  190. memcpy (o, p, label_len);
  191. o += label_len;
  192. *o++ = '.';
  193. remain -= label_len + 1;
  194. p = dot + 1;
  195. break;
  196. }
  197. }
  198. if (remain == 0) {
  199. rdns_info ("no buffer remain for punycoding query");
  200. goto err;
  201. }
  202. }
  203. *o = '\0';
  204. *outlen = o - *out;
  205. return true;
  206. err:
  207. free (*out);
  208. *out = NULL;
  209. free (uclabel);
  210. return false;
  211. }
  212. #define U16_TO_WIRE_ADVANCE(val, p8) \
  213. *p8++ = (uint8_t)(((uint16_t)(val)) >> 8); \
  214. *p8++ = (uint8_t)(((uint16_t)(val)) & 0xFF);
  215. bool
  216. rdns_add_rr (struct rdns_request *req, const char *name, unsigned int len,
  217. enum dns_type type, struct kh_rdns_compression_hash_s **comp)
  218. {
  219. uint8_t *p8;
  220. if (!rdns_write_name_compressed (req, name, len, comp)) {
  221. return false;
  222. }
  223. p8 = (req->packet + req->pos);
  224. U16_TO_WIRE_ADVANCE (type, p8);
  225. U16_TO_WIRE_ADVANCE (DNS_C_IN, p8);
  226. req->pos += sizeof (uint16_t) * 2;
  227. return true;
  228. }
  229. bool
  230. rdns_add_edns0 (struct rdns_request *req)
  231. {
  232. uint8_t *p8;
  233. p8 = (req->packet + req->pos);
  234. *p8++ = '\0'; /* Name is root */
  235. U16_TO_WIRE_ADVANCE (DNS_T_OPT, p8);
  236. U16_TO_WIRE_ADVANCE (UDP_PACKET_SIZE, p8);
  237. U16_TO_WIRE_ADVANCE (0, p8);
  238. if (req->resolver->enable_dnssec) {
  239. *p8++ = 0x80;
  240. }
  241. else {
  242. *p8++ = 0x00;
  243. }
  244. *p8++ = 0;
  245. /* Length */
  246. U16_TO_WIRE_ADVANCE (0, p8);
  247. req->pos += sizeof (uint8_t) + sizeof (uint16_t) * 5;
  248. return true;
  249. }