dns-domain: add dns_name_to_wire_format()
The function converts a domain name string to the wire format described in RFC 1035 Section 3.1.
This commit is contained in:
@@ -715,3 +715,37 @@ int dns_name_single_label(const char *name) {
|
||||
|
||||
return r == 0 && *name == 0;
|
||||
}
|
||||
|
||||
/* Encode a domain name according to RFC 1035 Section 3.1 */
|
||||
int dns_name_to_wire_format(const char *domain, uint8_t *buffer, size_t len) {
|
||||
uint8_t *label_length;
|
||||
uint8_t *out;
|
||||
int r;
|
||||
|
||||
assert_return(buffer, -EINVAL);
|
||||
assert_return(domain, -EINVAL);
|
||||
assert_return(domain[0], -EINVAL);
|
||||
|
||||
out = buffer;
|
||||
|
||||
do {
|
||||
/* reserve a byte for label length */
|
||||
if (len == 0)
|
||||
return -ENOBUFS;
|
||||
len--;
|
||||
label_length = out;
|
||||
out++;
|
||||
|
||||
/* convert and copy a single label */
|
||||
r = dns_label_unescape(&domain, (char *) out, len);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
/* fill label length, move forward */
|
||||
*label_length = r;
|
||||
out += r;
|
||||
len -= r;
|
||||
} while (r != 0);
|
||||
|
||||
return out - buffer;
|
||||
}
|
||||
|
||||
@@ -67,3 +67,5 @@ int dns_name_address(const char *p, int *family, union in_addr_union *a);
|
||||
|
||||
int dns_name_root(const char *name);
|
||||
int dns_name_single_label(const char *name);
|
||||
|
||||
int dns_name_to_wire_format(const char *domain, uint8_t *buffer, size_t len);
|
||||
|
||||
Reference in New Issue
Block a user