Enterprise code... or is that "serirpteEn deco?"

One of the largest problems you might have with Enterprise Grade Code is that it often isn’t.

Case in point: Program A and Program Module B does not know which hand is washing the other, or, indeed, which side it should be on… is it the top, or is it the bottom?

Module B was working, er, somewhat. All logging was failing, and any user stats were being bitswapped, but authentication features were functional.

Module A and B were running on the same platform, on the same hardware, with the same architecture. All IA32. All Apache 2.2.x with MySQL 5. All the same endian, even for the headers in the DB and Apache… Oh, yes, they’ve thought about these things, those desighers.

However, Module B was swapping things.

The authors of Module B evidently knew of this, and created a MODULEB_IP_REVERSED variable. This is a global variable, despite appearing otherwise.

Yes, it swapped the bytes for EVERYTHING that dealt with IP functions. Locally, externally, from Module B. Everything.

So, a user using Module B would be “1.2.3.4”, but once they were out of it, they’d be “4.3.2.1”. Lovely. At least they didn’t screw up the octets, just the storage, thereof.

I looked at the database (oh dear please don’t ask). It stores the IP as an unsigned int. Goody. This will be fun to fix.

So, I had to wrap all of the local functions, which, oddly enough, took an actual dotted quad syntax. Enter ‘nasty shim’ (product, of course, was written in PHP and Zend Encoded):

function ip_swap_addr($ip) {
  $Addr = ereg(":", $ip) ? array_reverse(explode(":", $ip)) \
  : array_reverse(explode(".", $ip));
  return (count($Addr) > 4) ? implode(":", $Addr) : \
    implode(".", $Addr);
}

So, now everything that passes an IP (has been wrapped by a little page which parses $_GET/$_POST, and searches for this, rewrites it, then passes it back to the original function in another page, before transparently posting back the results.

Yes, I made it fully IPV6 capable, because, damn it, I like to be safe with my “Enterprise Code.”