This example shows how bRAWcap can be used to filter (discard) specific packets by using a BPF (Berkley Packet Filter).
42void ParseArgs(
int argc,
char** argv,
int* exec_time,
const char** filter_string)
47 if(strcmp(argv[i],
"-t") == 0 && (i + 1) < argc)
50 *exec_time = strtol(argv[i + 1], &pEnd, 0);
53 else if (strcmp(argv[i],
"-f") == 0 && (i + 1) < argc)
55 *filter_string = argv[i + 1];
66int main(
int argc,
char** argv)
69 SetConsoleTitleA(
"bRAWcap Example - BPF Filtering");
73 unsigned char demoModeLogged = 0;
74 unsigned long long packetCounter = 0;
76 int runtime_sec = clock() / CLOCKS_PER_SEC;
77 const char* filter_string =
"udp port 1900";
80 ParseArgs(argc, argv, &exec_time, &filter_string);
105 printf(
"[WARNING] No bRAWcap adapter available... Will stop now.");
115 printf(
"[ERROR] Unexpected status while retrieving adapter name: %d",
brawcap_last_status());
129 printf(
"[INFO] Using BPF filter: \"%s\"\n", filter_string);
135 const char* compile_error = 0;
140 printf(
"[ERROR] BPF compile error: %s\n", compile_error);
175 printf(
"[ERROR] Unexpected status while reducing driver queue size: %d",
brawcap_last_status());
194 UINT64 timestamp_sec = 0;
195 UINT32 timestamp_ns = 0;
196 const unsigned char* pPayload = 0;
199 while (runtime_sec < exec_time)
211 printf(
"[NOTICE] DEMO MODE: Limitation period elapsed, receiving is available again.\n");
224 printf(
"%llu. Packet:\n", ++packetCounter);
225 printf(
" - Capture Length: %04u Bytes\n", captureLength);
226 printf(
" - Length On Wire: %04u Bytes\n", lengthOnWire);
227 printf(
" - Timestamp: %llu %09u ns\n", timestamp_sec, timestamp_ns);
228 printf(
" - Payload: ");
230 UINT32 byteCounter = 1;
231 for (
const unsigned char* pByte = pPayload; byteCounter <= captureLength; ++byteCounter)
233 printf(
"%02X ", (
unsigned char)*pByte++);
234 if (!(byteCounter % 16)) printf(
"\n ");
235 else if (!(byteCounter % 8)) printf(
" ");
251 printf(
"[WARNING] DEMO MODE: Receiving not available.\n");
263 runtime_sec = (clock() / CLOCKS_PER_SEC - runtime_sec);
struct _brawcap_handle brawcap_handle_t
A bRAWcap handle.
Definition: brawcap_types_um.h:184
brawcap_status_t brawcap_close(brawcap_handle_t *pHandle)
Closes the specified bRAWcap handle.
brawcap_status_t brawcap_open(const brawcap_adapter_name_t name, brawcap_handle_t **const pHandle)
Opens a new bRAWcap handle on the adapter, specified by it´s name.
brawcap_status_t brawcap_last_status()
Reads the last status appeared in bRAWcap, for the calling thread.
brawcap_status_t
bRAWcap status/return codes.
Definition: brawcap_types_shared.h:140
#define BRAWCAP_SUCCESS(status)
Checks if the returned status indicates a success with no additional info.
Definition: brawcap_types_shared.h:112
@ BRAWCAP_STATUS_SUCCESS
Definition: brawcap_types_shared.h:142
@ BRAWCAP_STATUS_INFO_NO_DATA
Indicates that a function returns without any data.
Definition: brawcap_types_shared.h:329
@ BRAWCAP_STATUS_ERROR_BPF_COMPILE_FAILED
Indicates that the BPF filter compilation failed.
Definition: brawcap_types_shared.h:241
@ BRAWCAP_STATUS_WARNING_DEMO_MODE
The operation was not executed due to demo mode limitations.
Definition: brawcap_types_shared.h:252
struct _brawcap_timestamp brawcap_timestamp_t
bRAWcap timestamp object.
Definition: brawcap_types_shared.h:608
brawcap_status_t brawcap_timestamp_value_ns_get(brawcap_timestamp_t *const pTimestamp, UINT64 *const pSeconds, UINT32 *const pNanoseconds)
Reads out the timestamp value in seconds and nanoseconds.
UINT16 brawcap_packet_size_t
Type for handling the number of payload bytes per packet.
Definition: brawcap_types_shared.h:666
brawcap_status_t brawcap_packet_create(brawcap_packet_t **const pPacket, const brawcap_packet_size_t maxSize)
Creates a new packet.
struct _brawcap_packet brawcap_packet_t
bRAWcap packet object.
Definition: brawcap_types_shared.h:675
brawcap_status_t brawcap_packet_payload_get_v2(brawcap_packet_t *const pPacket, const unsigned char **const pPayload, brawcap_packet_size_t *const pLength)
Reads out the payload of the specified packet.
#define BRAWCAP_PACKET_SIZE_MAX
The maximum supported (byte) size for a single packet payload.
Definition: brawcap_types_shared.h:645
brawcap_status_t brawcap_packet_length_on_wire_get(brawcap_packet_t *const pPacket, brawcap_packet_size_t *const pLengthOnWire)
Reads out the length on wire of the specified packet. This value is especially useful for received pa...
brawcap_status_t brawcap_packet_timestamp_get(brawcap_packet_t *const pPacket, brawcap_timestamp_t **const pTimestamp)
Reads out the timestamp object for the specified packet.
brawcap_status_t brawcap_adapter_list_at(const brawcap_adapter_count_t index, brawcap_adapter_name_t name)
Reads out the adapter name of the adapter at the adapter list index.
brawcap_status_t brawcap_adapter_list_count(brawcap_adapter_count_t *const pCount)
Reads out the current number of supported adapters in the adapter list.
char brawcap_adapter_name_t[BRAWCAP_ADAPTER_NAME_LENGTH]
Fixed size array containing a adapter name.
Definition: brawcap_types_um.h:301
UINT8 brawcap_adapter_count_t
Type used for counting the available/supported adapters on a machine.
Definition: brawcap_types_um.h:286
brawcap_status_t brawcap_rx_packet(brawcap_handle_t *const pHandle, brawcap_packet_t *const pPacket)
Receives a single packet from the specified handle.
brawcap_status_t brawcap_rx_driver_queue_size_set(brawcap_handle_t *const pHandle, const brawcap_queue_size_t size)
Sets the driver queue size (in number of packets) for the specified handle.
brawcap_status_t brawcap_rx_filter_set(brawcap_handle_t *const pHandle, const brawcap_filter_t *const pFilter)
Sets the specified filter to the specified handle.
brawcap_status_t brawcap_filter_create(brawcap_filter_t **const pFilter, const brawcap_filter_type_t type)
Creates a new filter.
struct _brawcap_filter brawcap_filter_t
bRAWcap filter object.
Definition: brawcap_types_shared.h:959
brawcap_status_t brawcap_filter_free(brawcap_filter_t *pFilter)
Frees the specified filter. When this function is called the given filter becomes invalid and it´s me...
brawcap_status_t brawcap_filter_bpf_set(brawcap_filter_t *const pFilter, const char *const filterString)
Sets the BPF filter string for the specified filter.
brawcap_status_t brawcap_filter_indicate_set(brawcap_filter_t *const pFilter, const BOOLEAN indicate)
Specifies if packets which matched the specified filter should be indicated to the network stack.
brawcap_status_t brawcap_filter_activate(brawcap_filter_t *const pFilter)
Sets the specified filter to active.
brawcap_status_t brawcap_filter_bpf_compile_error_get(brawcap_filter_t *const pFilter, const char *pError)
Retrieves the BPF compile error message from the specified filter.
@ BRAWCAP_FILTER_TYPE_BPF
A Berkley Packet Filter.
Definition: brawcap_types_shared.h:946