Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/dnssearch.c
2 views
/* SPDX-License-Identifier: MIT */1/*2* Domain search option for DHCP (RFC 3397)3*4* Copyright (c) 2012 Klaus Stengel5*6* Permission is hereby granted, free of charge, to any person obtaining a copy7* of this software and associated documentation files (the "Software"), to deal8* in the Software without restriction, including without limitation the rights9* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10* copies of the Software, and to permit persons to whom the Software is11* furnished to do so, subject to the following conditions:12*13* The above copyright notice and this permission notice shall be included in14* all copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL19* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* 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 IN22* THE SOFTWARE.23*/2425#include "slirp.h"2627static const uint8_t RFC3397_OPT_DOMAIN_SEARCH = 119;28static const uint8_t MAX_OPT_LEN = 255;29static const uint8_t OPT_HEADER_LEN = 2;30static const uint8_t REFERENCE_LEN = 2;3132struct compact_domain;3334typedef struct compact_domain {35struct compact_domain *self;36struct compact_domain *refdom;37uint8_t *labels;38size_t len;39size_t common_octets;40} CompactDomain;4142static size_t domain_suffix_diffoff(const CompactDomain *a,43const CompactDomain *b)44{45size_t la = a->len, lb = b->len;46uint8_t *da = a->labels + la, *db = b->labels + lb;47size_t i, lm = (la < lb) ? la : lb;4849for (i = 0; i < lm; i++) {50da--;51db--;52if (*da != *db) {53break;54}55}56return i;57}5859static int domain_suffix_ord(const void *cva, const void *cvb)60{61const CompactDomain *a = cva, *b = cvb;62size_t la = a->len, lb = b->len;63size_t doff = domain_suffix_diffoff(a, b);64uint8_t ca = a->labels[la - doff];65uint8_t cb = b->labels[lb - doff];6667if (ca < cb) {68return -1;69}70if (ca > cb) {71return 1;72}73if (la < lb) {74return -1;75}76if (la > lb) {77return 1;78}79return 0;80}8182static size_t domain_common_label(CompactDomain *a, CompactDomain *b)83{84size_t res, doff = domain_suffix_diffoff(a, b);85uint8_t *first_eq_pos = a->labels + (a->len - doff);86uint8_t *label = a->labels;8788while (*label && label < first_eq_pos) {89label += *label + 1;90}91res = a->len - (label - a->labels);92/* only report if it can help to reduce the packet size */93return (res > REFERENCE_LEN) ? res : 0;94}9596static void domain_fixup_order(CompactDomain *cd, size_t n)97{98size_t i;99100for (i = 0; i < n; i++) {101CompactDomain *cur = cd + i, *next = cd[i].self;102103while (!cur->common_octets) {104CompactDomain *tmp = next->self; /* backup target value */105106next->self = cur;107cur->common_octets++;108109cur = next;110next = tmp;111}112}113}114115static void domain_mklabels(CompactDomain *cd, const char *input)116{117uint8_t *len_marker = cd->labels;118uint8_t *output = len_marker; /* pre-incremented */119const char *in = input;120char cur_chr;121size_t len = 0;122123if (cd->len == 0) {124goto fail;125}126cd->len++;127128do {129cur_chr = *in++;130if (cur_chr == '.' || cur_chr == '\0') {131len = output - len_marker;132if ((len == 0 && cur_chr == '.') || len >= 64) {133goto fail;134}135*len_marker = len;136137output++;138len_marker = output;139} else {140output++;141*output = cur_chr;142}143} while (cur_chr != '\0');144145/* ensure proper zero-termination */146if (len != 0) {147*len_marker = 0;148cd->len++;149}150return;151152fail:153g_warning("failed to parse domain name '%s'\n", input);154cd->len = 0;155}156157static void domain_mkxrefs(CompactDomain *doms, CompactDomain *last,158size_t depth)159{160CompactDomain *i = doms, *target = doms;161162do {163if (i->labels < target->labels) {164target = i;165}166} while (i++ != last);167168for (i = doms; i != last; i++) {169CompactDomain *group_last;170size_t next_depth;171172if (i->common_octets == depth) {173continue;174}175176next_depth = -1;177for (group_last = i; group_last != last; group_last++) {178size_t co = group_last->common_octets;179if (co <= depth) {180break;181}182if (co < next_depth) {183next_depth = co;184}185}186domain_mkxrefs(i, group_last, next_depth);187188i = group_last;189if (i == last) {190break;191}192}193194if (depth == 0) {195return;196}197198i = doms;199do {200if (i != target && i->refdom == NULL) {201i->refdom = target;202i->common_octets = depth;203}204} while (i++ != last);205}206207static size_t domain_compactify(CompactDomain *domains, size_t n)208{209uint8_t *start = domains->self->labels, *outptr = start;210size_t i;211212for (i = 0; i < n; i++) {213CompactDomain *cd = domains[i].self;214CompactDomain *rd = cd->refdom;215216if (rd != NULL) {217size_t moff = (rd->labels - start) + (rd->len - cd->common_octets);218if (moff < 0x3FFFu) {219cd->len -= cd->common_octets - 2;220cd->labels[cd->len - 1] = moff & 0xFFu;221cd->labels[cd->len - 2] = 0xC0u | (moff >> 8);222}223}224225if (cd->labels != outptr) {226memmove(outptr, cd->labels, cd->len);227cd->labels = outptr;228}229outptr += cd->len;230}231return outptr - start;232}233234int translate_dnssearch(Slirp *s, const char **names)235{236size_t blocks, bsrc_start, bsrc_end, bdst_start;237size_t i, num_domains, memreq = 0;238uint8_t *result = NULL, *outptr;239CompactDomain *domains = NULL;240241num_domains = g_strv_length((GStrv)(void *)names);242if (num_domains == 0) {243return -2;244}245246domains = g_malloc(num_domains * sizeof(*domains));247248for (i = 0; i < num_domains; i++) {249size_t nlen = strlen(names[i]);250memreq += nlen + 2; /* 1 zero octet + 1 label length octet */251domains[i].self = domains + i;252domains[i].len = nlen;253domains[i].common_octets = 0;254domains[i].refdom = NULL;255}256257/* reserve extra 2 header bytes for each 255 bytes of output */258memreq += DIV_ROUND_UP(memreq, MAX_OPT_LEN) * OPT_HEADER_LEN;259result = g_malloc(memreq * sizeof(*result));260261outptr = result;262for (i = 0; i < num_domains; i++) {263domains[i].labels = outptr;264domain_mklabels(domains + i, names[i]);265if (domains[i].len == 0) {266/* Bogus entry, reject it all */267g_free(domains);268g_free(result);269return -1;270}271outptr += domains[i].len;272}273274qsort(domains, num_domains, sizeof(*domains), domain_suffix_ord);275domain_fixup_order(domains, num_domains);276277for (i = 1; i < num_domains; i++) {278size_t cl = domain_common_label(domains + i - 1, domains + i);279domains[i - 1].common_octets = cl;280}281282domain_mkxrefs(domains, domains + num_domains - 1, 0);283memreq = domain_compactify(domains, num_domains);284285blocks = DIV_ROUND_UP(memreq, MAX_OPT_LEN);286bsrc_end = memreq;287bsrc_start = (blocks - 1) * MAX_OPT_LEN;288bdst_start = bsrc_start + blocks * OPT_HEADER_LEN;289memreq += blocks * OPT_HEADER_LEN;290291while (blocks--) {292size_t len = bsrc_end - bsrc_start;293memmove(result + bdst_start, result + bsrc_start, len);294result[bdst_start - 2] = RFC3397_OPT_DOMAIN_SEARCH;295result[bdst_start - 1] = len;296bsrc_end = bsrc_start;297bsrc_start -= MAX_OPT_LEN;298bdst_start -= MAX_OPT_LEN + OPT_HEADER_LEN;299}300301g_free(domains);302s->vdnssearch = result;303s->vdnssearch_len = memreq;304return 0;305}306307308