bRAWcap 1.0.1
b-plus Technologies - Ethernet Performance Transmitter Receiver
Loading...
Searching...
No Matches
07_receive_buffered_receiver.c

This example shows how to use the bRAWcap single packet receive feature. It opens a handle to the first available bRAWcap adapter, creates a bRAWcap packet to store received data to. For each received packet it will print the packet information to console.

1/**
2 * @file 07_receive_buffered_receiver.c
3 *
4 * @brief bRAWcap Example - Demonstrates how to use bRAWcap to receive packets with packet buffers.
5 *
6 * It shows how to:
7 * - Open a handle to the first available adapter
8 * - Create a bRAWcap packet buffer objects
9 * - Start receiving on a handle
10 * - Using the receive callback
11 * - Iterating through a packet buffer
12 * - Accessing a buffered packet
13 *
14 * This example makes use of C only.
15 *
16 * @version 1.1
17 *
18 * @date 2023-03-30
19 *
20 * @copyright
21 * <b> © 2021 - b-plus technologies GmbH. All rights reserved!</b>
22 *
23 * All rights exclusively reserved for b-plus GmbH, unless expressly otherwise agreed.
24 *
25 * Redistribution in source or any other form, with or without modification, is not permitted.
26 *
27 * You may use this code under the according license terms of b-plus.
28 * Please contact b-plus at services@b-plus.com to get the appropriate terms and conditions.
29 */
30// Include bRAWcap
31#include "libbrawcap.h"
32
33// C STD
34#include <stdio.h> // for printf
35#include <stdlib.h> // for strtol
36
37typedef struct receive_counters
38{
39 unsigned long long packets;
40 unsigned long long bytes;
41}receive_counters_t;
42
43typedef struct example_context
44{
45 unsigned char demoLogged;
46 receive_counters_t counters;
47}example_context_t;
48
49
50// Check command line parameters if a custom execution time is specified
51void ParseArgs(int argc, char** argv, int* exec_time)
52{
53 if(argc > 1)
54 {
55 if(!memcmp(argv[1], "-t", 2))
56 {
57 char* pEnd = 0;
58 *exec_time = strtol(argv[2], &pEnd,0);
59 }
60 }
61}
62
63void receiveCallback(brawcap_handle_t* const pHandle, const brawcap_status_t status, brawcap_buffer_t* const pBuffer,
64 void* pUser)
65{
66 example_context_t* pContext = (example_context_t*) pUser;
68 {
69 if(!pContext->demoLogged)
70 {
71 printf("[WARNING] DEMO MODE: Receiving not available.\n");
72 pContext->demoLogged = 1;
73 }
74 }
75 else if(status == BRAWCAP_STATUS_SUCCESS)
76 {
77 if(pContext->demoLogged)
78 {
79 printf("[NOTICE] DEMO MODE: Limitation period elapsed, receiving is available again.\n");
80 pContext->demoLogged = 0;
81 }
82 brawcap_buffer_iterator_t* pIterator = 0;
83 brawcap_buffer_iterator_create(&pIterator, pBuffer, 0);
84
85 brawcap_packet_t* pPacket = 0;
86 brawcap_packet_size_t packetSize = 0;
87
88 do
89 {
90 pPacket = brawcap_buffer_iterator_eval(pIterator);
91 if(!pPacket)
92 break;
93
94 brawcap_packet_payload_size_get(pPacket, &packetSize);
95 pContext->counters.bytes += packetSize;
96 ++pContext->counters.packets;
97
99 } while (1);
100
102
103 printf("[NOTICE] Received new packets [Total Packets: %llu / Total Bytes: %llu].\n",
104 pContext->counters.packets, pContext->counters.bytes);
105 }
106}
107
108int main(int argc, char** argv)
109{
110 // Set console title
111 SetConsoleTitleA("bRAWcap Example - Buffered Receiver");
112
113 int exec_time = 300; // 5 minutes
114 int runtime_sec = 0;
115 ParseArgs(argc, argv, &exec_time);
116
117 // Some generic helper locals...
118 int retVal = 0;
119 example_context_t context = {0};
120
121 // Here we will store the status of any bRAWcap function
123
124 // Here we store the number of available bRAWcap adapters
125 brawcap_adapter_count_t numberAdapters = 0;
126 // This will contain our bRAWcap handle to the adapter we want to receive from
127 brawcap_handle_t* pHandle = 0;
128 // Our local packet buffers for storing received data
129 brawcap_buffer_t* pPacketBuffer1 = 0;
130 brawcap_buffer_t* pPacketBuffer2 = 0;
131
132 do
133 {
134 // First check if we have any bRAWcap adapters available.
135 // --> This function can only fail if we did something wrong.
136 // But we are sure that we did everything right...
137 // therefore we do not check it�s status.
138 brawcap_adapter_list_count(&numberAdapters);
139
140 // No adapters... nothing do receive from... lets exit ...
141 if (!numberAdapters)
142 {
143 printf("[WARNING] No bRAWcap adapter available... Will stop now.");
144 break;
145 }
146
147 // Get the name of the first adapter.
148 // To not overload the example with complex stuff,
149 // we always go for the first available adapter.
150 brawcap_adapter_name_t name = { '\0' };
152 {
153 printf("[ERROR] Unexpected status while retrieving adapter name: %d", brawcap_last_status());
154 retVal = -1;
155 break;
156 }
157
158 // Open a handle to the adapter by using the retrieved name.
159 if (!BRAWCAP_SUCCESS(brawcap_open(name, &pHandle)))
160 {
161 printf("[ERROR] Unexpected status while opening handle: %d", brawcap_last_status());
162 retVal = -1;
163 break;
164 }
165
166 // Create the packet buffers
167 // For simplicity and to make sure that we get all packets,
168 // we set the packet payload size to the max supported value.
170 {
171 printf("[ERROR] Unexpected status while creating packet buffer: %d", brawcap_last_status());
172 retVal = -1;
173 break;
174 }
176 {
177 printf("[ERROR] Unexpected status while creating packet buffer: %d", brawcap_last_status());
178 retVal = -1;
179 break;
180 }
181
182 // Attach the created packet buffers to the handle for reception
183 brawcap_rx_buffer_attach(pHandle, pPacketBuffer1);
184 brawcap_rx_buffer_attach(pHandle, pPacketBuffer2);
185
186 // Now start the the bRAWcap reception on the handle
187 brawcap_rx_start(pHandle, receiveCallback, &context, 0);
188
189 // After starting the reception our main thread has finished it´s work.
190 // Everything else will be done from inside the receive callback until we should stop receiving.
191 do
192 {
193 Sleep(1000);
194 } while (++runtime_sec < exec_time);
195
196 brawcap_rx_stop(pHandle);
197 brawcap_rx_buffer_detach(pHandle, pPacketBuffer2);
198 brawcap_rx_buffer_detach(pHandle, pPacketBuffer1);
199 }while(0);
200
201 // We do not want to produce any memory leaks...
202 // ... so always cleanup what we have created
203 // --> The functions here can only fail if we did something wrong.
204 // But we are sure that we did everything right...
205 // therefore we do not check their status.
206 if (pPacketBuffer1)
207 brawcap_buffer_free(pPacketBuffer1);
208 if(pPacketBuffer2)
209 brawcap_buffer_free(pPacketBuffer2);
210 if (pHandle)
211 brawcap_close(pHandle);
212
213 return retVal;
214}
bRAWcap main header.
struct _brawcap_handle brawcap_handle_t
A bRAWcap handle.
Definition: brawcap_types_um.h:173
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:150
#define BRAWCAP_SUCCESS(status)
Checks if the returned status indicates a success with no additional info.
Definition: brawcap_types_shared.h:122
@ BRAWCAP_STATUS_SUCCESS
Definition: brawcap_types_shared.h:152
@ BRAWCAP_STATUS_WARNING_DEMO_MODE
The operation was not executed due to demo mode limitations.
Definition: brawcap_types_shared.h:256
UINT16 brawcap_packet_size_t
Type for handling the number of payload bytes per packet.
Definition: brawcap_types_shared.h:670
brawcap_status_t brawcap_packet_payload_size_get(brawcap_packet_t *const pPacket, brawcap_packet_size_t *const pLength)
Reads out the captured payload byte size of the specified packet. This indicates the total byte size ...
struct _brawcap_packet brawcap_packet_t
bRAWcap packet object.
Definition: brawcap_types_shared.h:679
#define BRAWCAP_PACKET_SIZE_MAX
The maximum supported (byte) size for a single packet payload.
Definition: brawcap_types_shared.h:649
struct _brawcap_buffer brawcap_buffer_t
bRAWcap packet buffer object.
Definition: brawcap_types_shared.h:742
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...
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.
brawcap_status_t brawcap_buffer_iterator_free(brawcap_buffer_iterator_t *const pIterator)
Frees the specified iterator. When this function is called the specified iterator becomes invalid and...
struct _brawcap_buffer_iterator brawcap_buffer_iterator_t
bRAWcap packet buffer iterator object.
Definition: brawcap_types_shared.h:770
brawcap_packet_t * brawcap_buffer_iterator_eval(brawcap_buffer_iterator_t *const pIterator)
Returns the buffered packet for the specified iterator.
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_buffer_iterator_next(brawcap_buffer_iterator_t *const pIterator)
Increments the iterator to the next buffered 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:257
UINT8 brawcap_adapter_count_t
Type used for counting the available/supported adapters on a machine.
Definition: brawcap_types_um.h:242
brawcap_status_t brawcap_rx_buffer_detach(brawcap_handle_t *const pHandle, brawcap_buffer_t *const pBuffer)
Detaches the specified bRAWcap packet buffer from the specified handle.
brawcap_status_t brawcap_rx_start(brawcap_handle_t *const pHandle, brawcap_rx_callback_t const callback, void *const pUser, const BOOLEAN indicateNoPackets)
Starts the internal receive loop.
brawcap_status_t brawcap_rx_stop(brawcap_handle_t *const pHandle)
Stops the internal receive loop.
brawcap_status_t brawcap_rx_buffer_attach(brawcap_handle_t *const pHandle, brawcap_buffer_t *const pBuffer)
Attaches the specified bRAWcap packet buffer to the specified handle.