mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-01 13:10:43 +00:00
Add mg_dns_encode_name_s for non-NUL terminated strings
PUBLISHED_FROM=012e2939021d1b275a39632fd2982891d2c98c4e
This commit is contained in:
1 parent
cb602f178c
commit
10b11b03a8
4 files changed
+39
-20
No files matched your search
+14
-10
@@ -85,35 +85,39 @@ int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg) {
|
||||
return mbuf_append(io, begin, end - begin);
|
||||
}
|
||||
|
||||
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len) {
|
||||
int mg_dns_encode_name_s(struct mbuf *io, struct mg_str name) {
|
||||
const char *s;
|
||||
unsigned char n;
|
||||
size_t pos = io->len;
|
||||
|
||||
do {
|
||||
if ((s = strchr(name, '.')) == NULL) {
|
||||
s = name + len;
|
||||
if ((s = mg_strchr(name, '.')) == NULL) {
|
||||
s = name.p + name.len;
|
||||
}
|
||||
|
||||
if (s - name > 127) {
|
||||
if (s - name.p > 127) {
|
||||
return -1; /* TODO(mkm) cover */
|
||||
}
|
||||
n = s - name; /* chunk length */
|
||||
n = s - name.p; /* chunk length */
|
||||
mbuf_append(io, &n, 1); /* send length */
|
||||
mbuf_append(io, name, n);
|
||||
mbuf_append(io, name.p, n);
|
||||
|
||||
if (*s == '.') {
|
||||
if (n < name.len && *s == '.') {
|
||||
n++;
|
||||
}
|
||||
|
||||
name += n;
|
||||
len -= n;
|
||||
} while (*s != '\0');
|
||||
name.p += n;
|
||||
name.len -= n;
|
||||
} while (name.len > 0);
|
||||
mbuf_append(io, "\0", 1); /* Mark end of host name */
|
||||
|
||||
return io->len - pos;
|
||||
}
|
||||
|
||||
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len) {
|
||||
return mg_dns_encode_name_s(io, mg_mk_str_n(name, len));
|
||||
}
|
||||
|
||||
int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
|
||||
const char *name, size_t nlen, const void *rdata,
|
||||
size_t rlen) {
|
||||
|
||||
@@ -124,6 +124,7 @@ int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
|
||||
* Encodes a DNS name.
|
||||
*/
|
||||
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len);
|
||||
int mg_dns_encode_name_s(struct mbuf *io, struct mg_str name);
|
||||
|
||||
/* Low-level: parses a DNS response. */
|
||||
int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg);
|
||||
|
||||
Reference in new issue
Block a user