Skip to content

Commit 2fe6fae

Browse files
committed
doh: Change cmd line options -4 and -6 to specify transport only
Prior to this change -4 and -6 specified the resolve type for the DOH server and the user-specified hostname. (eg if -6 then connect to DOH server via IPv6 and request hostname AAAA records). That behavior could conflict since the -tTYPE option (requested record type) was added. Now -4/-6 only specify the resolve type for the DOH server. (eg if -6 then connect to DOH server via IPv6 and request hostname records based on -tTYPE, or all records if no type specified). Follow-up to acc9ab5. Closes #30
1 parent 88abf69 commit 2fe6fae

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
*.idb
4343
*.pdb
4444

45+
# Visual Studio files
46+
*.suo
47+
4548
# Kernel Module Compile Results
4649
*.mod*
4750
*.cmd

doh.1

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ Test mode. Returns an exit code if there isn't a proper DoH response.
2525
Verbose mode. Shows lots of details from the underlying HTTPS connection and
2626
transfer.
2727
.IP \-4
28-
Use only IPv4 transport
28+
Use only IPv4 transport to DOH server.
2929
.IP \-6
30-
Use only IPv6 transport
30+
Use only IPv6 transport to DOH server.
3131
.IP "-r NAME:PORT:ADDRESS"
3232
Provide a fixed IP address for the given host name + port pair. This option
3333
can be provided several times. This preloads the DNS cache.

doh.c

+39-28
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,14 @@ struct dnsentry {
217217

218218
static const char *type2name(int dnstype)
219219
{
220-
return (dnstype == 1)?"A":"AAAA";
220+
switch(dnstype) {
221+
case DNS_TYPE_A: return "A";
222+
case DNS_TYPE_NS: return "NS";
223+
case DNS_TYPE_CNAME: return "CNAME";
224+
case DNS_TYPE_TXT: return "TXT";
225+
case DNS_TYPE_AAAA: return "AAAA";
226+
}
227+
return "Unknown";
221228
}
222229

223230
static size_t
@@ -371,6 +378,7 @@ static DOHcode cnameappend(struct cnamestore *c,
371378
ptr = realloc(c->alloc, c->allocsize);
372379
if(!ptr) {
373380
free(c->alloc);
381+
c->alloc = NULL;
374382
return DOH_OUT_OF_MEM;
375383
}
376384
c->alloc = ptr;
@@ -755,7 +763,7 @@ static void help(const char *msg)
755763
fprintf(stderr, "Usage: doh [options] <host> [URL]\n"
756764
" -h this help\n"
757765
" -k insecure mode - don't validate TLS certificate\n"
758-
" -tTYPE (e.g., TXT, A, AAAA)\n"
766+
" -tTYPE (e.g., TXT, CNAME, A, AAAA)\n"
759767
" (to specify record type)\n"
760768
" -T test mode\n"
761769
" -v verbose mode\n"
@@ -867,43 +875,39 @@ int main(int argc, char **argv)
867875
doh_init(&d);
868876

869877
for(i = 0; i < n_urls; i++) {
870-
if((transport == v4 || transport == v46) &&
871-
(query_type == 0 || query_type == DNS_TYPE_A)) {
878+
if(query_type == 0 || query_type == DNS_TYPE_A) {
872879
rc = initprobe(DNS_TYPE_A, host, urls[i], multi,
873880
trace_enabled, headers, insecure_mode,
874881
transport, resolve);
875882
if(rc != 0) {
876-
fprintf(stderr, "initprobe() failed (v4)\n");
883+
fprintf(stderr, "initprobe() failed (DNS_TYPE_A)\n");
877884
exit(1);
878885
}
879886
}
880-
if((transport == v6 || transport == v46) &&
881-
(query_type == 0 || query_type == DNS_TYPE_AAAA)) {
887+
if(query_type == 0 || query_type == DNS_TYPE_AAAA) {
882888
rc = initprobe(DNS_TYPE_AAAA, host, urls[i], multi,
883889
trace_enabled, headers, insecure_mode,
884890
transport, resolve);
885891
if(rc != 0) {
886-
fprintf(stderr, "initprobe() failed (v6)\n");
892+
fprintf(stderr, "initprobe() failed (DNS_TYPE_AAAA)\n");
887893
exit(1);
888894
}
889895
}
890-
891896
if(query_type == DNS_TYPE_TXT) {
892897
rc = initprobe(DNS_TYPE_TXT, host, urls[i], multi,
893898
trace_enabled, headers, insecure_mode,
894899
transport, resolve);
895900
if(rc != 0) {
896-
fprintf(stderr, "initprobe() failed (v6)\n");
901+
fprintf(stderr, "initprobe() failed (DNS_TYPE_TXT)\n");
897902
exit(1);
898903
}
899904
}
900-
901905
if(query_type == DNS_TYPE_CNAME) {
902906
rc = initprobe(DNS_TYPE_CNAME, host, urls[i], multi,
903907
trace_enabled, headers, insecure_mode,
904908
transport, resolve);
905909
if(rc != 0) {
906-
fprintf(stderr, "initprobe() failed (v6)\n");
910+
fprintf(stderr, "initprobe() failed (DNS_TYPE_CNAME)\n");
907911
exit(1);
908912
}
909913
}
@@ -992,27 +996,34 @@ int main(int argc, char **argv)
992996
int i;
993997
printf("[%s]\n", host);
994998
printf("TTL: %u seconds\n", d.ttl);
995-
for(i=0; i < d.numv4; i++) {
996-
printf("A: %d.%d.%d.%d\n",
997-
d.v4addr[i]>>24,
998-
(d.v4addr[i]>>16) & 0xff,
999-
(d.v4addr[i]>>8) & 0xff,
1000-
d.v4addr[i] & 0xff);
999+
if(query_type == 0 || query_type == DNS_TYPE_A) {
1000+
for(i=0; i < d.numv4; i++) {
1001+
printf("A: %d.%d.%d.%d\n",
1002+
d.v4addr[i]>>24,
1003+
(d.v4addr[i]>>16) & 0xff,
1004+
(d.v4addr[i]>>8) & 0xff,
1005+
d.v4addr[i] & 0xff);
1006+
}
10011007
}
1002-
for(i=0; i < d.numv6; i++) {
1003-
int j;
1004-
printf("AAAA: ");
1005-
for(j=0; j<16; j+=2) {
1006-
printf("%s%02x%02x", j ? ":" : "", d.v6addr[i].byte[j],
1007-
d.v6addr[i].byte[j+1]);
1008+
if(query_type == 0 || query_type == DNS_TYPE_AAAA) {
1009+
for(i=0; i < d.numv6; i++) {
1010+
int j;
1011+
printf("AAAA: ");
1012+
for(j=0; j<16; j+=2) {
1013+
printf("%s%02x%02x", j ? ":" : "", d.v6addr[i].byte[j],
1014+
d.v6addr[i].byte[j+1]);
1015+
}
1016+
printf("\n");
10081017
}
1009-
printf("\n");
10101018
}
1011-
if(query_type == 0 || query_type == DNS_TYPE_CNAME)
1019+
if(query_type == 0 || query_type == DNS_TYPE_CNAME) {
10121020
for(i=0; i < d.numcname; i++)
10131021
printf("CNAME: %s\n", d.cname[i].alloc);
1014-
for(i=0; i < d.numtxt; i++)
1015-
printf("TXT: %s\n", d.txt[i].txt);
1022+
}
1023+
if(query_type == 0 || query_type == DNS_TYPE_TXT) {
1024+
for(i=0; i < d.numtxt; i++)
1025+
printf("TXT: %s\n", d.txt[i].txt);
1026+
}
10161027
}
10171028

10181029
doh_cleanup(&d);

0 commit comments

Comments
 (0)