As the subject suggests, I just wrote an opensource DNS daemon in PHP. I already know what any sane person is thinking right now:
[18:25:06] <Derick> MT`AwAy: you’re mad
Now that it’s said, let’s move on ; if you want to tell me I’m crazy, you can post it in a comment here, it makes me happy. I had some reasons to dislike bind9 which finally made me write my own DNS daemon, and I’ll explain that here.
My need was to have a stable dynamic DNS server working in most environnements, with an easy to configure master/slave relationship (with realtime synchronisation), and a way to change records instantly from PHP…
For those who already played with bind9, there’s a feature called dlz which basically allows to ask bind to get data from an SQL server. I could just configure another MySQL per slave, and put replication there, but it’s not that fast and I had some stability issues (both with MySQL replication, and with bind crashing in some weird cases).
So, instead of trying to fix bind9′s code (which would also include fixing MySQL replication – or trying newer MySQL’s row-based replication) or searching for another solution (there are zillions of dns servers around there, but they all have features I’ll never need), I decided to write my own DNS server (built on top of PInetd, my PHP networking framework for PHP 5.3.0), with only the features I needed.
So here are some of the features of this DNS daemon:
- Supports RFC 1035, and some others too (IPv6 AAAA records, and DNS OPT, with the goal of supporting DNSSEC at some point).
- Does NOT support AXFR nor IXFR, I have no need for the standard zone transfer protocol
- Data can be updated realtime with a provided client class, connecting to the server via a shared secret authentication (client computes checksum of his name, timestamp and secret, server replies with the same kind of data). Once authentication is finished data is sent cleartext, but adding SSL encoding wouldn’t be that hard (just need to create a “STARTTLS” function, that’s on the TODO list).
- Slave stays connected to master (keepalive packet sent every 15 minutes), and gets updates realtime.
- On first connection, slave will get all zones/domains/records from the master. If it gets disconnected later and reconnect, it will search for his last update, and will ask only for newer data from the master.
- Slave synchronisation is done in a separate process, meaning that even when processing a lot of updates, service is available.
As a test, I ran AFNIC’s zonecheck on a virtual zone I created on the server, and it works!
dig @dyndns1.ookoo.org version.dnsd ch txt
If you want to test this, I got a test domain with a record creation page, however I won’t post it here (or it might get abused) so if you want to test, feel free to contact me.
And finally the conclusion is simple: php can do anything you want to do, and even things you never wanted to.
PS: I’m looking for someone to look after PInetd‘s website, like removing this lipsum news, etc… (I don’t have time for that).

#1 by fa on 2009/02/16 - 12:18
Quote
People also gave me odd looks when I tried to develop a smtp server in PHP, but I never made it past planning stage.
Nice work, I’m looking forward to have a closer look on this.
#2 by Paul Reinheimer on 2009/02/17 - 20:11
Quote
You’re crazy.
I’ll play more later
#3 by EllisGL on 2009/02/17 - 23:36
Quote
Talking about crazy. I need to work on my AJAX / COMET war dialer that uses PHP and MySQL.
#4 by Alexis Voisin on 2009/02/17 - 23:58
Quote
You’re crazy (And now happy ^^?)
#5 by James Collins on 2009/02/18 - 01:00
Quote
Hi,
Fantastic idea! PHP is my preferred programming language, so I find myself attempting to use it in more and more situations.
I’m curious what the performance of your DNS daemon is like. Have you done any benchmarking at all?
We currently use PowerDNS with a MySQL backend.
I have seen some previous research that showed that PowerDNS was nowhere near as fast as Bind and NSD.
I’m assuming at this stage that your DNS daemon would be slower, but I’m wondering how much slower?
Keep up the good work.
James
#6 by drydenmaker on 2009/02/18 - 09:28
Quote
always good to see people who dont arbitrarily limit their thinking.
Pingback: PInetd: Portable INET Daemon » renemoser.net
#7 by J Pluijmers on 2009/02/18 - 11:10
Quote
You are crazy!
I hear that a lot, but I find that doing “stupid” things otherwise know as contoversial always teaches you the most. These are the topics you can’t google the solution for. I complement you on your effort to do something stupid
Keep us posted, I would surely find this usefull.
#8 by Uli on 2009/02/18 - 11:12
Quote
Great stuff, i really like this “no-limits”-thinking!
#9 by Gustav E. on 2009/02/18 - 21:46
Quote
Glad to see that someone else besides me tend to do some, what others might call, crazy stuff in PHP. I myself just created a httpd, but this is far more interesting, I also saw the performance tests, and it looks really neat I must say!
Great work!
#10 by Yvan on 2009/02/19 - 08:50
Quote
I don’t see the need for MySQL on slave servers with bind9. You just have to create slave zones in bind9, no? Then with the notifying system, your bind9 slaves will be updated almost instantly.
But great work anyway, PHP rules
Pingback: Your own DNSd daemon « A Wonderful Life in a Magical Tux
Pingback: DNS-Server auf der Basis von PHP im Leben des wolf-u.li
Pingback: My Heard Space » Blog Archive » Php DNS Daemon
Pingback: Precea World » Blog Archive » PInetd
#11 by Andrew Thompson on 2009/02/22 - 00:17
Quote
You’re definitely crazy, but well done for giving it a go and getting it successful.
In my experience PHP isn’t cut out for being a networking daemon.
a) It has way too many memory leaks. Unless you are managing your PHP processes, killing them every so often, you will have problems. There’s a reason Apache kills its processes every 1000 or so requests.
b) Its too slow and memory hogging. I wrote a native php http client for spidering multiple websites, which although is fine for few connections, the c++ version I wrote was about 100 times faster taking a fraction of the memory usage, allowing many more connections at once.
Something such as this for a production website/hosting provider would just be too crazy.
#12 by Codeslinger at Compsalot on 2009/02/27 - 21:56
Quote
Perhaps you are crazy “like a fox”. People underestimate the value of programmer productivity. You can already be finished writing your program in PHP while the c programmer is still declaring their header files.
Who cares how long it takes the program to run as long as it runs fast enough to get the job done? Far more important are the inherent security benefits of PHP with regard to buffer overflows and other attacks. It takes a lot of extra effort to make a c program secure against buffer overflows but PHP automatically and robustly provides that protection.
Slow and memory hogging?? I think not! Fact is PHP has a very small memory footprint and although there may have been some versions in the past that did leak memory those problems are long gone, PHP is very reliable as a daemon. If your program is leaking memory it’s not PHP’s fault.
I wrote an email server, POP3 and SMTP entirely in PHP. I wrote it because it was time to replace the creaky old email system with something more up to date. I spent two weeks looking at all the different email systems out there, reading the docs and especially the bug reports. And even installed a couple of them. And in the end I concluded that I could do a much better job than what was available, so I did.
My PHP email server has been running in production 24/7 with hundreds of users for over six months and has processed half a million messages. The only time it ever had a problem was when the maildir ran out of disk space… the problem was that my code to detect this was looking at the free space of the wrong disk/partition, oops…. but even that recovered gracefully.
So yes, daemons written in PHP are Fast Secure Reliable and programmer productivity is high.
I’ve also written a web server in PHP. But mine is not as full featured as the http://nanoweb.si.kz project that I recently discovered. I wrote my php web server to solve a specific problem for a specific need, but it turned out so well that I am thinking about replacing apache with it, the result should be higher performance and less memory use, due to the php framework only being loaded once instead of for each request, similar to how fastcgi works but with better integration.
Bottom line is that PHP is a wonderful and very capable language.
I am very interested in your DNS project, go for it!!!
Codeslinger
#13 by Codeslinger at Compsalot on 2009/02/28 - 18:00
Quote
“Who cares how long it takes the program to run as long as it runs fast enough to get the job done?”
oh by the way, I don’t want to give a wrong impression about the speed of php. Yes it is true that in a head to head race well written c will run faster than the equivalent php. But there is more to the story than that because how you approach the problem will have a huge affect on what the overall speed will be, as does the computer that it runs on.
In this specific case, I replaced a many years old (but no slouch) computer with a newer/faster one, and I replaced a “big name” email system written in c with a custom made program written in php. And I was pleasantly surprised that in the one place where it counted most, sending/receiving a message from/to a client email program. My php program was noticeably faster. Enough faster that end-users commented on it.
Now how much of that speed difference is because of the newer/faster computer, and how much of that speed difference is because the c program tried to be all things to all people and my php program was specialized in doing only one job and doing it well. I can not say what the cause of the difference is.
All I can tell you is that in this specific instance, the php program was measurably faster than the c program that it replaced. So it is not valid to assume that the c program will always be faster. But more importantly is just the fact that for most needs, php is fast enough.
Codeslinger
Pingback: PHP can do anything, what about some ssh? « MagicalTux in Japan
#14 by David on 2010/06/28 - 17:11
Quote
You’re crazy, but I like it.
#15 by ahu on 2010/12/03 - 19:07
Quote
nice work!I’ll keep in watch it!
#16 by dude on 2010/12/13 - 04:20
Quote
I have been writing full server(damon)/client apps with PHP for years. Never had any memory/speed issues. I have multi socket servers that handle just as much as any other C program. I have many custom services but also HTTP, SMTP, and GAME servers written.
As a business owner new Servers are cheaper then the time it takes a dev team to figure out how to save resources on hardware.