Commit 0bbf593e authored by Bruno Randolf's avatar Bruno Randolf

code cosmetics and smaller fixes

parent 9e67cdd2
This diff is collapsed.
......@@ -53,7 +53,6 @@ static void update_statistics(struct packet_info* pkt);
struct packet_info current_packet;
/* no, i dont want to implement a linked list now */
struct node_info nodes[MAX_NODES];
struct essid_info essids[MAX_ESSIDS];
struct split_info splits;
......@@ -67,6 +66,7 @@ struct config conf = {
static int mon; /* monitoring socket */
int
main(int argc, char** argv)
{
......@@ -98,41 +98,41 @@ main(int argc, char** argv)
{
handle_user_input();
if (!conf.paused && len != -1) {
if (conf.paused || len == -1) {
/* no packet received or paused: just wait 100ms */
usleep(100000);
continue;
}
#if DO_DEBUG
dump_packet(buffer, len);
dump_packet(buffer, len);
#endif
memset(&current_packet,0,sizeof(current_packet));
if (!parse_packet(buffer, len)) {
DEBUG("parsing failed\n");
continue;
}
if (filter_packet(&current_packet))
continue;
n = node_update(&current_packet);
memset(&current_packet,0,sizeof(current_packet));
if (!parse_packet(buffer, len)) {
DEBUG("parsing failed\n");
continue;
}
update_history(&current_packet);
update_statistics(&current_packet);
if (filter_packet(&current_packet))
continue;
check_ibss_split(&current_packet, n);
n = node_update(&current_packet);
if (conf.rport) {
net_send_packet();
continue;
}
update_history(&current_packet);
update_statistics(&current_packet);
check_ibss_split(&current_packet, n);
if (conf.rport) {
net_send_packet();
continue;
}
#if !DO_DEBUG
update_display(&current_packet, n);
update_display(&current_packet, n);
#endif
}
else
usleep(100000);
}
return 0;
}
static int
init_packet_socket(char* devname)
{
......@@ -140,7 +140,6 @@ init_packet_socket(char* devname)
int fd;
int ifindex;
/* an alternative could be to use the pcap library */
fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (fd < 0)
err(1, "could not create socket");
......@@ -164,22 +163,6 @@ init_packet_socket(char* devname)
}
#if 0
static void
device_address(int fd, const char *if_name)
{
struct ifreq req;
strncpy(req.ifr_name, if_name, IFNAMSIZ);
req.ifr_addr.sa_family = AF_INET;
ioctl(fd, SIOCGIFHWADDR, &req);
// ioctl(fd, SIOCGIFADDR, &req);
DEBUG("hw %s\n", ether_sprintf((const unsigned char *)&req.ifr_hwaddr.sa_data));
}
#endif
static int
device_index(int fd, const char *if_name)
{
......@@ -231,7 +214,7 @@ device_promisc(int fd, const char *if_name, int on)
static int
device_get_arptype(int fd, const char *device)
{
struct ifreq ifr;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
......@@ -286,7 +269,7 @@ get_options(int argc, char** argv)
case 'e':
conf.do_filter = 1;
convert_string_to_mac(optarg, conf.filtermac);
printf("%s\n",ether_sprintf(conf.filtermac));
printf("%s\n", ether_sprintf(conf.filtermac));
break;
case 'h':
default:
......@@ -297,6 +280,7 @@ get_options(int argc, char** argv)
}
}
void
finish_all(int sig)
{
......@@ -311,6 +295,7 @@ finish_all(int sig)
exit(0);
}
static void
copy_nodeinfo(struct node_info* n, struct packet_info* p)
{
......@@ -332,8 +317,9 @@ copy_nodeinfo(struct node_info* n, struct packet_info* p)
n->olsr_count++;
if (p->wlan_bssid[0] != 0xff &&
! (p->wlan_bssid[0] == 0 && p->wlan_bssid[1] == 0 && p->wlan_bssid[2] == 0 &&
p->wlan_bssid[3] == 0 && p->wlan_bssid[4] == 0 && p->wlan_bssid[5] == 0))
p->wlan_bssid[3] == 0 && p->wlan_bssid[4] == 0 && p->wlan_bssid[5] == 0)) {
memcpy(n->wlan_bssid, p->wlan_bssid, 6);
}
if ((p->wlan_type & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
(p->wlan_type & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
n->tsfl = *(unsigned long*)(&p->wlan_tsf[0]);
......@@ -350,20 +336,21 @@ copy_nodeinfo(struct node_info* n, struct packet_info* p)
n->channel = p->wlan_channel;
}
static int
node_update(struct packet_info* pkt)
{
int i;
if (pkt->wlan_src[0] == 0 && pkt->wlan_src[1] == 0 && pkt->wlan_src[2] == 0 &&
pkt->wlan_src[3] == 0 && pkt->wlan_src[4] == 0 && pkt->wlan_src[5] == 0)
pkt->wlan_src[3] == 0 && pkt->wlan_src[4] == 0 && pkt->wlan_src[5] == 0) {
return -1;
}
for (i=0;i<MAX_NODES;i++) {
for (i = 0; i < MAX_NODES; i++) {
if (nodes[i].status == 1) {
/* check existing node */
if (memcmp(pkt->wlan_src, nodes[i].last_pkt.wlan_src, 6) == 0) {
//wprintw(list_win,"found");
copy_nodeinfo(&nodes[i], pkt);
return i;
}
......@@ -396,7 +383,7 @@ check_ibss_split(struct packet_info* pkt, int pkt_node)
DEBUG("bssid %s\n", ether_sprintf(pkt->wlan_bssid));
/* find essid */
for (i=0; i<MAX_ESSIDS; i++) {
for (i = 0; i < MAX_ESSIDS; i++) {
if (essids[i].num_nodes == 0) {
/* unused entry */
break;
......@@ -409,33 +396,33 @@ check_ibss_split(struct packet_info* pkt, int pkt_node)
}
/* find node if already recorded */
for (n=0; n<essids[i].num_nodes && n<MAX_NODES; n++) {
for (n = 0; n < essids[i].num_nodes && n < MAX_NODES; n++) {
if (essids[i].nodes[n] == pkt_node) {
DEBUG("SPLIT node found %d\n", n);
break;
}
}
DEBUG("SPLIT at essid %d count %d node %d\n",i, essids[i].num_nodes, n);
DEBUG("SPLIT at essid %d count %d node %d\n", i, essids[i].num_nodes, n);
/* new essid */
if (essids[i].num_nodes==0) {
DEBUG("SPLIT new essid '%s'\n",pkt->wlan_essid);
if (essids[i].num_nodes == 0) {
DEBUG("SPLIT new essid '%s'\n", pkt->wlan_essid);
strncpy(essids[i].essid, pkt->wlan_essid, MAX_ESSID_LEN);
}
/* new node */
if (essids[i].num_nodes==0 || essids[i].nodes[n] != pkt_node) {
if (essids[i].num_nodes == 0 || essids[i].nodes[n] != pkt_node) {
DEBUG("SPLIT recorded new node nr %d %d %s\n", n, pkt_node,
ether_sprintf(pkt->wlan_src) );
essids[i].nodes[n] = pkt_node;
essids[i].num_nodes = n+1;
essids[i].num_nodes = n + 1;
nodes[pkt_node].essid = i;
}
/* check for split */
essids[i].split = 0;
for (n=0; n<essids[i].num_nodes && n<MAX_NODES; n++) {
for (n = 0; n < essids[i].num_nodes && n < MAX_NODES; n++) {
node = &nodes[essids[i].nodes[n]];
DEBUG("SPLIT %d. node %d src %s", n,
essids[i].nodes[n], ether_sprintf(node->last_pkt.wlan_src));
......@@ -444,17 +431,15 @@ check_ibss_split(struct packet_info* pkt, int pkt_node)
if (node->wlan_mode == WLAN_MODE_AP)
continue;
if (last_bssid && memcmp(last_bssid,node->wlan_bssid,6) != 0) {
if (last_bssid && memcmp(last_bssid, node->wlan_bssid, 6) != 0) {
essids[i].split = 1;
//XXX count number of different bssids
DEBUG("SPLIT *** DETECTED!!! %d different bssids\n", essids[i].split);
DEBUG("SPLIT *** DETECTED!!!\n");
}
last_bssid = node->wlan_bssid;
}
/* if a split occurred on this essid, record it */
//XXX record a list of all split essids
if (essids[i].split>0) {
if (essids[i].split > 0) {
DEBUG("SPLIT *** new record %d\n", i);
splits.count = 1;
splits.essid[0] = i;
......@@ -466,9 +451,10 @@ check_ibss_split(struct packet_info* pkt, int pkt_node)
static int
filter_packet(struct packet_info* pkt) {
filter_packet(struct packet_info* pkt)
{
//TODO add filter for packet types: OLSR, BEACON, CONTROL
return (conf.do_filter && 0 != memcmp(current_packet.wlan_src, conf.filtermac, sizeof(conf.filtermac)));
return (conf.do_filter && memcmp(current_packet.wlan_src, conf.filtermac, sizeof(conf.filtermac)) != 0);
}
......@@ -491,7 +477,7 @@ static void
update_statistics(struct packet_info* p) {
stats.packets++;
stats.bytes += p->len;
if (p->rate >= 0 && p->rate < MAX_RATES) {
if (p->rate > 0 && p->rate < MAX_RATES) {
stats.airtimes += p->len / p->rate;
stats.packets_per_rate[p->rate]++;
stats.bytes_per_rate[p->rate] += p->len;
......@@ -499,7 +485,7 @@ update_statistics(struct packet_info* p) {
if (p->wlan_type >= 0 && p->wlan_type < MAX_FSTYPE) {
stats.packets_per_type[p->wlan_type]++;
stats.bytes_per_type[p->wlan_type] += p->len;
if (p->rate >= 0 && p->rate < MAX_RATES)
if (p->rate > 0 && p->rate < MAX_RATES)
stats.airtime_per_type[p->wlan_type] += p->len / p->rate;
}
}
......@@ -26,13 +26,13 @@
#define DO_DEBUG 0
#endif
#define MAX_NODES 255
#define MAX_ESSIDS 255
#define MAX_BSSIDS 255
#define MAX_HISTORY 255
#define MAX_ESSID_LEN 255
#define MAX_RATES 55 /* 54M + 1 for array index */
#define MAX_FSTYPE 0xff
#define MAX_NODES 255
#define MAX_ESSIDS 255
#define MAX_BSSIDS 255
#define MAX_HISTORY 255
#define MAX_ESSID_LEN 255
#define MAX_RATES 55 /* 54M + 1 for array index */
#define MAX_FSTYPE 0xff
#define PKT_TYPE_IP 0x08
#define PKT_TYPE_OLSR 0x10
......@@ -43,7 +43,7 @@
#define WLAN_MODE_IBSS 0x02
#define WLAN_MODE_STA 0x04
#define NODE_TIMEOUT 60 /* seconds */
#define NODE_TIMEOUT 60 /* seconds */
struct packet_info {
int pkt_types;
......@@ -140,7 +140,7 @@ struct config {
int node_timeout;
unsigned char filtermac[6];
/* this isnt exactly config, but... */
/* this isn't exactly config, but wtf... */
int arphrd; // the device ARP type
int paused;
int do_filter;
......
......@@ -106,13 +106,13 @@ parse_prism_header(unsigned char** buf, int len)
* let's make sure here that SNR is always positive, so we
* don't have do handle special cases later
*/
if (((int)ph->noise.data)<0) {
if (((int)ph->noise.data) < 0) {
/* new madwifi */
current_packet.signal = ph->signal.data;
current_packet.noise = ph->noise.data;
current_packet.snr = ph->rssi.data;
}
else if (((int)ph->rssi.data)<0) {
else if (((int)ph->rssi.data) < 0) {
/* broadcom hack */
current_packet.signal = ph->rssi.data;
current_packet.noise = -95;
......@@ -126,12 +126,12 @@ parse_prism_header(unsigned char** buf, int len)
}
/* just in case...*/
if (current_packet.snr<0)
if (current_packet.snr < 0)
current_packet.snr = -current_packet.snr;
if (current_packet.snr>99)
if (current_packet.snr > 99)
current_packet.snr = 99;
current_packet.rate = ph->rate.data/2;
current_packet.rate = ph->rate.data / 2;
DEBUG("devname: %s\n", ph->devname);
DEBUG("signal: %d -> %d\n", ph->signal.data, current_packet.signal);
......@@ -167,19 +167,20 @@ parse_radiotap_header(unsigned char** buf, int len)
DEBUG("%08x\n", present);
/* check for header extension - ignore for now, just advance current position */
while (present & 0x80000000 && b-*buf < rh->it_len) {
while (present & 0x80000000 && b - *buf < rh->it_len) {
DEBUG("extension\n");
b = b + 4;
present = *(__le32*)b;
}
present = rh->it_present; // in case it move
present = rh->it_present; // in case it moved
/* radiotap bitmap has 32 bit, but we are only interrested until
* bit 12 (IEEE80211_RADIOTAP_DB_ANTSIGNAL) => i<13 */
for (i=0; i<13 && b-*buf < rh->it_len; i++) {
for (i = 0; i < 13 && b - *buf < rh->it_len; i++) {
if ((present >> i) & 1) {
DEBUG("1");
switch (i) {
/* just ignore the following (advance position only): */
/* just ignore the following (advance position only) */
case IEEE80211_RADIOTAP_TSFT:
DEBUG("[+8]");
b = b + 8;
......@@ -209,7 +210,7 @@ parse_radiotap_header(unsigned char** buf, int len)
/* we are only interrested in these: */
case IEEE80211_RADIOTAP_RATE:
DEBUG("[rate %0x]", *b);
current_packet.rate = (*b)/2;
current_packet.rate = (*b) / 2;
b++;
break;
case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
......@@ -234,7 +235,7 @@ parse_radiotap_header(unsigned char** buf, int len)
}
}
if (current_packet.snr>99)
if (current_packet.snr > 99)
current_packet.snr = 99;
DEBUG("\nrate: %d\n", current_packet.rate);
......@@ -311,7 +312,7 @@ parse_80211_header(unsigned char** buf, int len)
case IEEE80211_STYPE_PROBE_RESP:
memcpy(current_packet.wlan_tsf, &whm->u.beacon.timestamp, 8);
ieee802_11_parse_elems(whm->u.beacon.variable,
len - sizeof(struct ieee80211_mgmt) - 4, &current_packet);
len - sizeof(struct ieee80211_mgmt) - 4 /* FCS */, &current_packet);
DEBUG("ESSID %s \n", current_packet.wlan_essid );
DEBUG("CHAN %d \n", current_packet.wlan_channel );
if (whm->u.beacon.capab_info & WLAN_CAPABILITY_IBSS)
......@@ -323,22 +324,25 @@ parse_80211_header(unsigned char** buf, int len)
break;
}
if (sa != NULL)
if (sa != NULL) {
memcpy(current_packet.wlan_src, sa, 6);
if (da != NULL)
memcpy(current_packet.wlan_dst, da, 6);
if (bssid!=NULL)
memcpy(current_packet.wlan_bssid, bssid, 6);
if (sa != NULL)
DEBUG("SA %s\n", ether_sprintf(sa));
if (da != NULL)
}
if (da != NULL) {
memcpy(current_packet.wlan_dst, da, 6);
DEBUG("DA %s\n", ether_sprintf(da));
if (bssid!=NULL)
}
if (bssid!=NULL) {
memcpy(current_packet.wlan_bssid, bssid, 6);
DEBUG("BSSID %s\n", ether_sprintf(bssid));
}
*buf = *buf + hdrlen;
return len - hdrlen;
/* only data frames contain more info, otherwise stop parsing */
if ((current_packet.wlan_type & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
*buf = *buf + hdrlen;
return len - hdrlen;
}
return 0;
}
......@@ -352,11 +356,9 @@ parse_ip_header(unsigned char** buf, int len)
/* check type in LLC header */
*buf = *buf + 6;
if (**buf != 0x08) /* not IP */
return -1;
(*buf)++;
if (**buf != 0x00)
return -1;
(*buf)++;
......@@ -376,8 +378,8 @@ parse_ip_header(unsigned char** buf, int len)
if (ih->protocol != 17) /* UDP */
return 0;
*buf = *buf + ih->ihl*4;
return len - ih->ihl*4;
*buf = *buf + ih->ihl * 4;
return len - ih->ihl * 4;
}
......@@ -423,20 +425,20 @@ parse_olsr_packet(unsigned char** buf, int len)
current_packet.pkt_types |= PKT_TYPE_OLSR_LQ;
if (msgtype == HELLO_MESSAGE) {
number = (ntohs(oh->olsr_msg[0].olsr_msgsize)-12) / sizeof(struct hellomsg);
number = (ntohs(oh->olsr_msg[0].olsr_msgsize) - 12) / sizeof(struct hellomsg);
DEBUG("HELLO %d\n", number);
current_packet.olsr_neigh = number;
}
if (msgtype == LQ_HELLO_MESSAGE) {
number = (ntohs(oh->olsr_msg[0].olsr_msgsize)-16) / 12;
DEBUG("LQ_HELLO %d (%d)\n", number, (ntohs(oh->olsr_msg[0].olsr_msgsize)-16));
number = (ntohs(oh->olsr_msg[0].olsr_msgsize) - 16) / 12;
DEBUG("LQ_HELLO %d (%d)\n", number, (ntohs(oh->olsr_msg[0].olsr_msgsize) - 16));
current_packet.olsr_neigh = number;
}
#if 0
/* XXX: tc messages are relayed. so we would have to find the originating node (IP)
and store the information there. skip for now */
/*
if (msgtype == TC_MESSAGE) {
number = (ntohs(oh->olsr_msg[0].olsr_msgsize)-12) / sizeof(struct tcmsg);
DEBUG("TC %d\n", number);
......@@ -448,21 +450,22 @@ parse_olsr_packet(unsigned char** buf, int len)
DEBUG("LQ_TC %d (%d)\n", number, (ntohs(oh->olsr_msg[0].olsr_msgsize)-16));
current_packet.olsr_tc = number;
}
*/
#endif
if (msgtype == HNA_MESSAGE) {
/* same here, but we assume that nodes which relay a HNA with a default gateway
know how to contact the gw, so have a indirect connection to a GW themselves */
struct hnapair* hna;
number = (ntohs(oh->olsr_msg[0].olsr_msgsize)-12) / sizeof(struct hnapair);
DEBUG("HNA NUM: %d (%d) [%d]\n", number, ntohs(oh->olsr_msg[0].olsr_msgsize),sizeof(struct hnapair) );
for (i=0; i<number; i++) {
number = (ntohs(oh->olsr_msg[0].olsr_msgsize) - 12) / sizeof(struct hnapair);
DEBUG("HNA NUM: %d (%d) [%d]\n", number, ntohs(oh->olsr_msg[0].olsr_msgsize),
sizeof(struct hnapair) );
for (i = 0; i < number; i++) {
hna = &(oh->olsr_msg[0].message.hna.hna_net[i]);
DEBUG("HNA %s", ip_sprintf(hna->addr));
DEBUG("/%s\n", ip_sprintf(hna->netmask));
if (hna->addr==0 && hna->netmask==0)
if (hna->addr == 0 && hna->netmask == 0)
current_packet.pkt_types |= PKT_TYPE_OLSR_GW;
}
}
/* done for good */
return 0;
}
......@@ -33,7 +33,7 @@ struct pkt_names mgmt_names[] = {
{ 'a', "ASOCRQ" }, /* IEEE80211_STYPE_ASSOC_REQ 0x0000 */
{ 'A', "ASOCRP" }, /* IEEE80211_STYPE_ASSOC_RESP 0x0010 */
{ 'a', "REASRQ" }, /* IEEE80211_STYPE_REASSOC_REQ 0x0020 */
{ 'A', "REASRP" }, /* IEEE80211_STYPE_REASSOC_RESP 0x0030 */
{ 'A', "REASRP" }, /* IEEE80211_STYPE_REASSOC_RESP 0x0030 */
{ 'p', "PROBRQ" }, /* IEEE80211_STYPE_PROBE_REQ 0x0040 */
{ 'P', "PROBRP" }, /* IEEE80211_STYPE_PROBE_RESP 0x0050 */
{}, {}, /* unused */
......@@ -79,15 +79,15 @@ struct pkt_names data_names[] = {
inline int
normalize(float oval, float max_val, float max) {
normalize(float oval, int max_val, int max) {
int val;
val=(oval/max_val)*max;
if (val>max) /* cap if still bigger */
val=max;
if (val==0 && oval > 0)
val=1;
if (val<0)
val=0;
val= (oval / max_val) * max;
if (val > max) /* cap if still bigger */
val = max;
if (val == 0 && oval > 0)
val = 1;
if (val < 0)
val = 0;
return val;
}
......@@ -97,9 +97,9 @@ dump_packet(const unsigned char* buf, int len)
{
int i;
for (i = 0; i < len; i++) {
if ((i%2) == 0)
if ((i % 2) == 0)
DEBUG(" ");
if ((i%16) == 0)
if ((i % 16) == 0)
DEBUG("\n");
DEBUG("%02x", buf[i]);
}
......@@ -132,7 +132,7 @@ void
convert_string_to_mac(const char* string, unsigned char* mac)
{
int c;
for(c=0; c < 6 && string; c++)
for(c = 0; c < 6 && string; c++)
{
int x = 0;
if (string)
......
......@@ -39,10 +39,10 @@ void
convert_string_to_mac(const char* string, unsigned char* mac);
inline int
normalize(float val, float max_val, float max);
normalize(float val, int max_val, int max);
#define normalize_db(val, max) \
normalize(val-20, 80, (max))
#define normalize_db(_val, _max) \
normalize((_val) - 20, 80, (_max))
char get_packet_type_char(int type);
......
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