-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdomain_to_str.c
73 lines (63 loc) · 1.92 KB
/
domain_to_str.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* domain_to_str.c - convert wire-format DNS name to presentation format
* from wreck/wdns/msg/domain_to_str.c
*/
/*
* Copyright (c) 2009, 2010 by Internet Systems Consortium, Inc. ("ISC")
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
/**
* Convert a domain name to a human-readable string.
*
* \param[in] src domain name in wire format
* \param[in] src_len length of domain name in bytes
* \param[out] dst caller-allocated string buffer of size WDNS_PRESLEN_NAME
*
* \return Number of bytes read from src.
*/
size_t
domain_to_str(const uint8_t *src, size_t src_len, char *dst) {
size_t bytes_read = 0;
size_t bytes_remaining = src_len;
uint8_t oclen;
assert(src != NULL);
oclen = *src;
while (bytes_remaining > 0 && oclen != 0) {
src++;
bytes_remaining--;
bytes_read += oclen + 1 /* length octet */;
while (oclen-- && bytes_remaining > 0) {
uint8_t c = *src++;
bytes_remaining--;
if (c == '.') {
*dst++ = '\\';
*dst++ = c;
} else if (c >= '!' && c <= '~') {
*dst++ = c;
} else {
snprintf(dst, 5, "\\%.3d", c);
dst += 4;
}
}
*dst++ = '.';
oclen = *src;
}
if (bytes_read == 0)
*dst++ = '.';
bytes_read++;
*dst = '\0';
return (bytes_read);
}