How to find the address of default gateway and the local address facing towards it? : <p>I'm implementing a port control protocol (PCP) client working in Linux, implemented in C. It needs to connect to the default gateway to open ports. PCP needs to embed the local IP address in the message itself, not just in the IP headers (presumably this is for detecting the presence of unwanted NAT in the path, but it's a mystery to me how there could be a NAT between the machine itself and its default gateway). PCP also needs to connect to the default gateway, needing its address.</p>

<p>This raises two questions:</p>

<ol>
<li>How to find the IP address of the default gateway?</li>
<li>How to find the IP address of the machine towards the default gateway even in the presence of multiple interfaces and IP addresses?</li>
</ol>
