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 
20 #include <errno.h>
21 #include <string.h>
22 #include <stdint.h>
23 
24 #include "net/gnrc/netif/internal.h"
25 #include "net/gnrc/pkt.h"
26 #include "net/gnrc/pktbuf.h"
27 #include "net/gnrc/netif.h"
28 #include "time_units.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
38 #define GNRC_NETIF_HDR_L2ADDR_MAX_LEN (8)
39 
44 #define GNRC_NETIF_HDR_L2ADDR_PRINT_LEN (GNRC_NETIF_HDR_L2ADDR_MAX_LEN * 3)
45 
51 #define GNRC_NETIF_HDR_NO_RSSI (INT16_MIN)
57 #define GNRC_NETIF_HDR_NO_LQI (0)
58 
72 #define GNRC_NETIF_HDR_FLAGS_BROADCAST (0x80)
73 
85 #define GNRC_NETIF_HDR_FLAGS_MULTICAST (0x40)
86 
100 #define GNRC_NETIF_HDR_FLAGS_MORE_DATA (0x10)
101 
111 #define GNRC_NETIF_HDR_FLAGS_TIMESTAMP (0x08)
122 typedef struct {
123  uint8_t src_l2addr_len;
124  uint8_t dst_l2addr_len;
126  uint8_t flags;
130  uint8_t lqi;
134  int16_t rssi;
135 #if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP) || defined(DOXYGEN)
151  uint64_t timestamp;
152 #endif /* MODULE_GNRC_NETIF_TIMESTAMP */
154 
162 static inline void gnrc_netif_hdr_init(gnrc_netif_hdr_t *hdr, uint8_t src_l2addr_len,
163  uint8_t dst_l2addr_len)
164 {
165  hdr->src_l2addr_len = src_l2addr_len;
166  hdr->dst_l2addr_len = dst_l2addr_len;
167  hdr->if_pid = KERNEL_PID_UNDEF;
169  hdr->lqi = GNRC_NETIF_HDR_NO_LQI;
170  hdr->flags = 0;
171 }
172 
181 static inline size_t gnrc_netif_hdr_sizeof(const gnrc_netif_hdr_t *hdr)
182 {
183  return sizeof(gnrc_netif_hdr_t) + hdr->src_l2addr_len + hdr->dst_l2addr_len;
184 }
185 
194 static inline uint8_t *gnrc_netif_hdr_get_src_addr(const gnrc_netif_hdr_t *hdr)
195 {
196  return ((uint8_t *)(hdr + 1));
197 }
198 
207  const uint8_t *addr,
208  uint8_t addr_len)
209 {
210  if (addr_len != hdr->src_l2addr_len) {
211  return;
212  }
213 
214  memcpy(((uint8_t *)(hdr + 1)), addr, addr_len);
215 }
216 
225 static inline uint8_t *gnrc_netif_hdr_get_dst_addr(const gnrc_netif_hdr_t *hdr)
226 {
227  return (((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len);
228 }
229 
238  const uint8_t *addr,
239  uint8_t addr_len)
240 {
241  if (addr_len != hdr->dst_l2addr_len) {
242  return;
243  }
244 
245  memcpy(((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len, addr, addr_len);
246 }
247 
258  uint64_t timestamp)
259 {
260  (void)hdr;
261  (void)timestamp;
262 #if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP)
263  hdr->timestamp = timestamp;
265 #endif
266 }
267 
278 static inline int gnrc_netif_hdr_get_timestamp(const gnrc_netif_hdr_t *hdr,
279  uint64_t *dest)
280 {
281  (void)hdr;
282  (void)dest;
283 #if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP)
285  *dest = hdr->timestamp;
286  return 0;
287  }
288 #endif
289  return -1;
290 }
291 
292 #if defined(MODULE_GNRC_IPV6) || defined(DOXYGEN)
310 static inline int gnrc_netif_hdr_ipv6_iid_from_src(const gnrc_netif_t *netif,
311  const gnrc_netif_hdr_t *hdr,
312  eui64_t *iid)
313 {
314  return gnrc_netif_ipv6_iid_from_addr(netif,
316  hdr->src_l2addr_len,
317  iid);
318 }
319 
337 static inline int gnrc_netif_hdr_ipv6_iid_from_dst(const gnrc_netif_t *netif,
338  const gnrc_netif_hdr_t *hdr,
339  eui64_t *iid)
340 {
341  return gnrc_netif_ipv6_iid_from_addr(netif,
343  hdr->dst_l2addr_len,
344  iid);
345 }
346 #else /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
347 #define gnrc_netif_hdr_ipv6_iid_from_src(netif, hdr, iid) (-ENOTSUP);
348 #define gnrc_netif_hdr_ipv6_iid_from_dst(netif, hdr, iid) (-ENOTSUP);
349 #endif /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
350 
365 gnrc_pktsnip_t *gnrc_netif_hdr_build(const uint8_t *src, uint8_t src_len,
366  const uint8_t *dst, uint8_t dst_len);
367 
380 {
381  assert(hdr != NULL);
382  return gnrc_netif_get_by_pid(hdr->if_pid);
383 }
384 
393  const gnrc_netif_t *netif)
394 {
395  hdr->if_pid = (netif != NULL) ? netif->pid : KERNEL_PID_UNDEF;
396 }
397 
404 
414 
425 int gnrc_netif_hdr_get_dstaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
426 
437 int gnrc_netif_hdr_get_srcaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
438 
439 #ifdef __cplusplus
440 }
441 #endif
442 
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:143
Definition for GNRC's network interfaces.
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:135
#define KERNEL_PID_UNDEF
Canonical identifier for an invalid PID.
Definition: sched.h:106
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 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:181
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:162
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:206
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:278
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:310
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:225
#define GNRC_NETIF_HDR_FLAGS_TIMESTAMP
Indicate presence of a valid timestamp.
Definition: hdr.h:111
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.
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:392
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:194
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:337
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:379
#define GNRC_NETIF_HDR_NO_RSSI
Special value to indicate that no RSSI value is present.
Definition: hdr.h:51
void gnrc_netif_hdr_print(gnrc_netif_hdr_t *hdr)
Outputs a generic interface header to stdout.
#define GNRC_NETIF_HDR_NO_LQI
Special value to indicate that no LQI value is present.
Definition: hdr.h:57
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:237
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:257
gnrc_netif_t * gnrc_netif_get_by_pid(kernel_pid_t pid)
Get network interface by PID.
General definitions for network packets and their helper functions.
Interface definition for the global network buffer.
Generic network interface header.
Definition: hdr.h:122
kernel_pid_t if_pid
PID of network interface.
Definition: hdr.h:125
int16_t rssi
RSSI of received packet or GNRC_NETIF_HDR_NO_RSSI.
Definition: hdr.h:134
uint8_t dst_l2addr_len
length of l2 destination address in byte
Definition: hdr.h:124
uint8_t src_l2addr_len
length of l2 source address in byte
Definition: hdr.h:123
uint64_t timestamp
Timestamp of reception in nanoseconds since epoch.
Definition: hdr.h:151
uint8_t flags
flags as defined above
Definition: hdr.h:126
uint8_t lqi
LQI of received packet or GNRC_NETIF_HDR_NO_LQI.
Definition: hdr.h:130
Representation of a network interface.
Definition: netif.h:129
kernel_pid_t pid
PID of the network interface's thread.
Definition: netif.h:217
Type to represent parts (either headers or payload) of a packet, called snips.
Definition: pkt.h:105
Utility header providing time unit defines.
Data type to represent an EUI-64.
Definition: eui64.h:52