Friday, March 13, 2009

MicroDNS and NanoDNS

I have been posting a small program for real programmers that is a simple DNS server that always returns the same IP reply to any query sent. I also have a version of this program for wanna-be programmers; this program is properly indented, has comments, descriptive variable names, and other things real programmers prefer to do without. It even gives usage information if incorrectly invoked. It is available at samiam.org/software/microdns.html

However, if the comments and coding style of MicroDNS bothers you, I have managed to get rid of even more bloat from NanoDNS, and have the updated program posted here:

/*Placed in the public domain by Sam Trenholme*/
#include <arpa/inet.h>
#include <string.h>
#define Z struct sockaddr
#define Y sizeof(d)
int main(int a,char **b){int i;char q[512],p[17
]="\xc0\f\0\x01\0\x01\0\0\0\0\0\x04";if(a>1){a=
socket(AF_INET,SOCK_DGRAM,0);struct sockaddr_in
d;bzero(&d,Y);d.sin_port=htons(53);*((int *)(p+
12))=inet_addr(b[1]);d.sin_family=AF_INET;bind(
a,(Z*)&d,Y);socklen_t f=511;for(;;){i=recvfrom(
a,q,255,0,(Z*)&d,&f);if(i>9&&q[2]>=0){q[7]++;q[
2]|=128;memcpy(q+i,p,16);sendto(a,q,i+16,0,(Z*)
&d,Y);}}}return 0;}