77#include <linux/rtnetlink.h>
88#include <net/if.h>
99
10+ static uint32_t ffNetifGetNetlinkPortId (int sock_fd ) {
11+ struct sockaddr_nl addr = {};
12+ socklen_t addrLen = sizeof (addr );
13+ if (getsockname (sock_fd , (struct sockaddr * ) & addr , & addrLen ) < 0 ) {
14+ FF_DEBUG ("Failed to query netlink socket address (use PID instead): %s" , strerror (errno ));
15+ return instance .state .platform .pid ;
16+ } else {
17+ FF_DEBUG ("Netlink port ID: %u" , addr .nl_pid );
18+ return addr .nl_pid ;
19+ }
20+ }
21+
1022bool ffNetifGetDefaultRouteImplV4 (FFNetifDefaultRouteResult * result ) {
1123 FF_DEBUG ("Starting IPv4 default route detection" );
1224
@@ -17,9 +29,6 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) {
1729 }
1830 FF_DEBUG ("Created netlink socket: fd=%d" , sock_fd );
1931
20- uint32_t pid = instance .state .platform .pid ;
21- FF_DEBUG ("Process PID: %u" , pid );
22-
2332 // Bind socket
2433 struct sockaddr_nl addr = {
2534 .nl_family = AF_NETLINK ,
@@ -33,6 +42,8 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) {
3342 }
3443 FF_DEBUG ("Successfully bound socket" );
3544
45+ uint32_t pid = ffNetifGetNetlinkPortId (sock_fd );
46+
3647 struct __attribute__((__packed__ )) {
3748 struct nlmsghdr nlh ;
3849 struct rtmsg rtm ;
@@ -228,9 +239,6 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) {
228239 }
229240 FF_DEBUG ("Created netlink socket: fd=%d" , sock_fd );
230241
231- uint32_t pid = instance .state .platform .pid ;
232- FF_DEBUG ("Process PID: %u" , pid );
233-
234242 // Bind socket
235243 struct sockaddr_nl addr = {
236244 .nl_family = AF_NETLINK ,
@@ -244,6 +252,8 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) {
244252 }
245253 FF_DEBUG ("Successfully bound socket" );
246254
255+ uint32_t pid = ffNetifGetNetlinkPortId (sock_fd );
256+
247257 struct __attribute__((__packed__ )) {
248258 struct nlmsghdr nlh ;
249259 struct rtmsg rtm ;
0 commit comments