bRAWcap 1.1.0
b-plus Technologies - Ethernet Performance Transmitter Receiver
Loading...
Searching...
No Matches
05_receive_simple_packet_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 05_receive_simple_packet_receiver_f.c
3 *
4 * @brief bRAWcap Example - Demonstrates how to use bRAWcap to receive separated packets.
5 *
6 * It shows how to:
7 * - Open a handle to the first available adapter
8 * - Create a bRAWcap packet object
9 * - Receive single packets from the adapter
10 * - Access received packet data.
11 *
12 * This example makes use of C only.
13 *
14 * @version 1.1
15 *
16 * @date 2023-03-30
17 *
18 * HISTORY:
19 * - 2023-03-30: Initial version
20 * - 2025-10-08: Updated to new get packet payload functions (@ref brawcap_packet_payload_get_v2)
21 *
22 * @copyright
23 * <b> © 2021 - b-plus technologies GmbH. All rights reserved!</b>
24 *
25 * All rights exclusively reserved for b-plus GmbH, unless expressly otherwise agreed.
26 *
27 * Redistribution in source or any other form, with or without modification, is not permitted.
28 *
29 * You may use this code under the according license terms of b-plus.
30 * Please contact b-plus at services@b-plus.com to get the appropriate terms and conditions.
31 */
32// Include bRAWcap
33#include "libbrawcap.h"
34
35// C STD
36#include <stdio.h> // for printf
37
38int main(int argc, char** argv)
39{
40 // Set console title
41 SetConsoleTitleA("bRAWcap Example - Simple Packet Receiver");
42
43 // Some generic helper locals...
44 int retVal = 0;
45 unsigned char demoModeLogged = 0;
46 unsigned long long packetCounter = 0;
47
48 // Here we will store the status of any bRAWcap function
50
51 // Here we store the number of available bRAWcap adapters
52 brawcap_adapter_count_t numberAdapters = 0;
53 // This will contain our bRAWcap handle to the adapter we want to receive from
54 brawcap_handle_t* pHandle = 0;
55 // Our local packet for storing received data
56 brawcap_packet_t* pPacket = 0;
57
58 do
59 {
60 // First check if we have any bRAWcap adapters available.
61 // --> This function can only fail if we did something wrong.
62 // But we are sure that we did everything right...
63 // therefore we do not check it�s status.
64 brawcap_adapter_list_count(&numberAdapters);
65
66 // No adapters... nothing do receive from... lets exit ...
67 if (!numberAdapters)
68 {
69 printf("[WARNING] No bRAWcap adapter available... Will stop now.");
70 break;
71 }
72
73 // Get the name of the first adapter.
74 // To not overload the example with complex stuff,
75 // we always go for the first available adapter.
76 brawcap_adapter_name_t name = { '\0' };
78 {
79 printf("[ERROR] Unexpected status while retrieving adapter name: %d", brawcap_last_status());
80 retVal = -1;
81 break;
82 }
83
84 // Open a handle to the adapter by using the retrieved name.
85 if (!BRAWCAP_SUCCESS(brawcap_open(name, &pHandle)))
86 {
87 printf("[ERROR] Unexpected status while opening handle: %d", brawcap_last_status());
88 retVal = -1;
89 break;
90 }
91
92 // Create a packet object
93 // For simplicity and to make sure that we get all packets,
94 // we set the payload size to the max supported value.
96 {
97 printf("[ERROR] Unexpected status while creating packet: %d", brawcap_last_status());
98 retVal = -1;
99 break;
100 }
101
102 // Some local variables to buffer the packet data, later on.
103 brawcap_packet_size_t captureLength = 0;
104 brawcap_packet_size_t lengthOnWire = 0;
105 brawcap_timestamp_t* pTimestamp = 0;
106 UINT64 timestamp_sec = 0;
107 UINT32 timestamp_ns = 0;
108 const unsigned char* pPayload = 0;
109
110 // Our receive loop, we stay in here until we should stop receiving.
111 while(1)
112 {
113 // Now lets see if we have received a packet...
114 status = brawcap_rx_packet(pHandle, pPacket);
115
116 // Seems like we had success and got some packet data to look at. :-)
117 if(BRAWCAP_SUCCESS(status))
118 {
119 // Check if we made it out of the demo limitation... :-D
120 // ... notify the user and go ahead with receiving.
121 if (demoModeLogged)
122 {
123 printf("[NOTICE] DEMO MODE: Limitation period elapsed, receiving is available again.\n");
124 demoModeLogged = 0;
125 }
126
127 // --> The bRAWcap functions below can only fail if we did something wrong.
128 // But we are sure that we did everything right...
129 // therefore we do not check their status.
130 brawcap_packet_length_on_wire_get(pPacket, &lengthOnWire);
131 brawcap_packet_payload_get_v2(pPacket, &pPayload, &captureLength);
132 brawcap_packet_timestamp_get(pPacket, &pTimestamp);
133 brawcap_timestamp_value_ns_get(pTimestamp, &timestamp_sec, &timestamp_ns);
134
135 // Show the user what we received
136 printf("%llu. Packet:\n", ++packetCounter);
137 printf(" - Capture Length: %04u Bytes\n", captureLength);
138 printf(" - Length On Wire: %04u Bytes\n", lengthOnWire);
139 printf(" - Timestamp: %llu %09u ns\n", timestamp_sec, timestamp_ns);
140 printf(" - Payload: ");
141 // Do some nice formatting stuff, to improve readability...
142 UINT32 byteCounter = 1;
143 for (const unsigned char* pByte = pPayload; byteCounter <= captureLength; ++byteCounter)
144 {
145 printf("%02X ", (unsigned char)*pByte++);
146 if (!(byteCounter % 16)) printf("\n ");
147 else if(!(byteCounter % 8)) printf(" ");
148 }
149 printf("\n\n");
150 }
151 // Hm... we got a very silent connection...
152 // ... lets try again...
153 else if (status == BRAWCAP_STATUS_INFO_NO_DATA)
154 continue;
155 // Sad but we do not have a bRAWcap license for this feature... yet. :-(
156 // Notify our user about this sad information and
157 // wait some time to check if we are allowed to receive again...
158 else if (status == BRAWCAP_STATUS_WARNING_DEMO_MODE)
159 {
160 if (!demoModeLogged)
161 {
162 demoModeLogged = 1;
163 printf("[WARNING] DEMO MODE: Receiving not available.\n");
164 }
165 Sleep(1000);
166 }
167 // Ok... Something strange happened here... :-O
168 // Better stop now if we do not know how to handle it.
169 else
170 {
171 printf("[ERROR] Unexpected status while receiving packet: %d", brawcap_last_status());
172 retVal = -1;
173 break;
174 }
175 }
176 }while(0);
177
178 // We do not want to produce any memory leaks...
179 // ... so always cleanup what we have created
180 // --> The functions here can only fail if we did something wrong.
181 // But we are sure that we did everything right...
182 // therefore we do not check their status.
183 if (pPacket)
184 brawcap_packet_free(pPacket);
185 if (pHandle)
186 brawcap_close(pHandle);
187
188 return retVal;
189}
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_INFO_NO_DATA
Indicates that a function returns without any data.
Definition: brawcap_types_shared.h:329
@ 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_packet_free(brawcap_packet_t *pPacket)
Frees the specified packet. When this function is called the specified packet becomes invalid and it´...
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.