Skip to content

Commit

Permalink
dns: read domain names from tld.h as bytes, not strings
Browse files Browse the repository at this point in the history
Also updates the hard-coded root zone,
see handshake-org/hs-names#12
  • Loading branch information
pinheadmz committed Sep 28, 2022
1 parent ca447c8 commit bf6108b
Show file tree
Hide file tree
Showing 4 changed files with 7,610 additions and 8,555 deletions.
27 changes: 13 additions & 14 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -2553,34 +2553,33 @@ hsk_dns_name_cmp(const uint8_t *a, const uint8_t *b) {
uint8_t off = 0;

for (;;) {
// compare label length bytes
uint8_t labela = a[off];
uint8_t labelb = b[off];

if (labela < labelb)
return -1;

if (labela > labelb)
return 1;

if (labela == 0)
if (labelb == 0 && labela == 0)
return 0;

// labels are equal non-zero length: compare label
for (; labela > 0; labela--) {
for (; labela >= 0; labela--, labelb--) {
off++;

if (labela == 0 && labelb != 0)
return -1; // name b is longer

if (labelb == 0 && labela != 0)
return 1; // name a is longer

if (labelb == 0 && labela == 0)
break; // labels are the same

// neither label is 0 length,
// compare characters until one label runs out
if (a[off] > b[off])
return 1;

if (a[off] < b[off])
return -1;
}

off++;
}

return 0;
}

bool
Expand Down
3 changes: 1 addition & 2 deletions src/ns.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,7 @@ hsk_tld_index(const uint8_t *tld) {

while (start <= end) {
int pos = (start + end) >> 1;
int cmp = strcmp(HSK_TLD_NAMES[pos], (char *)&tld[1]);

int cmp = hsk_dns_name_cmp((const uint8_t *)HSK_TLD_NAMES[pos], tld);
if (cmp == 0)
return pos;

Expand Down
Loading

0 comments on commit bf6108b

Please sign in to comment.