samadhiweb

smalltalk programming for the web



Converting IPv4 addresses from/to ulogd integers

13 February 2016

The Linux server that hosts this blog uses ulogd to perform iptables-related logging to an SQLite database. ulogd logs IPv4 addresses as integers. Here's a handy snippet to convert from ulogd's integer to IPv4 dotted quad:

| i |
i := 1661053120.
ByteArray with: (i & 255)
    with: ((i >> 8) & 255)
    with: ((i >> 16) & 255)
    with: i >> 24
==> #[192 168 1 99]

And the reverse:

| ba |
ba := #[192 168 1 99].
((ba at: 4) << 24) + ((ba at: 3) << 16)
    + ((ba at: 2) << 8) + (ba at: 1)
==> 1661053120