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 <http://www.gnu.org/licenses/>.
28 #include "dns-stuff.h"
30 #define PGM "t-dns-stuff"
38 main (int argc, char **argv)
48 char const *name = NULL;
51 log_set_prefix (PGM, GPGRT_LOG_WITH_PREFIX);
54 while (argc && last_argc != argc )
57 if (!strcmp (*argv, "--"))
62 else if (!strcmp (*argv, "--help"))
64 fputs ("usage: " PGM " [HOST]\n"
66 " --verbose print timings etc.\n"
67 " --debug flyswatter\n"
68 " --use-tor use Tor\n"
69 " --bracket enclose v6 addresses in brackets\n"
70 " --cert lookup a CERT RR\n"
71 " --srv lookup a SRV RR\n"
72 " --cname lookup a CNAME RR\n"
76 else if (!strcmp (*argv, "--verbose"))
81 else if (!strcmp (*argv, "--debug"))
87 else if (!strcmp (*argv, "--use-tor"))
92 else if (!strcmp (*argv, "--bracket"))
97 else if (!strcmp (*argv, "--cert"))
99 any_options = opt_cert = 1;
102 else if (!strcmp (*argv, "--srv"))
104 any_options = opt_srv = 1;
107 else if (!strcmp (*argv, "--cname"))
109 any_options = opt_cname = 1;
112 else if (!strncmp (*argv, "--", 2))
114 fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
119 if (!argc && !any_options)
122 name = "simon.josefsson.org";
128 fprintf (stderr, PGM ": none or too many host names given\n");
134 err = enable_dns_tormode ();
137 fprintf (stderr, "error switching into Tor mode: %s\n",
151 printf ("CERT lookup on '%s'\n", name);
153 err = get_dns_cert (name, DNS_CERTTYPE_ANY, &key, &keylen,
154 &fpr, &fpr_len, &url);
156 printf ("get_dns_cert failed: %s <%s>\n",
157 gpg_strerror (err), gpg_strsource (err));
160 printf ("Key found (%u bytes)\n", (unsigned int)keylen);
168 printf ("Fingerprint found (%d bytes): ", (int)fpr_len);
169 for (i = 0; i < fpr_len; i++)
170 printf ("%02X", fpr[i]);
174 printf ("No fingerprint found\n");
177 printf ("URL found: %s\n", url);
179 printf ("No URL found\n");
191 printf ("CNAME lookup on '%s'\n", name);
192 err = get_dns_cname (name, &cname);
194 printf ("get_dns_cname failed: %s <%s>\n",
195 gpg_strerror (err), gpg_strsource (err));
198 printf ("CNAME found: '%s'\n", cname);
205 struct srventry *srv;
208 rc=getsrv("_hkp._tcp.wwwkeys.pgp.net",&srv);
209 printf("Count=%d\n\n",rc);
212 printf("priority=%hu\n",srv[i].priority);
213 printf("weight=%hu\n",srv[i].weight);
214 printf("port=%hu\n",srv[i].port);
215 printf("target=%s\n",srv[i].target);
221 else /* Standard lookup. */
224 dns_addrinfo_t aibuf, ai;
227 printf ("Lookup on '%s'\n", name);
229 err = resolve_dns_name (name, 0, 0, SOCK_STREAM, &aibuf, &cname);
232 fprintf (stderr, PGM": resolving '%s' failed: %s\n",
233 name, gpg_strerror (err));
238 printf ("cname: %s\n", cname);
239 for (ai = aibuf; ai; ai = ai->next)
241 printf ("%s %3d %3d ",
242 ai->family == AF_INET6? "inet6" :
243 ai->family == AF_INET? "inet4" : "? ",
244 ai->socktype, ai->protocol);
246 err = resolve_dns_addr (ai->addr, ai->addrlen,
248 | (opt_bracket? DNS_WITHBRACKET:0)),
251 printf ("[resolve_dns_addr failed: %s]", gpg_strerror (err));
258 err = resolve_dns_addr (ai->addr, ai->addrlen,
259 (opt_bracket? DNS_WITHBRACKET:0),
262 printf ("[resolve_dns_addr failed (2): %s]", gpg_strerror (err));
265 if (!is_ip_address (host))
266 printf (" (%s)", host);
272 free_dns_addrinfo (aibuf);