[Listener] Receive the new request format

Read the packet number and number of packets after the request type.
A bit of refactoring was done.
This commit is contained in:
Matteo Cypriani 2012-05-09 21:16:09 +02:00
parent 96d1c54e54
commit 07f5b16ca0
1 changed files with 44 additions and 76 deletions

View File

@ -853,10 +853,10 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
{
owl_captured_request request ; // Message to send to the aggregator
uint16_t rtap_bytes ; // Radiotap header size
uint_fast16_t offset ; // Offset to read the packet
owl_bool rtap_fields[15] ; // Present flags
uint8_t raw_packet_fc1 ; // First byte of the received frame's FC
uint8_t raw_packet_fc2 ; // Second byte of the received frame's FC
uint8_t raw_packet_flags ; // IEEE 802.11 header flags
// Size of the IEEE 802.11 header:
uint_fast8_t ieee80211_header_size = IEEE80211_HEADER_SIZE_DATA ;
uint16_t llc_packet_type = 0 ;
@ -878,7 +878,8 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
// Copy 2 bytes from the 3rd packet byte, that is the size of the rtap
// header (changes with the flags):
memcpy(&rtap_bytes, &packet[2], sizeof(rtap_bytes)) ;
offset = 2 ;
memcpy(&rtap_bytes, &packet[offset], sizeof(rtap_bytes)) ;
// Radiotap header is little-endian
rtap_bytes = le16toh(rtap_bytes) ;
// Check rtap_bytes for buggy values
@ -889,7 +890,16 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
// is the first byte of the Frame Control (FC) field, which contains
// the type of the packet (Management, Control or Data) and its subtype
// (QoS, etc.):
raw_packet_fc1 = packet[rtap_bytes] ;
offset = rtap_bytes ;
raw_packet_fc1 = packet[offset] ;
// The second byte of the FC field contains the frame flags. The two
// first bits indicate the frame source and destination types: the
// first bit is "To DS" and the second is "From DS", so if the second
// bit is 0 the frame comes from a STA. That's what we want for an
// explicit packet:
offset = rtap_bytes + 1 ;
raw_packet_fc2 = packet[offset] ;
if (! IS_DATA_FRAME(raw_packet_fc1)) // Data frame?
goto not_explicit_packet ;
@ -897,26 +907,19 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
if (DATA_FRAME_IS_QOS(raw_packet_fc1)) // QoS Data frame?
ieee80211_header_size += 2 ; // 2 bytes of QoS information
// The second byte of the FC field contains the frame flags. The two
// first bits indicate the frame source and destination types: the
// first bit is "To DS" and the second is "From DS", so if the second
// bit is 0 the frame comes from a STA. That's what we want for an
// explicit packet:
raw_packet_fc2 = packet[rtap_bytes+1] ;
if (! IS_FRAME_FROM_STA(raw_packet_fc2))
goto not_explicit_packet ;
// Get the packet type (protocol, 2 bytes) from the LLC header:
memcpy(&llc_packet_type,
&packet[rtap_bytes + ieee80211_header_size + 6], 2) ;
offset = rtap_bytes + ieee80211_header_size + 6 ;
memcpy(&llc_packet_type, &packet[offset], 2) ;
llc_packet_type = ntohs(llc_packet_type) ;
if (llc_packet_type != ETHERTYPE_IP) // IP packet?
goto not_explicit_packet ;
packet_ip_header = (struct iphdr *)
&packet[rtap_bytes + ieee80211_header_size + LLC_HEADER_SIZE] ;
offset = rtap_bytes + ieee80211_header_size + LLC_HEADER_SIZE ;
packet_ip_header = (struct iphdr *) &packet[offset] ;
// Get the source IP:
memcpy(request.mobile_ip_addr_bytes, &packet_ip_header->saddr, 4) ;
@ -929,9 +932,10 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
goto not_explicit_packet ;
// Check destination port:
packet_udp_header = (struct udphdr *)
&packet[rtap_bytes + ieee80211_header_size +
LLC_HEADER_SIZE + sizeof(struct iphdr)] ;
offset =
rtap_bytes + ieee80211_header_size +
LLC_HEADER_SIZE + sizeof(struct iphdr) ;
packet_udp_header = (struct udphdr *) &packet[offset] ;
dest_port = ntohs(packet_udp_header->dest) ;
#ifdef USE_PTHREAD
@ -953,15 +957,12 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
process_packet :
// Get 802.11 flags from the 802.11 header:
raw_packet_flags = packet[rtap_bytes+1] ;
if (IS_RETRY(raw_packet_flags) && VERBOSE_CHATTERBOX)
if (IS_RETRY(raw_packet_fc2) && VERBOSE_CHATTERBOX)
printf("This packet is a Retry.\n") ;
// Source MAC address is 10 bytes after the 802.11 packet type:
memcpy(request.mobile_mac_addr_bytes, &packet[rtap_bytes+10],
ETHER_ADDR_LEN) ;
offset = rtap_bytes + 10 ;
memcpy(request.mobile_mac_addr_bytes, &packet[offset], ETHER_ADDR_LEN) ;
// Drop the packet if it comes from the AP itself:
if (owl_mac_equals(my_mac_bytes, request.mobile_mac_addr_bytes))
@ -977,40 +978,31 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
if (is_explicit_packet
&& (GET_MODE() == MODE_ACTIVE || GET_MODE() == MODE_MIXED)
// FIXME: should we really ignore Retries?
&& ! IS_RETRY(raw_packet_flags))
&& ! IS_RETRY(raw_packet_fc2))
{
request.type =
packet[rtap_bytes + ieee80211_header_size + LLC_HEADER_SIZE
+ sizeof(struct iphdr) + sizeof(struct udphdr)] ;
offset =
rtap_bytes + ieee80211_header_size + LLC_HEADER_SIZE +
sizeof(struct iphdr) + sizeof(struct udphdr) ;
request.type = packet[offset] ;
extract_packet_numbers(&packet[++offset], &request) ;
offset += 2 * sizeof(uint16_t) ;
// Copy the timestamp "as is" (i.e. without changing endianess)
// because it will return to the network soon:
memcpy(&request.request_time, &packet[offset],
sizeof(owl_timestamp)) ;
offset += sizeof(owl_timestamp) ;
switch(request.type)
{
case OWL_REQUEST_NORMAL :
if (VERBOSE_INFO)
printf("\nExplicit packet received.\n") ;
extract_packet_numbers(&packet[rtap_bytes +
ieee80211_header_size +
LLC_HEADER_SIZE +
sizeof(struct iphdr) +
sizeof(struct udphdr) + 9],
&request) ;
break ;
case OWL_REQUEST_CALIBRATION :
if (VERBOSE_INFO)
printf("\nExplicit calibration packet received.\n") ;
extract_calibration_data(&packet[rtap_bytes +
ieee80211_header_size +
LLC_HEADER_SIZE +
sizeof(struct iphdr) +
sizeof(struct udphdr) + 9],
&request) ;
extract_packet_numbers(&packet[rtap_bytes +
ieee80211_header_size +
LLC_HEADER_SIZE +
sizeof(struct iphdr) +
sizeof(struct udphdr) + 10 +
3 * sizeof(float)],
&request) ;
extract_calibration_data(&packet[offset], &request) ;
break ;
case OWL_REQUEST_AUTOCALIBRATION :
@ -1021,19 +1013,7 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
printf(".. on the wrong port!") ;
putchar('\n') ;
}
extract_calibration_data(&packet[rtap_bytes +
ieee80211_header_size +
LLC_HEADER_SIZE +
sizeof(struct iphdr) +
sizeof(struct udphdr) + 9],
&request) ;
extract_packet_numbers(&packet[rtap_bytes +
ieee80211_header_size +
LLC_HEADER_SIZE +
sizeof(struct iphdr) +
sizeof(struct udphdr) + 10 +
3 * sizeof(float)],
&request) ;
extract_calibration_data(&packet[offset], &request) ;
break ;
default :
@ -1041,11 +1021,6 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
printf("\nStrange explicit packet received\n") ;
fprintf(stderr,
"Error! Unknown request type (%d).\n", request.type) ;
is_explicit_packet = owl_false ;
}
if (! is_explicit_packet)
{
if (GET_MODE() == MODE_ACTIVE)
return ;
else
@ -1056,14 +1031,6 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
request.type = OWL_REQUEST_IMPLICIT ;
}
}
else
// Copy the timestamp "as is" (i.e. without changing endianess)
// because it will return to the network soon:
memcpy(&request.request_time,
&packet[rtap_bytes + ieee80211_header_size +
LLC_HEADER_SIZE + sizeof(struct iphdr) +
sizeof(struct udphdr) + 1],
sizeof(owl_timestamp)) ;
}
else if (GET_MODE() == MODE_PASSIVE || GET_MODE() == MODE_MIXED)
@ -1174,13 +1141,14 @@ void extract_calibration_data(const u_char *packet,
void extract_packet_numbers(const u_char *packet,
owl_captured_request *request)
{
// Number of packets:
memcpy(&request->nb_packets, packet, sizeof(uint16_t)) ;
request->nb_packets = request->nb_packets ;
// Current packet's ID:
memcpy(&request->packet_id, &packet[sizeof(uint16_t)],
memcpy(&request->packet_id, packet,
sizeof(uint16_t)) ;
request->packet_id = request->packet_id ;
// Number of packets:
memcpy(&request->nb_packets, &packet[sizeof(uint16_t)],
sizeof(uint16_t)) ;
request->nb_packets = request->nb_packets ;
}