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.

311 lines
8.1KB

  1. /*
  2. * Domain search option for DHCP (RFC 3397)
  3. *
  4. * Copyright (c) 2012 Klaus Stengel
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "slirp.h"
  25. static const uint8_t RFC3397_OPT_DOMAIN_SEARCH = 119;
  26. static const uint8_t MAX_OPT_LEN = 255;
  27. static const uint8_t OPT_HEADER_LEN = 2;
  28. static const uint8_t REFERENCE_LEN = 2;
  29. struct compact_domain;
  30. typedef struct compact_domain {
  31. struct compact_domain *self;
  32. struct compact_domain *refdom;
  33. uint8_t *labels;
  34. size_t len;
  35. size_t common_octets;
  36. } CompactDomain;
  37. static size_t
  38. domain_suffix_diffoff(const CompactDomain *a, const CompactDomain *b)
  39. {
  40. size_t la = a->len, lb = b->len;
  41. uint8_t *da = a->labels + la, *db = b->labels + lb;
  42. size_t i, lm = (la < lb) ? la : lb;
  43. for (i = 0; i < lm; i++) {
  44. da--; db--;
  45. if (*da != *db) {
  46. break;
  47. }
  48. }
  49. return i;
  50. }
  51. static int domain_suffix_ord(const void *cva, const void *cvb)
  52. {
  53. const CompactDomain *a = cva, *b = cvb;
  54. size_t la = a->len, lb = b->len;
  55. size_t doff = domain_suffix_diffoff(a, b);
  56. uint8_t ca = a->labels[la - doff];
  57. uint8_t cb = b->labels[lb - doff];
  58. if (ca < cb) {
  59. return -1;
  60. }
  61. if (ca > cb) {
  62. return 1;
  63. }
  64. if (la < lb) {
  65. return -1;
  66. }
  67. if (la > lb) {
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. static size_t domain_common_label(CompactDomain *a, CompactDomain *b)
  73. {
  74. size_t res, doff = domain_suffix_diffoff(a, b);
  75. uint8_t *first_eq_pos = a->labels + (a->len - doff);
  76. uint8_t *label = a->labels;
  77. while (*label && label < first_eq_pos) {
  78. label += *label + 1;
  79. }
  80. res = a->len - (label - a->labels);
  81. /* only report if it can help to reduce the packet size */
  82. return (res > REFERENCE_LEN) ? res : 0;
  83. }
  84. static void domain_fixup_order(CompactDomain *cd, size_t n)
  85. {
  86. size_t i;
  87. for (i = 0; i < n; i++) {
  88. CompactDomain *cur = cd + i, *next = cd[i].self;
  89. while (!cur->common_octets) {
  90. CompactDomain *tmp = next->self; /* backup target value */
  91. next->self = cur;
  92. cur->common_octets++;
  93. cur = next;
  94. next = tmp;
  95. }
  96. }
  97. }
  98. static void domain_mklabels(CompactDomain *cd, const char *input)
  99. {
  100. uint8_t *len_marker = cd->labels;
  101. uint8_t *output = len_marker; /* pre-incremented */
  102. const char *in = input;
  103. char cur_chr;
  104. size_t len = 0;
  105. if (cd->len == 0) {
  106. goto fail;
  107. }
  108. cd->len++;
  109. do {
  110. cur_chr = *in++;
  111. if (cur_chr == '.' || cur_chr == '\0') {
  112. len = output - len_marker;
  113. if ((len == 0 && cur_chr == '.') || len >= 64) {
  114. goto fail;
  115. }
  116. *len_marker = len;
  117. output++;
  118. len_marker = output;
  119. } else {
  120. output++;
  121. *output = cur_chr;
  122. }
  123. } while (cur_chr != '\0');
  124. /* ensure proper zero-termination */
  125. if (len != 0) {
  126. *len_marker = 0;
  127. cd->len++;
  128. }
  129. return;
  130. fail:
  131. g_warning("failed to parse domain name '%s'\n", input);
  132. cd->len = 0;
  133. }
  134. static void
  135. domain_mkxrefs(CompactDomain *doms, CompactDomain *last, size_t depth)
  136. {
  137. CompactDomain *i = doms, *target = doms;
  138. do {
  139. if (i->labels < target->labels) {
  140. target = i;
  141. }
  142. } while (i++ != last);
  143. for (i = doms; i != last; i++) {
  144. CompactDomain *group_last;
  145. size_t next_depth;
  146. if (i->common_octets == depth) {
  147. continue;
  148. }
  149. next_depth = -1;
  150. for (group_last = i; group_last != last; group_last++) {
  151. size_t co = group_last->common_octets;
  152. if (co <= depth) {
  153. break;
  154. }
  155. if (co < next_depth) {
  156. next_depth = co;
  157. }
  158. }
  159. domain_mkxrefs(i, group_last, next_depth);
  160. i = group_last;
  161. if (i == last) {
  162. break;
  163. }
  164. }
  165. if (depth == 0) {
  166. return;
  167. }
  168. i = doms;
  169. do {
  170. if (i != target && i->refdom == NULL) {
  171. i->refdom = target;
  172. i->common_octets = depth;
  173. }
  174. } while (i++ != last);
  175. }
  176. static size_t domain_compactify(CompactDomain *domains, size_t n)
  177. {
  178. uint8_t *start = domains->self->labels, *outptr = start;
  179. size_t i;
  180. for (i = 0; i < n; i++) {
  181. CompactDomain *cd = domains[i].self;
  182. CompactDomain *rd = cd->refdom;
  183. if (rd != NULL) {
  184. size_t moff = (rd->labels - start)
  185. + (rd->len - cd->common_octets);
  186. if (moff < 0x3FFFu) {
  187. cd->len -= cd->common_octets - 2;
  188. cd->labels[cd->len - 1] = moff & 0xFFu;
  189. cd->labels[cd->len - 2] = 0xC0u | (moff >> 8);
  190. }
  191. }
  192. if (cd->labels != outptr) {
  193. memmove(outptr, cd->labels, cd->len);
  194. cd->labels = outptr;
  195. }
  196. outptr += cd->len;
  197. }
  198. return outptr - start;
  199. }
  200. int translate_dnssearch(Slirp *s, const char **names)
  201. {
  202. size_t blocks, bsrc_start, bsrc_end, bdst_start;
  203. size_t i, num_domains, memreq = 0;
  204. uint8_t *result = NULL, *outptr;
  205. CompactDomain *domains = NULL;
  206. const char **nameptr = names;
  207. while (*nameptr != NULL) {
  208. nameptr++;
  209. }
  210. num_domains = nameptr - names;
  211. if (num_domains == 0) {
  212. return -2;
  213. }
  214. domains = g_malloc(num_domains * sizeof(*domains));
  215. for (i = 0; i < num_domains; i++) {
  216. size_t nlen = strlen(names[i]);
  217. memreq += nlen + 2; /* 1 zero octet + 1 label length octet */
  218. domains[i].self = domains + i;
  219. domains[i].len = nlen;
  220. domains[i].common_octets = 0;
  221. domains[i].refdom = NULL;
  222. }
  223. /* reserve extra 2 header bytes for each 255 bytes of output */
  224. memreq += DIV_ROUND_UP(memreq, MAX_OPT_LEN) * OPT_HEADER_LEN;
  225. result = g_malloc(memreq * sizeof(*result));
  226. outptr = result;
  227. for (i = 0; i < num_domains; i++) {
  228. domains[i].labels = outptr;
  229. domain_mklabels(domains + i, names[i]);
  230. outptr += domains[i].len;
  231. }
  232. if (outptr == result) {
  233. g_free(domains);
  234. g_free(result);
  235. return -1;
  236. }
  237. qsort(domains, num_domains, sizeof(*domains), domain_suffix_ord);
  238. domain_fixup_order(domains, num_domains);
  239. for (i = 1; i < num_domains; i++) {
  240. size_t cl = domain_common_label(domains + i - 1, domains + i);
  241. domains[i - 1].common_octets = cl;
  242. }
  243. domain_mkxrefs(domains, domains + num_domains - 1, 0);
  244. memreq = domain_compactify(domains, num_domains);
  245. blocks = DIV_ROUND_UP(memreq, MAX_OPT_LEN);
  246. bsrc_end = memreq;
  247. bsrc_start = (blocks - 1) * MAX_OPT_LEN;
  248. bdst_start = bsrc_start + blocks * OPT_HEADER_LEN;
  249. memreq += blocks * OPT_HEADER_LEN;
  250. while (blocks--) {
  251. size_t len = bsrc_end - bsrc_start;
  252. memmove(result + bdst_start, result + bsrc_start, len);
  253. result[bdst_start - 2] = RFC3397_OPT_DOMAIN_SEARCH;
  254. result[bdst_start - 1] = len;
  255. bsrc_end = bsrc_start;
  256. bsrc_start -= MAX_OPT_LEN;
  257. bdst_start -= MAX_OPT_LEN + OPT_HEADER_LEN;
  258. }
  259. g_free(domains);
  260. s->vdnssearch = result;
  261. s->vdnssearch_len = memreq;
  262. return 0;
  263. }