Linux and the BSDs have getifaddrs(). Use it and save a bunch of complex coding.