diff --git a/dns.c b/dns.c index 6878f84..4a5cd8f 100644 --- a/dns.c +++ b/dns.c @@ -140,11 +140,13 @@ close_dns(int fd) void dns_ping(int dns_fd) { - dns_query(dns_fd, "4.kryo.se", 1); + dns_write(dns_fd, dns_fd, + "AAAAAAAAAAAKRYOAAAAAAAAAAAKRYOAAAAAAAAAAAAAKRYOAAAAAAAAAAAAAKRYOAAAAAAAAAAAAAAAKRYOAAAAAAAAAAAAAKRYOAAAAAAAAAAAAAAAAAAB", + 119); } void -dns_query(int fd, char *host, int type) +dns_query(int fd, int id, char *host, int type) { char *p; int len; @@ -157,7 +159,7 @@ dns_query(int fd, char *host, int type) header = (HEADER*)buf; - header->id = 0; + header->id = id; header->qr = 0; header->opcode = 0; header->aa = 0; @@ -179,6 +181,50 @@ dns_query(int fd, char *host, int type) sendto(fd, buf, len+1, 0, (struct sockaddr*)&peer, peerlen); } +int +dns_write(int fd, int id, char *buf, int len) +{ + int avail; + int i; + int parts; + int p; + char data[256]; + char *d; + +#define CHUNK 31 +// 31 bytes expands to 62 chars in domain +// We just use hex as encoding right now + + avail = 0xFF - strlen(topdomain) - 2; + + avail /= 2; // use two chars per byte in encoding + avail -= (avail/CHUNK); // make space for parts + + avail = MIN(avail, len); // do not use more bytes than is available; + d = data; + + parts = avail / CHUNK; + for (p = 0; p < parts; p++) { + for (i = 0; i < CHUNK; i++) { + sprintf(d, "%02X", buf[p*CHUNK + i]); + d += 2; + } + *d++ = '.'; + strcpy(d, topdomain); + } + parts = avail % CHUNK; + for (i = 0; i < parts; i++) { + sprintf(d, "%02X", buf[p*CHUNK + i]); + d += 2; + } + *d++ = '.'; + strcpy(d, topdomain); + + printf("Resolving %s\n", data); + dns_query(fd, id, data, T_A); + return avail; +} + int dns_read(int fd, char *buf, int buflen) { diff --git a/dns.h b/dns.h index e0c4a64..cb84fc3 100644 --- a/dns.h +++ b/dns.h @@ -23,7 +23,8 @@ int open_dns(const char *, const char *); void close_dns(int); void dns_ping(int); -void dns_query(int, char *, int); +void dns_query(int, int, char *, int); +int dns_write(int, int, char *, int); int dns_read(int, char *, int);