dns_test.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2015, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@digium.com>
  7. *
  8. * Includes code and algorithms from the Zapata library.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*** MODULEINFO
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. #include "asterisk/dns_core.h"
  25. #include "asterisk/dns_test.h"
  26. #include "asterisk/utils.h"
  27. #ifdef TEST_FRAMEWORK
  28. const char DNS_HEADER[] = {
  29. /* ID == 0 */
  30. 0x00, 0x00,
  31. /* QR == 1, Opcode == 0, AA == 1, TC == 0, RD == 1 */
  32. 0x85,
  33. /* RA == 1, Z == 0, RCODE == 0 */
  34. 0x80,
  35. /* QDCOUNT == 1 */
  36. 0x00, 0x01,
  37. /* ANCOUNT == 1 */
  38. 0x00, 0x00,
  39. /* NSCOUNT == 0 */
  40. 0x00, 0x00,
  41. /* ARCOUNT == 0 */
  42. 0x00, 0x00,
  43. };
  44. /*!
  45. * \brief Generate a DNS header and write it to a buffer
  46. *
  47. * The DNS header is the first part of a DNS request or response. In our
  48. * case, the only part of the header that a test can affect is the number
  49. * of answers. The rest of the DNS header is based on hard-coded values.
  50. *
  51. * There is no buffer size passed to this function since we provide
  52. * the data ourselves and have sized the buffer to be way larger
  53. * than necessary for the tests.
  54. *
  55. * \param num_records The number of DNS records in this DNS response
  56. * \param buf The buffer to write the header into
  57. * \retval The number of bytes written to the buffer
  58. */
  59. static int generate_dns_header(unsigned short num_records, char *buf)
  60. {
  61. unsigned short net_num_records = htons(num_records);
  62. memcpy(buf, DNS_HEADER, ARRAY_LEN(DNS_HEADER));
  63. /* Overwrite the ANCOUNT with the actual number of answers */
  64. memcpy(&buf[6], &net_num_records, sizeof(num_records));
  65. return ARRAY_LEN(DNS_HEADER);
  66. }
  67. const char DNS_QUESTION [] = {
  68. /* goose */
  69. 0x05, 0x67, 0x6f, 0x6f, 0x73, 0x65,
  70. /* feathers */
  71. 0x08, 0x66, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x73,
  72. /* end label */
  73. 0x00,
  74. /* NAPTR type */
  75. 0x00, 0x23,
  76. /* IN class */
  77. 0x00, 0x01,
  78. };
  79. /*!
  80. * \brief Generate a DNS question and write it to a buffer
  81. *
  82. * The DNS question is the second part of a DNS request or response.
  83. * All DNS questions in this file are for the same domain and thus
  84. * the DNS question is a hard-coded value.
  85. *
  86. * There is no buffer size passed to this function since we provide
  87. * the data ourselves and have sized the buffer to be way larger
  88. * than necessary for the tests.
  89. *
  90. * \param buf The buffer to write the question into
  91. * \retval The number of bytes written to the buffer
  92. */
  93. static int generate_dns_question(char *buf)
  94. {
  95. memcpy(buf, DNS_QUESTION, ARRAY_LEN(DNS_QUESTION));
  96. return ARRAY_LEN(DNS_QUESTION);
  97. }
  98. const char NAPTR_ANSWER [] = {
  99. /* Domain points to name from question */
  100. 0xc0, 0x0c,
  101. /* NAPTR type */
  102. 0x00, 0x23,
  103. /* IN Class */
  104. 0x00, 0x01,
  105. /* TTL (12345 by default) */
  106. 0x00, 0x00, 0x30, 0x39,
  107. };
  108. /*!
  109. * \brief Generate a DNS answer and write it to a buffer
  110. *
  111. * The DNS answer is the third (and in our case final) part of a
  112. * DNS response. The DNS answer generated here is only partial.
  113. * The record-specific data is generated by a separate function.
  114. * DNS answers in our tests may have variable TTLs, but the rest
  115. * is hard-coded.
  116. *
  117. * There is no buffer size passed to this function since we provide
  118. * the data ourselves and have sized the buffer to be way larger
  119. * than necessary for the tests.
  120. *
  121. * \param buf The buffer to write the answer into
  122. * \retval The number of bytes written to the buffer
  123. */
  124. static int generate_dns_answer(int ttl, char *buf)
  125. {
  126. int net_ttl = htonl(ttl);
  127. memcpy(buf, NAPTR_ANSWER, ARRAY_LEN(NAPTR_ANSWER));
  128. /* Overwrite TTL if one is provided */
  129. if (ttl) {
  130. memcpy(&buf[6], &net_ttl, sizeof(int));
  131. }
  132. return ARRAY_LEN(NAPTR_ANSWER);
  133. }
  134. /*!
  135. * \brief Write a DNS string to a buffer
  136. *
  137. * This writes the DNS string to the buffer and returns the total
  138. * number of bytes written to the buffer.
  139. *
  140. * There is no buffer size passed to this function since we provide
  141. * the data ourselves and have sized the buffer to be way larger
  142. * than necessary for the tests.
  143. *
  144. * \param string The string to write
  145. * \param buf The buffer to write the string into
  146. * \return The number of bytes written to the buffer
  147. */
  148. int ast_dns_test_write_string(const struct ast_dns_test_string *string, char *buf)
  149. {
  150. uint8_t len = string->len;
  151. size_t actual_len = strlen(string->val);
  152. buf[0] = len;
  153. /*
  154. * We use the actual length of the string instead of
  155. * the stated value since sometimes we're going to lie about
  156. * the length of the string
  157. */
  158. if (actual_len) {
  159. memcpy(&buf[1], string->val, strlen(string->val));
  160. }
  161. return actual_len + 1;
  162. }
  163. /*!
  164. * \brief Write a DNS domain to a buffer
  165. *
  166. * A DNS domain consists of a series of labels separated
  167. * by dots. Each of these labels gets written as a DNS
  168. * string. A DNS domain ends with a NULL label, which is
  169. * essentially a zero-length DNS string.
  170. *
  171. *
  172. * There is no buffer size passed to this function since we provide
  173. * the data ourselves and have sized the buffer to be way larger
  174. * than necessary for the tests.
  175. *
  176. * \param string The DNS domain to write
  177. * \param buf The buffer to write the domain into
  178. * \return The number of bytes written to the buffer
  179. */
  180. int ast_dns_test_write_domain(const char *string, char *buf)
  181. {
  182. char *copy = ast_strdupa(string);
  183. char *part;
  184. char *ptr = buf;
  185. static const struct ast_dns_test_string null_label = {
  186. .len = 0,
  187. .val = "",
  188. };
  189. while (1) {
  190. struct ast_dns_test_string dns_str;
  191. part = strsep(&copy, ".");
  192. if (ast_strlen_zero(part)) {
  193. break;
  194. }
  195. dns_str.len = strlen(part);
  196. dns_str.val = part;
  197. ptr += ast_dns_test_write_string(&dns_str, ptr);
  198. }
  199. ptr += ast_dns_test_write_string(&null_label, ptr);
  200. return ptr - buf;
  201. }
  202. int ast_dns_test_generate_result(struct ast_dns_query *query, void *records, size_t num_records,
  203. size_t record_size, record_fn generate, char *buffer)
  204. {
  205. char *ptr = buffer;
  206. char *record_iter;
  207. ptr += generate_dns_header(num_records, ptr);
  208. ptr += generate_dns_question(ptr);
  209. for (record_iter = records; record_iter < (char *) records + num_records * record_size; record_iter += record_size) {
  210. unsigned short rdlength;
  211. unsigned short net_rdlength;
  212. /* XXX Do we even want to override TTL? */
  213. ptr += generate_dns_answer(0, ptr);
  214. rdlength = generate(record_iter, ptr + 2);
  215. net_rdlength = htons(rdlength);
  216. memcpy(ptr, &net_rdlength, 2);
  217. ptr += 2;
  218. ptr += rdlength;
  219. }
  220. return ptr - buffer;
  221. }
  222. #else /* TEST_FRAMEWORK */
  223. int ast_dns_test_write_string(const struct ast_dns_test_string *string, char *buf)
  224. {
  225. return 0;
  226. }
  227. int ast_dns_test_write_domain(const char *string, char *buf)
  228. {
  229. return 0;
  230. }
  231. int ast_dns_test_generate_result(struct ast_dns_query *query, void *records, size_t num_records,
  232. size_t record_size, record_fn generate, char *buffer)
  233. {
  234. return 0;
  235. }
  236. #endif