1
1
#include "netif.h"
2
2
#include "common/io/io.h"
3
3
#include "util/mallocHelper.h"
4
+ #include "util/debug.h"
4
5
5
6
#include <arpa/inet.h>
6
7
#include <linux/rtnetlink.h>
11
12
12
13
bool ffNetifGetDefaultRouteImplV4 (FFNetifDefaultRouteResult * result )
13
14
{
15
+ FF_DEBUG ("Starting IPv4 default route detection" );
16
+
14
17
FF_AUTO_CLOSE_FD int sock_fd = socket (AF_NETLINK , SOCK_RAW | SOCK_CLOEXEC , NETLINK_ROUTE );
15
18
if (sock_fd < 0 )
19
+ {
20
+ FF_DEBUG ("Failed to create netlink socket: errno=%d" , errno );
16
21
return false;
22
+ }
23
+ FF_DEBUG ("Created netlink socket: fd=%d" , sock_fd );
17
24
18
25
unsigned pid = (unsigned ) getpid ();
26
+ FF_DEBUG ("Process PID: %u" , pid );
19
27
20
28
// Bind socket
21
29
struct sockaddr_nl addr = {
@@ -25,8 +33,10 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
25
33
};
26
34
27
35
if (bind (sock_fd , (struct sockaddr * )& addr , sizeof (addr )) < 0 ) {
36
+ FF_DEBUG ("Failed to bind socket: errno=%d" , errno );
28
37
return false;
29
38
}
39
+ FF_DEBUG ("Successfully bound socket" );
30
40
31
41
struct {
32
42
struct nlmsghdr nlh ;
@@ -72,8 +82,10 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
72
82
(struct sockaddr * )& dest_addr , sizeof (dest_addr ));
73
83
74
84
if (sent != sizeof (req )) {
85
+ FF_DEBUG ("Failed to send netlink request: sent=%zd, expected=%zu" , sent , sizeof (req ));
75
86
return false;
76
87
}
88
+ FF_DEBUG ("Sent netlink request: %zd bytes" , sent );
77
89
78
90
struct sockaddr_nl src_addr = {};
79
91
socklen_t src_addr_len = sizeof (src_addr );
@@ -88,42 +100,52 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
88
100
89
101
ssize_t peek_size = recvmsg (sock_fd , & msg , MSG_PEEK | MSG_TRUNC );
90
102
if (peek_size < 0 ) {
103
+ FF_DEBUG ("Failed to peek message size: errno=%d" , errno );
91
104
return false;
92
105
}
106
+ FF_DEBUG ("Message size: %zd bytes" , peek_size );
93
107
94
108
FF_AUTO_FREE uint8_t * buffer = malloc ((size_t )peek_size );
95
109
96
110
ssize_t received = recvfrom (sock_fd , buffer , (size_t )peek_size , 0 ,
97
111
(struct sockaddr * )& src_addr , & src_addr_len );
98
112
if (received != peek_size ) {
113
+ FF_DEBUG ("Failed to receive complete message: received=%zd, expected=%zd" , received , peek_size );
99
114
return false;
100
115
}
116
+ FF_DEBUG ("Received netlink response: %zd bytes" , received );
101
117
102
118
struct {
103
119
uint32_t metric ;
104
120
uint32_t ifindex ;
105
121
uint32_t prefsrc ;
106
122
} entry ;
107
123
uint32_t minMetric = UINT32_MAX ;
124
+ int routeCount = 0 ;
108
125
109
126
for (const struct nlmsghdr * nlh = (struct nlmsghdr * )buffer ;
110
127
NLMSG_OK (nlh , received );
111
128
nlh = NLMSG_NEXT (nlh , received )) {
112
129
if (nlh -> nlmsg_seq != 1 || nlh -> nlmsg_pid != pid )
113
130
continue ;
114
131
if (nlh -> nlmsg_type == NLMSG_DONE )
132
+ {
133
+ FF_DEBUG ("Received NLMSG_DONE, processed %d routes" , routeCount );
115
134
break ;
135
+ }
116
136
117
137
if (nlh -> nlmsg_type != RTM_NEWROUTE )
118
138
continue ;
119
139
140
+ routeCount ++ ;
120
141
struct rtmsg * rtm = (struct rtmsg * )NLMSG_DATA (nlh );
121
142
if (rtm -> rtm_family != AF_INET )
122
143
continue ;
123
144
124
145
if (rtm -> rtm_dst_len != 0 )
125
146
continue ;
126
147
148
+ FF_DEBUG ("Processing IPv4 default route candidate #%d" , routeCount );
127
149
entry = (__typeof__ (entry )) { .metric = UINT32_MAX };
128
150
129
151
// Parse route attributes
@@ -138,20 +160,25 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
138
160
uint32_t rta_data = * (uint32_t * ) RTA_DATA (rta );
139
161
switch (rta -> rta_type ) {
140
162
case RTA_DST :
163
+ FF_DEBUG ("Found destination: %s" , inet_ntoa (* (struct in_addr * )& rta_data ));
141
164
if (rta_data != 0 ) goto next ;
142
165
break ;
143
166
case RTA_OIF :
144
167
entry .ifindex = rta_data ;
168
+ FF_DEBUG ("Found interface index: %u" , entry .ifindex );
145
169
break ;
146
170
case RTA_GATEWAY :
147
171
if (rta_data == 0 ) goto next ;
172
+ FF_DEBUG ("Found gateway: %s" , inet_ntoa (* (struct in_addr * )& rta_data ));
148
173
break ;
149
174
case RTA_PRIORITY :
150
175
if (rta_data >= minMetric ) goto next ;
151
176
entry .metric = rta_data ;
177
+ FF_DEBUG ("Found metric: %u" , entry .metric );
152
178
break ;
153
179
case RTA_PREFSRC :
154
180
entry .prefsrc = rta_data ;
181
+ FF_DEBUG ("Found preferred source: %s" , inet_ntoa (* (struct in_addr * )& rta_data ));
155
182
break ;
156
183
}
157
184
}
@@ -163,24 +190,34 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result)
163
190
}
164
191
minMetric = entry .metric ;
165
192
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;
167
195
}
168
196
169
197
if (minMetric < UINT32_MAX )
170
198
{
171
199
if_indextoname (result -> ifIndex , result -> ifName );
200
+ FF_DEBUG ("Found default IPv4 route: interface=%s, index=%u, metric=%u" , result -> ifName , result -> ifIndex , minMetric );
172
201
return true;
173
202
}
203
+ FF_DEBUG ("No IPv4 default route found" );
174
204
return false;
175
205
}
176
206
177
207
bool ffNetifGetDefaultRouteImplV6 (FFNetifDefaultRouteResult * result )
178
208
{
209
+ FF_DEBUG ("Starting IPv6 default route detection" );
210
+
179
211
FF_AUTO_CLOSE_FD int sock_fd = socket (AF_NETLINK , SOCK_RAW | SOCK_CLOEXEC , NETLINK_ROUTE );
180
212
if (sock_fd < 0 )
213
+ {
214
+ FF_DEBUG ("Failed to create netlink socket: errno=%d" , errno );
181
215
return false;
216
+ }
217
+ FF_DEBUG ("Created netlink socket: fd=%d" , sock_fd );
182
218
183
219
unsigned pid = (unsigned ) getpid ();
220
+ FF_DEBUG ("Process PID: %u" , pid );
184
221
185
222
// Bind socket
186
223
struct sockaddr_nl addr = {
@@ -190,8 +227,10 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
190
227
};
191
228
192
229
if (bind (sock_fd , (struct sockaddr * )& addr , sizeof (addr )) < 0 ) {
230
+ FF_DEBUG ("Failed to bind socket: errno=%d" , errno );
193
231
return false;
194
232
}
233
+ FF_DEBUG ("Successfully bound socket" );
195
234
196
235
struct {
197
236
struct nlmsghdr nlh ;
@@ -237,8 +276,10 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
237
276
(struct sockaddr * )& dest_addr , sizeof (dest_addr ));
238
277
239
278
if (sent != sizeof (req )) {
279
+ FF_DEBUG ("Failed to send netlink request: sent=%zd, expected=%zu" , sent , sizeof (req ));
240
280
return false;
241
281
}
282
+ FF_DEBUG ("Sent netlink request: %zd bytes" , sent );
242
283
243
284
struct sockaddr_nl src_addr = {};
244
285
socklen_t src_addr_len = sizeof (src_addr );
@@ -253,41 +294,51 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
253
294
254
295
ssize_t peek_size = recvmsg (sock_fd , & msg , MSG_PEEK | MSG_TRUNC );
255
296
if (peek_size < 0 ) {
297
+ FF_DEBUG ("Failed to peek message size: errno=%d" , errno );
256
298
return false;
257
299
}
300
+ FF_DEBUG ("Message size: %zd bytes" , peek_size );
258
301
259
302
FF_AUTO_FREE uint8_t * buffer = malloc ((size_t )peek_size );
260
303
261
304
ssize_t received = recvfrom (sock_fd , buffer , (size_t )peek_size , 0 ,
262
305
(struct sockaddr * )& src_addr , & src_addr_len );
263
306
if (received != peek_size ) {
307
+ FF_DEBUG ("Failed to receive complete message: received=%zd, expected=%zd" , received , peek_size );
264
308
return false;
265
309
}
310
+ FF_DEBUG ("Received netlink response: %zd bytes" , received );
266
311
267
312
struct {
268
313
uint32_t metric ;
269
314
uint32_t ifindex ;
270
315
} entry ;
271
316
uint32_t minMetric = UINT32_MAX ;
317
+ int routeCount = 0 ;
272
318
273
319
for (const struct nlmsghdr * nlh = (struct nlmsghdr * )buffer ;
274
320
NLMSG_OK (nlh , received );
275
321
nlh = NLMSG_NEXT (nlh , received )) {
276
322
if (nlh -> nlmsg_seq != 1 || nlh -> nlmsg_pid != pid )
277
323
continue ;
278
324
if (nlh -> nlmsg_type == NLMSG_DONE )
325
+ {
326
+ FF_DEBUG ("Received NLMSG_DONE, processed %d routes" , routeCount );
279
327
break ;
328
+ }
280
329
281
330
if (nlh -> nlmsg_type != RTM_NEWROUTE )
282
331
continue ;
283
332
333
+ routeCount ++ ;
284
334
struct rtmsg * rtm = (struct rtmsg * )NLMSG_DATA (nlh );
285
335
if (rtm -> rtm_family != AF_INET6 )
286
336
continue ;
287
337
288
338
if (rtm -> rtm_dst_len != 0 )
289
339
continue ;
290
340
341
+ FF_DEBUG ("Processing IPv6 default route candidate #%d" , routeCount );
291
342
entry = (__typeof__ (entry )) { .metric = UINT32_MAX };
292
343
293
344
// Parse route attributes
@@ -299,26 +350,32 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
299
350
switch (rta -> rta_type ) {
300
351
case RTA_DST :
301
352
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 )));
302
355
struct in6_addr * dst = (struct in6_addr * ) RTA_DATA (rta );
303
356
if (!IN6_IS_ADDR_UNSPECIFIED (dst )) goto next ;
304
357
}
305
358
break ;
306
359
case RTA_OIF :
307
360
if (RTA_PAYLOAD (rta ) >= sizeof (uint32_t )) {
308
361
entry .ifindex = * (uint32_t * ) RTA_DATA (rta );
362
+ FF_DEBUG ("Found interface index: %u" , entry .ifindex );
309
363
}
310
364
break ;
311
365
case RTA_GATEWAY :
312
366
if (RTA_PAYLOAD (rta ) >= sizeof (struct in6_addr )) {
313
367
struct in6_addr * gw = (struct in6_addr * ) RTA_DATA (rta );
314
368
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 )));
315
371
}
316
372
break ;
317
373
case RTA_PRIORITY :
318
374
if (RTA_PAYLOAD (rta ) >= sizeof (uint32_t )) {
319
375
uint32_t metric = * (uint32_t * ) RTA_DATA (rta );
320
376
if (metric >= minMetric ) goto next ;
321
377
entry .metric = metric ;
378
+ FF_DEBUG ("Found metric: %u" , entry .metric );
322
379
}
323
380
break ;
324
381
}
@@ -331,12 +388,15 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result)
331
388
}
332
389
minMetric = entry .metric ;
333
390
result -> ifIndex = entry .ifindex ;
391
+ FF_DEBUG ("Updated best route: ifindex=%u, metric=%u" , entry .ifindex , entry .metric );
334
392
}
335
393
336
394
if (minMetric < UINT32_MAX )
337
395
{
338
396
if_indextoname (result -> ifIndex , result -> ifName );
397
+ FF_DEBUG ("Found default IPv6 route: interface=%s, index=%u, metric=%u" , result -> ifName , result -> ifIndex , minMetric );
339
398
return true;
340
399
}
400
+ FF_DEBUG ("No IPv6 default route found" );
341
401
return false;
342
402
}
0 commit comments