Commit 6ca8446a authored by Andreas Langer's avatar Andreas Langer

batman toolchain

parents
CC=gcc
CFLAGS= -Wall -O0 -g3
battool: battool.o ping.o
battool.o: battool.c battool.h
ping.o: ping.c battool.h
clean:
rm -f battool *.o *~
/* Copyright (C) 2007 B.A.T.M.A.N. contributors:
* Andreas Langer <a.langer@q-dsl.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "battool.h"
void usage() {
printf("Usage: battool modus destination\n");
printf("modus: ping\n");
printf("destination: 00:0a:00:93:d0:cf can write :a::93:d0:cf\n\n");
exit(EXIT_FAILURE);
}
int main( int argc, char **argv ) {
uint8_t mac[6];
char tmp[2];
int i,j=0;
if( argc < 3 ) {
usage();
}
if( argc == 3 ) {
if( strcmp(argv[2], "help") != 0 ) {
/* convert mac address in int array */
if( strlen( argv[2] ) > 17 ) {
printf("The mac address was not correct.\n");
exit(EXIT_FAILURE);
}
for( i = 0; i < strlen( argv[2] ) ; ) {
if( argv[2][i] != ':' ) {
tmp[0] = argv[2][i];
} else {
mac[j] = 0;
i++;
j++;
continue;
}
if( argv[2][i+1] != ':' ) {
tmp[1] = argv[2][i+1];
i+=3;
} else {
tmp[1] = tmp[0];
tmp[0] = '0';
i+=2;
}
mac[j] = strtol(tmp,NULL,16);
j++;
}
if( j < 5 || j > 6 ) {
printf("The mac address was not correct.\n");
exit(EXIT_FAILURE);
}
} else {
/* print help for modus in argv[1] */
}
if( strcmp(argv[1], "ping") == 0 ) {
/* call ping main function */
ping_main( mac );
} else {
usage();
}
} else {
printf("more options currently not supported\n");
usage();
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
/* Copyright (C) 2007 B.A.T.M.A.N. contributors:
* Andreas Langer <a.langer@q-dsl.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
#include <sys/un.h>
#include "packet.h"
#define UNIX_PATH "/var/run/batmand-adv.socket"
struct unix_if {
int32_t unix_sock;
struct sockaddr_un addr;
};
void ping_main( uint8_t *mac );
/*
* Copyright (C) 2006 B.A.T.M.A.N. contributors:
* Marek Lindner
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
#define ECHO_REPLY 0
#define DESTINATION_UNREACHABLE 3
#define ECHO_REQUEST 8
#define TTL_EXCEEDED 11
struct batman_packet
{
uint8_t packet_type;
uint8_t flags; /* 0x80: UNIDIRECTIONAL link, 0x40: DIRECTLINK flag, ... */
uint8_t ttl;
uint8_t orig[6];
uint16_t seqno;
uint8_t gwflags; /* flags related to gateway functions: gateway class */
uint8_t version; /* batman version field */
} __attribute__((packed));
struct icmp_packet
{
uint8_t packet_type;
uint8_t msg_type; /* 0 = ECHO REPLY, 3 = DESTINATION_UNREACHABLE, 8 = ECHO_REQUEST, 11 = TTL exceeded */
uint8_t dst[6];
uint8_t orig[6];
uint8_t ttl;
uint8_t uid;
uint16_t seqno;
} __attribute__((packed));
struct unicast_packet
{
uint8_t packet_type;
uint8_t ttl;
} __attribute__((packed));
struct bcast_packet
{
uint8_t packet_type;
uint8_t padding;
uint8_t orig[6];
uint16_t seqno;
} __attribute__((packed));
/* Copyright (C) 2007 B.A.T.M.A.N. contributors:
* Andreas Langer <a.langer@q-dsl.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "battool.h"
void ping_usage() {
return;
}
void ping_main( uint8_t *mac ) {
char *send_buff, *rec_buff;
char begin[] = "p:";
int sbsize,rbsize;
int32_t recv_buff_len;
struct icmp_packet icmp_packet;
struct unix_if unix_if;
sbsize = sizeof( struct icmp_packet ) + 2;
rbsize = sizeof( struct icmp_packet );
unix_if.unix_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
memset( &unix_if.addr, 0, sizeof(struct sockaddr_un) );
unix_if.addr.sun_family = AF_LOCAL;
strcpy( unix_if.addr.sun_path, UNIX_PATH );
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) );
close( unix_if.unix_sock );
exit(EXIT_FAILURE);
}
send_buff = malloc( sbsize );
memset(send_buff, '\0', sbsize );
rec_buff = malloc( rbsize );
memset(rec_buff, '\0', rbsize );
memcpy( &icmp_packet.dst,mac,6 );
icmp_packet.packet_type = 1;
icmp_packet.msg_type = ECHO_REQUEST;
icmp_packet.ttl = 20;
icmp_packet.seqno = 11;
memcpy( send_buff, begin, 2 );
memcpy( send_buff+2, &icmp_packet, rbsize );
if ( write( unix_if.unix_sock, send_buff, sbsize ) < 0 ) {
printf( "Error - can't write to unix socket: %s\n", strerror(errno) );
close( unix_if.unix_sock );
free( send_buff);
exit(EXIT_FAILURE);
}
while ( ( recv_buff_len = read( unix_if.unix_sock, rec_buff, rbsize ) ) > 0 ) {
printf("receive %d bytes\n", recv_buff_len );
if( recv_buff_len != rbsize )
break;
if( ( (struct icmp_packet *)rec_buff)->msg_type == DESTINATION_UNREACHABLE ) {
printf("Host unreachable\n");
break;
}
printf("%d %d\n", ((struct icmp_packet *)rec_buff)->msg_type,((struct icmp_packet *)rec_buff)->seqno );
}
return;
}
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