11#include "netif.h"
22#include "common/io/io.h"
33#include "util/mallocHelper.h"
4+ #include "util/debug.h"
45
56#include <arpa/inet.h>
67#include <linux/rtnetlink.h>
1112
1213bool ffNetifGetDefaultRouteImplV4 (FFNetifDefaultRouteResult * result )
1314{
15+ FF_DEBUG ("Starting IPv4 default route detection" );
16+
1417 FF_AUTO_CLOSE_FD int sock_fd = socket (AF_NETLINK , SOCK_RAW | SOCK_CLOEXEC , NETLINK_ROUTE );
1518 if (sock_fd < 0 )
19+ {
20+ FF_DEBUG ("Failed to create netlink socket: errno=%d" , errno );
1621 return false;
22+ }
23+ FF_DEBUG ("Created netlink socket: fd=%d" , sock_fd );
1724
1825 unsigned pid = (unsigned ) getpid ();
26+ FF_DEBUG ("Process PID: %u" , pid );
1927
2028 // Bind socket
2129 struct sockaddr_nl addr = {
@@ -25,8 +33,10 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
2533 };
2634
2735 if (bind (sock_fd , (struct sockaddr * )& addr , sizeof (addr )) < 0 ) {
36+ FF_DEBUG ("Failed to bind socket: errno=%d" , errno );
2837 return false;
2938 }
39+ FF_DEBUG ("Successfully bound socket" );
3040
3141 struct {
3242 struct nlmsghdr nlh ;
@@ -72,8 +82,10 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
7282 (struct sockaddr * )& dest_addr , sizeof (dest_addr ));
7383
7484 if (sent != sizeof (req )) {
85+ FF_DEBUG ("Failed to send netlink request: sent=%zd, expected=%zu" , sent , sizeof (req ));
7586 return false;
7687 }
88+ FF_DEBUG ("Sent netlink request: %zd bytes" , sent );
7789
7890 struct sockaddr_nl src_addr = {};
7991 socklen_t src_addr_len = sizeof (src_addr );
@@ -88,42 +100,52 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
88100
89101 ssize_t peek_size = recvmsg (sock_fd , & msg , MSG_PEEK | MSG_TRUNC );
90102 if (peek_size < 0 ) {
103+ FF_DEBUG ("Failed to peek message size: errno=%d" , errno );
91104 return false;
92105 }
106+ FF_DEBUG ("Message size: %zd bytes" , peek_size );
93107
94108 FF_AUTO_FREE uint8_t * buffer = malloc ((size_t )peek_size );
95109
96110 ssize_t received = recvfrom (sock_fd , buffer , (size_t )peek_size , 0 ,
97111 (struct sockaddr * )& src_addr , & src_addr_len );
98112 if (received != peek_size ) {
113+ FF_DEBUG ("Failed to receive complete message: received=%zd, expected=%zd" , received , peek_size );
99114 return false;
100115 }
116+ FF_DEBUG ("Received netlink response: %zd bytes" , received );
101117
102118 struct {
103119 uint32_t metric ;
104120 uint32_t ifindex ;
105121 uint32_t prefsrc ;
106122 } entry ;
107123 uint32_t minMetric = UINT32_MAX ;
124+ int routeCount = 0 ;
108125
109126 for (const struct nlmsghdr * nlh = (struct nlmsghdr * )buffer ;
110127 NLMSG_OK (nlh , received );
111128 nlh = NLMSG_NEXT (nlh , received )) {
112129 if (nlh -> nlmsg_seq != 1 || nlh -> nlmsg_pid != pid )
113130 continue ;
114131 if (nlh -> nlmsg_type == NLMSG_DONE )
132+ {
133+ FF_DEBUG ("Received NLMSG_DONE, processed %d routes" , routeCount );
115134 break ;
135+ }
116136
117137 if (nlh -> nlmsg_type != RTM_NEWROUTE )
118138 continue ;
119139
140+ routeCount ++ ;
120141 struct rtmsg * rtm = (struct rtmsg * )NLMSG_DATA (nlh );
121142 if (rtm -> rtm_family != AF_INET )
122143 continue ;
123144
124145 if (rtm -> rtm_dst_len != 0 )
125146 continue ;
126147
148+ FF_DEBUG ("Processing IPv4 default route candidate #%d" , routeCount );
127149 entry = (__typeof__ (entry )) { .metric = UINT32_MAX };
128150
129151 // Parse route attributes
@@ -138,20 +160,25 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
138160 uint32_t rta_data = * (uint32_t * ) RTA_DATA (rta );
139161 switch (rta -> rta_type ) {
140162 case RTA_DST :
163+ FF_DEBUG ("Found destination: %s" , inet_ntoa (* (struct in_addr * )& rta_data ));
141164 if (rta_data != 0 ) goto next ;
142165 break ;
143166 case RTA_OIF :
144167 entry .ifindex = rta_data ;
168+ FF_DEBUG ("Found interface index: %u" , entry .ifindex );
145169 break ;
146170 case RTA_GATEWAY :
147171 if (rta_data == 0 ) goto next ;
172+ FF_DEBUG ("Found gateway: %s" , inet_ntoa (* (struct in_addr * )& rta_data ));
148173 break ;
149174 case RTA_PRIORITY :
150175 if (rta_data >= minMetric ) goto next ;
151176 entry .metric = rta_data ;
177+ FF_DEBUG ("Found metric: %u" , entry .metric );
152178 break ;
153179 case RTA_PREFSRC :
154180 entry .prefsrc = rta_data ;
181+ FF_DEBUG ("Found preferred source: %s" , inet_ntoa (* (struct in_addr * )& rta_data ));
155182 break ;
156183 }
157184 }
@@ -163,24 +190,34 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
163190 }
164191 minMetric = entry .metric ;
165192 result -> ifIndex = entry .ifindex ;
166- result -> preferredSourceAddrV4 = entry .prefsrc ;
193+ FF_DEBUG ("Updated best route: ifindex=%u, metric=%u" , entry .ifindex , entry .metric );
194+ // result->preferredSourceAddrV4 = entry.prefsrc;
167195 }
168196
169197 if (minMetric < UINT32_MAX )
170198 {
171199 if_indextoname (result -> ifIndex , result -> ifName );
200+ FF_DEBUG ("Found default IPv4 route: interface=%s, index=%u, metric=%u" , result -> ifName , result -> ifIndex , minMetric );
172201 return true;
173202 }
203+ FF_DEBUG ("No IPv4 default route found" );
174204 return false;
175205}
176206
177207bool ffNetifGetDefaultRouteImplV6 (FFNetifDefaultRouteResult * result )
178208{
209+ FF_DEBUG ("Starting IPv6 default route detection" );
210+
179211 FF_AUTO_CLOSE_FD int sock_fd = socket (AF_NETLINK , SOCK_RAW | SOCK_CLOEXEC , NETLINK_ROUTE );
180212 if (sock_fd < 0 )
213+ {
214+ FF_DEBUG ("Failed to create netlink socket: errno=%d" , errno );
181215 return false;
216+ }
217+ FF_DEBUG ("Created netlink socket: fd=%d" , sock_fd );
182218
183219 unsigned pid = (unsigned ) getpid ();
220+ FF_DEBUG ("Process PID: %u" , pid );
184221
185222 // Bind socket
186223 struct sockaddr_nl addr = {
@@ -190,8 +227,10 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
190227 };
191228
192229 if (bind (sock_fd , (struct sockaddr * )& addr , sizeof (addr )) < 0 ) {
230+ FF_DEBUG ("Failed to bind socket: errno=%d" , errno );
193231 return false;
194232 }
233+ FF_DEBUG ("Successfully bound socket" );
195234
196235 struct {
197236 struct nlmsghdr nlh ;
@@ -237,8 +276,10 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
237276 (struct sockaddr * )& dest_addr , sizeof (dest_addr ));
238277
239278 if (sent != sizeof (req )) {
279+ FF_DEBUG ("Failed to send netlink request: sent=%zd, expected=%zu" , sent , sizeof (req ));
240280 return false;
241281 }
282+ FF_DEBUG ("Sent netlink request: %zd bytes" , sent );
242283
243284 struct sockaddr_nl src_addr = {};
244285 socklen_t src_addr_len = sizeof (src_addr );
@@ -253,41 +294,51 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
253294
254295 ssize_t peek_size = recvmsg (sock_fd , & msg , MSG_PEEK | MSG_TRUNC );
255296 if (peek_size < 0 ) {
297+ FF_DEBUG ("Failed to peek message size: errno=%d" , errno );
256298 return false;
257299 }
300+ FF_DEBUG ("Message size: %zd bytes" , peek_size );
258301
259302 FF_AUTO_FREE uint8_t * buffer = malloc ((size_t )peek_size );
260303
261304 ssize_t received = recvfrom (sock_fd , buffer , (size_t )peek_size , 0 ,
262305 (struct sockaddr * )& src_addr , & src_addr_len );
263306 if (received != peek_size ) {
307+ FF_DEBUG ("Failed to receive complete message: received=%zd, expected=%zd" , received , peek_size );
264308 return false;
265309 }
310+ FF_DEBUG ("Received netlink response: %zd bytes" , received );
266311
267312 struct {
268313 uint32_t metric ;
269314 uint32_t ifindex ;
270315 } entry ;
271316 uint32_t minMetric = UINT32_MAX ;
317+ int routeCount = 0 ;
272318
273319 for (const struct nlmsghdr * nlh = (struct nlmsghdr * )buffer ;
274320 NLMSG_OK (nlh , received );
275321 nlh = NLMSG_NEXT (nlh , received )) {
276322 if (nlh -> nlmsg_seq != 1 || nlh -> nlmsg_pid != pid )
277323 continue ;
278324 if (nlh -> nlmsg_type == NLMSG_DONE )
325+ {
326+ FF_DEBUG ("Received NLMSG_DONE, processed %d routes" , routeCount );
279327 break ;
328+ }
280329
281330 if (nlh -> nlmsg_type != RTM_NEWROUTE )
282331 continue ;
283332
333+ routeCount ++ ;
284334 struct rtmsg * rtm = (struct rtmsg * )NLMSG_DATA (nlh );
285335 if (rtm -> rtm_family != AF_INET6 )
286336 continue ;
287337
288338 if (rtm -> rtm_dst_len != 0 )
289339 continue ;
290340
341+ FF_DEBUG ("Processing IPv6 default route candidate #%d" , routeCount );
291342 entry = (__typeof__ (entry )) { .metric = UINT32_MAX };
292343
293344 // Parse route attributes
@@ -299,26 +350,32 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
299350 switch (rta -> rta_type ) {
300351 case RTA_DST :
301352 if (RTA_PAYLOAD (rta ) >= sizeof (struct in6_addr )) {
353+ FF_MAYBE_UNUSED char str [INET6_ADDRSTRLEN ];
354+ FF_DEBUG ("Found destination: %s" , inet_ntop (AF_INET6 , RTA_DATA (rta ), str , sizeof (str )));
302355 struct in6_addr * dst = (struct in6_addr * ) RTA_DATA (rta );
303356 if (!IN6_IS_ADDR_UNSPECIFIED (dst )) goto next ;
304357 }
305358 break ;
306359 case RTA_OIF :
307360 if (RTA_PAYLOAD (rta ) >= sizeof (uint32_t )) {
308361 entry .ifindex = * (uint32_t * ) RTA_DATA (rta );
362+ FF_DEBUG ("Found interface index: %u" , entry .ifindex );
309363 }
310364 break ;
311365 case RTA_GATEWAY :
312366 if (RTA_PAYLOAD (rta ) >= sizeof (struct in6_addr )) {
313367 struct in6_addr * gw = (struct in6_addr * ) RTA_DATA (rta );
314368 if (IN6_IS_ADDR_UNSPECIFIED (gw )) goto next ;
369+ FF_MAYBE_UNUSED char str [INET6_ADDRSTRLEN ];
370+ FF_DEBUG ("Found gateway: %s" , inet_ntop (AF_INET6 , gw , str , sizeof (str )));
315371 }
316372 break ;
317373 case RTA_PRIORITY :
318374 if (RTA_PAYLOAD (rta ) >= sizeof (uint32_t )) {
319375 uint32_t metric = * (uint32_t * ) RTA_DATA (rta );
320376 if (metric >= minMetric ) goto next ;
321377 entry .metric = metric ;
378+ FF_DEBUG ("Found metric: %u" , entry .metric );
322379 }
323380 break ;
324381 }
@@ -331,12 +388,15 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
331388 }
332389 minMetric = entry .metric ;
333390 result -> ifIndex = entry .ifindex ;
391+ FF_DEBUG ("Updated best route: ifindex=%u, metric=%u" , entry .ifindex , entry .metric );
334392 }
335393
336394 if (minMetric < UINT32_MAX )
337395 {
338396 if_indextoname (result -> ifIndex , result -> ifName );
397+ FF_DEBUG ("Found default IPv6 route: interface=%s, index=%u, metric=%u" , result -> ifName , result -> ifIndex , minMetric );
339398 return true;
340399 }
400+ FF_DEBUG ("No IPv6 default route found" );
341401 return false;
342402}
0 commit comments