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