-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdnsmx.c
160 lines (135 loc) · 4.01 KB
/
dnsmx.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* dnsmx.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 <string.h>
#include <stdlib.h>
#include <getopt.h>
#include "version.h"
#include "str.h"
#include "fmt.h"
#include "dns.h"
#include "byte.h"
#include "buffer.h"
#include "strerr.h"
#include "uint16.h"
static char *q;
static stralloc out;
static stralloc fqdn;
char strnum[FMT_ULONG];
static char seed[128];
static char *prog = NULL;
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", " -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[] = "+:hv";
struct option lopt[] = \
{
{ "help", no_argument, NULL, 'h' },
{ "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 '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[])
{
char *x = NULL;
uint16 pref = 0;
int i = 0, j = 0;
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_mx (&out, &fqdn) == -1)
errx (-1, "could not find MX records for `%s'", *argv);
if (!out.len)
{
if (!dns_domain_fromdot (&q, *argv, str_len (*argv)))
err (-1, "could not allocate enough memory");
if (!stralloc_copys (&out, "0 "))
err (-1, "could not allocate enough memory");
if (!dns_domain_todot_cat (&out, q))
err (-1, "could not allocate enough memory");
if (!stralloc_cats (&out, "\n"))
err (-1, "could not allocate enough memory");
buffer_put (buffer_1, out.s, out.len);
}
else
{
i = 0;
while ((unsigned)i + 2 < out.len)
{
j = byte_chr (out.s + i + 2, out.len - i - 2, 0);
uint16_unpack_big (out.s + i, &pref);
buffer_put (buffer_1, strnum, fmt_ulong (strnum, pref));
buffer_puts (buffer_1, " ");
buffer_put (buffer_1, out.s + i + 2, j);
buffer_puts (buffer_1, "\n");
i += j + 3;
}
}
++argv;
}
buffer_flush (buffer_1);
return 0;
}