This example shows how to use bRAWcap to transmit packets using a buffered approach.
The example demonstrates how to create a buffer, fill it with packets, and transmit the entire buffer over the network. The buffered approach allows for more efficient transmission of multiple packets at once.
32typedef struct receive_counters
34 unsigned long long packets;
35 unsigned long long bytes;
38typedef struct example_context
40 unsigned char demoLogged;
41 receive_counters_t counters;
45BOOL g_transmission_pending = FALSE;
48void ParseArgs(
int argc,
char** argv,
int* exec_time)
52 if(!memcmp(argv[1],
"-t", 2))
55 *exec_time = strtol(argv[2], &pEnd,0);
66 char* friendlyName = 0;
70 friendlyName = (
char*)malloc(length);
80 printf(
"[INFO] %s: Transmission successful (Packets: %u)\n", friendlyName ? friendlyName : name, numPackets);
83 printf(
"[WARNING] %s: Transmission failed with status 0x%04x.\n", friendlyName ? friendlyName : name, status);
90 g_transmission_pending = FALSE;
93int main(
int argc,
char** argv)
96 SetConsoleTitleA(
"bRAWcap Example - Buffered Transmitter");
99 time_t startup_sec = time(NULL);
100 ParseArgs(argc, argv, &exec_time);
104 example_context_t context = {0};
126 printf(
"[WARNING] No bRAWcap adapter available... Will stop now.");
136 printf(
"[ERROR] Unexpected status while retrieving adapter name: %d",
brawcap_last_status());
152 printf(
"[ERROR] Unexpected status while creating packet buffer: %d",
brawcap_last_status());
164 unsigned char payload[] = {
165 0x68, 0xB9, 0x83, 0x01, 0x7b, 0xff, 0x68, 0xB9, 0x83, 0x01, 0x36, 0xff, 0x08, 0x00,
166 0x45, 0x00, 0x04, 0x1c, 0xff, 0x6c, 0x00, 0x00, 0xff, 0x11, 0xff, 0x10, 0xff, 0xff,
167 0x06, 0x02, 0xC0, 0xA8, 0x06, 0x01, 0xff, 0xff, 0x1f, 0x90, 0x04, 0x08, 0x58, 0x24
172 for(
int i = 0; i < 5; ++i)
180 g_transmission_pending = TRUE;
181 while(g_transmission_pending)
186 }
while (time(NULL) - startup_sec < exec_time);
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_ERROR_OVERRUN
A buffer overrun was detected.
Definition: brawcap_types_shared.h:235
brawcap_status_t brawcap_packet_create(brawcap_packet_t **const pPacket, const brawcap_packet_size_t maxSize)
Creates a new packet.
brawcap_status_t brawcap_packet_payload_set_v2(brawcap_packet_t *const pPacket, const unsigned char *const pPayload, const brawcap_packet_size_t length)
Sets the packet payload of the specified packet.
struct _brawcap_packet brawcap_packet_t
bRAWcap packet object.
Definition: brawcap_types_shared.h:675
#define BRAWCAP_PACKET_SIZE_MAX
The maximum supported (byte) size for a single packet payload.
Definition: brawcap_types_shared.h:645
struct _brawcap_buffer brawcap_buffer_t
bRAWcap packet buffer object.
Definition: brawcap_types_shared.h:746
brawcap_status_t brawcap_buffer_add_back(brawcap_buffer_t *const pBuffer, const brawcap_packet_t *const pPacket)
Adds the specified packet at the end of the specified packet buffer.
brawcap_status_t brawcap_buffer_free(brawcap_buffer_t *pBuffer)
Frees the specified packet buffer. When this function is called the specified packet buffer becomes i...
UINT32 brawcap_buffer_packet_count_t
Type for handling the number of packets which can be stored in a packet buffer.
Definition: brawcap_types_shared.h:738
brawcap_status_t brawcap_buffer_count(brawcap_buffer_t *const pBuffer, brawcap_buffer_packet_count_t *const pPacketCount)
Reads out the number of currently buffered packets in the specified packet buffer.
brawcap_status_t brawcap_buffer_create(brawcap_buffer_t **const pBuffer, const brawcap_packet_size_t maxPacketPayloadSize, const brawcap_buffer_packet_count_t numPackets)
Creates a new packet buffer.
struct _brawcap_buffer_iterator brawcap_buffer_iterator_t
bRAWcap packet buffer iterator object.
Definition: brawcap_types_shared.h:774
brawcap_status_t brawcap_buffer_iterator_create(brawcap_buffer_iterator_t **const pIterator, brawcap_buffer_t *const pBuffer, brawcap_buffer_packet_count_t startPosition)
Creates a new iterator for the specified buffer.
brawcap_status_t brawcap_adapter_name_by_handle(brawcap_handle_t *const pHandle, brawcap_adapter_name_t name)
Reads out the adapter name of the specified adapter.
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_friendly_name_by_handle(brawcap_handle_t *const pHandle, char *const friendlyName, UINT32 *const pLength)
Reads out the currently set friendly name of the specified adapter.
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_tx_stop(brawcap_handle_t *const pHandle)
Stops the internal transmit loop.
brawcap_status_t brawcap_tx_start(brawcap_handle_t *const pHandle, brawcap_tx_callback_t const callback, void *pUser)
Starts the internal transmit loop.
brawcap_status_t brawcap_tx_buffer_send(brawcap_handle_t *const pHandle, brawcap_buffer_t *const pBuffer, const BOOLEAN synchronized)
Pushes the specified buffer to the specified handle transmit queue.