Sophie

Sophie

distrib > Mandriva > 2011.0 > i586 > media > contrib-release-debug > by-pkgid > fc6244ec1546b2aefed163228a9637d4 > files > 17

airfart-debug-0.2.1-10mdv2011.0.i586.rpm


/*
   This file is part of AirFart.

   AirFart 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.

   AirFart 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 AirFart; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef FRAMESNIFFER_H
#define FRAMESNIFFER_H

/*
 *  This is a test of CVS
 */

/* system includes */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/socket.h>
#include <linux/if_ether.h> // gives us ETH_P_ALL
#include <netinet/in.h>

/* My header files */
#include "stats.h"
#include "address.h"

/* Shall we dump some helpful debugging output? */
//#define DEBUG_SNIFFER 1

/* the biggest frame we can read */
#define MAX_BUFFER_SIZE 4000

/* Offsets in the AVS header (that wlan-ng puts on each frame) */
#define DOT11_IDENT_OFFSET 0
#define AVS_HEADER_LEN_OFFSET 4
#define SSI_TYPE_OFFSET 44
#define SSI_SIGNAL_OFFSET 48
#define SSI_NOISE_OFFSET 52
#define ETH_HEADER_OFFSET 64
#define DST_ETH_ADDR_OFFSET 68
#define SRC_ETH_ADDR_OFFSET 74
#define BCAST_ADDR { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };

/* Define PRISM3 is you are running a Prism3 chipset
   like the Lynksys WPC11 version 3.0 */
#define PRISM3 1

/* Some byte lenghts */
#define ETH_ADDR_LEN 6
#define AVS_HEADER_LEN 64

/* Channel to sniff */
#define DEFAULT_CHANNEL 6

/* 802.11 value (first word of every wlan packet) */
#define DOT11_IDENT { 0x80, 0x21, 0x10, 0x01 }
#define DOT11_IDENT_LEN 4

/* Some constants for signal strengths from linux-wlan-ng's capturefrm.txt */
#define MAX_RAW_RSSI_VALUE 255
#define MAX_NORMALIZED_RSSI_VALUE 1000
#define MAX_DBM_VALUE 108
#define MIN_DBM_VALUE 10

/* Types of signal strength reporting as defined in the wlan header */
#define SIGNAL_TYPE_NONE 0
#define SIGNAL_TYPE_NORMALIZED_RSSI 1
#define SIGNAL_TYPE_DBM 2
#define SIGNAL_TYPE_RAW_RSSI 3

/* SS ID stff */
#define SSID_LEN_OFFSET 101
#define SSID_OFFSET 102
#define SSID_VALID_OFFSET 65
#define SSID_VALID 0x00
#define SSID_MAX_LEN 255

/**
 * Global function for invoking the frame sniffer in a separate thread, icky
 */
void *start_sniffing_thread( void *arg );

/**
 * Class responsible for sniffing ethernet frames, extracting their signal
 * strength and ethernet address, and passing them to the statistics unit
 * for analysis
 */
class frame_sniffer
{
	public:
		/* Sets our pointer to an initialized stats collector */
		void set_stats_collector( Stats *s );
		/* Gentlemen, start your engines -- this is an infinite loop */
		bool start_sniffing_loop();
		/* Fires up a thread that simply calls start_sniffing_loop() */
		bool start_sniffing();

	private:
		/* The SS ID of the frame we just sniffed */
		char foreign_ss_id[255];
		/* The socket we open to the wireless ethernet device, wlan0 */
		int skt;
		/* Our pointer to the stats collector, set with method above*/
		Stats *stats;
		/* We dump every sniffed frame into this buffer */
		unsigned char msgbuf[MAX_BUFFER_SIZE];
		/* for binding the socket to the ethernet device */
		struct sockaddr_ll
		{
			unsigned short sll_family;
			unsigned short sll_protocol;
			int sll_ifindex;
			unsigned short sll_hatype;
			unsigned char sll_pkttyhpe;
			unsigned char sll_halen;
			unsigned char sll_addr[8];
		};

		/* Represents a wlan header (64 bytes) */
		struct wlan_header
		{
			unsigned int version;
			unsigned int length;
			unsigned char mactime[8];
			unsigned char hosttime[8];
			unsigned int phytype;
			unsigned int channel;
			unsigned int datarate;
			unsigned int antenna;
			unsigned int priority;
			unsigned int signal_strength_type;
			int signal_strength;
			int signal_noise;
			unsigned int preamble;
			unsigned int encoding;
		};
		
		/* An instance (pointer) of wlan_header */
		struct wlan_header *wlan_hdr;

		/* Extracts and returns the SS ID from an 802.11 frame */
		char *get_ss_id( unsigned char *buf );
		/* Prettily prints an ethernet address (debug purposes) */
		void print_eth_addr( unsigned char *eth_addr );
		/* Builds a socket and sets skt to it*/
		int set_socket();
		/* Binds skt to the ethernet device */
		int bind_socket();
		/* Puts the wireless card in monitor mode (uses wlanctl-ng) */
		int go_monitor_mode( int channel=DEFAULT_CHANNEL );
		/* Converts a signal strength into a percentage */
		int signal_strength_to_percent( int signal_type, 
						int signal_strength );
		/* Checks an ethernet frame for errors */
		int error_check_frame( unsigned char *frame, int len );
};


#endif