| 1 |
#include <string.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <windows.h> |
| 5 |
|
| 6 |
#include <airpcap.h> |
| 7 |
|
| 8 |
#include "osdep.h" |
| 9 |
#include "tap-win32/common.h" |
| 10 |
|
| 11 |
static const char * DEFAULT_ADAPT_NAME = "\\\\.\\airpcap00"; |
| 12 |
static const char * DEVICE_HEADER = "\\\\.\\"; |
| 13 |
|
| 14 |
|
| 15 |
//pcap_t *winpcap_adapter; |
| 16 |
static PAirpcapHandle airpcap_handle; |
| 17 |
|
| 18 |
|
| 19 |
// Use PPI later |
| 20 |
#define PPH_PH_VERSION ((u_int8_t)0x00) |
| 21 |
#define PPI_FIELD_TYPE_802_11_COMMON ((u_int16_t)0x02) |
| 22 |
|
| 23 |
typedef struct _PPI_PACKET_HEADER |
| 24 |
{ |
| 25 |
u_int8_t PphVersion; |
| 26 |
u_int8_t PphFlags; |
| 27 |
u_int16_t PphLength; |
| 28 |
u_int32_t PphDlt; |
| 29 |
} |
| 30 |
PPI_PACKET_HEADER, *PPPI_PACKET_HEADER; |
| 31 |
|
| 32 |
typedef struct _PPI_FIELD_HEADER |
| 33 |
{ |
| 34 |
u_int16_t PfhType; |
| 35 |
u_int16_t PfhLength; |
| 36 |
} |
| 37 |
PPI_FIELD_HEADER, *PPPI_FIELD_HEADER; |
| 38 |
|
| 39 |
typedef struct _PPI_FIELD_802_11_COMMON |
| 40 |
{ |
| 41 |
u_int64_t TsfTimer; |
| 42 |
u_int16_t Flags; |
| 43 |
u_int16_t Rate; |
| 44 |
u_int16_t ChannelFrequency; |
| 45 |
u_int16_t ChannelFlags; |
| 46 |
u_int8_t FhssHopset; |
| 47 |
u_int8_t FhssPattern; |
| 48 |
int8_t DbmAntSignal; |
| 49 |
int8_t DbmAntNoise; |
| 50 |
} |
| 51 |
PPI_FIELD_802_11_COMMON, *PPPI_FIELD_802_11_COMMON; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
static int ppi_decode(const u_char *p, int caplen, int *hdrlen, int *power) |
| 56 |
{ |
| 57 |
PPPI_PACKET_HEADER pPpiPacketHeader; |
| 58 |
PPPI_FIELD_HEADER pFieldHeader; |
| 59 |
ULONG position = 0; |
| 60 |
|
| 61 |
// Sanity checks |
| 62 |
if (caplen < sizeof(*pPpiPacketHeader)) |
| 63 |
{ |
| 64 |
// Packet smaller than the PPI fixed header |
| 65 |
return( 1 ); |
| 66 |
} |
| 67 |
|
| 68 |
pPpiPacketHeader = (PPPI_PACKET_HEADER)p; |
| 69 |
|
| 70 |
*hdrlen = pPpiPacketHeader->PphLength; |
| 71 |
|
| 72 |
if(caplen < *hdrlen) |
| 73 |
{ |
| 74 |
// Packet smaller than the PPI fixed header |
| 75 |
return( 1 ); |
| 76 |
} |
| 77 |
|
| 78 |
position = sizeof(*pPpiPacketHeader); |
| 79 |
|
| 80 |
if (pPpiPacketHeader->PphVersion != PPH_PH_VERSION) |
| 81 |
{ |
| 82 |
fprintf( stderr, "Unknown PPI packet header version (%u)\n", pPpiPacketHeader->PphVersion); |
| 83 |
return( 1 ); |
| 84 |
} |
| 85 |
|
| 86 |
do |
| 87 |
{ |
| 88 |
// now we suppose to have an 802.11-Common header |
| 89 |
if (*hdrlen < sizeof(*pFieldHeader) + position) |
| 90 |
{ |
| 91 |
break; |
| 92 |
} |
| 93 |
|
| 94 |
pFieldHeader = (PPPI_FIELD_HEADER)(p + position); |
| 95 |
position += sizeof(*pFieldHeader); |
| 96 |
|
| 97 |
switch(pFieldHeader->PfhType) |
| 98 |
{ |
| 99 |
case PPI_FIELD_TYPE_802_11_COMMON: |
| 100 |
if (pFieldHeader->PfhLength != sizeof(PPI_FIELD_802_11_COMMON) || caplen - position < sizeof(PPI_FIELD_802_11_COMMON)) |
| 101 |
{ |
| 102 |
// the header is bogus, just skip it |
| 103 |
fprintf( stderr, "Bogus 802.11-Common Field. Skipping it.\n"); |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
PPPI_FIELD_802_11_COMMON pField = (PPPI_FIELD_802_11_COMMON)(p + position); |
| 108 |
|
| 109 |
if (pField->DbmAntSignal != -128) |
| 110 |
{ |
| 111 |
*power = (int)pField->DbmAntSignal; |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
*power = 0; |
| 116 |
} |
| 117 |
} |
| 118 |
break; |
| 119 |
|
| 120 |
default: |
| 121 |
// we do not know this field. Just print type and length and skip |
| 122 |
break; |
| 123 |
} |
| 124 |
|
| 125 |
position += pFieldHeader->PfhLength; |
| 126 |
} |
| 127 |
while(TRUE); |
| 128 |
|
| 129 |
return( 0 ); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
int cygwin_set_mac(unsigned char *mac) |
| 134 |
{ |
| 135 |
if (*mac) {} |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
void cygwin_close(void) |
| 140 |
{ |
| 141 |
// By default, when plugged in, the adapter is set in monitor mode; |
| 142 |
// Application may assume it's already in monitor mode and forget to set it |
| 143 |
// So, do not remove monitor mode. |
| 144 |
if (airpcap_handle != NULL) |
| 145 |
{ |
| 146 |
AirpcapClose(airpcap_handle); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
int cygwin_get_mac(unsigned char *mac) |
| 151 |
{ |
| 152 |
// Don't use the function from Airpcap |
| 153 |
if (*mac) {} |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
// Use PPI headers to obtain the different information for ri |
| 159 |
// Use AirpcapConvertFrequencyToChannel() to get channel |
| 160 |
// Add an option to give frequency instead of channel |
| 161 |
int cygwin_sniff(void *buf, int len, struct rx_info *ri) |
| 162 |
{ |
| 163 |
UINT BytesReceived = 0; |
| 164 |
|
| 165 |
// Wait for the next packet |
| 166 |
// Maybe add an event packets to read |
| 167 |
// WaitForSingleObject(ReadEvent, INFINITE); |
| 168 |
|
| 169 |
// Read a packet |
| 170 |
if(AirpcapRead(airpcap_handle, buf, len, &BytesReceived)) |
| 171 |
return (int)BytesReceived; |
| 172 |
|
| 173 |
return -1; |
| 174 |
} |
| 175 |
|
| 176 |
int cygwin_inject(void *buf, int len, struct tx_info *ti) |
| 177 |
{ |
| 178 |
if (AirpcapWrite (airpcap_handle, buf, len) != 1) |
| 179 |
return -1; |
| 180 |
|
| 181 |
return len; |
| 182 |
} |
| 183 |
|
| 184 |
static int printErrorCloseAndReturn(const char * err, int retValue) |
| 185 |
{ |
| 186 |
if (err && airpcap_handle) |
| 187 |
{ |
| 188 |
if (strlen(err)) |
| 189 |
{ |
| 190 |
if (airpcap_handle) |
| 191 |
fprintf( stderr, err, AirpcapGetLastError(airpcap_handle)); |
| 192 |
else |
| 193 |
fprintf( stderr, err); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
cygwin_close(); |
| 198 |
|
| 199 |
return retValue; |
| 200 |
} |
| 201 |
|
| 202 |
int cygwin_init(char *param) |
| 203 |
{ |
| 204 |
char * iface; |
| 205 |
char errbuf[AIRPCAP_ERRBUF_SIZE ]; |
| 206 |
|
| 207 |
iface = (char *)calloc(1, strlen(param) + strlen(DEVICE_HEADER) +1); |
| 208 |
strcpy (iface, DEFAULT_ADAPT_NAME); |
| 209 |
|
| 210 |
if (param) |
| 211 |
{ |
| 212 |
// if it's empty, use the default adapter |
| 213 |
if (*param != 0) |
| 214 |
{ |
| 215 |
// Make sure the adapter name contains the '\\.\' at its beginning |
| 216 |
memset(iface, 0, strlen(param) + strlen(DEVICE_HEADER) +1); |
| 217 |
|
| 218 |
if (strstr(param, DEVICE_HEADER) == NULL) |
| 219 |
{ |
| 220 |
// Not found, add it |
| 221 |
|
| 222 |
strcpy(iface, DEVICE_HEADER); |
| 223 |
strcat(iface, param); |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
// Already contains the adapter header |
| 228 |
strcpy(iface, param); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
airpcap_handle = AirpcapOpen(iface, errbuf); |
| 234 |
|
| 235 |
if(airpcap_handle == NULL) |
| 236 |
{ |
| 237 |
fprintf( stderr, "This adapter doesn't have wireless extensions. Quitting\n"); |
| 238 |
//pcap_close( winpcap_adapter ); |
| 239 |
return( -1 ); |
| 240 |
} |
| 241 |
|
| 242 |
/* Tell the adapter that the packets we'll send and receive don't include the FCS */ |
| 243 |
if(!AirpcapSetFcsPresence(airpcap_handle, FALSE)) |
| 244 |
return printErrorCloseAndReturn("Error setting FCS presence: %s\n", -1); |
| 245 |
|
| 246 |
/* Set the link layer to bare 802.11 */ |
| 247 |
if(!AirpcapSetLinkType(airpcap_handle, AIRPCAP_LT_802_11)) |
| 248 |
return printErrorCloseAndReturn("Error setting the link type: %s\n", -1); |
| 249 |
|
| 250 |
/* Accept correct frames only */ |
| 251 |
if( !AirpcapSetFcsValidation(airpcap_handle, AIRPCAP_VT_ACCEPT_CORRECT_FRAMES) ) |
| 252 |
return printErrorCloseAndReturn("Error setting FCS validation: %s\n", -1); |
| 253 |
|
| 254 |
/* Set a low mintocopy for better responsiveness */ |
| 255 |
if(!AirpcapSetMinToCopy(airpcap_handle, 1)) |
| 256 |
return printErrorCloseAndReturn("Error setting MinToCopy: %s\n", -1); |
| 257 |
|
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
int cygwin_set_chan(int chan) |
| 262 |
{ |
| 263 |
// Make sure a valid channel is given |
| 264 |
if (chan <= 0) |
| 265 |
return -1; |
| 266 |
|
| 267 |
if(!AirpcapSetDeviceChannel(airpcap_handle, chan)) |
| 268 |
{ |
| 269 |
printf("Error setting the channel to %d: %s\n", chan, AirpcapGetLastError(airpcap_handle)); |
| 270 |
return -1; |
| 271 |
} |
| 272 |
|
| 273 |
return 0; |
| 274 |
} |