bRAWcap 1.1.0
b-plus Technologies - Ethernet Performance Transmitter Receiver
Loading...
Searching...
No Matches
07_receive_buffered_receiver_f.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_f.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 * HISTORY:
21 * - 2023-03-30: Initial version
22 * - 2023-08-10: Set all indentation to 4 spaces
23 *
24 * @copyright
25 * <b> © 2021 - b-plus technologies GmbH. All rights reserved!</b>
26 *
27 * All rights exclusively reserved for b-plus GmbH, unless expressly otherwise agreed.
28 *
29 * Redistribution in source or any other form, with or without modification, is not permitted.
30 *
31 * You may use this code under the according license terms of b-plus.
32 * Please contact b-plus at services@b-plus.com to get the appropriate terms and conditions.
33 */
34// Include bRAWcap
35#include "libbrawcap.h"
36
37// C STD
38#include <stdio.h> // for printf
39#include <stdlib.h> // for strtol
40
41typedef struct receive_counters
42{
43 unsigned long long packets;
44 unsigned long long bytes;
45}receive_counters_t;
46
47typedef struct example_context
48{
49 unsigned char demoLogged;
50 receive_counters_t counters;
51}example_context_t;
52
53
54// Check command line parameters if a custom execution time is specified
55void ParseArgs(int argc, char** argv, int* exec_time)
56{
57 if(argc > 1)
58 {
59 if(!memcmp(argv[1], "-t", 2))
60 {
61 char* pEnd = 0;
62 *exec_time = strtol(argv[2], &pEnd,0);
63 }
64 }
65}
66
67void receiveCallback(brawcap_handle_t* const pHandle, const brawcap_status_t status, brawcap_buffer_t* const pBuffer,
68 void* pUser)
69{
70 example_context_t* pContext = (example_context_t*) pUser;
72 {
73 if(!pContext->demoLogged)
74 {
75 printf("[WARNING] DEMO MODE: Receiving not available.\n");
76 pContext->demoLogged = 1;
77 }
78 }
79 else if(status == BRAWCAP_STATUS_SUCCESS)
80 {
81 if(pContext->demoLogged)
82 {
83 printf("[NOTICE] DEMO MODE: Limitation period elapsed, receiving is available again.\n");
84 pContext->demoLogged = 0;
85 }
86 brawcap_buffer_iterator_t* pIterator = 0;
87 brawcap_buffer_iterator_create(&pIterator, pBuffer, 0);
88
89 brawcap_packet_t* pPacket = 0;
90 brawcap_packet_size_t packetSize = 0;
91
92 do
93 {
94 pPacket = brawcap_buffer_iterator_eval(pIterator);
95 if(!pPacket)
96 break;
97
98 brawcap_packet_payload_size_get(pPacket, &packetSize);
99 pContext->counters.bytes += packetSize;
100 ++pContext->counters.packets;
101
103 } while (1);
104
106
107 printf("[NOTICE] Received new packets [Total Packets: %llu / Total Bytes: %llu].\n",
108 pContext->counters.packets, pContext->counters.bytes);
109 }
110}
111
112int main(int argc, char** argv)
113{
114 // Set console title
115 SetConsoleTitleA("bRAWcap Example - Buffered Receiver");
116
117 int exec_time = 300; // 5 minutes
118 int runtime_sec = 0;
119 ParseArgs(argc, argv, &exec_time);
120
121 // Some generic helper locals...
122 int retVal = 0;
123 example_context_t context = {0};
124
125 // Here we will store the status of any bRAWcap function
127
128 // Here we store the number of available bRAWcap adapters
129 brawcap_adapter_count_t numberAdapters = 0;
130 // This will contain our bRAWcap handle to the adapter we want to receive from
131 brawcap_handle_t* pHandle = 0;
132 // Our local packet buffers for storing received data
133 brawcap_buffer_t* pPacketBuffer1 = 0;
134 brawcap_buffer_t* pPacketBuffer2 = 0;
135
136 do
137 {
138 // First check if we have any bRAWcap adapters available.
139 // --> This function can only fail if we did something wrong.
140 // But we are sure that we did everything right...
141 // therefore we do not check it�s status.
142 brawcap_adapter_list_count(&numberAdapters);
143
144 // No adapters... nothing do receive from... lets exit ...
145 if (!numberAdapters)
146 {
147 printf("[WARNING] No bRAWcap adapter available... Will stop now.");
148 break;
149 }
150
151 // Get the name of the first adapter.
152 // To not overload the example with complex stuff,
153 // we always go for the first available adapter.
154 brawcap_adapter_name_t name = { '\0' };
156 {
157 printf("[ERROR] Unexpected status while retrieving adapter name: %d", brawcap_last_status());
158 retVal = -1;
159 break;
160 }
161
162 // Open a handle to the adapter by using the retrieved name.
163 if (!BRAWCAP_SUCCESS(brawcap_open(name, &pHandle)))
164 {
165 printf("[ERROR] Unexpected status while opening handle: %d", brawcap_last_status());
166 retVal = -1;
167 break;
168 }
169
170 // Create the packet buffers
171 // For simplicity and to make sure that we get all packets,
172 // we set the packet payload size to the max supported value.
174 {
175 printf("[ERROR] Unexpected status while creating packet buffer: %d", brawcap_last_status());
176 retVal = -1;
177 break;
178 }
180 {
181 printf("[ERROR] Unexpected status while creating packet buffer: %d", brawcap_last_status());
182 retVal = -1;
183 break;
184 }
185
186 // Attach the created packet buffers to the handle for reception
187 brawcap_rx_buffer_attach(pHandle, pPacketBuffer1);
188 brawcap_rx_buffer_attach(pHandle, pPacketBuffer2);
189
190 // Now start the the bRAWcap reception on the handle
191 brawcap_rx_start(pHandle, receiveCallback, &context, 0);
192
193 // After starting the reception our main thread has finished it´s work.
194 // Everything else will be done from inside the receive callback until we should stop receiving.
195 do
196 {
197 Sleep(1000);
198 } while (++runtime_sec < exec_time);
199
200 brawcap_rx_stop(pHandle);
201 brawcap_rx_buffer_detach(pHandle, pPacketBuffer2);
202 brawcap_rx_buffer_detach(pHandle, pPacketBuffer1);
203 }while(0);
204
205 // We do not want to produce any memory leaks...
206 // ... so always cleanup what we have created
207 // --> The functions here can only fail if we did something wrong.
208 // But we are sure that we did everything right...
209 // therefore we do not check their status.
210 if (pPacketBuffer1)
211 brawcap_buffer_free(pPacketBuffer1);
212 if(pPacketBuffer2)
213 brawcap_buffer_free(pPacketBuffer2);
214 if (pHandle)
215 brawcap_close(pHandle);
216
217 return retVal;
218}
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_WARNING_DEMO_MODE
The operation was not executed due to demo mode limitations.
Definition: brawcap_types_shared.h:252
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_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: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_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:774
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: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_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.