bRAWcap 1.1.0
b-plus Technologies - Ethernet Performance Transmitter Receiver
Loading...
Searching...
No Matches
10_transmit_buffered_transmitter_f.c

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.

1/**
2 * @file 10_transmit_buffered_transmitter_f.c
3 *
4 * @brief bRAWcap Example - Demonstrates how to transmit packets using a buffered approach.
5 *
6 * @version 1.0
7 *
8 * @date 2025-10-08
9 *
10 * HISTORY:
11 * - 2025-10-08: Initial version
12 *
13 * @copyright
14 * <b> © 2021 - b-plus technologies GmbH. All rights reserved!</b>
15 *
16 * All rights exclusively reserved for b-plus GmbH, unless expressly otherwise agreed.
17 *
18 * Redistribution in source or any other form, with or without modification, is not permitted.
19 *
20 * You may use this code under the according license terms of b-plus.
21 * Please contact b-plus at services@b-plus.com to get the appropriate terms and conditions.
22 */
23// Include bRAWcap
24// Include bRAWcap
25#include "libbrawcap.h"
26
27// C STD
28#include <stdio.h> // for printf
29#include <stdlib.h> // for strtol
30#include <time.h> // for time
31
32typedef struct receive_counters
33{
34 unsigned long long packets;
35 unsigned long long bytes;
36}receive_counters_t;
37
38typedef struct example_context
39{
40 unsigned char demoLogged;
41 receive_counters_t counters;
42}example_context_t;
43
44
45BOOL g_transmission_pending = FALSE;
46
47// Check command line parameters if a custom execution time is specified
48void ParseArgs(int argc, char** argv, int* exec_time)
49{
50 if(argc > 1)
51 {
52 if(!memcmp(argv[1], "-t", 2))
53 {
54 char* pEnd = 0;
55 *exec_time = strtol(argv[2], &pEnd,0);
56 }
57 }
58}
59
60// Transmit callback function
61// This function will be called when the transmission is done
62static void tx_callback(brawcap_handle_t* const pHandle, const brawcap_status_t status,
63 brawcap_buffer_t* const pBuffer, void* pUser)
64{
65 UINT32 length = 0;
66 char* friendlyName = 0;
69 {
70 friendlyName = (char*)malloc(length);
71 brawcap_adapter_friendly_name_by_handle(pHandle, 0, &length);
72 }
73 else {
74 brawcap_adapter_name_by_handle(pHandle, name);
75 }
76
77 if (BRAWCAP_SUCCESS(status)) {
79 brawcap_buffer_count(pBuffer, &numPackets);
80 printf("[INFO] %s: Transmission successful (Packets: %u)\n", friendlyName ? friendlyName : name, numPackets);
81 }
82 else {
83 printf("[WARNING] %s: Transmission failed with status 0x%04x.\n", friendlyName ? friendlyName : name, status);
84 }
85 if (friendlyName)
86 {
87 free(friendlyName);
88 }
89
90 g_transmission_pending = FALSE;
91}
92
93int main(int argc, char** argv)
94{
95 // Set console title
96 SetConsoleTitleA("bRAWcap Example - Buffered Transmitter");
97
98 int exec_time = 300; // 5 minutes
99 time_t startup_sec = time(NULL);
100 ParseArgs(argc, argv, &exec_time);
101
102 // Some generic helper locals...
103 int retVal = 0;
104 example_context_t context = {0};
105
106 // Here we will store the status of any bRAWcap function
108
109 // Here we store the number of available bRAWcap adapters
110 brawcap_adapter_count_t numberAdapters = 0;
111 // This will contain our bRAWcap handle to the adapter we want to receive from
112 brawcap_handle_t* pHandle = 0;
113 // Our local packet buffer we will fill with packets to transmit
114 brawcap_buffer_t* pPacketBuffer = 0;
115 do
116 {
117 // First check if we have any bRAWcap adapters available.
118 // --> This function can only fail if we did something wrong.
119 // But we are sure that we did everything right...
120 // therefore we do not check it�s status.
121 brawcap_adapter_list_count(&numberAdapters);
122
123 // No adapters... nothing do receive from... lets exit ...
124 if (!numberAdapters)
125 {
126 printf("[WARNING] No bRAWcap adapter available... Will stop now.");
127 break;
128 }
129
130 // Get the name of the first adapter.
131 // To not overload the example with complex stuff,
132 // we always go for the first available adapter.
133 brawcap_adapter_name_t name = { '\0' };
135 {
136 printf("[ERROR] Unexpected status while retrieving adapter name: %d", brawcap_last_status());
137 retVal = -1;
138 break;
139 }
140
141 // Open a handle to the adapter by using the retrieved name.
142 if (!BRAWCAP_SUCCESS(brawcap_open(name, &pHandle)))
143 {
144 printf("[ERROR] Unexpected status while opening handle: %d", brawcap_last_status());
145 retVal = -1;
146 break;
147 }
148
149 // Create the packet buffers
151 {
152 printf("[ERROR] Unexpected status while creating packet buffer: %d", brawcap_last_status());
153 retVal = -1;
154 break;
155 }
156
157 // Fill the packet buffer with some packets to transmit
158 brawcap_buffer_iterator_t* pIterator = 0;
159 brawcap_buffer_iterator_create(&pIterator, pPacketBuffer, 0);
160
161 brawcap_packet_t* pPacket = 0;
163 // Define the payload as a byte array
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
168 };
169 // Set the payload of the packet
170 brawcap_packet_payload_set_v2(pPacket, payload, sizeof(payload));
171
172 for(int i = 0; i < 5; ++i)
173 brawcap_buffer_add_back(pPacketBuffer, pPacket);
174
175 // Transmit the packets in the buffer until the execution time is reached
176 brawcap_tx_start(pHandle, tx_callback, NULL);
177 do
178 {
179 brawcap_tx_buffer_send(pHandle, pPacketBuffer, FALSE);
180 g_transmission_pending = TRUE;
181 while(g_transmission_pending)
182 {
183 // Wait until the transmission is done
184 Sleep(500);
185 }
186 } while (time(NULL) - startup_sec < exec_time);
187 brawcap_tx_stop(pHandle);
188 }while(0);
189
190 // We do not want to produce any memory leaks...
191 // ... so always cleanup what we have created
192 // --> The functions here can only fail if we did something wrong.
193 // But we are sure that we did everything right...
194 // therefore we do not check their status.
195 if (pPacketBuffer)
196 brawcap_buffer_free(pPacketBuffer);
197 if (pHandle)
198 brawcap_close(pHandle);
199
200 return retVal;
201}
bRAWcap main header.
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.