Getting a usable, non-localhost IP address for a JVM

I recently had a task where I needed to inform a docker image of the host OS’ IP address. (The docker image needed to make an HTTP call to the host machine, for testing purposes.) However, it’s not trivial to find code to actually get a host machine’s IP address. Here’s some code in Kotlin (and in Java) to accomplish the task; I can’t say it’s perfect (and won’t) but it worked for me, and hopefully this can serve as a tentpole for perhaps better code.
There are two pieces of code here; both eliminate the 10.*.*.* network. If you don’t need that network to be eliminated, well, remove that line.

Kotlin

import java.net.NetworkInterface
fun getAddress(): String {
    return NetworkInterface.getNetworkInterfaces().toList().flatMap {
        it.inetAddresses.toList()
                .filter { it.address.size == 4 }
                .filter { !it.isLoopbackAddress }
                .filter { it.address[0] != 10.toByte() }
                .map { it.hostAddress }
    }.first()
}

Java

The Java code’s a lot more convoluted, and isn’t helped at all by the requirement for a means by which to convert an Enumeration to a Stream (copied shamelessly from “Iterate an Enumeration in Java 8“, by the way.) However, it’s functionally equivalent to the Kotlin code. If it can’t find a valid address or an internal exception is thrown, a RuntimeException is created and thrown.

private static  Stream enumerationAsStream(Enumeration e) {
    return StreamSupport.stream(
            Spliterators.spliteratorUnknownSize(
                    new Iterator() {
                        public T next() {
                            return e.nextElement();
                        }
                        public boolean hasNext() {
                            return e.hasMoreElements();
                        }
                    },
                    Spliterator.ORDERED), false);
}
public static String getAddress() {
    try {
        Optional address = enumerationAsStream(NetworkInterface.getNetworkInterfaces())
                .flatMap(networkInterface ->
                        networkInterface.getInterfaceAddresses().stream()
                )
                .filter(ia -> !ia.getAddress().isLoopbackAddress())
                .map(InterfaceAddress::getAddress)
                .filter(a -> a.getAddress().length == 4)
                .filter(a -> a.getAddress()[0] != 10)
                .findFirst();
        if (address.isPresent()) {
            return address.get().getHostAddress();
        } else {
            throw new RuntimeException("Address not accessible");
        }
    } catch (SocketException e) {
        throw new RuntimeException(e);
    }
}

Summary

This is not perfect code, I wouldn’t think. It was thrown together to fit a specific need, and ideally should have been easily located on StackOverflow or something like that.

Interesting Links – 8 Nov 2017

  • From ernimril: a video! CppCon 2016: Jason Turner “Rich Code for Tiny Computers: A Simple Commodore 64 Game in C++17” is an hour and twenty minutes of Jason Turner talking about writing a game for the Commodore 64 using, surprise, C++17 and translating to 6502 assembly. (Play at 1.25x speed to save some time – or 2x speed if you want that Brian Goetz effect.) It’s actually really fascinating to watch, and has nothing to do with Java whatsoever.
  • For Mac users, particularly on Sierra: “MacOS Sierra problems with java.net.InetAddress: getLocalHost()” documents some lookup problems on the recent MacOS update. Short form: make sure your /etc/hosts actually has your local domain name resolving to 127.0.0.1.
  • FindBugs is apparently having some problems.
  • Non-java, but useful for programmers anyway: Bulletproof Mind: 6 Techniques for Mental Resilience from the Navy SEALs. Some adult language, but it’s an excellent article and we’re all adults anyway.
  • Docker in Production: A History of Failure” is a litany of issues with the popular virtualization technology. It’s worth reading, even if you’ve deployed Docker successfully – if only to keep track of how far there is to go.
  • From the Python world: EAFP and LBYL. In Python, apparently using the “Easier to Ask For Permission” approach yields massive performance gains; Java, like C and C++, tends to prefer LBYL, which stands for “Look Before You Leap.” Worth keeping in mind, especially as Java adds more functional programming concepts. It’d be interesting to see EAFP and LBYL contrasted well in Java – and note that EAFP tends to prefer try/catch to manual boundary checking, so maybe Java’s already there to a large degree.