Commit 8d857ccf authored by Andreas Langer's avatar Andreas Langer

implement hash

parent c06bb15a
......@@ -31,8 +31,8 @@ LOG_BRANCH= trunk/battool
SRC_FILES= "\(\.c\)\|\(\.h\)\|\(Makefile\)\|\(INSTALL\)\|\(LIESMICH\)\|\(README\)\|\(THANKS\)\|\(TRASH\)\|\(Doxyfile\)\|\(./posix\)\|\(./linux\)\|\(./bsd\)\|\(./man\)\|\(./doc\)"
SRC_C= battool.c functions.c batping.c batroute.c batdump.c list-batman.c
SRC_H= battool.h functions.h list-batman.h batdump.h
SRC_C= battool.c functions.c batping.c batroute.c batdump.c list-batman.c hash.c
SRC_H= battool.h functions.h list-batman.h batdump.h hash.h
PACKAGE_NAME= battool
......
#include "list-batman.h"
#define ARPOP_REQUEST 1 /* ARP request. */
#define ARPOP_REPLY 2 /* ARP reply. */
#define ARPOP_REPLY 2 /* ARP reply. */
#define ARPOP_RREQUEST 3 /* RARP request. */
#define ARPOP_RREPLY 4 /* RARP reply. */
#define ARPOP_InREQUEST 8 /* InARP request. */
#define ARPOP_InREPLY 9 /* InARP reply. */
#define ARPOP_NAK 10 /* (ATM)ARP NAK. */
#define ARPOP_RREPLY 4 /* RARP reply. */
#define ARPOP_InREQUEST 8 /* InARP request. */
#define ARPOP_InREPLY 9 /* InARP reply. */
#define ARPOP_NAK 10 /* (ATM)ARP NAK. */
/* protocol numbers */
#define ICMP 0x01
......
......@@ -61,7 +61,7 @@ void handler( int32_t sig ) {
}
}
int batping_main( int argc, char **argv, struct hosts *hosts ) {
int batping_main( int argc, char **argv, struct hashtable_t *hash ) {
char *send_buff, *rec_buff;
char begin[] = "p:";
......@@ -87,14 +87,16 @@ int batping_main( int argc, char **argv, struct hosts *hosts ) {
int loop_count = -1;
int loop_interval = 0;
int time_out = 1;
char *mac_string = NULL;
struct hosts *tmp_hosts;
struct hash_it_t *hashit = NULL;
while ( ( optchar = getopt ( argc, argv, "hc:i:t:" ) ) != -1 ) {
switch( optchar ) {
case 'h':
batping_usage();
exit(EXIT_SUCCESS);
return(EXIT_SUCCESS);
break;
case 'c':
loop_count = strtol(optarg, NULL , 10);
......@@ -111,25 +113,36 @@ int batping_main( int argc, char **argv, struct hosts *hosts ) {
break;
default:
batping_usage();
exit(EXIT_FAILURE);
return(EXIT_FAILURE);
}
}
if ( argc <= found_args ) {
batping_usage();
exit(EXIT_FAILURE);
return(EXIT_FAILURE);
}
find_mac_address( hosts , tmp_hosts, argv[found_args], mac_string, name, mac );
if( mac_string == NULL )
mac_string = argv[found_args];
while ( NULL != ( hashit = hash_iterate( hash, hashit ) ) ) {
if( ( mac = ether_aton( mac_string ) ) == NULL ) {
printf("The mac address was not correct.\n");
exit(EXIT_FAILURE);
tmp_hosts = (struct hosts *)hashit->bucket->data;
if(strcmp(tmp_hosts->name, argv[found_args]) == 0)
break;
else
tmp_hosts = NULL;
}
if( tmp_hosts == NULL ) {
if( ( mac = ether_aton( argv[found_args] ) ) == NULL ) {
DBG("the mac address was not correct");
return(EXIT_FAILURE);
}
} else
mac = &tmp_hosts->mac;
mac_string = ether_ntoa(mac);
signal( SIGINT, handler );
signal( SIGTERM, handler );
......@@ -145,9 +158,9 @@ int batping_main( int argc, char **argv, struct hosts *hosts ) {
if ( connect ( unix_if.unix_sock, (struct sockaddr *)&unix_if.addr, sizeof(struct sockaddr_un) ) < 0 ) {
printf( "Error - can't connect to unix socket '%s': %s ! Is batmand running on this host ?\n", UNIX_PATH, strerror(errno) );
DBG( "can't connect to unix socket '%s': %s ! Is batmand running on this host ?", UNIX_PATH, strerror(errno) );
close( unix_if.unix_sock );
exit(EXIT_FAILURE);
return(EXIT_FAILURE);
}
......@@ -184,7 +197,7 @@ int batping_main( int argc, char **argv, struct hosts *hosts ) {
printf( "Error - can't write to unix socket: %s\n", strerror(errno) );
close( unix_if.unix_sock );
free( send_buff);
exit(EXIT_FAILURE);
return(EXIT_FAILURE);
}
gettimeofday(&start,(struct timezone*)0);
......
......@@ -44,18 +44,16 @@ void batroute_usage() {
return;
}
int batroute_main( int argc, char **argv, struct hosts *hosts ) {
char *send_buff, /* buffer to send */
*rec_buff, /* receive buffer */
*mac_string = NULL, /* string of mac address */
*host_name = NULL, /* name of host */
*return_mac,
begin[] = "p:"; /* send buffer need two chars at begin for batman-advance socket*/
int sbsize, /* size of send buffer */
rbsize, /* size of receive buffer */
optchar, /* ascii code of programm option */
int batroute_main( int argc, char **argv, struct hashtable_t *hash ) {
char *send_buff, /* buffer to send */
*rec_buff, /* receive buffer */
*return_mac,
begin[] = "p:"; /* send buffer need two chars at begin for batman-advance socket*/
int sbsize, /* size of send buffer */
rbsize, /* size of receive buffer */
optchar, /* ascii code of programm option */
i,
count=0;
......@@ -70,12 +68,12 @@ int batroute_main( int argc, char **argv, struct hosts *hosts ) {
struct icmp_packet icmp_packet;
struct unix_if unix_if;
struct timeval start,
end;
struct timeval start, end;
struct timeval timeout;
struct hosts *tmp_hosts;
struct ether_addr *mac;
struct hash_it_t *hashit = NULL;
fd_set read_socket;
......@@ -96,16 +94,25 @@ int batroute_main( int argc, char **argv, struct hosts *hosts ) {
exit(EXIT_FAILURE);
}
find_mac_address( hosts , tmp_hosts, argv[found_args], mac_string, name, mac );
while ( NULL != ( hashit = hash_iterate( hash, hashit ) ) ) {
if( mac_string == NULL )
mac_string = argv[found_args];
if( ( mac = ether_aton( mac_string ) ) == NULL ) {
printf("The mac address was not correct.\n");
exit(EXIT_FAILURE);
tmp_hosts = (struct hosts *)hashit->bucket->data;
if(strcmp(tmp_hosts->name, argv[found_args]) == 0)
break;
else
tmp_hosts = NULL;
}
if( tmp_hosts == NULL ) {
if( ( mac = ether_aton( argv[found_args] ) ) == NULL ) {
DBG("the mac address was not correct");
return(EXIT_FAILURE);
}
} else
mac = &tmp_hosts->mac;
unix_if.unix_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
memset( &unix_if.addr, 0, sizeof(struct sockaddr_un) );
......@@ -179,12 +186,13 @@ int batroute_main( int argc, char **argv, struct hosts *hosts ) {
exit( EXIT_FAILURE );
}
find_mac_address( hosts, tmp_hosts, return_mac, host_name, mac, name );
tmp_hosts = NULL;
tmp_hosts = ((struct hosts *)hash_find(hash, return_mac));
if(host_name == NULL )
if(tmp_hosts == NULL )
printf("%u: %s %.3f ms", ntohs( ( ( struct icmp_packet * ) rec_buff )->seqno ), return_mac, time_delta );
else
printf("%u: %s (%s) %.3f ms", ntohs( ( ( struct icmp_packet * ) rec_buff )->seqno ), return_mac,host_name, time_delta );
printf("%u: %s (%s) %.3f ms", ntohs( ( ( struct icmp_packet * ) rec_buff )->seqno ), return_mac,tmp_hosts->name, time_delta );
} else {
printf(" %.3f ms", time_delta );
}
......
......@@ -36,10 +36,38 @@ void usage() {
exit(EXIT_FAILURE);
}
void parse_hosts_file( struct hosts **tmp, char path[] ) {
int compare_mac(void *data1, void *data2)
{
return ( memcmp( data1, data2, sizeof(struct ether_addr) ) );
}
int choose_mac(void *data, int32_t size)
{
unsigned char *key= data;
uint32_t hash = 0, m_size = sizeof(struct ether_addr);
size_t i;
for (i = 0; i < m_size; i++) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return (hash%size);
}
void parse_hosts_file( struct hashtable_t *hash, char path[] ) {
FILE *fd;
char name[50], mac[18];
struct ether_addr *tmp_mac;
struct hosts *tmp_hosts;
struct hashtable_t *swaphash;
name[0] = mac[0] = '\0';
......@@ -48,15 +76,34 @@ void parse_hosts_file( struct hosts **tmp, char path[] ) {
while( fscanf(fd,"%[^ \t]%s\n", name, mac ) != EOF ) {
if( (*tmp) == NULL ) {
(*tmp) = malloc( sizeof( struct hosts) );
if( ( tmp_mac = ether_aton( mac ) ) == NULL ) {
DBG("the mac address was not correct");
exit(EXIT_FAILURE);
}
if( (*tmp) != NULL ) {
strncpy( (*tmp)->name, name, 49 );
strncpy( (*tmp)->mac, mac, 17 );
(*tmp)->next = NULL;
tmp = &(*tmp)->next;
if( ( tmp_hosts = malloc(sizeof(struct hosts) ) ) == NULL ) {
DBG("not enough memory for malloc");
exit(EXIT_FAILURE);
}
memcpy(&tmp_hosts->mac, tmp_mac, sizeof(struct ether_addr));
strncpy( tmp_hosts->name, name, 49 );
hash_add(hash, tmp_hosts);
if (hash->elements * 4 > hash->size) {
swaphash = hash_resize(hash, hash->size * 2);
if (swaphash == NULL) {
printf("Couldn't resize hash table\n");
}
hash = swaphash;
}
}
......@@ -66,7 +113,9 @@ void parse_hosts_file( struct hosts **tmp, char path[] ) {
}
int main( int argc, char **argv ) {
int uid;
int uid, ret=0;
struct hashtable_t *host_hash;
if( argc < 2 ) {
usage();
......@@ -81,20 +130,22 @@ int main( int argc, char **argv ) {
uid = getuid();
if(uid != 0) { printf("You must have UID 0 instead of %d.\n",uid); exit(EXIT_FAILURE); }
struct hosts *hosts = NULL;
parse_hosts_file( &hosts,HOSTS_FILE );
host_hash = hash_new( 64, compare_mac, choose_mac );
parse_hosts_file( host_hash,HOSTS_FILE );
if (strcmp(argv[1], "ping") == 0 ||strcmp(argv[1], "batping") == 0 || strcmp(argv[1], "bp") == 0 ) {
/* call ping main function */
return ( batping_main( argc-1, argv+1, hosts ) );
ret = batping_main( argc-1, argv+1, host_hash );
} else if(strcmp(argv[1], "traceroute") == 0 || strcmp(argv[1], "batroute") == 0 || strcmp(argv[1], "br") == 0 ) {
/* call trace main function */
return ( batroute_main( argc-1, argv+1, hosts ) );
ret = batroute_main( argc-1, argv+1, host_hash );
} else if( strcmp(argv[1], "batdump") == 0 || strcmp(argv[1], "bd") == 0 ) {
/* call trace main function */
return ( batdump_main( argc-1, argv+1 ) );
ret = batdump_main( argc-1, argv+1 );
} else {
......@@ -102,6 +153,6 @@ int main( int argc, char **argv ) {
}
exit(EXIT_SUCCESS);
hash_destroy(host_hash);
return(ret);
}
......@@ -17,7 +17,10 @@
*
*/
#include <netinet/ether.h>
#include <sys/un.h>
#include "hash.h"
#include "packet.h"
#define VERSION "0.1-alpha" //put exactly one distinct word inside the string like "0.3-pre-alpha" or "0.3-rc1" or "0.3"
......@@ -26,14 +29,7 @@
#define UNIX_PATH "/var/run/batmand-adv.socket"
#define HOSTS_FILE "bat-hosts"
#define find_mac_address(list, tmp , search, target, search_type, target_type ) \
for( tmp = list; tmp != NULL; tmp = tmp->next ) { \
if( strcmp( tmp->search_type, search ) == 0 ) { \
target = tmp->target_type; \
break; \
} \
}
#define DBG(msg,args...) do { printf("batgat: [%s:%u] " msg "\n", __func__ ,__LINE__, ##args); } while(0)
struct unix_if {
int32_t unix_sock;
......@@ -41,11 +37,10 @@ struct unix_if {
};
struct hosts {
struct ether_addr mac;
char name[50];
char mac[18];
struct hosts *next;
};
int batping_main( int argc, char **argv, struct hosts *hosts );
int batroute_main( int argc, char **argv, struct hosts *hosts );
int batping_main( int argc, char **argv, struct hashtable_t *hash );
int batroute_main( int argc, char **argv, struct hashtable_t *hash );
int batdump_main( int argc, char **argv );
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment