This code already does this if IPv6 support is compiled in. It was dereferencing a NULL pointer in the IPv4 only case.