1 | /****************************************************************************** |
---|
2 | * $Id: platform.c 2154 2007-06-18 19:39:52Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #ifdef SYS_BEOS |
---|
26 | #include <fs_info.h> |
---|
27 | #include <FindDirectory.h> |
---|
28 | #endif |
---|
29 | #include <sys/types.h> |
---|
30 | #include <dirent.h> |
---|
31 | |
---|
32 | #include "transmission.h" |
---|
33 | |
---|
34 | #if !defined( SYS_BEOS ) && !defined( __AMIGAOS4__ ) |
---|
35 | |
---|
36 | #include <pwd.h> |
---|
37 | |
---|
38 | const char * |
---|
39 | tr_getHomeDirectory( void ) |
---|
40 | { |
---|
41 | static char homeDirectory[MAX_PATH_LENGTH]; |
---|
42 | static int init = 0; |
---|
43 | char * envHome; |
---|
44 | struct passwd * pw; |
---|
45 | |
---|
46 | if( init ) |
---|
47 | { |
---|
48 | return homeDirectory; |
---|
49 | } |
---|
50 | |
---|
51 | envHome = getenv( "HOME" ); |
---|
52 | if( NULL == envHome ) |
---|
53 | { |
---|
54 | pw = getpwuid( getuid() ); |
---|
55 | endpwent(); |
---|
56 | if( NULL == pw ) |
---|
57 | { |
---|
58 | /* XXX need to handle this case */ |
---|
59 | return NULL; |
---|
60 | } |
---|
61 | envHome = pw->pw_dir; |
---|
62 | } |
---|
63 | |
---|
64 | snprintf( homeDirectory, MAX_PATH_LENGTH, "%s", envHome ); |
---|
65 | init = 1; |
---|
66 | |
---|
67 | return homeDirectory; |
---|
68 | } |
---|
69 | |
---|
70 | #else |
---|
71 | |
---|
72 | const char * |
---|
73 | tr_getHomeDirectory( void ) |
---|
74 | { |
---|
75 | /* XXX */ |
---|
76 | return ""; |
---|
77 | } |
---|
78 | |
---|
79 | #endif /* !SYS_BEOS && !__AMIGAOS4__ */ |
---|
80 | |
---|
81 | static void |
---|
82 | tr_migrateResume( const char *oldDirectory, const char *newDirectory ) |
---|
83 | { |
---|
84 | DIR * dirh = opendir( oldDirectory ); |
---|
85 | |
---|
86 | if( dirh != NULL ) |
---|
87 | { |
---|
88 | struct dirent * dirp; |
---|
89 | |
---|
90 | while( ( dirp = readdir( dirh ) ) ) |
---|
91 | { |
---|
92 | if( !strncmp( "resume.", dirp->d_name, 7 ) ) |
---|
93 | { |
---|
94 | char o[MAX_PATH_LENGTH]; |
---|
95 | char n[MAX_PATH_LENGTH]; |
---|
96 | tr_buildPath( o, sizeof(o), oldDirectory, dirp->d_name, NULL ); |
---|
97 | tr_buildPath( n, sizeof(n), newDirectory, dirp->d_name, NULL ); |
---|
98 | rename( o, n ); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | closedir( dirh ); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | const char * |
---|
107 | tr_getPrefsDirectory( void ) |
---|
108 | { |
---|
109 | static char buf[MAX_PATH_LENGTH]; |
---|
110 | static int init = 0; |
---|
111 | static size_t buflen = sizeof(buf); |
---|
112 | const char* h; |
---|
113 | |
---|
114 | if( init ) |
---|
115 | return buf; |
---|
116 | |
---|
117 | h = tr_getHomeDirectory(); |
---|
118 | #ifdef SYS_BEOS |
---|
119 | find_directory( B_USER_SETTINGS_DIRECTORY, |
---|
120 | dev_for_path("/boot"), true, buf, buflen ); |
---|
121 | strcat( buf, "/Transmission" ); |
---|
122 | #elif defined( SYS_DARWIN ) |
---|
123 | tr_buildPath ( buf, buflen, h, |
---|
124 | "Library", "Application Support", "Transmission", NULL ); |
---|
125 | #elif defined(__AMIGAOS4__) |
---|
126 | snprintf( buf, buflen, "PROGDIR:.transmission" ); |
---|
127 | #else |
---|
128 | tr_buildPath ( buf, buflen, h, ".transmission", NULL ); |
---|
129 | #endif |
---|
130 | |
---|
131 | tr_mkdir( buf ); |
---|
132 | init = 1; |
---|
133 | |
---|
134 | #ifdef SYS_DARWIN |
---|
135 | char old[MAX_PATH_LENGTH]; |
---|
136 | tr_buildPath ( old, sizeof(old), h, ".transmission", NULL ); |
---|
137 | tr_migrateResume( old, buf ); |
---|
138 | rmdir( oldDirectory ); |
---|
139 | #endif |
---|
140 | |
---|
141 | return buf; |
---|
142 | } |
---|
143 | |
---|
144 | const char * |
---|
145 | tr_getCacheDirectory( void ) |
---|
146 | { |
---|
147 | static char buf[MAX_PATH_LENGTH]; |
---|
148 | static int init = 0; |
---|
149 | static const size_t buflen = sizeof(buf); |
---|
150 | const char * p; |
---|
151 | |
---|
152 | if( init ) |
---|
153 | return buf; |
---|
154 | |
---|
155 | p = tr_getPrefsDirectory(); |
---|
156 | #ifdef SYS_BEOS |
---|
157 | tr_buildPath( buf, buflen, p, "Cache", NULL ); |
---|
158 | #elif defined( SYS_DARWIN ) |
---|
159 | tr_buildPath( buf, buflen, tr_getHomeDirectory(), |
---|
160 | "Library", "Caches", "Transmission", NULL ); |
---|
161 | #else |
---|
162 | tr_buildPath( buf, buflen, p, "cache", NULL ); |
---|
163 | #endif |
---|
164 | |
---|
165 | tr_mkdir( buf ); |
---|
166 | init = 1; |
---|
167 | |
---|
168 | if( strcmp( p, buf ) ) |
---|
169 | tr_migrateResume( p, buf ); |
---|
170 | |
---|
171 | return buf; |
---|
172 | } |
---|
173 | |
---|
174 | const char * |
---|
175 | tr_getTorrentsDirectory( void ) |
---|
176 | { |
---|
177 | static char buf[MAX_PATH_LENGTH]; |
---|
178 | static int init = 0; |
---|
179 | static const size_t buflen = sizeof(buf); |
---|
180 | const char * p; |
---|
181 | |
---|
182 | if( init ) |
---|
183 | return buf; |
---|
184 | |
---|
185 | p = tr_getPrefsDirectory (); |
---|
186 | |
---|
187 | #ifdef SYS_BEOS |
---|
188 | tr_buildPath( buf, buflen, p, "Torrents", NULL ); |
---|
189 | #elif defined( SYS_DARWIN ) |
---|
190 | tr_buildPath( buf, buflen, p, "Torrents", NULL ); |
---|
191 | #else |
---|
192 | tr_buildPath( buf, buflen, p, "torrents", NULL ); |
---|
193 | #endif |
---|
194 | |
---|
195 | tr_mkdir( buf ); |
---|
196 | init = 1; |
---|
197 | return buf; |
---|
198 | } |
---|
199 | |
---|
200 | static void ThreadFunc( void * _t ) |
---|
201 | { |
---|
202 | tr_thread_t * t = _t; |
---|
203 | |
---|
204 | #ifdef SYS_BEOS |
---|
205 | /* This is required because on BeOS, SIGINT is sent to each thread, |
---|
206 | which kills them not nicely */ |
---|
207 | signal( SIGINT, SIG_IGN ); |
---|
208 | #endif |
---|
209 | |
---|
210 | tr_dbg( "Thread '%s' started", t->name ); |
---|
211 | t->func( t->arg ); |
---|
212 | tr_dbg( "Thread '%s' exited", t->name ); |
---|
213 | } |
---|
214 | |
---|
215 | void tr_threadCreate( tr_thread_t * t, void (*func)(void *), void * arg, |
---|
216 | char * name ) |
---|
217 | { |
---|
218 | t->func = func; |
---|
219 | t->arg = arg; |
---|
220 | t->name = strdup( name ); |
---|
221 | #ifdef SYS_BEOS |
---|
222 | t->thread = spawn_thread( (void *) ThreadFunc, name, |
---|
223 | B_NORMAL_PRIORITY, t ); |
---|
224 | resume_thread( t->thread ); |
---|
225 | #else |
---|
226 | pthread_create( &t->thread, NULL, (void *) ThreadFunc, t ); |
---|
227 | #endif |
---|
228 | } |
---|
229 | |
---|
230 | const tr_thread_t THREAD_EMPTY = { NULL, NULL, NULL, 0 }; |
---|
231 | |
---|
232 | void tr_threadJoin( tr_thread_t * t ) |
---|
233 | { |
---|
234 | if (t->func != NULL) |
---|
235 | { |
---|
236 | #ifdef SYS_BEOS |
---|
237 | long exit; |
---|
238 | wait_for_thread( t->thread, &exit ); |
---|
239 | #else |
---|
240 | pthread_join( t->thread, NULL ); |
---|
241 | #endif |
---|
242 | tr_dbg( "Thread '%s' joined", t->name ); |
---|
243 | free( t->name ); |
---|
244 | t->name = NULL; |
---|
245 | t->func = NULL; |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | void tr_lockInit( tr_lock_t * l ) |
---|
250 | { |
---|
251 | #ifdef SYS_BEOS |
---|
252 | *l = create_sem( 1, "" ); |
---|
253 | #else |
---|
254 | pthread_mutex_init( l, NULL ); |
---|
255 | #endif |
---|
256 | } |
---|
257 | |
---|
258 | void tr_lockClose( tr_lock_t * l ) |
---|
259 | { |
---|
260 | #ifdef SYS_BEOS |
---|
261 | delete_sem( *l ); |
---|
262 | #else |
---|
263 | pthread_mutex_destroy( l ); |
---|
264 | #endif |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | void tr_condInit( tr_cond_t * c ) |
---|
269 | { |
---|
270 | #ifdef SYS_BEOS |
---|
271 | *c = -1; |
---|
272 | #else |
---|
273 | pthread_cond_init( c, NULL ); |
---|
274 | #endif |
---|
275 | } |
---|
276 | |
---|
277 | void tr_condWait( tr_cond_t * c, tr_lock_t * l ) |
---|
278 | { |
---|
279 | #ifdef SYS_BEOS |
---|
280 | *c = find_thread( NULL ); |
---|
281 | release_sem( *l ); |
---|
282 | suspend_thread( *c ); |
---|
283 | acquire_sem( *l ); |
---|
284 | *c = -1; |
---|
285 | #else |
---|
286 | pthread_cond_wait( c, l ); |
---|
287 | #endif |
---|
288 | } |
---|
289 | |
---|
290 | void tr_condSignal( tr_cond_t * c ) |
---|
291 | { |
---|
292 | #ifdef SYS_BEOS |
---|
293 | while( *c != -1 ) |
---|
294 | { |
---|
295 | thread_info info; |
---|
296 | get_thread_info( *c, &info ); |
---|
297 | if( info.state == B_THREAD_SUSPENDED ) |
---|
298 | { |
---|
299 | resume_thread( *c ); |
---|
300 | break; |
---|
301 | } |
---|
302 | snooze( 5000 ); |
---|
303 | } |
---|
304 | #else |
---|
305 | pthread_cond_signal( c ); |
---|
306 | #endif |
---|
307 | } |
---|
308 | |
---|
309 | void tr_condClose( tr_cond_t * c ) |
---|
310 | { |
---|
311 | #ifdef SYS_BEOS |
---|
312 | *c = -1; /* Shut up gcc */ |
---|
313 | #else |
---|
314 | pthread_cond_destroy( c ); |
---|
315 | #endif |
---|
316 | } |
---|
317 | |
---|
318 | |
---|
319 | #if defined( BSD ) |
---|
320 | |
---|
321 | #include <sys/sysctl.h> |
---|
322 | #include <net/route.h> |
---|
323 | |
---|
324 | static uint8_t * |
---|
325 | getroute( int * buflen ); |
---|
326 | static int |
---|
327 | parseroutes( uint8_t * buf, int len, struct in_addr * addr ); |
---|
328 | |
---|
329 | int |
---|
330 | tr_getDefaultRoute( struct in_addr * addr ) |
---|
331 | { |
---|
332 | uint8_t * buf; |
---|
333 | int len; |
---|
334 | |
---|
335 | buf = getroute( &len ); |
---|
336 | if( NULL == buf ) |
---|
337 | { |
---|
338 | tr_err( "failed to get default route (BSD)" ); |
---|
339 | return 1; |
---|
340 | } |
---|
341 | |
---|
342 | len = parseroutes( buf, len, addr ); |
---|
343 | free( buf ); |
---|
344 | |
---|
345 | return len; |
---|
346 | } |
---|
347 | |
---|
348 | #ifndef SA_SIZE |
---|
349 | #define ROUNDUP( a, size ) \ |
---|
350 | ( ( (a) & ( (size) - 1 ) ) ? ( 1 + ( (a) | ( (size) - 1 ) ) ) : (a) ) |
---|
351 | #define SA_SIZE( sap ) \ |
---|
352 | ( sap->sa_len ? ROUNDUP( (sap)->sa_len, sizeof( u_long ) ) : \ |
---|
353 | sizeof( u_long ) ) |
---|
354 | #endif /* !SA_SIZE */ |
---|
355 | #define NEXT_SA( sap ) \ |
---|
356 | (struct sockaddr *) ( (caddr_t) (sap) + ( SA_SIZE( (sap) ) ) ) |
---|
357 | |
---|
358 | static uint8_t * |
---|
359 | getroute( int * buflen ) |
---|
360 | { |
---|
361 | int mib[6]; |
---|
362 | size_t len; |
---|
363 | uint8_t * buf; |
---|
364 | |
---|
365 | mib[0] = CTL_NET; |
---|
366 | mib[1] = PF_ROUTE; |
---|
367 | mib[2] = 0; |
---|
368 | mib[3] = AF_INET; |
---|
369 | mib[4] = NET_RT_FLAGS; |
---|
370 | mib[5] = RTF_GATEWAY; |
---|
371 | |
---|
372 | if( sysctl( mib, 6, NULL, &len, NULL, 0 ) ) |
---|
373 | { |
---|
374 | if( ENOENT != errno ) |
---|
375 | { |
---|
376 | tr_err( "sysctl net.route.0.inet.flags.gateway failed (%s)", |
---|
377 | strerror( errno ) ); |
---|
378 | } |
---|
379 | *buflen = 0; |
---|
380 | return NULL; |
---|
381 | } |
---|
382 | |
---|
383 | buf = malloc( len ); |
---|
384 | if( NULL == buf ) |
---|
385 | { |
---|
386 | *buflen = 0; |
---|
387 | return NULL; |
---|
388 | } |
---|
389 | |
---|
390 | if( sysctl( mib, 6, buf, &len, NULL, 0 ) ) |
---|
391 | { |
---|
392 | tr_err( "sysctl net.route.0.inet.flags.gateway failed (%s)", |
---|
393 | strerror( errno ) ); |
---|
394 | free( buf ); |
---|
395 | *buflen = 0; |
---|
396 | return NULL; |
---|
397 | } |
---|
398 | |
---|
399 | *buflen = len; |
---|
400 | |
---|
401 | return buf; |
---|
402 | } |
---|
403 | |
---|
404 | static int |
---|
405 | parseroutes( uint8_t * buf, int len, struct in_addr * addr ) |
---|
406 | { |
---|
407 | uint8_t * end; |
---|
408 | struct rt_msghdr * rtm; |
---|
409 | struct sockaddr * sa; |
---|
410 | struct sockaddr_in * sin; |
---|
411 | int ii; |
---|
412 | struct in_addr dest, gw; |
---|
413 | |
---|
414 | end = buf + len; |
---|
415 | while( end > buf + sizeof( *rtm ) ) |
---|
416 | { |
---|
417 | rtm = (struct rt_msghdr *) buf; |
---|
418 | buf += rtm->rtm_msglen; |
---|
419 | if( end >= buf ) |
---|
420 | { |
---|
421 | dest.s_addr = INADDR_NONE; |
---|
422 | gw.s_addr = INADDR_NONE; |
---|
423 | sa = (struct sockaddr *) ( rtm + 1 ); |
---|
424 | |
---|
425 | for( ii = 0; ii < RTAX_MAX && (uint8_t *) sa < buf; ii++ ) |
---|
426 | { |
---|
427 | if( buf < (uint8_t *) NEXT_SA( sa ) ) |
---|
428 | { |
---|
429 | break; |
---|
430 | } |
---|
431 | |
---|
432 | if( rtm->rtm_addrs & ( 1 << ii ) ) |
---|
433 | { |
---|
434 | if( AF_INET == sa->sa_family ) |
---|
435 | { |
---|
436 | sin = (struct sockaddr_in *) sa; |
---|
437 | switch( ii ) |
---|
438 | { |
---|
439 | case RTAX_DST: |
---|
440 | dest = sin->sin_addr; |
---|
441 | break; |
---|
442 | case RTAX_GATEWAY: |
---|
443 | gw = sin->sin_addr; |
---|
444 | break; |
---|
445 | } |
---|
446 | } |
---|
447 | sa = NEXT_SA( sa ); |
---|
448 | } |
---|
449 | } |
---|
450 | |
---|
451 | if( INADDR_ANY == dest.s_addr && INADDR_NONE != gw.s_addr ) |
---|
452 | { |
---|
453 | *addr = gw; |
---|
454 | return 0; |
---|
455 | } |
---|
456 | } |
---|
457 | } |
---|
458 | |
---|
459 | return 1; |
---|
460 | } |
---|
461 | |
---|
462 | #elif defined( linux ) || defined( __linux ) || defined( __linux__ ) |
---|
463 | |
---|
464 | #include <linux/types.h> |
---|
465 | #include <linux/netlink.h> |
---|
466 | #include <linux/rtnetlink.h> |
---|
467 | |
---|
468 | #define SEQNUM 195909 |
---|
469 | |
---|
470 | static int |
---|
471 | getsock( void ); |
---|
472 | static uint8_t * |
---|
473 | getroute( int fd, unsigned int * buflen ); |
---|
474 | static int |
---|
475 | parseroutes( uint8_t * buf, unsigned int len, struct in_addr * addr ); |
---|
476 | |
---|
477 | int |
---|
478 | tr_getDefaultRoute( struct in_addr * addr ) |
---|
479 | { |
---|
480 | int fd, ret; |
---|
481 | unsigned int len; |
---|
482 | uint8_t * buf; |
---|
483 | |
---|
484 | ret = 1; |
---|
485 | fd = getsock(); |
---|
486 | if( 0 <= fd ) |
---|
487 | { |
---|
488 | while( ret ) |
---|
489 | { |
---|
490 | buf = getroute( fd, &len ); |
---|
491 | if( NULL == buf ) |
---|
492 | { |
---|
493 | break; |
---|
494 | } |
---|
495 | ret = parseroutes( buf, len, addr ); |
---|
496 | free( buf ); |
---|
497 | } |
---|
498 | close( fd ); |
---|
499 | } |
---|
500 | |
---|
501 | if( ret ) |
---|
502 | { |
---|
503 | tr_err( "failed to get default route (Linux)" ); |
---|
504 | } |
---|
505 | |
---|
506 | return ret; |
---|
507 | } |
---|
508 | |
---|
509 | static int |
---|
510 | getsock( void ) |
---|
511 | { |
---|
512 | int fd, flags; |
---|
513 | struct |
---|
514 | { |
---|
515 | struct nlmsghdr nlh; |
---|
516 | struct rtgenmsg rtg; |
---|
517 | } req; |
---|
518 | struct sockaddr_nl snl; |
---|
519 | |
---|
520 | fd = socket( PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE ); |
---|
521 | if( 0 > fd ) |
---|
522 | { |
---|
523 | tr_err( "failed to create routing socket (%s)", strerror( errno ) ); |
---|
524 | return -1; |
---|
525 | } |
---|
526 | |
---|
527 | flags = fcntl( fd, F_GETFL ); |
---|
528 | if( 0 > flags || 0 > fcntl( fd, F_SETFL, O_NONBLOCK | flags ) ) |
---|
529 | { |
---|
530 | tr_err( "failed to set socket nonblocking (%s)", strerror( errno ) ); |
---|
531 | close( fd ); |
---|
532 | return -1; |
---|
533 | } |
---|
534 | |
---|
535 | bzero( &snl, sizeof( snl ) ); |
---|
536 | snl.nl_family = AF_NETLINK; |
---|
537 | |
---|
538 | bzero( &req, sizeof( req ) ); |
---|
539 | req.nlh.nlmsg_len = NLMSG_LENGTH( sizeof( req.rtg ) ); |
---|
540 | req.nlh.nlmsg_type = RTM_GETROUTE; |
---|
541 | req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
---|
542 | req.nlh.nlmsg_seq = SEQNUM; |
---|
543 | req.nlh.nlmsg_pid = 0; |
---|
544 | req.rtg.rtgen_family = AF_INET; |
---|
545 | |
---|
546 | if( 0 > sendto( fd, &req, sizeof( req ), 0, |
---|
547 | (struct sockaddr *) &snl, sizeof( snl ) ) ) |
---|
548 | { |
---|
549 | tr_err( "failed to write to routing socket (%s)", strerror( errno ) ); |
---|
550 | close( fd ); |
---|
551 | return -1; |
---|
552 | } |
---|
553 | |
---|
554 | return fd; |
---|
555 | } |
---|
556 | |
---|
557 | static uint8_t * |
---|
558 | getroute( int fd, unsigned int * buflen ) |
---|
559 | { |
---|
560 | void * buf; |
---|
561 | unsigned int len; |
---|
562 | ssize_t res; |
---|
563 | struct sockaddr_nl snl; |
---|
564 | socklen_t slen; |
---|
565 | |
---|
566 | len = 8192; |
---|
567 | buf = calloc( 1, len ); |
---|
568 | if( NULL == buf ) |
---|
569 | { |
---|
570 | *buflen = 0; |
---|
571 | return NULL; |
---|
572 | } |
---|
573 | |
---|
574 | for( ;; ) |
---|
575 | { |
---|
576 | bzero( &snl, sizeof( snl ) ); |
---|
577 | slen = sizeof( snl ); |
---|
578 | res = recvfrom( fd, buf, len, 0, (struct sockaddr *) &snl, &slen ); |
---|
579 | if( 0 > res ) |
---|
580 | { |
---|
581 | if( EAGAIN != errno ) |
---|
582 | { |
---|
583 | tr_err( "failed to read from routing socket (%s)", |
---|
584 | strerror( errno ) ); |
---|
585 | } |
---|
586 | free( buf ); |
---|
587 | *buflen = 0; |
---|
588 | return NULL; |
---|
589 | } |
---|
590 | if( slen < sizeof( snl ) || AF_NETLINK != snl.nl_family ) |
---|
591 | { |
---|
592 | tr_err( "bad address" ); |
---|
593 | free( buf ); |
---|
594 | *buflen = 0; |
---|
595 | return NULL; |
---|
596 | } |
---|
597 | |
---|
598 | if( 0 == snl.nl_pid ) |
---|
599 | { |
---|
600 | break; |
---|
601 | } |
---|
602 | } |
---|
603 | |
---|
604 | *buflen = res; |
---|
605 | |
---|
606 | return buf; |
---|
607 | } |
---|
608 | |
---|
609 | static int |
---|
610 | parseroutes( uint8_t * buf, unsigned int len, struct in_addr * addr ) |
---|
611 | { |
---|
612 | struct nlmsghdr * nlm; |
---|
613 | struct nlmsgerr * nle; |
---|
614 | struct rtmsg * rtm; |
---|
615 | struct rtattr * rta; |
---|
616 | int rtalen; |
---|
617 | struct in_addr gw, dst; |
---|
618 | |
---|
619 | nlm = ( struct nlmsghdr * ) buf; |
---|
620 | while( NLMSG_OK( nlm, len ) ) |
---|
621 | { |
---|
622 | gw.s_addr = INADDR_ANY; |
---|
623 | dst.s_addr = INADDR_ANY; |
---|
624 | if( NLMSG_ERROR == nlm->nlmsg_type ) |
---|
625 | { |
---|
626 | nle = (struct nlmsgerr *) NLMSG_DATA( nlm ); |
---|
627 | if( NLMSG_LENGTH( NLMSG_ALIGN( sizeof( struct nlmsgerr ) ) ) > |
---|
628 | nlm->nlmsg_len ) |
---|
629 | { |
---|
630 | tr_err( "truncated netlink error" ); |
---|
631 | } |
---|
632 | else |
---|
633 | { |
---|
634 | tr_err( "netlink error (%s)", strerror( nle->error ) ); |
---|
635 | } |
---|
636 | return 1; |
---|
637 | } |
---|
638 | else if( RTM_NEWROUTE == nlm->nlmsg_type && SEQNUM == nlm->nlmsg_seq && |
---|
639 | getpid() == (pid_t) nlm->nlmsg_pid && |
---|
640 | NLMSG_LENGTH( sizeof( struct rtmsg ) ) <= nlm->nlmsg_len ) |
---|
641 | { |
---|
642 | rtm = NLMSG_DATA( nlm ); |
---|
643 | rta = RTM_RTA( rtm ); |
---|
644 | rtalen = RTM_PAYLOAD( nlm ); |
---|
645 | |
---|
646 | while( RTA_OK( rta, rtalen ) ) |
---|
647 | { |
---|
648 | if( sizeof( struct in_addr ) <= RTA_PAYLOAD( rta ) ) |
---|
649 | { |
---|
650 | switch( rta->rta_type ) |
---|
651 | { |
---|
652 | case RTA_GATEWAY: |
---|
653 | memcpy( &gw, RTA_DATA( rta ), sizeof( gw ) ); |
---|
654 | break; |
---|
655 | case RTA_DST: |
---|
656 | memcpy( &dst, RTA_DATA( rta ), sizeof( dst ) ); |
---|
657 | break; |
---|
658 | } |
---|
659 | } |
---|
660 | rta = RTA_NEXT( rta, rtalen ); |
---|
661 | } |
---|
662 | } |
---|
663 | |
---|
664 | if( INADDR_NONE != gw.s_addr && INADDR_ANY != gw.s_addr && |
---|
665 | INADDR_ANY == dst.s_addr ) |
---|
666 | { |
---|
667 | *addr = gw; |
---|
668 | return 0; |
---|
669 | } |
---|
670 | |
---|
671 | nlm = NLMSG_NEXT( nlm, len ); |
---|
672 | } |
---|
673 | |
---|
674 | return 1; |
---|
675 | } |
---|
676 | |
---|
677 | #else /* not BSD or Linux */ |
---|
678 | |
---|
679 | int |
---|
680 | tr_getDefaultRoute( struct in_addr * addr UNUSED ) |
---|
681 | { |
---|
682 | tr_inf( "don't know how to get default route on this platform" ); |
---|
683 | return 1; |
---|
684 | } |
---|
685 | |
---|
686 | #endif |
---|