release
[hcoop/zz_old/debian/djbdns.git] / dnsmx.c
1 #include "buffer.h"
2 #include "exit.h"
3 #include "strerr.h"
4 #include "uint16.h"
5 #include "byte.h"
6 #include "str.h"
7 #include "fmt.h"
8 #include "dns.h"
9
10 #define FATAL "dnsmx: fatal: "
11
12 void nomem(void)
13 {
14 strerr_die2x(111,FATAL,"out of memory");
15 }
16
17 static char seed[128];
18
19 static stralloc fqdn;
20 static char *q;
21 static stralloc out;
22 char strnum[FMT_ULONG];
23
24 int main(int argc,char **argv)
25 {
26 int i;
27 int j;
28 uint16 pref;
29
30 dns_random_init(seed);
31
32 if (*argv) ++argv;
33
34 while (*argv) {
35 if (!stralloc_copys(&fqdn,*argv)) nomem();
36 if (dns_mx(&out,&fqdn) == -1)
37 strerr_die4sys(111,FATAL,"unable to find MX records for ",*argv,": ");
38
39 if (!out.len) {
40 if (!dns_domain_fromdot(&q,*argv,str_len(*argv))) nomem();
41 if (!stralloc_copys(&out,"0 ")) nomem();
42 if (!dns_domain_todot_cat(&out,q)) nomem();
43 if (!stralloc_cats(&out,"\n")) nomem();
44 buffer_put(buffer_1,out.s,out.len);
45 }
46 else {
47 i = 0;
48 while (i + 2 < out.len) {
49 j = byte_chr(out.s + i + 2,out.len - i - 2,0);
50 uint16_unpack_big(out.s + i,&pref);
51 buffer_put(buffer_1,strnum,fmt_ulong(strnum,pref));
52 buffer_puts(buffer_1," ");
53 buffer_put(buffer_1,out.s + i + 2,j);
54 buffer_puts(buffer_1,"\n");
55 i += j + 3;
56 }
57 }
58
59 ++argv;
60 }
61
62 buffer_flush(buffer_1);
63 _exit(0);
64 }