summaryrefslogtreecommitdiff
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/sniff.cc48
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc7
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh9
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc15
-rw-r--r--noncore/net/wellenreiter/libwellenreiter/source/wl_sock.hh7
5 files changed, 45 insertions, 41 deletions
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc b/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc
index e516177..6e512c4 100644
--- a/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc
+++ b/noncore/net/wellenreiter/libwellenreiter/source/sniff.cc
@@ -1,338 +1,348 @@
/*
* rfmon mode sniffer
*
* $Id$
*/
#include "sniff.hh"
#include "ieee802_11.hh"
#include "extract.hh"
#include "wl_log.hh"
+#include "wl_types.hh"
#include "wl_proto.hh"
/* Main function, checks packets */
-void process_packets(const struct pcap_pkthdr *pkthdr, const unsigned char *packet, char *guihost, int guiport)
+void process_packets(const struct pcap_pkthdr *pkthdr,
+ const unsigned char *packet,
+ char *guihost,
+ int guiport)
{
unsigned int caplen = pkthdr->caplen;
unsigned int length = pkthdr->len;
u_int16_t fc;
unsigned int HEADER_LENGTH;
/* pinfo holds all interresting information for us */
struct packetinfo pinfo;
struct packetinfo *pinfoptr;
/* wl_network_t will finally be set and send to the ui */
wl_network_t wl_net;
pinfoptr=&pinfo;
pinfoptr->isvalid = 0;
pinfoptr->pktlen = pkthdr->len;
if (caplen < IEEE802_11_FC_LEN)
{
/* This is a garbage packet, because is does not long enough
to hold a 802.11b header */
pinfoptr->isvalid = 0;
return;
}
/* Gets the framecontrol bits (2bytes long) */
fc = EXTRACT_LE_16BITS(packet);
HEADER_LENGTH = GetHeaderLength(fc);
if (caplen < HEADER_LENGTH)
{
/* This is a garbage packet, because it is not long enough
to hold a correct header of its type */
pinfoptr->isvalid = 0;
return;
}
/* Decode 802.11b header out of the packet */
if (decode_80211b_hdr(packet,pinfoptr) == 0)
{
/* Justification of the ofset to further process the packet */
length -= HEADER_LENGTH;
caplen -= HEADER_LENGTH;
packet += HEADER_LENGTH;
}
else /* Something is wrong,could not be a correct packet */
return;
switch (FC_TYPE(fc))
{
/* Is it a managemnet frame? */
case T_MGMT:
switch (FC_SUBTYPE(fc))
{
case ST_BEACON:
if (handle_beacon(fc, packet,pinfoptr) ==0)
{
if (!strcmp(pinfoptr->desthwaddr,"ff:ff:ff:ff:ff:ff") == 0)
{
/* Every beacon must have the broadcast as destination
so it must be a shitti packet */
pinfoptr->isvalid = 0;
return;
}
if (pinfoptr->cap_ESS == pinfoptr->cap_IBSS)
{
/* Only one of both are possible, so must be
a noise packet, if this comes up */
pinfoptr->isvalid = 0;
return;
}
if (pinfoptr->channel < 1 || pinfoptr->channel > 14)
{
/* Only channels between 1 and 14 are possible
others must be noise packets */
pinfoptr->isvalid = 0;
return;
}
/* Here should be the infos to the gui issued */
- if (pinfoptr->cap_ESS == 1 &&pinfoptr->cap_IBSS ==0)
- {
- wl_loginfo("Found an access point");
- wl_net.net_type=1;
- }
+ if (pinfoptr->cap_ESS == 1 && pinfoptr->cap_IBSS ==0)
+ {
+ wl_loginfo("Found an access point");
+ wl_net.net_type=1;
+ }
else if(pinfoptr->cap_ESS == 0 && pinfoptr->cap_IBSS == 2)
- {
- wl_loginfo("Found an ad-hoc network");
- wl_net.net_type=2;
- }
-
- memset(wl_net.bssid, 0, sizeof(wl_net.bssid));
+ {
+ wl_loginfo("Found an ad-hoc network");
+ wl_net.net_type=2;
+ }
if (strcmp (pinfoptr->ssid,NONBROADCASTING) ==0)
wl_loginfo("Net is a non-broadcasting network");
else
wl_loginfo("SSID is: %s", pinfoptr->ssid);
wl_loginfo("SSID is: %s", pinfoptr->ssid);
+ memset(wl_net.bssid, 0, sizeof(wl_net.bssid));
memcpy(wl_net.bssid, pinfoptr->ssid, sizeof(wl_net.bssid)-1);
wl_loginfo("SSID length is: %d", pinfoptr->ssid_len);
wl_net.ssid_len=pinfoptr->ssid_len;
wl_loginfo("Channel is: %d", pinfoptr->channel);
wl_net.channel=pinfoptr->channel;
wl_net.wep=pinfoptr->cap_WEP;
wl_loginfo("Mac is: %s", pinfoptr->sndhwaddr);
memset(wl_net.mac, 0, sizeof(wl_net.mac));
memcpy(wl_net.mac, pinfoptr->sndhwaddr, sizeof(wl_net.mac)-1);
if(!send_network_found((char *)guihost, guiport, &wl_net))
{
wl_logerr("Error sending data to UI: %s", strerror(errno));
break;
}
wl_loginfo("Sent network to GUI '%s:%d'", guihost, guiport);
}
break;
default:
wl_logerr("Unknown IEEE802.11 frame subtype (%d)", FC_SUBTYPE(fc));
break;
} /* End of switch over different mgt frame types */
break;
case T_CTRL:
wl_loginfo("Received control frame, not implemented yet");
break;
case T_DATA:
wl_loginfo("Received date frame, not implemented yet");
break;
default:
wl_logerr("Unknown IEEE802.11 frame type (%d)", FC_TYPE(fc));
break;
}
}
/* This decodes the 802.11b frame header out of the 802.11b packet
all the infos is placed into the packetinfo structure */
int decode_80211b_hdr(const u_char *p,struct packetinfo *ppinfo)
{
const struct mgmt_header_t *mgthdr = (const struct mgmt_header_t *) p;
ppinfo->fcsubtype = FC_SUBTYPE(mgthdr->fc);
/* Get the sender, bssid and dest mac address */
etheraddr_string(mgthdr->bssid,ppinfo->bssid);
etheraddr_string(mgthdr->da,ppinfo->desthwaddr);
etheraddr_string(mgthdr->sa,ppinfo->sndhwaddr);
ppinfo->fc_wep = FC_WEP(mgthdr->fc);
return 0;
}
void etheraddr_string(register const u_char *ep, char *text)
{
static char hex[] = "0123456789abcdef";
register unsigned int i, j;
register char *cp;
char buf[sizeof("00:00:00:00:00:00\0")];
cp = buf;
if ((j = *ep >> 4) != 0)
{
*cp++ = hex[j];
}
else
{
*cp++ = '0';
}
*cp++ = hex[*ep++ & 0xf];
for (i = 5; (int)--i >= 0;)
{
*cp++ = ':';
if ((j = *ep >> 4) != 0)
{
*cp++ = hex[j];
}
else
{
*cp++ = '0';
}
*cp++ = hex[*ep++ & 0xf];
}
*cp = '\0';
strcpy(text,buf);
}
/* beacon handler */
int handle_beacon(u_int16_t fc, const u_char *p,struct packetinfo *ppinfo)
{
struct mgmt_body_t pbody;
int offset = 0;
/* Get the static informations out of the packet */
memset(&pbody, 0, sizeof(pbody));
memcpy(&pbody.timestamp, p, 8);
offset += 8;
pbody.beacon_interval = EXTRACT_LE_16BITS(p+offset);
offset += 2;
pbody.capability_info = EXTRACT_LE_16BITS(p+offset);
offset += 2;
/* Gets the different flags out of the capabilities */
ppinfo->cap_ESS = CAPABILITY_ESS(pbody.capability_info);
ppinfo->cap_IBSS = CAPABILITY_IBSS(pbody.capability_info);
ppinfo->cap_WEP = CAPABILITY_PRIVACY(pbody.capability_info);
/* Gets the tagged elements out of the packets */
while (offset + 1 < ppinfo->pktlen)
{
switch (*(p + offset))
{
case E_SSID:
memcpy(&(pbody.ssid),p+offset,2); offset += 2;
if (pbody.ssid.length > 0)
{
- memcpy(&(pbody.ssid.ssid),p+offset,pbody.ssid.length); offset += pbody.ssid.length;
+ memcpy(&(pbody.ssid.ssid),p+offset,pbody.ssid.length);
+ offset += pbody.ssid.length;
pbody.ssid.ssid[pbody.ssid.length]='\0';
if (strcmp((char *)pbody.ssid.ssid,"")==0)
memcpy(ppinfo->ssid, NONBROADCASTING, sizeof(ppinfo->ssid));
else
memcpy(ppinfo->ssid, pbody.ssid.ssid, sizeof(ppinfo->ssid));
ppinfo->ssid_len = pbody.ssid.length;
}
break;
case E_CHALLENGE:
memcpy(&(pbody.challenge),p+offset,2); offset += 2;
if (pbody.challenge.length > 0)
{
- memcpy(&(pbody.challenge.text),p+offset,pbody.challenge.length); offset += pbody.challenge.length;
+ memcpy(&(pbody.challenge.text),p+offset,pbody.challenge.length);
+ offset += pbody.challenge.length;
pbody.challenge.text[pbody.challenge.length]='\0';
}
break;
case E_RATES:
memcpy(&(pbody.rates),p+offset,2); offset += 2;
if (pbody.rates.length > 0)
{
- memcpy(&(pbody.rates.rate),p+offset,pbody.rates.length); offset += pbody.rates.length;
+ memcpy(&(pbody.rates.rate),p+offset,pbody.rates.length);
+ offset += pbody.rates.length;
}
break;
case E_DS:
- memcpy(&(pbody.ds),p+offset,3); offset +=3;
+ memcpy(&(pbody.ds),p+offset,3);
+ offset +=3;
ppinfo->channel = pbody.ds.channel;
break;
case E_CF:
- memcpy(&(pbody.cf),p+offset,8); offset +=8;
+ memcpy(&(pbody.cf),p+offset,8);
+ offset +=8;
break;
case E_TIM:
- memcpy(&(pbody.tim),p+offset,2); offset +=2;
- memcpy(&(pbody.tim.count),p+offset,3); offset +=3;
+ memcpy(&(pbody.tim),p+offset,2);
+ offset +=2;
+ memcpy(&(pbody.tim.count),p+offset,3);
+ offset +=3;
if ((pbody.tim.length -3) > 0)
{
memcpy((pbody.tim.bitmap),p+(pbody.tim.length -3),(pbody.tim.length -3));
offset += pbody.tim.length -3;
}
break;
default:
offset+= *(p+offset+1) + 2;
break;
} /* end of switch*/
} /* end of for loop */
return 0;
} /* End of handle_beacon */
int GetHeaderLength(u_int16_t fc)
{
int iLength=0;
switch (FC_TYPE(fc))
{
case T_MGMT:
iLength = MGMT_HEADER_LEN;
break;
case T_CTRL:
switch (FC_SUBTYPE(fc))
{
case CTRL_PS_POLL:
iLength = CTRL_PS_POLL_LEN;
break;
case CTRL_RTS:
iLength = CTRL_RTS_LEN;
break;
case CTRL_CTS:
iLength = CTRL_CTS_LEN;
break;
case CTRL_ACK:
iLength = CTRL_ACK_LEN;
break;
case CTRL_CF_END:
iLength = CTRL_END_LEN;
break;
case CTRL_END_ACK:
iLength = CTRL_END_ACK_LEN;
break;
default:
iLength = 0;
break;
}
break;
case T_DATA:
if (FC_TO_DS(fc) && FC_FROM_DS(fc))
iLength = 30;
else
iLength = 24;
break;
default:
wl_logerr("unknown IEEE802.11 frame type (%d)", FC_TYPE(fc));
break;
}
return iLength;
}
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc
index 0630d04..664ba92 100644
--- a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc
+++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.cc
@@ -1,127 +1,128 @@
/*
* Communication protocol
*
* $Id$
*/
+#include "wl_types.hh"
#include "wl_proto.hh"
#include "wl_log.hh"
#include "wl_sock.hh"
/* Adds a field to the buffer */
int add_field(char *buffer, const char *string, int len)
{
char newlen[5];
/* 3 Byte = Length */
snprintf(newlen, sizeof(newlen) - 1, "%.3d", len);
memcpy(buffer, newlen, 3);
/* Length bytes = Value */
memcpy(buffer + 3, string, atoi(newlen));
/* Return length of attached field */
return (atoi(newlen) + 3);
}
int get_field(const char *buffer, char *out, int maxlen)
{
char len[5];
/* Get length of value */
memcpy(len, buffer, 3);
/* Copy buffer to out pointer */
memset(out, 0, maxlen);
- if(atoi(len)-3 > maxlen -1)
+ if(atoi(len) > maxlen -1)
memcpy(out, buffer + 3, maxlen - 1);
else
memcpy(out, buffer + 3, atoi(len));
/* Return length of whole field (including 3 byte length) */
return (atoi(len) + 3);
}
/* Send found network to UI */
int send_network_found (const char *guihost, int guiport, void *structure)
{
wl_network_t *ptr;
char buffer[2048], temp[5];
unsigned int len = 0;
ptr = (wl_network_t *)structure;
/* Type = Found new net (without length field) */
memset(temp, 0, sizeof(temp));
- snprintf(temp, sizeof(temp), "%.2d", NETFOUND);
+ snprintf(temp, sizeof(temp), "%.2d", WL_NETFOUND);
memcpy(buffer, temp, 2);
len += 2;
/* Set Net-type */
memset(temp, 0, sizeof(temp));
snprintf(temp, sizeof(temp), "%d", ptr->net_type);
len += add_field(buffer + len, temp, 1);
/* Set channel */
memset(temp, 0, sizeof(temp));
snprintf(temp, sizeof(temp), "%.2d", ptr->channel);
len += add_field(buffer + len, temp, 2);
/* Set WEP y/n */
memset(temp, 0, sizeof(temp));
snprintf(temp, sizeof(temp), "%d", ptr->wep);
len += add_field(buffer + len, temp, 1);
/* Set Mac */
len += add_field(buffer + len, ptr->mac, 17);
/* Set ssid */
if(len + ptr->ssid_len < sizeof(buffer) - 1)
len += add_field(buffer + len, ptr->bssid, ptr->ssid_len);
else
len += add_field(buffer + len, ptr->bssid, sizeof(buffer) - len - 1);
/* Send prepared buffer to UI */
#ifdef DEBUG
wl_loginfo("Sending network to UI: '%s'", buffer);
#endif
return ((!wl_send(guihost, guiport, buffer)) ? 0 : 1);
}
/* Fill buffer into structur */
int get_network_found (void *structure, const char *buffer)
{
wl_network_t *ptr;
char temp[5];
unsigned int len = 0;
ptr = (wl_network_t *)structure;
-
+
/* packet type already determined, skip check */
len += 2;
/* Get net type (accesspoint || ad-hoc || ...) */
memset(temp, 0, sizeof(temp));
len += get_field(buffer + len, temp, sizeof(temp));
ptr->net_type = atoi(temp);
/* Get channel */
memset(temp, 0, sizeof(temp));
len += get_field(buffer + len, temp, sizeof(temp));
ptr->channel = atoi(temp);
/* Set WEP y/n */
memset(temp, 0, sizeof(temp));
len += get_field(buffer + len, temp, sizeof(temp));
ptr->wep = atoi(temp);
/* Set MAC address */
len += get_field(buffer + len, ptr->mac, sizeof(ptr->mac));
/* Set BSSID */
len += get_field(buffer + len, ptr->bssid, sizeof(ptr->bssid));
return 1;
}
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh
index f645f58..46ef8e5 100644
--- a/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh
+++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_proto.hh
@@ -1,30 +1,21 @@
/* $Id$ */
#ifndef WLPROTO_HH
#define WLPROTO_HH
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Type definitions, to be continued */
#define NETFOUND 01
#define NETLOST 02
#define STARTSNIFF 98
#define STOPSNIFF 99
int add_field(char *, const char *, int);
int get_field(const char *, char *, int);
int send_network_found (const char *, int, void *);
int get_network_found (void *, const char *);
-typedef struct {
- int net_type; /* 1 = Accesspoint ; 2 = Ad-Hoc */
- int ssid_len; /* Length of SSID */
- int channel; /* Channel */
- int wep; /* 1 = WEP enabled ; 0 = disabled */
- char mac[64]; /* MAC address of Accesspoint */
- char bssid[128]; /* BSSID of Net */
-} wl_network_t;
-
#endif /* WLPROTO_HH */
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc
index db6b7b8..6e26cb8 100644
--- a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc
+++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.cc
@@ -1,91 +1,92 @@
/*
* Socket operations for wellenreiter
*
* $Id$
*/
#include "wl_sock.hh"
#include "wl_log.hh"
/* Setup UDP Socket for incoming commands */
-int wl_setupsock(const char *host, int port)
+int wl_setupsock(const char *host, int port, struct sockaddr_in saddr)
{
- struct sockaddr_in saddr;
int sock;
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
wl_logerr("Cannot set up socket: %s", strerror(errno));
return -1;
}
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = PF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr.s_addr = inet_addr(host);
-
+
if(bind(sock,(struct sockaddr *)&saddr, sizeof(saddr)) < 0)
{
wl_logerr("Cannot bind socket: %s", strerror(errno));
close(sock);
return -1;
}
return sock;
}
/* Send a string to commsock */
int wl_send(const char *host, int port, const char *string, ...)
{
int sock;
char buffer[4096];
struct sockaddr_in saddr;
va_list ap;
/* Generate string */
memset(buffer, 0, sizeof(buffer));
va_start(ap, string);
vsnprintf(buffer, sizeof(buffer)-1, string, ap);
va_end(ap);
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr.s_addr = inet_addr(host);
/* Setup socket */
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
wl_logerr("Cannot set up socket: %s", strerror(errno));
return 0;
}
if(sendto(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
{
wl_logerr("Cannot write to socket: %s", strerror(errno));
close(sock);
return 0;
}
if(close(sock) < 0)
wl_logerr("Cannot close socket: %s", strerror(errno));
return 1;
}
/* Check for new messages on commsock */
-int wl_recv(int *sock, char *out, int maxlen)
+int wl_recv(int *sock, struct sockaddr_in cliaddr, char *out, int maxlen)
{
- struct sockaddr_in *cliaddr;
socklen_t len = sizeof(struct sockaddr);
char retval[3];
-
+
memset(out, 0, maxlen);
- if(recvfrom(*sock, out, maxlen - 1, 0, (struct sockaddr *)cliaddr, &len) < 0)
+ if(recvfrom(*sock, out, maxlen - 1, 0, (struct sockaddr *)&cliaddr, &len) < 0)
+ {
+ wl_logerr("Cannot receive from socket: %s", strerror(errno));
return -1;
+ }
/* Get packet type and return it */
memset(retval, 0, sizeof(retval));
memcpy(retval, out, 2);
return atoi(retval);
}
diff --git a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.hh b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.hh
index 6ddbaef..e7d9747 100644
--- a/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.hh
+++ b/noncore/net/wellenreiter/libwellenreiter/source/wl_sock.hh
@@ -1,20 +1,21 @@
-/* $id */
+/* $Id */
#ifndef WLSOCK_HH
#define WLSOCK_HH
+#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
-int wl_setupsock(const char *, int);
+int wl_setupsock(const char *, int, struct sockaddr_in);
int wl_send(const char *, int, const char *, ...);
-int wl_recv(int *, char *, int);
+int wl_recv(int *, struct sockaddr_in, char *, int);
#endif /* WLSOCK_HH */