forked from dterweij/ndjbdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsip.c
144 lines (122 loc) · 3.43 KB
/
dnsip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* dnsip.c: This file is part of the `djbdns' project, originally written
* by Dr. D J Bernstein and later released under public-domain since late
* December 2007 (http://cr.yp.to/distributors.html).
*
* Copyright (C) 2009 - 2012 Prasad J Pandit
*
* This program is a free software; you can redistribute it and/or modify
* it under the terms of GNU General Public License as published by Free
* Software Foundation; either version 2 of the license or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* of FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "version.h"
#include "ip4.h"
#include "dns.h"
#include "strerr.h"
#include "buffer.h"
char str[IP4_FMT];
static stralloc out;
static stralloc fqdn;
static char seed[128];
short mode = 0;
static char *prog = NULL;
enum output_mode { OLD = 0, NEW = 1 };
void
usage (void)
{
printf ("Usage: %s <domain-name> [<domain-name> ...]\n", prog);
}
void
printh (void)
{
usage ();
printf ("\n Options:\n");
printf ("%-17s %s\n", " -h --help", "print this help");
printf ("%-17s %s\n", " -n --new", "use new output style");
printf ("%-17s %s\n", " -v --version", "print version information");
printf ("\nReport bugs to <[email protected]>\n");
}
int
check_option (int argc, char *argv[])
{
int n = 0, ind = 0;
const char optstr[] = "+:hnv";
struct option lopt[] = \
{
{ "help", no_argument, NULL, 'h' },
{ "new", no_argument, NULL, 'n' },
{ "version", no_argument, NULL, 'v' },
{ 0, 0, 0, 0 }
};
if (argc < 2)
{
usage ();
exit (0);
}
opterr = optind = 0;
while ((n = getopt_long (argc, argv, optstr, lopt, &ind)) != -1)
{
switch (n)
{
case 'h':
printh ();
exit (0);
case 'n':
mode = NEW;
break;
case 'v':
printf ("%s is part of djbdns version %s\n", prog, VERSION);
exit (0);
default:
errx (-1, "unknown option `%c', see: --help", optopt);
}
}
return optind;
}
int
main (int argc, char *argv[])
{
int i = 0;
char *x = NULL;
prog = strdup ((x = strrchr (argv[0], '/')) != NULL ? x + 1 : argv[0]);
i = check_option (argc, argv);
argv += i;
argc -= i;
dns_random_init (seed);
while (*argv)
{
if (!stralloc_copys (&fqdn, *argv))
err (-1, "could not allocate enough memory");
if (dns_ip4 (&out, &fqdn) == -1)
errx (-1, "could not find IP address for `%s'", *argv);
if (mode & NEW)
{
buffer_puts (buffer_1, *argv);
buffer_puts (buffer_1, ": ");
}
for (i = 0; (unsigned)i + 4 <= out.len; i += 4)
{
buffer_put (buffer_1, str, ip4_fmt (str, out.s + i));
buffer_puts (buffer_1, " ");
}
buffer_puts (buffer_1, "\n");
++argv;
}
buffer_flush (buffer_1);
return 0;
}