1
0
mirror of https://github.com/moparisthebest/curl synced 2025-01-10 21:48:10 -05:00

doh: keep the IPv4 address in (original) network byte order

Ideally this will fix the reversed order shown in SPARC tests:

  resp 8: Expected 127.0.0.1 got 1.0.0.127

Closes #3091
This commit is contained in:
Daniel Stenberg 2018-10-04 11:57:29 +02:00
parent ec49132faf
commit 7f00146d00
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
3 changed files with 19 additions and 24 deletions

View File

@ -358,7 +358,7 @@ static DOHcode store_a(unsigned char *doh, int index, struct dohentry *d)
if(d->numaddr < DOH_MAX_ADDR) { if(d->numaddr < DOH_MAX_ADDR) {
struct dohaddr *a = &d->addr[d->numaddr]; struct dohaddr *a = &d->addr[d->numaddr];
a->type = DNS_TYPE_A; a->type = DNS_TYPE_A;
a->ip.v4 = ntohl(get32bit(doh, index)); memcpy(&a->ip.v4, &doh[index], 4);
d->numaddr++; d->numaddr++;
} }
return DOH_OK; return DOH_OK;
@ -369,9 +369,8 @@ static DOHcode store_aaaa(unsigned char *doh, int index, struct dohentry *d)
/* silently ignore addresses over the limit */ /* silently ignore addresses over the limit */
if(d->numaddr < DOH_MAX_ADDR) { if(d->numaddr < DOH_MAX_ADDR) {
struct dohaddr *a = &d->addr[d->numaddr]; struct dohaddr *a = &d->addr[d->numaddr];
struct addr6 *inet6p = &a->ip.v6;
a->type = DNS_TYPE_AAAA; a->type = DNS_TYPE_AAAA;
memcpy(inet6p, &doh[index], 16); memcpy(&a->ip.v6, &doh[index], 16);
d->numaddr++; d->numaddr++;
} }
return DOH_OK; return DOH_OK;
@ -649,9 +648,9 @@ static void showdoh(struct Curl_easy *data,
for(i = 0; i < d->numaddr; i++) { for(i = 0; i < d->numaddr; i++) {
struct dohaddr *a = &d->addr[i]; struct dohaddr *a = &d->addr[i];
if(a->type == DNS_TYPE_A) { if(a->type == DNS_TYPE_A) {
infof(data, "DOH A: %d.%d.%d.%d\n", infof(data, "DOH A: %u.%u.%u.%u\n",
a->ip.v4 & 0xff, (a->ip.v4>>8) & 0xff, a->ip.v4[0], a->ip.v4[1],
(a->ip.v4>>16) & 0xff, a->ip.v4 >>24); a->ip.v4[2], a->ip.v4[3]);
} }
else if(a->type == DNS_TYPE_AAAA) { else if(a->type == DNS_TYPE_AAAA) {
int j; int j;
@ -663,8 +662,8 @@ static void showdoh(struct Curl_easy *data,
len = 118; len = 118;
for(j = 0; j < 16; j += 2) { for(j = 0; j < 16; j += 2) {
size_t l; size_t l;
snprintf(ptr, len, "%s%02x%02x", j?":":"", d->addr[i].ip.v6.byte[j], snprintf(ptr, len, "%s%02x%02x", j?":":"", d->addr[i].ip.v6[j],
d->addr[i].ip.v6.byte[j + 1]); d->addr[i].ip.v6[j + 1]);
l = strlen(ptr); l = strlen(ptr);
len -= l; len -= l;
ptr += l; ptr += l;
@ -764,7 +763,7 @@ doh2ai(const struct dohentry *de, const char *hostname, int port)
switch(ai->ai_family) { switch(ai->ai_family) {
case AF_INET: case AF_INET:
addr = (void *)ai->ai_addr; /* storage area for this info */ addr = (void *)ai->ai_addr; /* storage area for this info */
DEBUGASSERT(sizeof(struct in_addr) == sizeof(de->addr[i].ip.v4));
memcpy(&addr->sin_addr, &de->addr[i].ip.v4, sizeof(struct in_addr)); memcpy(&addr->sin_addr, &de->addr[i].ip.v4, sizeof(struct in_addr));
addr->sin_family = (CURL_SA_FAMILY_T)addrtype; addr->sin_family = (CURL_SA_FAMILY_T)addrtype;
addr->sin_port = htons((unsigned short)port); addr->sin_port = htons((unsigned short)port);
@ -773,7 +772,7 @@ doh2ai(const struct dohentry *de, const char *hostname, int port)
#ifdef ENABLE_IPV6 #ifdef ENABLE_IPV6
case AF_INET6: case AF_INET6:
addr6 = (void *)ai->ai_addr; /* storage area for this info */ addr6 = (void *)ai->ai_addr; /* storage area for this info */
DEBUGASSERT(sizeof(struct in6_addr) == sizeof(de->addr[i].ip.v6));
memcpy(&addr6->sin6_addr, &de->addr[i].ip.v6, sizeof(struct in6_addr)); memcpy(&addr6->sin6_addr, &de->addr[i].ip.v6, sizeof(struct in6_addr));
addr6->sin6_family = (CURL_SA_FAMILY_T)addrtype; addr6->sin6_family = (CURL_SA_FAMILY_T)addrtype;
addr6->sin6_port = htons((unsigned short)port); addr6->sin6_port = htons((unsigned short)port);

View File

@ -67,10 +67,6 @@ typedef enum {
#define DOH_MAX_ADDR 24 #define DOH_MAX_ADDR 24
#define DOH_MAX_CNAME 4 #define DOH_MAX_CNAME 4
struct addr6 {
unsigned char byte[16];
};
struct cnamestore { struct cnamestore {
size_t len; /* length of cname */ size_t len; /* length of cname */
char *alloc; /* allocated pointer */ char *alloc; /* allocated pointer */
@ -80,8 +76,8 @@ struct cnamestore {
struct dohaddr { struct dohaddr {
int type; int type;
union { union {
unsigned int v4; unsigned char v4[4]; /* network byte order */
struct addr6 v6; unsigned char v6[16];
} ip; } ip;
}; };

View File

@ -155,6 +155,7 @@ UNITTEST_START
size_t size; size_t size;
unsigned char buffer[256]; unsigned char buffer[256];
size_t i; size_t i;
unsigned char *p;
for(i = 0; i < sizeof(req) / sizeof(req[0]); i++) { for(i = 0; i < sizeof(req) / sizeof(req[0]); i++) {
int rc = doh_encode(req[i].name, req[i].type, int rc = doh_encode(req[i].name, req[i].type,
buffer, sizeof(buffer), &size); buffer, sizeof(buffer), &size);
@ -198,9 +199,8 @@ UNITTEST_START
struct dohaddr *a; struct dohaddr *a;
a = &d.addr[u]; a = &d.addr[u];
if(resp[i].type == DNS_TYPE_A) { if(resp[i].type == DNS_TYPE_A) {
snprintf(ptr, len, "%d.%d.%d.%d ", p = &a->ip.v4[0];
a->ip.v4 & 0xff, (a->ip.v4>>8) & 0xff, snprintf(ptr, len, "%u.%u.%u.%u ", p[0], p[1], p[2], p[3]);
(a->ip.v4>>16) & 0xff, a->ip.v4 >>24);
o = strlen(ptr); o = strlen(ptr);
len -= o; len -= o;
ptr += o; ptr += o;
@ -209,8 +209,8 @@ UNITTEST_START
int j; int j;
for(j = 0; j < 16; j += 2) { for(j = 0; j < 16; j += 2) {
size_t l; size_t l;
snprintf(ptr, len, "%s%02x%02x", j?":":"", a->ip.v6.byte[j], snprintf(ptr, len, "%s%02x%02x", j?":":"", a->ip.v6[j],
a->ip.v6.byte[j + 1]); a->ip.v6[j + 1]);
l = strlen(ptr); l = strlen(ptr);
len -= l; len -= l;
ptr += l; ptr += l;
@ -270,9 +270,9 @@ UNITTEST_START
DNS_TYPE_A, &d); DNS_TYPE_A, &d);
fail_if(d.numaddr != 1, "missing address"); fail_if(d.numaddr != 1, "missing address");
a = &d.addr[0]; a = &d.addr[0];
snprintf((char *)buffer, sizeof(buffer), "%d.%d.%d.%d", p = &a->ip.v4[0];
a->ip.v4 & 0xff, (a->ip.v4>>8) & 0xff, snprintf((char *)buffer, sizeof(buffer),
(a->ip.v4>>16) & 0xff, a->ip.v4 >>24); "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
if(rc || strcmp((char *)buffer, "127.0.0.1")) { if(rc || strcmp((char *)buffer, "127.0.0.1")) {
fprintf(stderr, "bad address decoded: %s, rc == %d\n", buffer, rc); fprintf(stderr, "bad address decoded: %s, rc == %d\n", buffer, rc);
return 7; return 7;