PageRenderTime 1489ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/slirp/dnssearch.c

https://github.com/mstsirkin/qemu
C | 311 lines | 238 code | 47 blank | 26 comment | 64 complexity | 1b23bc9f3bb4c030c3ee45799a9cba4f MD5 | raw file
  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 "qemu/osdep.h"
  25. #include "slirp.h"
  26. static const uint8_t RFC3397_OPT_DOMAIN_SEARCH = 119;
  27. static const uint8_t MAX_OPT_LEN = 255;
  28. static const uint8_t OPT_HEADER_LEN = 2;
  29. static const uint8_t REFERENCE_LEN = 2;
  30. struct compact_domain;
  31. typedef struct compact_domain {
  32. struct compact_domain *self;
  33. struct compact_domain *refdom;
  34. uint8_t *labels;
  35. size_t len;
  36. size_t common_octets;
  37. } CompactDomain;
  38. static size_t
  39. domain_suffix_diffoff(const CompactDomain *a, const CompactDomain *b)
  40. {
  41. size_t la = a->len, lb = b->len;
  42. uint8_t *da = a->labels + la, *db = b->labels + lb;
  43. size_t i, lm = (la < lb) ? la : lb;
  44. for (i = 0; i < lm; i++) {
  45. da--; db--;
  46. if (*da != *db) {
  47. break;
  48. }
  49. }
  50. return i;
  51. }
  52. static int domain_suffix_ord(const void *cva, const void *cvb)
  53. {
  54. const CompactDomain *a = cva, *b = cvb;
  55. size_t la = a->len, lb = b->len;
  56. size_t doff = domain_suffix_diffoff(a, b);
  57. uint8_t ca = a->labels[la - doff];
  58. uint8_t cb = b->labels[lb - doff];
  59. if (ca < cb) {
  60. return -1;
  61. }
  62. if (ca > cb) {
  63. return 1;
  64. }
  65. if (la < lb) {
  66. return -1;
  67. }
  68. if (la > lb) {
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. static size_t domain_common_label(CompactDomain *a, CompactDomain *b)
  74. {
  75. size_t res, doff = domain_suffix_diffoff(a, b);
  76. uint8_t *first_eq_pos = a->labels + (a->len - doff);
  77. uint8_t *label = a->labels;
  78. while (*label && label < first_eq_pos) {
  79. label += *label + 1;
  80. }
  81. res = a->len - (label - a->labels);
  82. /* only report if it can help to reduce the packet size */
  83. return (res > REFERENCE_LEN) ? res : 0;
  84. }
  85. static void domain_fixup_order(CompactDomain *cd, size_t n)
  86. {
  87. size_t i;
  88. for (i = 0; i < n; i++) {
  89. CompactDomain *cur = cd + i, *next = cd[i].self;
  90. while (!cur->common_octets) {
  91. CompactDomain *tmp = next->self; /* backup target value */
  92. next->self = cur;
  93. cur->common_octets++;
  94. cur = next;
  95. next = tmp;
  96. }
  97. }
  98. }
  99. static void domain_mklabels(CompactDomain *cd, const char *input)
  100. {
  101. uint8_t *len_marker = cd->labels;
  102. uint8_t *output = len_marker; /* pre-incremented */
  103. const char *in = input;
  104. char cur_chr;
  105. size_t len = 0;
  106. if (cd->len == 0) {
  107. goto fail;
  108. }
  109. cd->len++;
  110. do {
  111. cur_chr = *in++;
  112. if (cur_chr == '.' || cur_chr == '\0') {
  113. len = output - len_marker;
  114. if ((len == 0 && cur_chr == '.') || len >= 64) {
  115. goto fail;
  116. }
  117. *len_marker = len;
  118. output++;
  119. len_marker = output;
  120. } else {
  121. output++;
  122. *output = cur_chr;
  123. }
  124. } while (cur_chr != '\0');
  125. /* ensure proper zero-termination */
  126. if (len != 0) {
  127. *len_marker = 0;
  128. cd->len++;
  129. }
  130. return;
  131. fail:
  132. g_warning("failed to parse domain name '%s'\n", input);
  133. cd->len = 0;
  134. }
  135. static void
  136. domain_mkxrefs(CompactDomain *doms, CompactDomain *last, size_t depth)
  137. {
  138. CompactDomain *i = doms, *target = doms;
  139. do {
  140. if (i->labels < target->labels) {
  141. target = i;
  142. }
  143. } while (i++ != last);
  144. for (i = doms; i != last; i++) {
  145. CompactDomain *group_last;
  146. size_t next_depth;
  147. if (i->common_octets == depth) {
  148. continue;
  149. }
  150. next_depth = -1;
  151. for (group_last = i; group_last != last; group_last++) {
  152. size_t co = group_last->common_octets;
  153. if (co <= depth) {
  154. break;
  155. }
  156. if (co < next_depth) {
  157. next_depth = co;
  158. }
  159. }
  160. domain_mkxrefs(i, group_last, next_depth);
  161. i = group_last;
  162. if (i == last) {
  163. break;
  164. }
  165. }
  166. if (depth == 0) {
  167. return;
  168. }
  169. i = doms;
  170. do {
  171. if (i != target && i->refdom == NULL) {
  172. i->refdom = target;
  173. i->common_octets = depth;
  174. }
  175. } while (i++ != last);
  176. }
  177. static size_t domain_compactify(CompactDomain *domains, size_t n)
  178. {
  179. uint8_t *start = domains->self->labels, *outptr = start;
  180. size_t i;
  181. for (i = 0; i < n; i++) {
  182. CompactDomain *cd = domains[i].self;
  183. CompactDomain *rd = cd->refdom;
  184. if (rd != NULL) {
  185. size_t moff = (rd->labels - start)
  186. + (rd->len - cd->common_octets);
  187. if (moff < 0x3FFFu) {
  188. cd->len -= cd->common_octets - 2;
  189. cd->labels[cd->len - 1] = moff & 0xFFu;
  190. cd->labels[cd->len - 2] = 0xC0u | (moff >> 8);
  191. }
  192. }
  193. if (cd->labels != outptr) {
  194. memmove(outptr, cd->labels, cd->len);
  195. cd->labels = outptr;
  196. }
  197. outptr += cd->len;
  198. }
  199. return outptr - start;
  200. }
  201. int translate_dnssearch(Slirp *s, const char **names)
  202. {
  203. size_t blocks, bsrc_start, bsrc_end, bdst_start;
  204. size_t i, num_domains, memreq = 0;
  205. uint8_t *result = NULL, *outptr;
  206. CompactDomain *domains = NULL;
  207. const char **nameptr = names;
  208. while (*nameptr != NULL) {
  209. nameptr++;
  210. }
  211. num_domains = nameptr - names;
  212. if (num_domains == 0) {
  213. return -2;
  214. }
  215. domains = g_malloc(num_domains * sizeof(*domains));
  216. for (i = 0; i < num_domains; i++) {
  217. size_t nlen = strlen(names[i]);
  218. memreq += nlen + 2; /* 1 zero octet + 1 label length octet */
  219. domains[i].self = domains + i;
  220. domains[i].len = nlen;
  221. domains[i].common_octets = 0;
  222. domains[i].refdom = NULL;
  223. }
  224. /* reserve extra 2 header bytes for each 255 bytes of output */
  225. memreq += DIV_ROUND_UP(memreq, MAX_OPT_LEN) * OPT_HEADER_LEN;
  226. result = g_malloc(memreq * sizeof(*result));
  227. outptr = result;
  228. for (i = 0; i < num_domains; i++) {
  229. domains[i].labels = outptr;
  230. domain_mklabels(domains + i, names[i]);
  231. outptr += domains[i].len;
  232. }
  233. if (outptr == result) {
  234. g_free(domains);
  235. g_free(result);
  236. return -1;
  237. }
  238. qsort(domains, num_domains, sizeof(*domains), domain_suffix_ord);
  239. domain_fixup_order(domains, num_domains);
  240. for (i = 1; i < num_domains; i++) {
  241. size_t cl = domain_common_label(domains + i - 1, domains + i);
  242. domains[i - 1].common_octets = cl;
  243. }
  244. domain_mkxrefs(domains, domains + num_domains - 1, 0);
  245. memreq = domain_compactify(domains, num_domains);
  246. blocks = DIV_ROUND_UP(memreq, MAX_OPT_LEN);
  247. bsrc_end = memreq;
  248. bsrc_start = (blocks - 1) * MAX_OPT_LEN;
  249. bdst_start = bsrc_start + blocks * OPT_HEADER_LEN;
  250. memreq += blocks * OPT_HEADER_LEN;
  251. while (blocks--) {
  252. size_t len = bsrc_end - bsrc_start;
  253. memmove(result + bdst_start, result + bsrc_start, len);
  254. result[bdst_start - 2] = RFC3397_OPT_DOMAIN_SEARCH;
  255. result[bdst_start - 1] = len;
  256. bsrc_end = bsrc_start;
  257. bsrc_start -= MAX_OPT_LEN;
  258. bdst_start -= MAX_OPT_LEN + OPT_HEADER_LEN;
  259. }
  260. g_free(domains);
  261. s->vdnssearch = result;
  262. s->vdnssearch_len = memreq;
  263. return 0;
  264. }