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