Please read my article which provides comprehensive explanation about approach implemented in this repository.
In this repostory you can find source code to retrieve default outgoing IPv4 or IPv6 address on Linux using only standard Linux API.
There are alternative very unreliable ways to accomplish this task. The main point of this repository to provide reliable and very portable approach for this task.