1 /* t-dns-cert.c - Module test for dns-stuff.c
2 * Copyright (C) 2011 Free Software Foundation, Inc.
3 * Copyright (C) 2011, 2015 Werner Koch
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
28 #include "dns-stuff.h"
30 #define PGM "t-dns-stuff"
39 #ifdef HAVE_W32_SYSTEM
42 WSAStartup (0x202, &wsadat);
48 main (int argc, char **argv)
54 int opt_new_circuit = 0;
59 char const *name = NULL;
62 log_set_prefix (PGM, GPGRT_LOG_WITH_PREFIX);
65 while (argc && last_argc != argc )
68 if (!strcmp (*argv, "--"))
73 else if (!strcmp (*argv, "--help"))
75 fputs ("usage: " PGM " [HOST]\n"
77 " --verbose print timings etc.\n"
78 " --debug flyswatter\n"
79 " --standard-resolver use the system's resolver\n"
80 " --use-tor use Tor\n"
81 " --new-circuit use a new Tor circuit\n"
82 " --bracket enclose v6 addresses in brackets\n"
83 " --cert lookup a CERT RR\n"
84 " --srv lookup a SRV RR\n"
85 " --cname lookup a CNAME RR\n"
86 " --timeout SECONDS timeout after SECONDS\n"
90 else if (!strcmp (*argv, "--verbose"))
95 else if (!strcmp (*argv, "--debug"))
101 else if (!strcmp (*argv, "--use-tor"))
106 else if (!strcmp (*argv, "--new-circuit"))
111 else if (!strcmp (*argv, "--standard-resolver"))
113 enable_standard_resolver (1);
116 else if (!strcmp (*argv, "--recursive-resolver"))
118 enable_recursive_resolver (1);
121 else if (!strcmp (*argv, "--bracket"))
126 else if (!strcmp (*argv, "--cert"))
128 any_options = opt_cert = 1;
131 else if (!strcmp (*argv, "--srv"))
133 any_options = opt_srv = 1;
136 else if (!strcmp (*argv, "--cname"))
138 any_options = opt_cname = 1;
141 else if (!strcmp (*argv, "--timeout"))
146 set_dns_timeout (atoi (*argv));
150 else if (!strncmp (*argv, "--", 2))
152 fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
157 if (!argc && !any_options)
160 name = "simon.josefsson.org";
166 fprintf (stderr, PGM ": none or too many host names given\n");
170 set_dns_verbose (verbose, debug);
175 err = enable_dns_tormode (opt_new_circuit);
178 fprintf (stderr, "error switching into Tor mode: %s\n",
192 if (verbose || any_options)
193 printf ("CERT lookup on '%s'\n", name);
195 err = get_dns_cert (name, DNS_CERTTYPE_ANY, &key, &keylen,
196 &fpr, &fpr_len, &url);
198 printf ("get_dns_cert failed: %s <%s>\n",
199 gpg_strerror (err), gpg_strsource (err));
202 if (verbose || any_options)
203 printf ("Key found (%u bytes)\n", (unsigned int)keylen);
211 printf ("Fingerprint found (%d bytes): ", (int)fpr_len);
212 for (i = 0; i < fpr_len; i++)
213 printf ("%02X", fpr[i]);
217 printf ("No fingerprint found\n");
220 printf ("URL found: %s\n", url);
222 printf ("No URL found\n");
234 printf ("CNAME lookup on '%s'\n", name);
235 err = get_dns_cname (name, &cname);
237 printf ("get_dns_cname failed: %s <%s>\n",
238 gpg_strerror (err), gpg_strsource (err));
241 printf ("CNAME found: '%s'\n", cname);
247 struct srventry *srv;
251 err = get_dns_srv (name? name : "_hkp._tcp.wwwkeys.pgp.net",
254 printf ("get_dns_srv failed: %s <%s>\n",
255 gpg_strerror (err), gpg_strsource (err));
258 printf ("count=%u\n",count);
259 for (i=0; i < count; i++)
261 printf("priority=%-8hu ",srv[i].priority);
262 printf("weight=%-8hu ",srv[i].weight);
263 printf("port=%-5hu ",srv[i].port);
264 printf("target=%s\n",srv[i].target);
270 else /* Standard lookup. */
273 dns_addrinfo_t aibuf, ai;
276 printf ("Lookup on '%s'\n", name);
278 err = resolve_dns_name (name, 0, 0, SOCK_STREAM, &aibuf, &cname);
281 fprintf (stderr, PGM": resolving '%s' failed: %s\n",
282 name, gpg_strerror (err));
287 printf ("cname: %s\n", cname);
288 for (ai = aibuf; ai; ai = ai->next)
290 printf ("%s %3d %3d ",
291 ai->family == AF_INET6? "inet6" :
292 ai->family == AF_INET? "inet4" : "? ",
293 ai->socktype, ai->protocol);
295 err = resolve_dns_addr (ai->addr, ai->addrlen,
297 | (opt_bracket? DNS_WITHBRACKET:0)),
300 printf ("[resolve_dns_addr failed: %s]", gpg_strerror (err));
307 err = resolve_dns_addr (ai->addr, ai->addrlen,
308 (opt_bracket? DNS_WITHBRACKET:0),
311 printf (" [resolve_dns_addr failed (2): %s]", gpg_strerror (err));
314 if (!is_ip_address (host))
315 printf (" (%s)", host);
321 free_dns_addrinfo (aibuf);
324 reload_dns_stuff (1); /* Release objects. */