Networking: February 2009 Archives

 

February 25, 2009

I've got some code that needs to convert an IP address into a string. This is one of those cases where there's a twisty maze of APIs, all slightly different. The traditional API here is:

    char *
    inet_ntoa(struct in_addr in);

inet_ntoa() has two deficiencies, one important and one trivial: it doesn't support IPv6 and it returns a pointer to a statically allocated buffer, so it's not thread safe (I'll let you figure out which is which). Luckily, there's another API: addr2ascii():

    char *
    addr2ascii(int af, const void *addrp, int len, char *buf);

If you pass buf=0, addr2ascii() will return a pointer to a static buffer like inet_ntoa(). However, if you pass it an allocated buffer it will return the result in buf. Unfortunately, if you actually try to use addr2ascii() in threaded code you will quickly discover something unpleasant, at least on FreeBSD: you occasionally get the result "[inet_ntoa error]" or some fraction thereof. The answer is hidden in the EXAMPLES section of the man page:

In actuality, this cannot be done because addr2ascii() and ascii2addr() are implemented in terms of the inet(3) functions, rather than the other way around.

More specifically, on FreeBSD, it looks like this:

    case AF_INET:
        if (len != sizeof(struct in_addr)) {
	    errno = ENAMETOOLONG;
            return 0;
        }
        strcpy(buf, inet_ntoa(*(const struct in_addr *)addrp));
        break;

In other words, even though addr2ascii() doesn't explicitly use a static buffer, since it depends on inet_ntoa() it's still not thread safe. In order to get thread safety, you need to use yet another API:

    const char *
    inet_ntop(int af, const void *restrict src, char *restrict dst,
        socklen_t size);

Outstanding!

UPDATE: Clarified that this is a problem on FreeBSD. I don't know if it's an issue on all other platforms. Linux, for instance, doesn't have addr2ascii()
UPDATE2: Trivial vs. important.

 

February 9, 2009

This is an interesting development. The California SoS posts a list of donors to various political campaigns, but it's pretty un-user friendly. Someone has done a mashup with Google Maps so you can see everyone in your area who donated to Proposition Eight. Obviously, this is trivially extensible to any arbitrary political issue; it's just that Prop 8 has generated a huge amount of heat in a relatively tech savvy community. I wonder how long it is before a site like this is up for every issue or, perhaps more interestingly, before you can profile every donation for everyone who lives near you. For all I know you can do it now.
 

February 2, 2009

This afternoon Congressman apprarently John Fleming (R-LA) decided he really wanted to update me on what he was doing for his constituents, so he sent me 25 copies of the same email:
I am honored to serve you as the new representative of the 4th Congressional District. My first priority is to stay in touch with everyone I represent, including understanding your interests and concerns and keeping you up to date on news and proposed legislation affecting you. As part of my effort to meet that goal, I will be periodically sending e-newsletters.

I invite you to contact me, anytime, with your opinions and ideas, as your feedback is invaluable to help me do the best job I can as your representative.

It has been a busy start to the 111th Congress and the economy remains my top priority. I hear from Northwest Louisiana residents every day who are asking for Congress to help stabilize the economy, but not to increase the burden for our families. I hear you and I agree. That is the reason I voted no on the Economic Stimulus Bill (H.R. 1) filed by the Speaker Pelosi led majority in the House.

...

Blah blah blah.

It's actually a little hard to tell if this is authentic: it looks to be a real newsletter, but I don't see any link off his main site at house.gov, and the site that's hosting the newsletter is different. Whois reveals that the domain is owned by this dude:

Name:		Gregory    Hildebrand
Address:	12121 Wilshire Blve
750
		Los Angeles, Ca  90025
		US

Email Address:	gregory@politicalsystems.net
Phone Number:	(310)696-2250

But www.politicalsystems.net leads to a Coldfusion stack trace. So, this could be someone trying to make Fleming look bad, but per Hanlon's razor, I suspect that it's just super-incompetent political marketing.