[kismac] [binaervarianz] r234 - trunk/Sources/WaveDrivers

  • From: svn@xxxxxxxxxxxxxxxx
  • To: kismac@xxxxxxxxxxxxx
  • Date: Tue, 24 Apr 2007 06:47:04 +0200

Author: themacuser
Date: 2007-04-24 06:46:59 +0200 (Tue, 24 Apr 2007)
New Revision: 234

Added:
   trunk/Sources/WaveDrivers/WaveDriverKismetDrone.h
   trunk/Sources/WaveDrivers/WaveDriverKismetDrone.m
Log:
... and the two most important files of all

Added: trunk/Sources/WaveDrivers/WaveDriverKismetDrone.h
===================================================================
--- trunk/Sources/WaveDrivers/WaveDriverKismetDrone.h   2007-04-24 04:45:15 UTC 
(rev 233)
+++ trunk/Sources/WaveDrivers/WaveDriverKismetDrone.h   2007-04-24 04:46:59 UTC 
(rev 234)
@@ -0,0 +1,407 @@
+/*
+        
+        File:                  WaveDriverKismetDrone.m
+        Program:               KisMAC
+               Author:                 Geordie Millar
+                                               themacuser@xxxxxxxxx
+                                               Contains a lot of code from 
Kismet - 
+                                               http://kismetwireless.net/
+                                               
+               Description:    Scan with a Kismet drone (as opposed to kismet 
server) in KisMac.
+               
+               Details:                Tested with kismet_drone 2006.04.R1 on 
OpenWRT White Russian RC6 on a Diamond Digital R100
+                                               (broadcom mini-PCI card, wrt54g 
capturesource)
+                                               and kismet_drone 2006.04.R1 on 
Voyage Linux on a PC Engines WRAP.2E
+                                               (CM9 mini-PCI card, madwifing)
+                
+        This file is part of KisMAC.
+
+    KisMAC is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    KisMAC 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 KisMAC; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#import <Cocoa/Cocoa.h>
+#import "WaveDriver.h"
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+
+#define STREAM_DRONE_VERSION 9
+
+#define STREAM_SENTINEL      0xDECAFBAD
+
+#define STREAM_FTYPE_VERSION 1
+#define STREAM_FTYPE_PACKET  2
+
+#define STREAM_COMMAND_FLUSH -1
+
+#define MAX_PACKET_LEN 10240
+
+#define SSID_SIZE 255
+
+struct stream_frame_header {
+    uint32_t frame_sentinel;
+    uint8_t frame_type;
+    uint32_t frame_len;
+} __attribute__((__packed__));
+
+struct stream_version_packet {
+    uint16_t drone_version;
+       uint8_t gps_enabled;
+};
+
+struct stream_packet_header {
+    uint32_t header_len;
+    uint16_t drone_version;
+    uint32_t len;
+    uint32_t caplen;
+    uint64_t tv_sec;
+    uint64_t tv_usec;
+    uint16_t quality;
+    uint16_t signal;
+    uint16_t noise;
+    uint8_t error;
+    uint8_t channel;
+    uint8_t carrier;
+    uint8_t encoding;
+    uint32_t datarate;
+
+    int16_t gps_lat;
+    int64_t gps_lat_mant;
+    int16_t gps_lon;
+    int64_t gps_lon_mant;
+    int16_t gps_alt;
+    int64_t gps_alt_mant;
+    int16_t gps_spd;
+    int64_t gps_spd_mant;
+    int16_t gps_heading;
+    int64_t gps_heading_mant;
+    int8_t gps_fix;
+
+    uint8_t sourcename[32];
+} __attribute__((__packed__));
+
+typedef enum {
+    carrier_unknown,
+    carrier_80211b,
+    carrier_80211bplus,
+    carrier_80211a,
+    carrier_80211g,
+    carrier_80211fhss,
+    carrier_80211dsss
+} carrier_type;
+
+typedef enum {
+    encoding_unknown,
+    encoding_cck,
+    encoding_pbcc,
+    encoding_ofdm
+} encoding_type;
+
+struct packet_parm {
+    int fuzzy_crypt;
+       int fuzzy_decode;
+};
+
+typedef struct kis_packet {
+    unsigned int len;          // The amount of data we've actually got
+    unsigned int caplen;       // The amount of data originally captured
+    struct timeval ts;          // Capture timestamp
+    int quality;                // Signal quality
+    int signal;                 // Signal strength
+    int noise;                  // Noise level
+    int error;                  // Capture source told us this was a bad packet
+    int channel;                // Hardware receive channel, if the drivers 
tell us
+    int modified;               // Has moddata been populated?
+    uint8_t *data;              // Raw packet data
+    uint8_t *moddata;           // Modified packet data
+    char sourcename[32];        // Name of the source that generated the data
+       carrier_type carrier;       // Signal carrier
+       encoding_type encoding;     // Signal encoding
+    int datarate;               // Data rate in units of 100 kbps
+    float gps_lat;              // GPS coordinates
+    float gps_lon;
+    float gps_alt;
+    float gps_spd;
+    float gps_heading;
+    int gps_fix;
+    struct packet_parm parm;           // Parameters from the packet source 
that trickle down
+} kismet_packet;
+
+typedef enum {
+    packet_noise = -2,  // We're too short or otherwise corrupted
+    packet_unknown = -1, // What are we?
+    packet_management = 0, // LLC management
+    packet_phy = 1, // Physical layer packets, most drivers can't provide these
+    packet_data = 2 // Data frames
+} packet_type;
+
+// Subtypes are a little odd because we re-use values depending on the type
+typedef enum {
+    packet_sub_unknown = -1,
+    // Management subtypes
+    packet_sub_association_req = 0,
+    packet_sub_association_resp = 1,
+    packet_sub_reassociation_req = 2,
+    packet_sub_reassociation_resp = 3,
+    packet_sub_probe_req = 4,
+    packet_sub_probe_resp = 5,
+    packet_sub_beacon = 8,
+    packet_sub_atim = 9,
+    packet_sub_disassociation = 10,
+    packet_sub_authentication = 11,
+    packet_sub_deauthentication = 12,
+    // Phy subtypes
+    packet_sub_rts = 11,
+    packet_sub_cts = 12,
+    packet_sub_ack = 13,
+    packet_sub_cf_end = 14,
+    packet_sub_cf_end_ack = 15,
+    // Data subtypes
+    packet_sub_data = 0,
+    packet_sub_data_cf_ack = 1,
+    packet_sub_data_cf_poll = 2,
+    packet_sub_data_cf_ack_poll = 3,
+    packet_sub_data_null = 4,
+    packet_sub_cf_ack = 5,
+    packet_sub_cf_ack_poll = 6,
+    packet_sub_data_qos_data = 8,
+    packet_sub_data_qos_data_cf_ack = 9,
+    packet_sub_data_qos_data_cf_poll = 10,
+    packet_sub_data_qos_data_cf_ack_poll = 11,
+    packet_sub_data_qos_null = 12,
+    packet_sub_data_qos_cf_poll_nod = 14,
+    packet_sub_data_qos_cf_ack_poll = 15
+} packet_sub_type;
+
+// distribution directions
+typedef enum {
+    no_distribution, from_distribution, to_distribution, inter_distribution, 
adhoc_distribution
+} distribution_type;
+
+typedef struct {
+       short unsigned int macaddr[6];
+} mac_addr;
+
+typedef enum {
+    proto_unknown,
+    proto_udp, proto_misc_tcp, proto_arp, proto_dhcp_server,
+    proto_cdp,
+    proto_netbios, proto_netbios_tcp,
+    proto_ipx,
+    proto_ipx_tcp,
+    proto_turbocell,
+    proto_netstumbler,
+    proto_lucenttest,
+    proto_wellenreiter,
+    proto_iapp,
+    proto_leap,
+    proto_ttls,
+    proto_tls,
+    proto_peap,
+    proto_isakmp,
+    proto_pptp,
+} protocol_info_type;
+
+typedef struct {
+    unsigned int : 8 __attribute__ ((packed));
+    unsigned int : 8 __attribute__ ((packed));
+
+    unsigned int : 8 __attribute__ ((packed));
+    unsigned int : 1 __attribute__ ((packed));
+    unsigned int level1 : 1 __attribute__ ((packed));
+    unsigned int igmp_forward : 1 __attribute__ ((packed));
+    unsigned int nlp : 1 __attribute__ ((packed));
+    unsigned int level2_switching : 1 __attribute__ ((packed));
+    unsigned int level2_sourceroute : 1 __attribute__ ((packed));
+    unsigned int level2_transparent : 1 __attribute__ ((packed));
+    unsigned int level3 : 1 __attribute__ ((packed));
+} cdp_capabilities;
+
+#if BYTE_ORDER == BIG_ENDIAN
+typedef struct {
+    unsigned short subtype : 4 __attribute__ ((packed));
+    unsigned short type : 2 __attribute__ ((packed));
+    unsigned short version : 2 __attribute__ ((packed));
+
+    unsigned short order : 1 __attribute__ ((packed));
+    unsigned short wep : 1 __attribute__ ((packed));
+    unsigned short more_data : 1 __attribute__ ((packed));
+    unsigned short power_management : 1 __attribute__ ((packed));
+
+    unsigned short retry : 1 __attribute__ ((packed));
+    unsigned short more_fragments : 1 __attribute__ ((packed));
+    unsigned short from_ds : 1 __attribute__ ((packed));
+    unsigned short to_ds : 1 __attribute__ ((packed));
+} frame_control;
+
+typedef struct {
+    uint8_t timestamp[8];
+
+    // This field must be converted to host-endian before being used
+    unsigned int beacon : 16 __attribute__ ((packed));
+
+    unsigned short agility : 1 __attribute__ ((packed));
+    unsigned short pbcc : 1 __attribute__ ((packed));
+    unsigned short short_preamble : 1 __attribute__ ((packed));
+    unsigned short wep : 1 __attribute__ ((packed));
+
+    unsigned short unused2 : 1 __attribute__ ((packed));
+    unsigned short unused1 : 1 __attribute__ ((packed));
+    unsigned short ibss : 1 __attribute__ ((packed));
+    unsigned short ess : 1 __attribute__ ((packed));
+
+    unsigned int coordinator : 8 __attribute__ ((packed));
+
+} fixed_parameters;
+
+#else
+typedef struct {
+    unsigned short version : 2 __attribute__ ((packed));
+    unsigned short type : 2 __attribute__ ((packed));
+    unsigned short subtype : 4 __attribute__ ((packed));
+
+    unsigned short to_ds : 1 __attribute__ ((packed));
+    unsigned short from_ds : 1 __attribute__ ((packed));
+    unsigned short more_fragments : 1 __attribute__ ((packed));
+    unsigned short retry : 1 __attribute__ ((packed));
+
+    unsigned short power_management : 1 __attribute__ ((packed));
+    unsigned short more_data : 1 __attribute__ ((packed));
+    unsigned short wep : 1 __attribute__ ((packed));
+    unsigned short order : 1 __attribute__ ((packed));
+} frame_control;
+
+typedef struct {
+    uint8_t timestamp[8];
+
+    // This field must be converted to host-endian before being used
+    unsigned int beacon : 16 __attribute__ ((packed));
+
+    unsigned short ess : 1 __attribute__ ((packed));
+    unsigned short ibss : 1 __attribute__ ((packed));
+    unsigned short unused1 : 1 __attribute__ ((packed));
+    unsigned short unused2 : 1 __attribute__ ((packed));
+
+    unsigned short wep : 1 __attribute__ ((packed));
+    unsigned short short_preamble : 1 __attribute__ ((packed));
+    unsigned short pbcc : 1 __attribute__ ((packed));
+    unsigned short agility : 1 __attribute__ ((packed));
+
+    unsigned int coordinator : 8 __attribute__ ((packed));
+} fixed_parameters;
+#endif
+
+typedef struct {
+    char dev_id[128];
+    uint8_t ip[4];
+    char interface[128];
+    cdp_capabilities cap;
+    char software[512];
+    char platform[128];
+} cdp_packet;
+
+typedef enum {
+    proto_netbios_unknown,
+    proto_netbios_host, proto_netbios_master,
+    proto_netbios_domain, proto_netbios_query, proto_netbios_pdcquery
+} protocol_netbios_type;
+
+typedef struct {
+    protocol_info_type type;
+    uint8_t source_ip[4];
+    uint8_t dest_ip[4];
+    uint8_t misc_ip[4];
+    uint8_t mask[4];
+    uint8_t gate_ip[4];
+    uint16_t sport, dport;
+    cdp_packet cdp;
+    char netbios_source[17];
+    protocol_netbios_type nbtype;
+    int prototype_extra;
+} proto_info;
+
+typedef enum {
+    turbocell_unknown,
+    turbocell_ispbase, // 0xA0
+    turbocell_pollbase, // 0x80
+    turbocell_nonpollbase, // 0x00
+    turbocell_base // 0x40
+} turbocell_type;
+
+typedef struct {
+    packet_type type;
+    packet_sub_type subtype;
+    uint16_t qos; 
+    int corrupt;
+    int reason_code;
+    struct timeval ts;
+    int quality;
+    int signal;
+    int noise;
+    char ssid[SSID_SIZE+1];
+    int ssid_len;
+    char sourcename[32];
+    distribution_type distrib;
+       int crypt_set;
+    int fuzzy;
+    int ess;
+    int channel;
+    int encrypted;
+    int decoded;
+    int interesting;
+    carrier_type carrier;
+    encoding_type encoding;
+    int datarate;
+    mac_addr source_mac;
+    mac_addr dest_mac;
+    mac_addr bssid_mac;
+    int beacon;
+    char beacon_info[SSID_SIZE+1];
+    unsigned int header_offset;
+    proto_info proto;
+    double maxrate;
+    uint64_t timestamp;
+    int sequence_number;
+    int frag_number;
+    int duration;
+    int datasize;
+    int turbocell_nid;
+    turbocell_type turbocell_mode;
+    int turbocell_sat;
+    float gps_lat, gps_lon, gps_alt, gps_spd, gps_heading;
+    int gps_fix;
+    uint32_t ivset;
+} packet_info;
+
+@interface WaveDriverKismetDrone : WaveDriver {
+    struct sockaddr_in drone_sock, local_sock;
+       int drone_fd;
+       int valid;
+    int resyncs;       
+    unsigned int resyncing;
+    unsigned int stream_recv_bytes;
+       struct stream_frame_header fhdr;
+       struct stream_version_packet vpkt;
+       struct stream_packet_header phdr;
+    uint8_t databuf[MAX_PACKET_LEN];
+       kismet_packet *packet;
+       uint8_t data[MAX_PACKET_LEN];
+    uint8_t moddata[MAX_PACKET_LEN];
+}
+
+
+@end

Added: trunk/Sources/WaveDrivers/WaveDriverKismetDrone.m
===================================================================
--- trunk/Sources/WaveDrivers/WaveDriverKismetDrone.m   2007-04-24 04:45:15 UTC 
(rev 233)
+++ trunk/Sources/WaveDrivers/WaveDriverKismetDrone.m   2007-04-24 04:46:59 UTC 
(rev 234)
@@ -0,0 +1,513 @@
+/*
+        
+        File:                  WaveDriverKismetDrone.m
+        Program:               KisMAC
+               Author:                 Geordie Millar
+                                               themacuser@xxxxxxxxx
+                                               Contains a lot of code from 
Kismet - 
+                                               http://kismetwireless.net/
+                                               
+               Description:    Scan with a Kismet drone (as opposed to kismet 
server) in KisMac.
+               
+               Details:                Tested with kismet_drone 2006.04.R1 on 
OpenWRT White Russian RC6 on a Diamond Digital R100
+                                               (broadcom mini-PCI card, wrt54g 
capturesource)
+                                               and kismet_drone 2006.04.R1 on 
Voyage Linux on a PC Engines WRAP.2E
+                                               (CM9 mini-PCI card, madwifing)
+                
+               This file is part of KisMAC.
+
+    KisMAC is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    KisMAC 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 KisMAC; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#import "WaveDriverKismetDrone.h"
+#import "ImportController.h"
+#import "WaveHelper.h"
+#import <BIGeneric/BIGeneric.h>
+
+@implementation WaveDriverKismetDrone
+
++ (enum WaveDriverType) type {
+    return passiveDriver;
+}
+
++ (bool) allowsInjection {
+    return NO;
+}
+
++ (bool) wantsIPAndPort {
+    return YES;
+}
+
++ (bool) allowsChannelHopping {
+    return NO;
+}
+
++ (NSString*) description {
+    return NSLocalizedString(@"Kismet Drone (raw packets), passive mode", 
"long driver description");
+}
+
++ (NSString*) deviceName {
+    return NSLocalizedString(@"Kismet Drone", "short driver description");
+}
+
+#pragma mark -
+
+
++ (BOOL)deviceAvailable {
+       return YES;
+}
+
+
++ (int) initBackend {
+       return YES;
+}
+
++ (bool) loadBackend {
+       return YES;
+}
+
++ (bool) unloadBackend {
+       return YES;
+}
+
+#pragma mark -
+
+- (id)init {
+       return self;
+}
+
+#pragma mark -
+
+- (unsigned short) getChannelUnCached {
+       return _currentChannel;
+}
+
+- (bool) setChannel:(unsigned short)newChannel {
+       _currentChannel = newChannel;
+       return YES;
+}
+
+- (bool) startCapture:(unsigned short)newChannel {
+    return YES;
+}
+
+-(bool) stopCapture {
+       close(drone_fd);
+    return YES;
+}
+
+#pragma mark -
+
+-(bool) startedScanning {
+       NSUserDefaults *defs;
+    defs = [NSUserDefaults standardUserDefaults];
+       const char* hostname;
+       unsigned int port;
+
+       int foundhostname=0;
+       int foundport=0;
+       
+       NSArray *activeDrivers;
+       activeDrivers = [defs objectForKey:@"ActiveDrivers"];
+       NSEnumerator *e = [activeDrivers objectEnumerator];
+       NSDictionary *drvr;
+       @try { // todo: not multiple instance safe yet. not a problem currently.
+               while ( (drvr = [e nextObject]) ) {
+                       if ([[drvr objectForKey:@"driverID"] 
isEqualToString:@"WaveDriverKismetDrone"]) {
+                               hostname = [[drvr 
objectForKey:@"kismetserverhost"] cString];
+                               foundhostname = 1;
+                               port = [[drvr objectForKey:@"kismetserverport"] 
intValue];
+                               foundport = 1;
+                       }
+               }
+       }
+       @catch (NSException * ex) {
+               NSLog(@"Exception getting the hostname and port from plist...");
+               NSLog(@"Error getting host and port!"); 
+                       NSRunCriticalAlertPanel(
+            NSLocalizedString(@"No host/port set to connect to!", "Error 
dialog title"),
+            NSLocalizedString(@"Check that one is set in the preferences", 
"LONG desc"),
+            OK, Nil, Nil);
+               return nil;
+       }
+
+       if (foundhostname + foundport < 2) {
+               NSLog(@"Error getting the hostname and port from plist...");
+               NSLog(@"Error getting host and port!"); 
+               NSRunCriticalAlertPanel(
+           NSLocalizedString(@"No host/port set to connect to!", "Error dialog 
title"),
+            NSLocalizedString(@"Check that one is set in the preferences", 
"LONG desc"),
+            OK, Nil, Nil);
+               return nil;
+       }
+
+       UInt32 ip = inet_addr(hostname);
+               
+       drone_sock.sin_addr.s_addr = ip;
+
+
+       memset(&drone_sock, 0, sizeof(drone_sock));
+       drone_sock.sin_addr.s_addr = ip;
+       drone_sock.sin_family = AF_INET;
+       drone_sock.sin_port = htons(port); // option as well
+       
+       if ((drone_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+        NSLog(@"socket() failed %d (%s)\n", errno, strerror(errno));
+                       NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       [NSString stringWithFormat:@"%s",strerror(errno)],
+            OK, Nil, Nil);
+               return nil;
+    }
+
+       local_sock.sin_family = AF_INET;
+    local_sock.sin_addr.s_addr = htonl(INADDR_ANY);
+    local_sock.sin_port = htons(0);
+
+    if (bind(drone_fd, (struct sockaddr *) &local_sock, sizeof(local_sock)) < 
0) {
+         NSLog(@"bind() failed %d (%s)\n", errno, strerror(errno));
+                       NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       [NSString stringWithFormat:@"%s",strerror(errno)],
+            OK, Nil, Nil);
+        return NULL;
+    }
+
+    // Connect
+    if (connect(drone_fd, (struct sockaddr *) &drone_sock, sizeof(drone_sock)) 
< 0) {
+         NSLog(@"connect() failed %d (%s)\n", errno, strerror(errno));
+                       NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       [NSString stringWithFormat:@"%s",strerror(errno)],
+            OK, Nil, Nil);
+               return nil;
+    }
+
+    valid = 1;
+
+    resyncs = 0;
+    resyncing = 0;
+       
+    stream_recv_bytes = 0;
+
+       return YES;
+}
+
+#pragma mark -
+
+- (WLFrame*) nextFrame {
+       WLFrame *thisFrame;
+       static UInt8 frame[2500];
+       thisFrame = (WLFrame*)frame;
+       
+       uint8_t *inbound;
+       int ret = 0;
+       fd_set rset;
+       struct timeval tm;
+       unsigned int offset = 0;
+               
+       top:;
+       int noValidFrame = 1;
+       
+       while (noValidFrame) {
+          if (stream_recv_bytes < sizeof(struct stream_frame_header)) {
+                       inbound = (uint8_t *) &fhdr;
+                       if ((ret = read(drone_fd, &inbound[stream_recv_bytes],
+                                (ssize_t) sizeof(struct stream_frame_header) - 
stream_recv_bytes)) < 0) {
+                               NSLog(@"drone read() error getting frame header 
%d:%s",
+                                                errno, strerror(errno));
+                                                       NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       @"Drone read() error getting frame header",
+            OK, Nil, Nil);
+                       }
+                       stream_recv_bytes += ret;
+
+                       if (stream_recv_bytes < sizeof(struct 
stream_frame_header))
+                               goto top;
+                       
+                       // Validate it
+                       if (ntohl(fhdr.frame_sentinel) != STREAM_SENTINEL) {
+                               int8_t cmd = STREAM_COMMAND_FLUSH;
+                               int ret = 0;
+
+                               stream_recv_bytes = 0;
+                               resyncs++;
+
+                               if (resyncs > 20) {
+                                  NSLog(@"too many resync attempts, something 
is wrong.");
+                                       NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       @"Resync attempted too many times.",
+            OK, Nil, Nil);
+                                       return NULL;
+                               }
+
+                               if (resyncing == 1)
+                                       goto top;
+
+                               resyncing = 1;
+                               
+                               if ((ret = write(drone_fd, &cmd, 1)) < 1) {
+                                       NSLog(@"write() error attempting to 
flush "
+                                                        "packet stream: %d %s",
+                                                        errno, 
strerror(errno));
+                                                        
+                                                       NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       @"Write error flushing packet stream",
+            OK, Nil, Nil);
+                               
+                                       return NULL;
+                               }
+                       }
+               }
+               
+               ////////
+               offset = sizeof(struct stream_frame_header);
+               if (fhdr.frame_type == STREAM_FTYPE_VERSION && 
stream_recv_bytes >= offset && 
+                       stream_recv_bytes < offset + sizeof(struct 
stream_version_packet)) {
+
+                       inbound = (uint8_t *) &vpkt;
+                       if ((ret = read(drone_fd, &inbound[stream_recv_bytes - 
offset],
+                                                       (ssize_t) sizeof(struct 
stream_version_packet) - 
+                                                       (stream_recv_bytes - 
offset))) < 0) {
+
+                               NSLog(@"drone read() error getting version "
+                                                "packet %d:%s", errno, 
strerror(errno));
+                               
+                                                                       
NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       @"Read error getting version",
+            OK, Nil, Nil);              
+                               
+                               return NULL;
+                       }
+                       stream_recv_bytes += ret;
+
+                       // Leave if we aren't done
+                       if ((stream_recv_bytes - offset) < sizeof(struct 
stream_version_packet))
+                               goto top;
+
+                       // Validate
+                       if (ntohs(vpkt.drone_version) != STREAM_DRONE_VERSION) {
+                               NSLog(@"version mismatch:  Drone sending 
version %d, "
+                                                "expected %d.", 
ntohs(vpkt.drone_version), STREAM_DRONE_VERSION);
+                                                       NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       [NSString stringWithFormat:@"version mismatch:  Drone 
sending version %d, expected %d.", ntohs(vpkt.drone_version), 
STREAM_DRONE_VERSION],
+            OK, Nil, Nil);
+                               return NULL;
+                       }
+
+                       stream_recv_bytes = 0;
+
+                        NSLog(@"debug - version packet valid\n\n");
+               } 
+
+       if (fhdr.frame_type == STREAM_FTYPE_PACKET && stream_recv_bytes >= 
offset &&
+                       stream_recv_bytes < offset + sizeof(struct 
stream_packet_header)) {
+                       
+                       // Bail if we have a frame header too small for a 
packet of any sort
+                       if (ntohl(fhdr.frame_len) <= sizeof(struct 
stream_packet_header)) {
+                               NSLog(@"frame too small to hold a packet.");
+                               NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       [NSString stringWithFormat:@"Frame too small to hold a 
packet", ntohs(vpkt.drone_version), STREAM_DRONE_VERSION],
+            OK, Nil, Nil);
+                               return NULL;
+                       }
+
+                       inbound = (uint8_t *) &phdr;
+                       if ((ret = read(drone_fd, &inbound[stream_recv_bytes - 
offset],
+                                                       (ssize_t) sizeof(struct 
stream_packet_header) - 
+                                                       (stream_recv_bytes - 
offset))) < 0) {
+                               NSLog(@"drone read() error getting packet "
+                                                "header %d:%s", errno, 
strerror(errno));
+                               
+                               NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       [NSString stringWithFormat:@"drone read() error getting 
packet header %d:%s", errno, strerror(errno)],
+            OK, Nil, Nil);
+                                                
+                               return NULL;
+                       }
+                       stream_recv_bytes += ret;
+
+                       // Leave if we aren't done
+                       if ((stream_recv_bytes - offset) < sizeof(struct 
stream_packet_header))
+                               goto top;
+
+                       if (ntohs(phdr.drone_version) != STREAM_DRONE_VERSION) {
+                               NSLog(@"version mismatch:  Drone sending 
version %d, "
+                                                "expected %d.", 
ntohs(phdr.drone_version), STREAM_DRONE_VERSION);
+                       NSRunCriticalAlertPanel(@"The connection to the Kismet 
drone failed",
+                       [NSString stringWithFormat:@"version mismatch:  Drone 
sending version %d, expected %d.", ntohs(phdr.drone_version), 
STREAM_DRONE_VERSION], 
+                       OK, Nil, Nil);
+
+                               
+                               return NULL;
+                       }
+
+                       if (ntohl(phdr.caplen) <= 0 || ntohl(phdr.len) <= 0) {
+                               NSLog(@"drone sent us a 0-length packet.");
+                                
NSRunCriticalAlertPanel(NSLocalizedString(@"The connection to the Kismet drone 
failed", "Error dialog title"),
+                       @"Drone sent us a zero-length packet",
+            OK, Nil, Nil);
+                               return NULL;
+                       }
+
+                       if (ntohl(phdr.caplen) > MAX_PACKET_LEN || 
ntohl(phdr.len) > MAX_PACKET_LEN) {
+                               NSLog(@"drone sent us an oversized packet.");
+                               NSRunCriticalAlertPanel(NSLocalizedString(@"The 
connection to the Kismet drone failed", "Error dialog title"),
+                       @"Drone sent us an oversized packet",
+            OK, Nil, Nil);
+                               return NULL;
+                       }
+                       
+                       // See if we keep looking for more packet pieces
+                       FD_ZERO(&rset);
+                       FD_SET(drone_fd, &rset);
+                       tm.tv_sec = 0;
+                       tm.tv_usec = 0;
+
+                       if (select(drone_fd + 1, &rset, NULL, NULL, &tm) <= 0)
+                               goto top;
+
+               }
+
+               offset = sizeof(struct stream_frame_header) + sizeof(struct 
stream_packet_header);
+               if (fhdr.frame_type == STREAM_FTYPE_PACKET && stream_recv_bytes 
>= offset) {
+
+                       unsigned int plen = (uint32_t) ntohl(phdr.len);
+
+                       inbound = (uint8_t *) databuf;
+                       if ((ret = read(drone_fd, &inbound[stream_recv_bytes - 
offset],
+                                                       (ssize_t) plen - 
(stream_recv_bytes - offset))) < 0) {
+                               NSLog(@"drone read() error getting packet "
+                                                "header %d:%s", errno, 
strerror(errno));
+                               
+                                                NSRunCriticalAlertPanel(
+            NSLocalizedString(@"The connection to the Kismet drone failed", 
"Error dialog title"),
+                       [NSString stringWithFormat:@"drone read() error getting 
packet header %d:%s", errno, strerror(errno)],
+            OK, Nil, Nil);              
+                               
+                               return NULL;
+                       }
+                       stream_recv_bytes += ret;
+
+                       if ((stream_recv_bytes - offset) < plen)
+                               goto top;
+                       
+
+               thisFrame->dataLen = (UInt16) ntohl(phdr.len);
+               thisFrame->signal = (UInt8) ntohs(phdr.signal);
+               thisFrame->channel = (UInt16) phdr.channel;
+               thisFrame->rate = (UInt8) ntohl(phdr.datarate);
+       
+               memcpy(&thisFrame->address1,&databuf[4],6);
+               memcpy(&thisFrame->address3,&databuf[16],6);
+               memcpy(&thisFrame->address2,&databuf[10],6); 
+               memcpy(&thisFrame->address4,&databuf[24],6);
+       
+               frame_control *fc = (frame_control *) databuf;
+               thisFrame->frameControl = 0;
+       
+               if (fc->to_ds == 0 && fc->from_ds == 0)
+                       thisFrame->frameControl = thisFrame->frameControl | 
IEEE80211_DIR_NODS;
+               else if (fc->to_ds == 0 && fc->from_ds == 1)
+                       thisFrame->frameControl = thisFrame->frameControl | 
IEEE80211_DIR_FROMDS;
+               else if (fc->to_ds == 1 && fc->from_ds == 0)
+                       thisFrame->frameControl = thisFrame->frameControl | 
IEEE80211_DIR_TODS;
+               else if (fc->to_ds == 1 && fc->from_ds == 1)
+                  thisFrame->frameControl = thisFrame->frameControl | 
IEEE80211_DIR_DSTODS;
+               
+               if (fc->type == 0) {
+                       thisFrame->frameControl = thisFrame->frameControl | 
IEEE80211_TYPE_MGT;
+               } else if (fc->type == 2) {
+                       thisFrame->frameControl = thisFrame->frameControl | 
IEEE80211_TYPE_DATA;
+               } else {
+                       //thisFrame->frameControl = thisFrame->frameControl | 
IEEE80211_TYPE_DATA;
+               }
+               
+               if (fc->subtype < 16) {
+                       thisFrame->frameControl = 
OSSwapBigToHostConstInt16(0x1000) * fc->subtype;
+               }
+
+               #ifdef DEBUG
+               printf("----------------------------\n");
+               printf("fc->type == %i\n",fc->type);
+               printf("fc->subtype = %i\n",fc->subtype);
+               printf("bssid: %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n", databuf[4], 
databuf[5], databuf[6], databuf[7], databuf[8], databuf[9]); // dest
+               printf("source: %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n", databuf[10], 
databuf[11], databuf[12], databuf[13], databuf[14], databuf[15]);
+               printf("dest: %.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n", databuf[16], 
databuf[17], databuf[18], databuf[19], databuf[20], databuf[21]);
+               #endif
+       
+       unsigned int framelen;
+       
+       if (thisFrame->dataLen < 2500) { // no buffer overflows please
+               framelen = thisFrame->dataLen;
+       } else {
+               framelen = 2500;
+               NSLog(@"Captured frame >2500 octets");
+       }
+
+               memcpy((frame + sizeof(WLFrame)), &databuf[24], framelen); // 
todo: limit this to 2500!
+               noValidFrame = 0;
+               stream_recv_bytes = 0;
+               
+               } //else {
+                       // printf("debug - somehow not a stream packet or too 
much data...  type %d recv %d\n", fhdr.frame_type, stream_recv_bytes);
+               // }
+
+               if (fhdr.frame_type != STREAM_FTYPE_PACKET && 
+                       fhdr.frame_type != STREAM_FTYPE_VERSION) {
+                       // Bail if we don't know the packet type
+                       NSLog(@"unknown frame type %d", fhdr.frame_type);
+
+                       // debug
+                       unsigned int x = 0;
+                       while (x < sizeof(struct stream_frame_header)) {
+                               printf("%02X ", ((uint8_t *) &fhdr)[x]);
+                               x++;
+                       }
+                       printf("\n");
+                       
+                       return NULL;
+               }
+       }
+       return thisFrame; // finally!
+}
+
+
+
+#pragma mark -
+
+-(bool) sendFrame:(UInt8*)f withLength:(int) size atInterval:(int)interval {
+    return NO;
+}
+
+-(bool) stopSendingFrames {    
+    return NO;
+}
+
+#pragma mark -
+
+-(void) dealloc {
+               NSLog(@"dealloc called");
+    [super dealloc];
+}
+
+@end


Other related posts:

  • » [kismac] [binaervarianz] r234 - trunk/Sources/WaveDrivers