aboutsummaryrefslogtreecommitdiff
path: root/dnsmx.c
blob: 5d75d39fda766f6d0cd14c0ea49aafa6133a94e0 (plain)
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
#include "buffer.h"
#include "exit.h"
#include "strerr.h"
#include "uint16.h"
#include "byte.h"
#include "str.h"
#include "fmt.h"
#include "dns.h"

#define FATAL "dnsmx: fatal: "

void nomem(void)
{
  strerr_die2x(111,FATAL,"out of memory");
}

static char seed[128];

static stralloc fqdn;
static char *q;
static stralloc out;
char strnum[FMT_ULONG];

int main(int argc,char **argv)
{
  int i;
  int j;
  uint16 pref;

  dns_random_init(seed);

  if (*argv) ++argv;

  while (*argv) {
    if (!stralloc_copys(&fqdn,*argv)) nomem();
    if (dns_mx(&out,&fqdn) == -1)
      strerr_die4sys(111,FATAL,"unable to find MX records for ",*argv,": ");

    if (!out.len) {
      if (!dns_domain_fromdot(&q,*argv,str_len(*argv))) nomem();
      if (!stralloc_copys(&out,"0 ")) nomem();
      if (!dns_domain_todot_cat(&out,q)) nomem();
      if (!stralloc_cats(&out,"\n")) nomem();
      buffer_put(buffer_1,out.s,out.len);
    }
    else {
      i = 0;
      while (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);
  _exit(0);
}