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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 (*p) {
  70. *label_len = p - begin;
  71. }
  72. else {
  73. *label_len = p - begin;
  74. }
  75. return ret;
  76. }
  77. bool
  78. rdns_format_dns_name (struct rdns_resolver *resolver, const char *in,
  79. size_t inlen,
  80. char **out, size_t *outlen)
  81. {
  82. const uint8_t *dot;
  83. const uint8_t *p = in, *end = in + inlen;
  84. char *o;
  85. int labels = 0;
  86. size_t label_len, olen, remain;
  87. uint32_t *uclabel;
  88. size_t punylabel_len, uclabel_len;
  89. char tmp_label[DNS_D_MAXLABEL];
  90. bool need_encode = false;
  91. if (inlen == 0) {
  92. inlen = strlen (in);
  93. }
  94. /* Check for non-ascii characters */
  95. while (p != end) {
  96. if (*p >= 0x80) {
  97. need_encode = true;
  98. }
  99. else if (*p == '.') {
  100. labels ++;
  101. }
  102. p ++;
  103. }
  104. if (!need_encode) {
  105. *out = malloc (inlen + 1);
  106. if (*out == NULL) {
  107. return false;
  108. }
  109. o = *out;
  110. memcpy (o, in, inlen);
  111. o[inlen] = '\0';
  112. *outlen = inlen;
  113. return true;
  114. }
  115. /* We need to encode */
  116. p = in;
  117. /* We allocate 4 times more memory as we cannot guarantee encoding bounds */
  118. olen = inlen * sizeof (int32_t) + 1 + sizeof ("xn--") * labels;
  119. *out = malloc (olen + 1);
  120. if (*out == NULL) {
  121. return false;
  122. }
  123. o = *out;
  124. remain = olen;
  125. while (p != end) {
  126. /* Check label for unicode characters */
  127. if (rdns_maybe_punycode_label (p, &dot, &label_len)) {
  128. /* Convert to ucs4 */
  129. if (rdns_utf8_to_ucs4 (p, label_len, &uclabel, &uclabel_len) == 0) {
  130. punylabel_len = DNS_D_MAXLABEL;
  131. rdns_punycode_label_toascii (uclabel, uclabel_len,
  132. tmp_label, &punylabel_len);
  133. if (remain >= punylabel_len + 1) {
  134. memcpy (o, tmp_label, punylabel_len);
  135. o += punylabel_len;
  136. *o++ = '.';
  137. remain -= punylabel_len + 1;
  138. }
  139. else {
  140. rdns_info ("no buffer remain for punycoding query");
  141. goto err;
  142. }
  143. free (uclabel);
  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. return false;
  210. }
  211. bool
  212. rdns_add_rr (struct rdns_request *req, const char *name, unsigned int len,
  213. enum dns_type type, struct rdns_compression_entry **comp)
  214. {
  215. uint16_t *p;
  216. if (!rdns_write_name_compressed (req, name, len, comp)) {
  217. return false;
  218. }
  219. p = (uint16_t *)(req->packet + req->pos);
  220. *p++ = htons (type);
  221. *p = htons (DNS_C_IN);
  222. req->pos += sizeof (uint16_t) * 2;
  223. return true;
  224. }
  225. bool
  226. rdns_add_edns0 (struct rdns_request *req)
  227. {
  228. uint8_t *p8;
  229. uint16_t *p16;
  230. p8 = (uint8_t *)(req->packet + req->pos);
  231. *p8 = '\0'; /* Name is root */
  232. p16 = (uint16_t *)(req->packet + req->pos + 1);
  233. *p16++ = htons (DNS_T_OPT);
  234. /* UDP packet length */
  235. *p16++ = htons (UDP_PACKET_SIZE);
  236. /* Extended rcode 00 00 */
  237. *p16++ = 0;
  238. /* Z 10000000 00000000 to allow dnssec */
  239. p8 = (uint8_t *)p16;
  240. if (req->resolver->enable_dnssec) {
  241. *p8++ = 0x80;
  242. }
  243. else {
  244. *p8++ = 0x00;
  245. }
  246. *p8++ = 0;
  247. p16 = (uint16_t *)p8;
  248. /* Length */
  249. *p16 = 0;
  250. req->pos += sizeof (uint8_t) + sizeof (uint16_t) * 5;
  251. return true;
  252. }