Monday, May 6, 2013

How to discover if an IP address is yours

A quick post - sometimes you need to know if an IP address is yours. One way of figuring this out is to ask the kernel to give you a list of all IP addresses it considers local, and go from there. This is pretty laborious however, and requires special processing for 127.0.0.0/8 for example, *all* of which is local.

Another way which I heard of uses getsockname(2), a call which determines the local address of a socket. If you setup a connection, the kernel will automatically pick the most appropriate source address for you. And should you be setting up a connection to yourself, the source address will be identical to the destination address!

This way, you can easily detect if you own an IP address. The initial downside is that this appears to require sending packets, but it turns out you can avoid this by connect(2)ing a connectionless datagram socket.

The final sequence is (minus error checking):
int s = socket(AF_INET, SOCK_DGRAM, 0);
connect(s, (struct sockaddr*)& remote, sizeof(remote));
struct sockaddr_in local;
socklen_t socklen = sizeof(local);
getsockname(s, (struct sockaddr*) &local, &socklen);
return local.sin_addr.s_addr == remote.sin_addr.s_addr;
//      return memcmp(&local.sin6_addr.s6_addr, &local.sin6_addr.s6_addr, 16)==0;
This trick is described in Steven's Unix Network Programming volume one, section 8.14.

 
 

Wednesday, May 1, 2013

PowerDNS now has its own dedicated blog

Dear readers,

PowerDNS now has its own blog, which can be found on http://blog.powerdns.com. This blog will continue to sprout occasional programming oddities and observations.

But if you want to read about PowerDNS, head on to http://blog.powerdns.com!

Nothing else is changing, but we want to have a clear PowerDNS blog which can also be used by other PowerDNS employees & contributors.

      Bert

Wednesday, April 10, 2013

Bitcoins explained for normal people -or- please get back to work


Bitcoins have been taking the world by storm, and what I’ve been reading has been making me angry. Consider this post my version of:
http://xkcd.com/386/

In short, I believe bitcoins are an interesting experiment, but that most people currently promoting bitcoins stand to profit from luring more folks into buying them and believing in them. In other words, they have a stake in convincing you to join in.

In this explanation, I hope to educate readers about how bitcoins:
  • cannot do what normal money does and 
  • as an investment are a pyramid scheme and thus
  • are not “the new world order”.
I will first try to explain what bitcoins are and aren’t so you can make an informed decision if you want to partake in this gold rush.

A first stab: naive digital currency

Let’s start with a simple experiment. I want to start a currency, but not go through the hassle of actually minting coins or printing notes. So what I do is I take otherwise unremarkable pieces of paper and write numbers on them, say 0 through 21 million, and I tell everyone all those numbers are money.

And if people believe me, it will work, and you can then do payments with these pieces of paper. Interestingly, we don’t even need the pieces of paper: it is the number that is the money (there is no shortage of paper). This also makes it a breeze to do payments online - no need to ship paper, no bank involved!  Instead of keeping large stacks of paper to prove our worth, we only keep the numbers in a file somewhere (if you delete the file, you lose your money though).

The problem now is that nothing stops people from spending their money twice. So I take my number 3141593 and use it to buy bread and simultaneously order some stuff online with it. Clearly, numbers as money are neat in theory, but soon the currency collapses since everybody has a copy of every number. People can just keep spending their numbers, and no one wants them anymore.


Second iteration: keeping track to prevent double spending

Within bitcoin, all transactions are recorded. So if you spend a bitcoin (which, mathematically,  is a very large number), that transaction gets broadcast and stored in the network. If I then try to spend the same bitcoin again, I’ll find that the network refuses to register that transaction -  because there already is a longer transaction chain recorded that includes the new owner. Thus, double spending is prevented.

This means two things though:

  • Each transaction is logged! So if you get your wages in bitcoins, and spend them at the local supplier of recreational drugs, that transaction is recorded forever. This might haunt you at a later stage. At the very least your employer now knows where you spend your money.
  • Recording & verifying the transaction takes time. Because the bitcoin network is fully distributed and has no trusted central hub, transactions are only assumed to be distributed if enough parts of the network have verified them. This takes around 10 minutes, and for absolute certainty, an hour is recommended. So forget about a quick shopping trip using bitcoins.

Third iteration: adding back privacy

This recording thing does not sit well with anyone and is an obvious flaw. A “solution” has been found however. When someone wants to send you money, you create a fresh identity for that transaction.

By exchanging these custom identities, each individual transaction gets a sheen of anonymity, but not really. Because whole transaction chains are available, it is nice that individual transactions are anonymous, but if you want to spend your coins, you’ll link them together on the outgoing transaction anyhow.

Fourth iteration: money supply issues

So where do the bitcoins come from? In a normal currency, a central bank creates money, usually in line with the (intended) growth of the economy. As bitcoin has no central bank, means have been found to allow people to ‘mine’ for new coins at a predetermined rate.

There is a steady supply of new bitcoins, mathematically set at 150 bitcoins/hour up to 2017, at which point this will slow down to 75 bitcoins/hour. Eventually, there will be around 21 million bitcoins, and not ever more.

This makes it impossible to look at bitcoins as ‘money’. Whole economies have been killed by setting the money creation rate wrong (too low in 1710 in France causing deflation, too high has happened to almost every currency, resulting in inflation). While we can be angry at how central banks did not stop the current ‘old school currency’ mess, bitcoins have only one money creation rate, and it is fully set in stone.

In short, the bitcoin economy may grow, but the number of bitcoins in circulation can not be matched to that growth. As long as the interest in bitcoins grows faster than the creation rate, as is currently the case, the bitcoin shows heavy deflationary behaviour - each individual bitcoin becomes worth ever more ‘regular money’, making it in effect a very bad idea to actually *spend* your coins.

Under deflationary conditions, in nominal terms, things keep getting cheaper. Why buy a car now when you can be sure it will be cheaper next week? Such conditions have killed whole economies.


The issues in short


  1. Bitcoin transactions are (way) slower than regular money transactions (10 minutes - 1 hour)
  2. Every bitcoin transaction leaves a publicly visible trace that can only be obscured but not removed
  3. Because the bitcoin supply rate is fixed, the regular money value of bitcoins will fluctuate wildly, making them unsuitable as normal currency
Many bitcoin adherents will agree with the points above, and offer two answers:

  1. Bitcoin is not a regular currency, but an investment
  2. Most of the problems can be solved by calculating the bitcoin value of a transaction against current conversion rates to regular money

This holds no water. If we look at bitcoins as an investment, this only works if we can convince people to join in and thus grow the bitcoin economy. But why would they join in? Why, because the value of the coin keeps increasing! This is known as a pyramid scheme, where people who join in first take the money of those that enter the game later. These in turn only make money if they entice even more people to take part. In the mid 1990s, pyramid schemes wrecked the Albanian (real) economy, leading to the 1997 Albanian revolution.

If we look at bitcoins as currency, but admit that we still need traditional money as an adjunct, then bitcoin is attempting to do a revolution with aid of the establishment, something that has rarely worked. Any of the purported advantages of bitcoins disappears if it needs regular currency as an adjunct before being useful!


Finally

So, before jumping on the bitcoin bandwagon, realize that as a currency, bitcoins are flawed, and as an investment, you are late to the game, and you are merely funding the folks that got in earlier. And before you know it, you'll find yourself enthusing about bitcoins at birthday parties because, you know, you are now part of the pyramid!

I wrote this page out of sheer frustration that many of my smartest friends are devoting enormous amounts of energy to bitcoin-related projects and not actually contributing to their own or society’s well-being.

Hence the second title of this rant: and now get back to work.

Tuesday, March 26, 2013

A quick note on cable modems and "Serious Switches"

Posting this so someone else might find it and save a day of headbashing.

I have a Ziggo Motorola cable modem which acts as a bridge. Recently, I revamped our home routing infrastructure (because our old 4-port server died), and installed a most excellent HP 1810g switch.

Because this switch supports VLANs, I was able to configure a Raspberry Pi as a "router on a stick" that routed between 3 VLANs, our house LAN, the Ziggo cable and Telfort DSL.

I did note I had to reboot the cable modem a bit to get things to work, but then they did. When the Raspberry Pi was retired from routing & VoIP switching duty, it got replaced by a most excellent HP MicroServer N40L, but try as a might, I couldn't get a DHCP lease through the Ziggo cable modem bridge.

I did see ARP packets come in for other Ziggo IP addresses, but my DHCP requests would never get an answer. I rebooted the modem a view times and performed various other tricks, but nothing helped.

On the internet, some people noted the cable modem would only work for 1 MAC address at a time, so I changed my MAC address to that of the Raspberry, but still no dice.

Yesterday it dawned on me - my fancy switch itself generates LLDP packets! And once the cable modem has seen the switch MAC address, it considers that to be its friend. And thus blocking all my Linux server's DHCP requests!

I turned off LLDP, rebooted the modem, and was back in business.

Moral of this story - your fancy "enterprise" equipment may upset your consumer electronics.



Saturday, February 23, 2013

A horse, a donkey, a cow: a genetic diff

So, continuing the series 'if you are a hammer, everything looks like a nail', here we'll bridge the worlds of genetic sequencing and programming and show the diff between a horse, a cow and a donkey.

DNA is a lot like computer code, except that it is not an imperative nor a functional programming language. DNA describes what amino acids end up in proteins, and these proteins have shapes and chemical properties which makes them interact in a way that we call 'life'. For more context, see my earlier article 'DNA as seen through the eyes of a computer programmer'.

Like code, DNA evolves in the course of the development of life. Some code never changes, because it is so vital and tricky that any change immediately leads to a non-functioning organism. Other code is so uncritical or unimportant that it can (and does) change at a high clip, leading to many useful or perhaps detrimental mutations.

In between are pieces of DNA that are very consistent within species, but show remarkable change between them. Such code is used to fingerprint organisms, live, but mostly dead. Such a fingerprint (or better, a barcode) can quickly and reliably tell if we are eating horse, donkey or beef.

Huge databases have been established, one of which (BOLDSystems) can be queried here. This is called 'the barcode of life', and for animals, this has been standardized on the mitochondrial CO-1 gene, which encodes part of our aerobic metabolism, powering our cells.

So, what does this look like? Behold, the diff between a horse and a donkey:


As we can see, most DNA is identical, with variations mostly impacting individual nucleotides. In addition, there is one longer stretch that is different.

Now let's make a very current and relevant comparison: a horse and a cow:


Note that we still have lots of single mutations, but we also see a whole line that is mostly different! Clearly, a horse is not a cow. No matter how well you cook it!
If you want to make your own comparison, first look up the scientific (latin) name of the desired animal. Next, look it up, and from the list of sequences, pick two with the same CO-1 length (some barcodes contain more DNA than others). 
Then use this tiny Python script to generate the nice html diffs you see above:
import sys, os, time, difflib, optparse

def main():
    usage = "usage: %prog [options] fromfile tofile"
    parser = optparse.OptionParser(usage)
    (options, args) = parser.parse_args()

    fromfile, tofile = args

    one =open(fromfile).readlines()
    theother = open(tofile).readlines()
  
    d = difflib.HtmlDiff()
    result = d.make_file(one, theother, fromfile, tofile)
    sys.stdout.writelines(result)

if __name__ == '__main__':
    main()

Good luck!

Sunday, January 20, 2013

A PowerDNS... WOK?!

From a letter, where you should know that 'Mok' is Dutch for 'Mug':
Dear PomerDNS management,
Recently me ordered, via a mebform or your mebsite, a so called PomerDNS Mok. but something ment terribly mrong since me received a PomerDNS Wok! Therefor me send this back to you in hope it can be changed for the proper article!
Mith friendly greetings,
A PomerDNS user
In the box, I found this... Wok:

Amazing proof that humor is not dead ;-)

Thanks to +Reinoud van Leeuwen for this wonderful letter, and I can assure you the wok is quite genuine - it will find a proud place in the office!
 
 

Monday, December 10, 2012

Get your free PowerDNS Contributor mug here!

Hi everybody,

PowerDNS is what it is because of its wonderful community! If you ever contributed to PowerDNS in any way, please head over to this page to claim your free PowerDNS mug!

Thanks.