Showing posts with label NanoDNS. Show all posts
Showing posts with label NanoDNS. Show all posts

Thursday, August 5, 2010

NanoDNS updated

I’ve updated NanoDNS to work (in theory) on 64-bit machines, and to handle EDNS packets a little better:

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

This is a little bigger than the last version of NanoDNS I posted, but it’s still the world’s smallest useful DNS server. The above code handles a problem people frequently ask on serverfault: “How can I set up a DNS server to always return the same IP in reply to any query?” The program takes one argument: The IP we return. This program binds to all IP addresses a given machine has on the DNS port (port 53).

I’ve also updated MicroDNS (NanoDNS’s big sister, with fancy features like selectable IP to bind to) to better support EDNS packets:

http://samiam.org/software/microdns.html

Update: For people who wonder how NanoDNS does its magic, I now have a page that explains its source code line-by-line.

Saturday, March 14, 2009

Windows FAT patent rant; NanoDNS bug

As had been widely reported in the press, Microsoft is suing someone for using the FAT32 filesystem without licensing. This is downright anti-competitive monopolistic behavior and behavior Microsoft should not be allowed to do without repercussions.

The problem is this: For a device, such as a USB stick, digital camera, GPS unit, or whatever to connect to and work with Microsoft Windows, the device in question must use a filesystem Microsoft Windows supports, since Microsoft has a monopoly position on the desktop. Unfortunately, Microsoft has a patent for any and all filesystems they support, so to make a device you are forced to use a patented filesystem and get a patent license from Microsoft.

It used to be possible to create a driver to give Microsoft support for another filesystem, such as ext2 in pre-Vista versions of the operating system. Indeed, I use the open-source ext2fsd on my XP system to have the majority of the data on my hard disk stored using Linux's second extended filesystem (the native filesystem for Linux throughout the 1990s and early 2000s; the updates to ext2 are mostly ext2-compatible, and Linux still has 100% ext2 support). So, this stops the majority of my data from being stored using a patented filesystem.

Unfortunately, Windows Vista no longer supports third-party filesystems. Let me take that back. In theory, Windows supports non-patented filesystems, but you have to get rid of the UAC (the Visty thingy that makes sure a virus doesn't do something it shouldn't do without your permission) to give Vista support for non-patented filesystems (see this page for a description of the issue).

This reminds me of when Microsoft made it so Windows 3 would not work with DrDos because they wanted to stop people from using non-Microsoft operating systems for their computer. Microsoft, with Windows Vista, has broken the ability to use a non-patented filesystem (that is more modern than having 8+3 filenames) with a normal install of their OS.

Since Microsoft has made a technical solution impossible, the only solution is a legal solution. One solution is to invalidate the FAT32 patent, because everything it patents was done before (the idea of using meta-information to map long file names to short file names was done with Rock Ridge which was published before Microsoft filed for the FAT32 patent). Another solution is to sue Microsoft for engaging in antitrust behavior, since it can be shown, with Vista, that Microsoft removed compatibility for non-patented filesystems, forcing people to license Microsoft's patents, not because the patents are novel, but because the patented filesystems must be used for interoperability purposes.
The copy of NanoDNS I posted yesterday has a bug; it will not work on systems where int is not 32 bits in size. No, I will not fix this bug; people can just use MicroDNS if they need to run a small DNS server on a system that is 64-bit or whatever.

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;}

Thursday, March 12, 2009

The world's smallest DNS server made smaller

After posting the tiny DNS server yesterday, I realized that this code was somewhat bloated. Going over the code this morning, I have managed to remove some bloat and have made the DNS server 17% smaller, resulting in this compact and efficient DNS server:

/*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){long int i;char q[512],p
[17]="\xc0\f\0\x01\0\x01\0\0\0\0\0\x04";socklen_t
f=511;if(a>1){i=htonl(inet_addr(b[1]));p[15]=255&
i;struct sockaddr_in d;bzero(&d,Y);p[14]=(65280&i
)>>8;a=socket(AF_INET,SOCK_DGRAM,0);d.sin_family=
AF_INET;d.sin_port=htons(53);p[13]=(0xff0000&i)>>
16;p[12]=i>>24;bind(a,(Z*)&d,Y);for(;;){if(11<(i=
recvfrom(a,q,255,0,(Z*)&d,&f))&&q[2]>=0){q[7]++;q
[2]|=128;memcpy(q+i,p,16);sendto(a,q,i+16,0,(Z*)&
d,Y);}}}}

Note that, if an argument for the IP to give to DNS queries is not given, the program exits with a semi-random exit code instead of giving the IP 0.0.0.0 to all queries (I did it this way to remove some blot from the code), and that there is now a single compile-time warning when compiled with -Wall as a result; if this bothers you, you can place 10 bytes of bloat in the form of return 0; at the end of the program (before the final }).

Note also that this program violates Deadwood coding style standards because, among other reasons, a variable is declared after other actions are performed.

Usage is the same as the more bloated form of this program that I posted yesterday; compile the program and give it the name nanodns and run the program as something like nanodns 192.168.146.134 to give, say, the IP 192.168.146.134 as a reply to any DNS query sent on port 53 of any IP the machine has (and can bind to) when the program is started. Yes, I know, real programmers do not need documentation and can figure all this out just by looking at the above code. I apologize for making concessions for wanna-be programmers (I also have a social life, use Windows, and even have a girlfriend [1]).

OK, OK, tomorrow, I will talk about MicroDNS, which is a large, bloated form of the above program, including such unneeded bloat like comments, indentation, documentation, descriptive variable names, usage information if invoked incorrectly, and other bloat that real programmers do not need. In addition, MicroDNS has some bloat because you can optionally specify which IP the server binds to (if not specified, it binds to all IPs).

- Sam

[1] Took me nearly a year of work to find one; this is why Deadwood 2.1.01 was released in 2009 instead of 2008.

Wednesday, March 11, 2009

The world's smallest usable DNS server

Sometimes, I get requests for MaraDNS to be able to always return a given IP for any query. While MaraDNS is able to do this (with csv2_default_zonefile), it is actually possible to have a much more compact DNS server do the same thing.

I am posting today in this blog a tiny DNS server that does just that. This DNS server always sends the same IP for any and all DNS queries it receives. This IP defaults to "0.0.0.0", but can be changed by supplying a different IP on the command line. I call this program "nanodns", and its source code is as follows:

/*Placed in the public domain by Sam Trenholme*/
#include <arpa/inet.h>
#include <string.h>
#include <stdint.h>
#define Z struct sockaddr
int main(int a,char **b){struct sockaddr_in d;
char q[512];socklen_t f=sizeof(q);uint32_t i=0;
char p[17]="\xc0\x0c\0\x01\0\x01\0\0\0\0\0\x04";
int s;if(a==2){i = inet_addr(b[1]);}i = htonl(i);
p[12]=i>>24;p[13]=(i&0xff0000)>>16;p[14]=(i&65280
)>>8;p[15]=i&255;s=socket(AF_INET,SOCK_DGRAM,0);
memset(&d,0,sizeof(d));d.sin_family = AF_INET;
d.sin_port = htons(53);bind(s,(Z *)&d,sizeof(d));
for(;;){i=recvfrom(s,q,255,0,(Z *)&d,&f);if(i<12
||(q[2]&128)){continue;}q[2]|=128;q[7]++;for(a=0;
a<16;a++) {q[i+a]=p[a];}sendto(s,q,i+16,0,(Z *)&d
,sizeof(Z));}}

To use this program, compile the above source and give it the name "nanodns". To specify the IP you want returned, specify this as the one argument to the program. For example, to always give the IP "192.168.146.132" to any DNS query received by the server, run this program as nanodns 192.168.146.132

This program binds to all addresses the machine running this program has on port 53 (the DNS port); I have a larger (and more readable) version of this program that allows one to change the IP one can bind to which I will put on my webpage and talk about tomorrow.

I will also discuss Deadwood's coding style in future installments of this blog. For example, the above code does not conform to Deadwood's coding style standards (but fits in this blog).