Loading...
Searching...
No Matches
hdr.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 Freie Universität Berlin
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
19
20#include <errno.h>
21#include <string.h>
22#include <stdint.h>
23
24#include "compiler_hints.h"
25#include "net/gnrc/netif/internal.h"
26#include "net/gnrc/pkt.h"
27#include "net/gnrc/pktbuf.h"
28#include "net/gnrc/netif.h"
29#include "time_units.h"
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
39#define GNRC_NETIF_HDR_L2ADDR_MAX_LEN (8)
40
45#define GNRC_NETIF_HDR_L2ADDR_PRINT_LEN (GNRC_NETIF_HDR_L2ADDR_MAX_LEN * 3)
46
52#define GNRC_NETIF_HDR_NO_RSSI (INT16_MIN)
58#define GNRC_NETIF_HDR_NO_LQI (0)
59
73#define GNRC_NETIF_HDR_FLAGS_BROADCAST (0x80)
74
86#define GNRC_NETIF_HDR_FLAGS_MULTICAST (0x40)
87
101#define GNRC_NETIF_HDR_FLAGS_MORE_DATA (0x10)
102
112#define GNRC_NETIF_HDR_FLAGS_TIMESTAMP (0x08)
116
123typedef struct {
127 uint8_t flags;
131 uint8_t lqi;
135 int16_t rssi;
136#if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP) || defined(DOXYGEN)
152 uint64_t timestamp;
153#endif /* MODULE_GNRC_NETIF_TIMESTAMP */
155
163static inline void gnrc_netif_hdr_init(gnrc_netif_hdr_t *hdr, uint8_t src_l2addr_len,
164 uint8_t dst_l2addr_len)
165{
166 hdr->src_l2addr_len = src_l2addr_len;
167 hdr->dst_l2addr_len = dst_l2addr_len;
171 hdr->flags = 0;
172}
173
182static inline size_t gnrc_netif_hdr_sizeof(const gnrc_netif_hdr_t *hdr)
183{
184 return sizeof(gnrc_netif_hdr_t) + hdr->src_l2addr_len + hdr->dst_l2addr_len;
185}
186
195static inline uint8_t *gnrc_netif_hdr_get_src_addr(const gnrc_netif_hdr_t *hdr)
196{
197 return ((uint8_t *)(hdr + 1));
198}
199
207ACCESS(read_only, 2, 3)
209 const uint8_t *addr,
210 uint8_t addr_len)
211{
212 if (addr_len != hdr->src_l2addr_len) {
213 return;
214 }
215
216 memcpy(((uint8_t *)(hdr + 1)), addr, addr_len);
217}
218
227static inline uint8_t *gnrc_netif_hdr_get_dst_addr(const gnrc_netif_hdr_t *hdr)
228{
229 return (((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len);
230}
231
239ACCESS(read_only, 2, 3)
241 const uint8_t *addr,
242 uint8_t addr_len)
243{
244 if (addr_len != hdr->dst_l2addr_len) {
245 return;
246 }
247
248 memcpy(((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len, addr, addr_len);
249}
250
261 uint64_t timestamp)
262{
263 (void)hdr;
264 (void)timestamp;
265#if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP)
266 hdr->timestamp = timestamp;
268#endif
269}
270
282 uint64_t *dest)
283{
284 (void)hdr;
285 (void)dest;
286#if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP)
288 *dest = hdr->timestamp;
289 return 0;
290 }
291#endif
292 return -1;
293}
294
295#if defined(MODULE_GNRC_IPV6) || defined(DOXYGEN)
313static inline int gnrc_netif_hdr_ipv6_iid_from_src(const gnrc_netif_t *netif,
314 const gnrc_netif_hdr_t *hdr,
315 eui64_t *iid)
316{
317 return gnrc_netif_ipv6_iid_from_addr(netif,
319 hdr->src_l2addr_len,
320 iid);
321}
322
340static inline int gnrc_netif_hdr_ipv6_iid_from_dst(const gnrc_netif_t *netif,
341 const gnrc_netif_hdr_t *hdr,
342 eui64_t *iid)
343{
344 return gnrc_netif_ipv6_iid_from_addr(netif,
346 hdr->dst_l2addr_len,
347 iid);
348}
349#else /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
350#define gnrc_netif_hdr_ipv6_iid_from_src(netif, hdr, iid) (-ENOTSUP);
351#define gnrc_netif_hdr_ipv6_iid_from_dst(netif, hdr, iid) (-ENOTSUP);
352#endif /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
353
368ACCESS(read_only, 1, 2)
369ACCESS(read_only, 3, 4)
370gnrc_pktsnip_t *gnrc_netif_hdr_build(const uint8_t *src, uint8_t src_len,
371 const uint8_t *dst, uint8_t dst_len);
372
385{
386 assert(hdr != NULL);
387 return gnrc_netif_get_by_pid(hdr->if_pid);
388}
389
398 const gnrc_netif_t *netif)
399{
400 hdr->if_pid = (netif != NULL) ? netif->pid : KERNEL_PID_UNDEF;
401}
402
409
419
430int gnrc_netif_hdr_get_dstaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
431
442int gnrc_netif_hdr_get_srcaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
443
444#ifdef __cplusplus
445}
446#endif
447
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
Common macros and compiler attributes/pragmas configuration.
#define ACCESS(mode, ptr_idx, size_idx)
Emit an attribute (if supported by the compiler) that declares how a function will access its paramet...
Definition for GNRC's network interfaces.
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:134
#define KERNEL_PID_UNDEF
Canonical identifier for an invalid PID.
Definition sched.h:105
int gnrc_netif_hdr_get_srcaddr(gnrc_pktsnip_t *pkt, uint8_t **pointer_to_addr)
Extract the source address out of a gnrc packet.
uint8_t gnrc_netif_hdr_get_flag(gnrc_pktsnip_t *pkt)
Fetch the netif header flags of a gnrc packet.
static uint8_t * gnrc_netif_hdr_get_src_addr(const gnrc_netif_hdr_t *hdr)
Get the source address from the given header.
Definition hdr.h:195
static size_t gnrc_netif_hdr_sizeof(const gnrc_netif_hdr_t *hdr)
Get the size of the given generic network interface header.
Definition hdr.h:182
int gnrc_netif_hdr_get_dstaddr(gnrc_pktsnip_t *pkt, uint8_t **pointer_to_addr)
Extract the destination address out of a gnrc packet.
static void gnrc_netif_hdr_init(gnrc_netif_hdr_t *hdr, uint8_t src_l2addr_len, uint8_t dst_l2addr_len)
Initialize the given generic network interface header.
Definition hdr.h:163
static void gnrc_netif_hdr_set_src_addr(gnrc_netif_hdr_t *hdr, const uint8_t *addr, uint8_t addr_len)
Set the source address in the given header.
Definition hdr.h:208
static int gnrc_netif_hdr_get_timestamp(const gnrc_netif_hdr_t *hdr, uint64_t *dest)
Get the timestamp of the frame in nanoseconds since epoch.
Definition hdr.h:281
static uint8_t * gnrc_netif_hdr_get_dst_addr(const gnrc_netif_hdr_t *hdr)
Get the destination address from the given header.
Definition hdr.h:227
static int gnrc_netif_hdr_ipv6_iid_from_src(const gnrc_netif_t *netif, const gnrc_netif_hdr_t *hdr, eui64_t *iid)
Converts the source address of a given Generic network interface header to an IPv6 IID.
Definition hdr.h:313
#define GNRC_NETIF_HDR_FLAGS_TIMESTAMP
Indicate presence of a valid timestamp.
Definition hdr.h:112
static void gnrc_netif_hdr_set_netif(gnrc_netif_hdr_t *hdr, const gnrc_netif_t *netif)
Convenience function to set the interface of an interface header, given the network interface.
Definition hdr.h:397
static gnrc_netif_t * gnrc_netif_hdr_get_netif(const gnrc_netif_hdr_t *hdr)
Convenience function to get the corresponding interface struct for a given interface header.
Definition hdr.h:384
static int gnrc_netif_hdr_ipv6_iid_from_dst(const gnrc_netif_t *netif, const gnrc_netif_hdr_t *hdr, eui64_t *iid)
Converts the destination address of a given Generic network interface header to an IPv6 IID.
Definition hdr.h:340
#define GNRC_NETIF_HDR_NO_RSSI
Special value to indicate that no RSSI value is present.
Definition hdr.h:52
void gnrc_netif_hdr_print(gnrc_netif_hdr_t *hdr)
Outputs a generic interface header to stdout.
gnrc_pktsnip_t * gnrc_netif_hdr_build(const uint8_t *src, uint8_t src_len, const uint8_t *dst, uint8_t dst_len)
Builds a generic network interface header for sending and adds it to the packet buffer.
#define GNRC_NETIF_HDR_NO_LQI
Special value to indicate that no LQI value is present.
Definition hdr.h:58
static void gnrc_netif_hdr_set_dst_addr(gnrc_netif_hdr_t *hdr, const uint8_t *addr, uint8_t addr_len)
Set the destination address in the given header.
Definition hdr.h:240
static void gnrc_netif_hdr_set_timestamp(gnrc_netif_hdr_t *hdr, uint64_t timestamp)
Set the timestamp in the netif header.
Definition hdr.h:260
gnrc_netif_t * gnrc_netif_get_by_pid(kernel_pid_t pid)
Get network interface by PID.
struct gnrc_pktsnip gnrc_pktsnip_t
Type to represent parts (either headers or payload) of a packet, called snips.
General definitions for network packets and their helper functions.
Interface definition for the global network buffer.
Generic network interface header.
Definition hdr.h:123
kernel_pid_t if_pid
PID of network interface.
Definition hdr.h:126
int16_t rssi
RSSI of received packet or GNRC_NETIF_HDR_NO_RSSI.
Definition hdr.h:135
uint8_t dst_l2addr_len
length of l2 destination address in byte
Definition hdr.h:125
uint8_t src_l2addr_len
length of l2 source address in byte
Definition hdr.h:124
uint64_t timestamp
Timestamp of reception in nanoseconds since epoch.
Definition hdr.h:152
uint8_t flags
flags as defined above
Definition hdr.h:127
uint8_t lqi
LQI of received packet or GNRC_NETIF_HDR_NO_LQI.
Definition hdr.h:131
Representation of a network interface.
Definition netif.h:137
kernel_pid_t pid
PID of the network interface's thread.
Definition netif.h:225
Utility header providing time unit defines.
Data type to represent an EUI-64.
Definition eui64.h:52