Skip to content

Commit 334e95b

Browse files
committed
Write out batmand.peers
1 parent 5c44f27 commit 334e95b

File tree

3 files changed

+123
-6
lines changed

3 files changed

+123
-6
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ endif
2626
endif
2727

2828
# activate this variable to deactivate policy routing for backward compatibility
29-
#NO_POLICY_ROUTING = -DNO_POLICY_ROUTING
29+
NO_POLICY_ROUTING = -DNO_POLICY_ROUTING
3030

3131
CC = gcc
3232
CFLAGS += -pedantic -Wall -W -Os -g3 -std=gnu99
3333
EXTRA_CFLAGS = -DDEBUG_MALLOC -DMEMORY_USAGE -DPROFILE_DATA $(NO_POLICY_ROUTING) -DREVISION_VERSION=$(REVISION_VERSION)
34-
LDFLAGS += -lpthread
34+
LDFLAGS += -lpthread -static
3535

3636
SBINDIR = $(INSTALL_PREFIX)/usr/sbin
3737

batman.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@
4444
#define ADDR_STR_LEN 16
4545
#define TQ_MAX_VALUE 255
4646

47-
#define UNIX_PATH "/var/run/batmand.socket"
48-
49-
47+
#define UNIX_PATH "/data/data/org.servalproject/var/batmand.socket"
48+
#define PEER_PATH "/data/data/org.servalproject/var/batmand.peers"
5049

5150
/***
5251
*

originator.c

+119-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@
1919
*
2020
*/
2121

22-
22+
struct reachable_peer {
23+
unsigned char addr_len;
24+
unsigned char addr[32];
25+
unsigned char tq_avg;
26+
};
2327

2428
#include <string.h>
2529
#include <stdlib.h>
2630
#include <stdio.h>
31+
#include <unistd.h>
2732
#include "os.h"
2833
#include "batman.h"
2934
#include "originator.h"
@@ -461,6 +466,119 @@ void debug_orig(void) {
461466

462467
}
463468

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+
464582
if ( ( debug_clients.clients_num[0] > 0 ) || ( debug_clients.clients_num[3] > 0 ) ) {
465583

466584
addr_to_string( ((struct batman_if *)if_list.next)->addr.sin_addr.s_addr, orig_str, sizeof(orig_str) );

0 commit comments

Comments
 (0)