pcap.c (d9528177797382e90c28c9d89dfdb49be680c952) pcap.c (cf15ca5e1750a2ef48ccf81c5a78b87cd3cc43bb)
1/* -*- c -*- */
2/*
3 * Copyright 2014 Christopher D. Kilgour techie AT whiterocker.com
4 *
5 * This file is part of libbtbb
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by

--- 116 unchanged lines hidden (view full) ---

125 (payload_transport << 4) | payload_rate;
126 pkt->bredr_bb_header.corrected_header_bits = corrected_header_bits;
127 pkt->bredr_bb_header.corrected_payload_bits = htole16( corrected_payload_bits );
128 pkt->bredr_bb_header.lap = htole32( lap );
129 pkt->bredr_bb_header.ref_lap_uap = htole32( reflapuap );
130 pkt->bredr_bb_header.bt_header = htole16( bt_header );
131 pkt->bredr_bb_header.flags = htole16( flags );
132 if (caplen) {
1/* -*- c -*- */
2/*
3 * Copyright 2014 Christopher D. Kilgour techie AT whiterocker.com
4 *
5 * This file is part of libbtbb
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by

--- 116 unchanged lines hidden (view full) ---

125 (payload_transport << 4) | payload_rate;
126 pkt->bredr_bb_header.corrected_header_bits = corrected_header_bits;
127 pkt->bredr_bb_header.corrected_payload_bits = htole16( corrected_payload_bits );
128 pkt->bredr_bb_header.lap = htole32( lap );
129 pkt->bredr_bb_header.ref_lap_uap = htole32( reflapuap );
130 pkt->bredr_bb_header.bt_header = htole16( bt_header );
131 pkt->bredr_bb_header.flags = htole16( flags );
132 if (caplen) {
133 assert(caplen <= sizeof(pkt->bredr_payload)); // caller ensures this, but to be safe..
133 (void) memcpy( &pkt->bredr_payload[0], payload, caplen );
134 }
135 else {
136 pkt->bredr_bb_header.flags &= htole16( ~BREDR_PAYLOAD_PRESENT );
137 }
138}
139
140int

--- 132 unchanged lines hidden (view full) ---

273 const uint8_t rf_channel,
274 const int8_t signal_power,
275 const int8_t noise_power,
276 const uint8_t access_address_offenses,
277 const uint32_t ref_access_address,
278 const uint16_t flags,
279 const uint8_t * lepkt )
280{
134 (void) memcpy( &pkt->bredr_payload[0], payload, caplen );
135 }
136 else {
137 pkt->bredr_bb_header.flags &= htole16( ~BREDR_PAYLOAD_PRESENT );
138 }
139}
140
141int

--- 132 unchanged lines hidden (view full) ---

274 const uint8_t rf_channel,
275 const int8_t signal_power,
276 const int8_t noise_power,
277 const uint8_t access_address_offenses,
278 const uint32_t ref_access_address,
279 const uint16_t flags,
280 const uint8_t * lepkt )
281{
281 uint32_t pcap_caplen = sizeof(pcap_bluetooth_le_ll_header)+caplen;
282 uint32_t incl_len = MIN(LE_MAX_PAYLOAD, caplen);
282
283 pkt->pcap_header.ts.tv_sec = ns / 1000000000ull;
284 pkt->pcap_header.ts.tv_usec = ns % 1000000000ull;
283
284 pkt->pcap_header.ts.tv_sec = ns / 1000000000ull;
285 pkt->pcap_header.ts.tv_usec = ns % 1000000000ull;
285 pkt->pcap_header.caplen = pcap_caplen;
286 pkt->pcap_header.len = pcap_caplen;
286 pkt->pcap_header.len = sizeof(pcap_bluetooth_le_ll_header)+caplen;
287 pkt->pcap_header.caplen = sizeof(pcap_bluetooth_le_ll_header)+incl_len;
287
288 pkt->le_ll_header.rf_channel = rf_channel;
289 pkt->le_ll_header.signal_power = signal_power;
290 pkt->le_ll_header.noise_power = noise_power;
291 pkt->le_ll_header.access_address_offenses = access_address_offenses;
292 pkt->le_ll_header.ref_access_address = htole32( ref_access_address );
293 pkt->le_ll_header.flags = htole16( flags );
288
289 pkt->le_ll_header.rf_channel = rf_channel;
290 pkt->le_ll_header.signal_power = signal_power;
291 pkt->le_ll_header.noise_power = noise_power;
292 pkt->le_ll_header.access_address_offenses = access_address_offenses;
293 pkt->le_ll_header.ref_access_address = htole32( ref_access_address );
294 pkt->le_ll_header.flags = htole16( flags );
294 (void) memcpy( &pkt->le_packet[0], lepkt, caplen );
295 (void) memcpy( &pkt->le_packet[0], lepkt, incl_len );
295}
296
297int
298lell_pcap_append_packet(lell_pcap_handle * h, const uint64_t ns,
299 const int8_t sigdbm, const int8_t noisedbm,
300 const uint32_t refAA, const lell_packet *pkt)
301{
302 if (h && h->dumper &&
303 (h->dlt == DLT_BLUETOOTH_LE_LL_WITH_PHDR)) {
304 uint16_t flags = LE_DEWHITENED | LE_AA_OFFENSES_VALID |
305 LE_SIGPOWER_VALID |
306 ((noisedbm < sigdbm) ? LE_NOISEPOWER_VALID : 0) |
307 (lell_packet_is_data(pkt) ? 0 : LE_REF_AA_VALID);
308 pcap_le_packet pcap_pkt;
309 assemble_pcapng_le_packet( &pcap_pkt,
310 0,
311 ns,
296}
297
298int
299lell_pcap_append_packet(lell_pcap_handle * h, const uint64_t ns,
300 const int8_t sigdbm, const int8_t noisedbm,
301 const uint32_t refAA, const lell_packet *pkt)
302{
303 if (h && h->dumper &&
304 (h->dlt == DLT_BLUETOOTH_LE_LL_WITH_PHDR)) {
305 uint16_t flags = LE_DEWHITENED | LE_AA_OFFENSES_VALID |
306 LE_SIGPOWER_VALID |
307 ((noisedbm < sigdbm) ? LE_NOISEPOWER_VALID : 0) |
308 (lell_packet_is_data(pkt) ? 0 : LE_REF_AA_VALID);
309 pcap_le_packet pcap_pkt;
310 assemble_pcapng_le_packet( &pcap_pkt,
311 0,
312 ns,
312 9+pkt->length,
313 pkt->length + 4 + 2 + 3, // AA + header + CRC
313 pkt->channel_k,
314 sigdbm,
315 noisedbm,
316 pkt->access_address_offenses,
317 refAA,
318 flags,
319 &pkt->symbols[0] );
320 pcap_dump((u_char *)h->dumper, &pcap_pkt.pcap_header, (u_char *)&pcap_pkt.le_ll_header);

--- 37 unchanged lines hidden (view full) ---

358
359int
360lell_pcap_append_ppi_packet(lell_pcap_handle * h, const uint64_t ns,
361 const uint8_t clkn_high,
362 const int8_t rssi_min, const int8_t rssi_max,
363 const int8_t rssi_avg, const uint8_t rssi_count,
364 const lell_packet *pkt)
365{
314 pkt->channel_k,
315 sigdbm,
316 noisedbm,
317 pkt->access_address_offenses,
318 refAA,
319 flags,
320 &pkt->symbols[0] );
321 pcap_dump((u_char *)h->dumper, &pcap_pkt.pcap_header, (u_char *)&pcap_pkt.le_ll_header);

--- 37 unchanged lines hidden (view full) ---

359
360int
361lell_pcap_append_ppi_packet(lell_pcap_handle * h, const uint64_t ns,
362 const uint8_t clkn_high,
363 const int8_t rssi_min, const int8_t rssi_max,
364 const int8_t rssi_avg, const uint8_t rssi_count,
365 const lell_packet *pkt)
366{
366 const uint16_t ppi_packet_header_sz = sizeof(ppi_packet_header_t);
367 const uint16_t ppi_fieldheader_sz = sizeof(ppi_fieldheader_t);
368 const uint16_t le_ll_ppi_header_sz = sizeof(ppi_btle_t);
369
370 if (h && h->dumper &&
371 (h->dlt == DLT_PPI)) {
372 pcap_ppi_le_packet pcap_pkt;
367 if (h && h->dumper &&
368 (h->dlt == DLT_PPI)) {
369 pcap_ppi_le_packet pcap_pkt;
373 uint32_t pcap_caplen =
374 ppi_packet_header_sz+ppi_fieldheader_sz+le_ll_ppi_header_sz+pkt->length+9;
370 const uint16_t pcap_headerlen =
371 sizeof(ppi_packet_header_t) +
372 sizeof(ppi_fieldheader_t) +
373 sizeof(ppi_btle_t);
375 uint16_t MHz = 2402 + 2*lell_get_channel_k(pkt);
374 uint16_t MHz = 2402 + 2*lell_get_channel_k(pkt);
375 unsigned packet_len = pkt->length + 4 + 2 + 3; // AA + header + CRC
376 unsigned incl_len = MIN(LE_MAX_PAYLOAD, packet_len);
376
377 pcap_pkt.pcap_header.ts.tv_sec = ns / 1000000000ull;
378 pcap_pkt.pcap_header.ts.tv_usec = ns % 1000000000ull;
377
378 pcap_pkt.pcap_header.ts.tv_sec = ns / 1000000000ull;
379 pcap_pkt.pcap_header.ts.tv_usec = ns % 1000000000ull;
379 pcap_pkt.pcap_header.caplen = pcap_caplen;
380 pcap_pkt.pcap_header.len = pcap_caplen;
380 pcap_pkt.pcap_header.caplen = pcap_headerlen + incl_len;
381 pcap_pkt.pcap_header.len = pcap_headerlen + packet_len;
381
382 pcap_pkt.ppi_packet_header.pph_version = 0;
383 pcap_pkt.ppi_packet_header.pph_flags = 0;
382
383 pcap_pkt.ppi_packet_header.pph_version = 0;
384 pcap_pkt.ppi_packet_header.pph_flags = 0;
384 pcap_pkt.ppi_packet_header.pph_len = htole16(ppi_packet_header_sz+ppi_fieldheader_sz+le_ll_ppi_header_sz);
385 pcap_pkt.ppi_packet_header.pph_len = htole16(pcap_headerlen);
385 pcap_pkt.ppi_packet_header.pph_dlt = htole32(DLT_USER0);
386
387 pcap_pkt.ppi_fieldheader.pfh_type = htole16(PPI_BTLE);
386 pcap_pkt.ppi_packet_header.pph_dlt = htole32(DLT_USER0);
387
388 pcap_pkt.ppi_fieldheader.pfh_type = htole16(PPI_BTLE);
388 pcap_pkt.ppi_fieldheader.pfh_datalen = htole16(le_ll_ppi_header_sz);
389 pcap_pkt.ppi_fieldheader.pfh_datalen = htole16(sizeof(ppi_btle_t));
389
390 pcap_pkt.le_ll_ppi_header.btle_version = h->btle_ppi_version;
391 pcap_pkt.le_ll_ppi_header.btle_channel = htole16(MHz);
392 pcap_pkt.le_ll_ppi_header.btle_clkn_high = clkn_high;
393 pcap_pkt.le_ll_ppi_header.btle_clk100ns = htole32(pkt->clk100ns);
394 pcap_pkt.le_ll_ppi_header.rssi_max = rssi_max;
395 pcap_pkt.le_ll_ppi_header.rssi_min = rssi_min;
396 pcap_pkt.le_ll_ppi_header.rssi_avg = rssi_avg;
397 pcap_pkt.le_ll_ppi_header.rssi_count = rssi_count;
390
391 pcap_pkt.le_ll_ppi_header.btle_version = h->btle_ppi_version;
392 pcap_pkt.le_ll_ppi_header.btle_channel = htole16(MHz);
393 pcap_pkt.le_ll_ppi_header.btle_clkn_high = clkn_high;
394 pcap_pkt.le_ll_ppi_header.btle_clk100ns = htole32(pkt->clk100ns);
395 pcap_pkt.le_ll_ppi_header.rssi_max = rssi_max;
396 pcap_pkt.le_ll_ppi_header.rssi_min = rssi_min;
397 pcap_pkt.le_ll_ppi_header.rssi_avg = rssi_avg;
398 pcap_pkt.le_ll_ppi_header.rssi_count = rssi_count;
398 (void) memcpy( &pcap_pkt.le_packet[0], &pkt->symbols[0], pkt->length + 9 ); // FIXME where does the 9 come from?
399 (void) memcpy( &pcap_pkt.le_packet[0], &pkt->symbols[0], incl_len);
399 pcap_dump((u_char *)h->dumper, &pcap_pkt.pcap_header, (u_char *)&pcap_pkt.ppi_packet_header);
400 pcap_dump_flush(h->dumper);
401 return 0;
402 }
403 return -PCAP_INVALID_HANDLE;
404}
405
406int

--- 16 unchanged lines hidden ---
400 pcap_dump((u_char *)h->dumper, &pcap_pkt.pcap_header, (u_char *)&pcap_pkt.ppi_packet_header);
401 pcap_dump_flush(h->dumper);
402 return 0;
403 }
404 return -PCAP_INVALID_HANDLE;
405}
406
407int

--- 16 unchanged lines hidden ---