|
19 | 19 | *
|
20 | 20 | */
|
21 | 21 |
|
22 |
| - |
| 22 | +struct reachable_peer { |
| 23 | + unsigned char addr_len; |
| 24 | + unsigned char addr[32]; |
| 25 | + unsigned char tq_avg; |
| 26 | +}; |
23 | 27 |
|
24 | 28 | #include <string.h>
|
25 | 29 | #include <stdlib.h>
|
26 | 30 | #include <stdio.h>
|
| 31 | +#include <unistd.h> |
27 | 32 | #include "os.h"
|
28 | 33 | #include "batman.h"
|
29 | 34 | #include "originator.h"
|
@@ -461,6 +466,119 @@ void debug_orig(void) {
|
461 | 466 |
|
462 | 467 | }
|
463 | 468 |
|
| 469 | + /* PGS 20110314 - Write list of peers out to a file. |
| 470 | + Toggle where we write in the file so that there is always a consistent copy on disk. |
| 471 | + XXX - Does not make sure the early-half version doesn't get too big and overwrite the late-half version. |
| 472 | +
|
| 473 | + */ |
| 474 | + if (1) |
| 475 | + { |
| 476 | + FILE *f=fopen(PEER_PATH,"r+"); |
| 477 | + struct reachable_peer p; |
| 478 | + unsigned int peers=0; |
| 479 | + |
| 480 | + if (!f) f=fopen(PEER_PATH,"w+"); |
| 481 | + |
| 482 | + if (!f) |
| 483 | + { |
| 484 | + perror("Couldn't fopen PEER_PATH"); |
| 485 | + } |
| 486 | + else |
| 487 | + { |
| 488 | + unsigned int offset; |
| 489 | + if (fread(&offset,sizeof(int),1,f)==1) |
| 490 | + { |
| 491 | + offset=ntohl(offset); |
| 492 | + offset^=0x20000; |
| 493 | + if (fseek(f,offset,SEEK_SET)) |
| 494 | + perror("fseek() failed writing to peer list file"); |
| 495 | + } |
| 496 | + else |
| 497 | + /* Initial writing offset */ |
| 498 | + offset=0x100; |
| 499 | + |
| 500 | + unsigned int t=htonl(time(0)); |
| 501 | + if (fwrite(&t,sizeof(int),1,f)!=1) { |
| 502 | + /* Dang it, couldn't write the timestamp */ |
| 503 | + } |
| 504 | + |
| 505 | + while ( NULL != ( hashit = hash_iterate( orig_hash, hashit ) ) ) { |
| 506 | + |
| 507 | + orig_node = hashit->bucket->data; |
| 508 | + |
| 509 | + if ( orig_node->router == NULL ) |
| 510 | + { |
| 511 | + continue; |
| 512 | + } |
| 513 | + |
| 514 | + /* Build record to write to file: |
| 515 | + Address length (0 for last record in file) |
| 516 | + Address |
| 517 | + TQ_AVG (i.e. link score) |
| 518 | + */ |
| 519 | + p.addr_len=4; |
| 520 | + bzero(&p.addr[0],32); |
| 521 | + bcopy((unsigned char *)&orig_node->orig,&p.addr[0],4); |
| 522 | + p.tq_avg=orig_node->router->tq_avg; |
| 523 | + peers++; |
| 524 | + |
| 525 | + /* Write to file */ |
| 526 | + if (fwrite(&p,sizeof(p),1,f)!=1) |
| 527 | + { |
| 528 | + /* Dang it, the record didn't get written */ |
| 529 | + } |
| 530 | + } |
| 531 | + |
| 532 | +#ifdef _POSIX_SYNCHRONIZED_IO |
| 533 | +#if _POSIX_SYNCHRONIZED_IO > 0 |
| 534 | + fdatasync(fileno(f)); |
| 535 | +#else |
| 536 | + fsync(fileno(f)); |
| 537 | +#endif |
| 538 | +#else |
| 539 | + fsync(fileno(f)); |
| 540 | +#endif |
| 541 | + /* Mark end of data with an empty record */ |
| 542 | + p.addr_len=0; |
| 543 | + bzero(&p.addr[0],32); |
| 544 | + p.tq_avg=0; |
| 545 | + /* Write to file */ |
| 546 | + if (fwrite(&p,sizeof(p),1,f)!=1) |
| 547 | + { |
| 548 | + /* Dang it, the record didn't get written */ |
| 549 | + } |
| 550 | + |
| 551 | + /* Update offset toggle */ |
| 552 | + if (fseek(f,0,SEEK_SET)==0) { |
| 553 | + offset=htonl(offset); |
| 554 | + if (fwrite(&offset,sizeof(int),1,f)!=1) |
| 555 | + { |
| 556 | + /* Dang it, couldn't write the offset */ |
| 557 | + } |
| 558 | + } |
| 559 | + else |
| 560 | + { |
| 561 | + /* Dang it, couldn't seek back to start of file */ |
| 562 | + } |
| 563 | + |
| 564 | + /* Update peer count */ |
| 565 | + if (fseek(f,4,SEEK_SET)==0) { |
| 566 | + peers=htonl(peers); |
| 567 | + if (fwrite(&peers,sizeof(int),1,f)!=1) |
| 568 | + { |
| 569 | + /* Dang it, couldn't write the peer count */ |
| 570 | + } |
| 571 | + } |
| 572 | + else |
| 573 | + { |
| 574 | + /* Dang it, couldn't seek back to start of file */ |
| 575 | + } |
| 576 | + |
| 577 | + |
| 578 | + fclose(f); |
| 579 | + } |
| 580 | + } |
| 581 | + |
464 | 582 | if ( ( debug_clients.clients_num[0] > 0 ) || ( debug_clients.clients_num[3] > 0 ) ) {
|
465 | 583 |
|
466 | 584 | addr_to_string( ((struct batman_if *)if_list.next)->addr.sin_addr.s_addr, orig_str, sizeof(orig_str) );
|
|
0 commit comments