// for use with djbernstein library, 
/* licence 4, i keep the rights to modify, extends, change it drastically or not, rename it,
 * though you can do the same thing,
 * but i am hereby solely permitted to delete it */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include <dns.h>

static char seed[128];

int main(int ac, char **av) {
	stralloc out = {0};
	stralloc fqdn = {0};
	char *curl,ip[16];
	int r=254,i;
	double decip, a,b,c,d;

	if (ac<2) return 255;
	++av;
	stralloc_copys(&fqdn,*av);
	++av;

	dns_random_init(seed);
	switch (atoi(*av)) {
		case 0:
			r=dns_ip4(&out,&fqdn);
			printf("%d:%d:", r, out.len/4);
			curl=out.s;
			for ( i=0; i<out.len/4; i++) {
				printf("%d.%d.%d.%d ", (unsigned char)*(curl+0), (unsigned char)*(curl+1), (unsigned char)*(curl+2), (unsigned char)*(curl+3));
				a=*(curl+0) & 0x00FF;
				b=*(curl+1) & 0x00FF;
				c=*(curl+2) & 0x00FF;
				d=*(curl+3) & 0x00FF;
				decip=a*pow(2,24) + b*pow(2,16) + c*pow(2,8) + d;
				printf("%0.f: ", decip);
				curl=curl+4;
			}
			break;
		case 1:
			r=dns_txt(&out,&fqdn);
			printf("%d:%s", r, out.s);
			break;
		case 2:
			r=dns_mx(&out,&fqdn);
			printf("%d:%d:", r, out.len);
			curl=out.s+2;
			while ( curl <= out.s+out.len ) {
				printf("%d:", *(curl-1));
				i=printf("%s:", curl);
				curl=curl+i+2;
			}
			break;
	}
	printf("\n");
	return r;
}
