Loading...
Searching...
No Matches
hdr.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Freie Universität Berlin
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
9#pragma once
10
22
23#include <errno.h>
24#include <string.h>
25#include <stdint.h>
26
27#include "compiler_hints.h"
28#include "net/gnrc/netif/internal.h"
29#include "net/gnrc/pkt.h"
30#include "net/gnrc/pktbuf.h"
31#include "net/gnrc/netif.h"
32#include "time_units.h"
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
42#define GNRC_NETIF_HDR_L2ADDR_MAX_LEN (8)
43
48#define GNRC_NETIF_HDR_L2ADDR_PRINT_LEN (GNRC_NETIF_HDR_L2ADDR_MAX_LEN * 3)
49
55#define GNRC_NETIF_HDR_NO_RSSI (INT16_MIN)
61#define GNRC_NETIF_HDR_NO_LQI (0)
62
76#define GNRC_NETIF_HDR_FLAGS_BROADCAST (0x80)
77
89#define GNRC_NETIF_HDR_FLAGS_MULTICAST (0x40)
90
104#define GNRC_NETIF_HDR_FLAGS_MORE_DATA (0x10)
105
115#define GNRC_NETIF_HDR_FLAGS_TIMESTAMP (0x08)
119
126typedef struct {
130 uint8_t flags;
134 uint8_t lqi;
138 int16_t rssi;
139#if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP) || defined(DOXYGEN)
155 uint64_t timestamp;
156#endif /* MODULE_GNRC_NETIF_TIMESTAMP */
158
166static inline void gnrc_netif_hdr_init(gnrc_netif_hdr_t *hdr, uint8_t src_l2addr_len,
167 uint8_t dst_l2addr_len)
168{
169 hdr->src_l2addr_len = src_l2addr_len;
170 hdr->dst_l2addr_len = dst_l2addr_len;
174 hdr->flags = 0;
175}
176
185static inline size_t gnrc_netif_hdr_sizeof(const gnrc_netif_hdr_t *hdr)
186{
187 return sizeof(gnrc_netif_hdr_t) + hdr->src_l2addr_len + hdr->dst_l2addr_len;
188}
189
198static inline uint8_t *gnrc_netif_hdr_get_src_addr(const gnrc_netif_hdr_t *hdr)
199{
200 return ((uint8_t *)(hdr + 1));
201}
202
210ACCESS(read_only, 2, 3)
212 const uint8_t *addr,
213 uint8_t addr_len)
214{
215 if (addr_len != hdr->src_l2addr_len) {
216 return;
217 }
218
219 memcpy(((uint8_t *)(hdr + 1)), addr, addr_len);
220}
221
230static inline uint8_t *gnrc_netif_hdr_get_dst_addr(const gnrc_netif_hdr_t *hdr)
231{
232 return (((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len);
233}
234
242ACCESS(read_only, 2, 3)
244 const uint8_t *addr,
245 uint8_t addr_len)
246{
247 if (addr_len != hdr->dst_l2addr_len) {
248 return;
249 }
250
251 memcpy(((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len, addr, addr_len);
252}
253
264 uint64_t timestamp)
265{
266 (void)hdr;
267 (void)timestamp;
268#if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP)
269 hdr->timestamp = timestamp;
271#endif
272}
273
285 uint64_t *dest)
286{
287 (void)hdr;
288 (void)dest;
289#if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP)
291 *dest = hdr->timestamp;
292 return 0;
293 }
294#endif
295 return -1;
296}
297
298#if defined(MODULE_GNRC_IPV6) || defined(DOXYGEN)
316static inline int gnrc_netif_hdr_ipv6_iid_from_src(const gnrc_netif_t *netif,
317 const gnrc_netif_hdr_t *hdr,
318 eui64_t *iid)
319{
320 return gnrc_netif_ipv6_iid_from_addr(netif,
322 hdr->src_l2addr_len,
323 iid);
324}
325
343static inline int gnrc_netif_hdr_ipv6_iid_from_dst(const gnrc_netif_t *netif,
344 const gnrc_netif_hdr_t *hdr,
345 eui64_t *iid)
346{
347 return gnrc_netif_ipv6_iid_from_addr(netif,
349 hdr->dst_l2addr_len,
350 iid);
351}
352#else /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
353#define gnrc_netif_hdr_ipv6_iid_from_src(netif, hdr, iid) (-ENOTSUP);
354#define gnrc_netif_hdr_ipv6_iid_from_dst(netif, hdr, iid) (-ENOTSUP);
355#endif /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
356
371ACCESS(read_only, 1, 2)
372ACCESS(read_only, 3, 4)
373gnrc_pktsnip_t *gnrc_netif_hdr_build(const uint8_t *src, uint8_t src_len,
374 const uint8_t *dst, uint8_t dst_len);
375
388{
389 assert(hdr != NULL);
390 return gnrc_netif_get_by_pid(hdr->if_pid);
391}
392
401 const gnrc_netif_t *netif)
402{
403 hdr->if_pid = (netif != NULL) ? netif->pid : KERNEL_PID_UNDEF;
404}
405
412
422
433int gnrc_netif_hdr_get_dstaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
434
445int gnrc_netif_hdr_get_srcaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
446
447#ifdef __cplusplus
448}
449#endif
450
#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:198
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:185
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:166
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:211
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:284
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:230
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:316
#define GNRC_NETIF_HDR_FLAGS_TIMESTAMP
Indicate presence of a valid timestamp.
Definition hdr.h:115
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:400
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:387
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:343
#define GNRC_NETIF_HDR_NO_RSSI
Special value to indicate that no RSSI value is present.
Definition hdr.h:55
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:61
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:243
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:263
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:126
kernel_pid_t if_pid
PID of network interface.
Definition hdr.h:129
int16_t rssi
RSSI of received packet or GNRC_NETIF_HDR_NO_RSSI.
Definition hdr.h:138
uint8_t dst_l2addr_len
length of l2 destination address in byte
Definition hdr.h:128
uint8_t src_l2addr_len
length of l2 source address in byte
Definition hdr.h:127
uint64_t timestamp
Timestamp of reception in nanoseconds since epoch.
Definition hdr.h:155
uint8_t flags
flags as defined above
Definition hdr.h:130
uint8_t lqi
LQI of received packet or GNRC_NETIF_HDR_NO_LQI.
Definition hdr.h:134
Representation of a network interface.
Definition netif.h:140
kernel_pid_t pid
PID of the network interface's thread.
Definition netif.h:228
Utility header providing time unit defines.
Data type to represent an EUI-64.
Definition eui64.h:55