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 
21 #ifndef NET_GNRC_NETIF_HDR_H
22 #define NET_GNRC_NETIF_HDR_H
23 
24 #include <errno.h>
25 #include <string.h>
26 #include <stdint.h>
27 
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
35 extern "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)
126 typedef struct {
127  uint8_t src_l2addr_len;
128  uint8_t dst_l2addr_len;
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 
166 static 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;
171  hdr->if_pid = KERNEL_PID_UNDEF;
173  hdr->lqi = GNRC_NETIF_HDR_NO_LQI;
174  hdr->flags = 0;
175 }
176 
185 static 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 
198 static inline uint8_t *gnrc_netif_hdr_get_src_addr(const gnrc_netif_hdr_t *hdr)
199 {
200  return ((uint8_t *)(hdr + 1));
201 }
202 
211  const uint8_t *addr,
212  uint8_t addr_len)
213 {
214  if (addr_len != hdr->src_l2addr_len) {
215  return;
216  }
217 
218  memcpy(((uint8_t *)(hdr + 1)), addr, addr_len);
219 }
220 
229 static inline uint8_t *gnrc_netif_hdr_get_dst_addr(const gnrc_netif_hdr_t *hdr)
230 {
231  return (((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len);
232 }
233 
242  const uint8_t *addr,
243  uint8_t addr_len)
244 {
245  if (addr_len != hdr->dst_l2addr_len) {
246  return;
247  }
248 
249  memcpy(((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len, addr, addr_len);
250 }
251 
262  uint64_t timestamp)
263 {
264  (void)hdr;
265  (void)timestamp;
266 #if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP)
267  hdr->timestamp = timestamp;
269 #endif
270 }
271 
282 static inline int gnrc_netif_hdr_get_timestamp(const gnrc_netif_hdr_t *hdr,
283  uint64_t *dest)
284 {
285  (void)hdr;
286  (void)dest;
287 #if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP)
289  *dest = hdr->timestamp;
290  return 0;
291  }
292 #endif
293  return -1;
294 }
295 
296 #if defined(MODULE_GNRC_IPV6) || defined(DOXYGEN)
314 static inline int gnrc_netif_hdr_ipv6_iid_from_src(const gnrc_netif_t *netif,
315  const gnrc_netif_hdr_t *hdr,
316  eui64_t *iid)
317 {
318  return gnrc_netif_ipv6_iid_from_addr(netif,
320  hdr->src_l2addr_len,
321  iid);
322 }
323 
341 static inline int gnrc_netif_hdr_ipv6_iid_from_dst(const gnrc_netif_t *netif,
342  const gnrc_netif_hdr_t *hdr,
343  eui64_t *iid)
344 {
345  return gnrc_netif_ipv6_iid_from_addr(netif,
347  hdr->dst_l2addr_len,
348  iid);
349 }
350 #else /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
351 #define gnrc_netif_hdr_ipv6_iid_from_src(netif, hdr, iid) (-ENOTSUP);
352 #define gnrc_netif_hdr_ipv6_iid_from_dst(netif, hdr, iid) (-ENOTSUP);
353 #endif /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
354 
369 gnrc_pktsnip_t *gnrc_netif_hdr_build(const uint8_t *src, uint8_t src_len,
370  const uint8_t *dst, uint8_t dst_len);
371 
384 {
385  assert(hdr != NULL);
386  return gnrc_netif_get_by_pid(hdr->if_pid);
387 }
388 
397  const gnrc_netif_t *netif)
398 {
399  hdr->if_pid = (netif != NULL) ? netif->pid : KERNEL_PID_UNDEF;
400 }
401 
408 
418 
429 int gnrc_netif_hdr_get_dstaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
430 
441 int gnrc_netif_hdr_get_srcaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
442 
443 #ifdef __cplusplus
444 }
445 #endif
446 
447 #endif /* NET_GNRC_NETIF_HDR_H */
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:136
Definition for GNRC's network interfaces.
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:139
#define KERNEL_PID_UNDEF
Canonical identifier for an invalid PID.
Definition: sched.h:110
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: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:210
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:282
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:314
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:229
#define GNRC_NETIF_HDR_FLAGS_TIMESTAMP
Indicate presence of a valid timestamp.
Definition: hdr.h:115
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:396
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 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:341
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:383
#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.
#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:241
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:261
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: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:135
kernel_pid_t pid
PID of the network interface's thread.
Definition: netif.h:226
Type to represent parts (either headers or payload) of a packet, called snips.
Definition: pkt.h:108
Utility header providing time unit defines.
Data type to represent an EUI-64.
Definition: eui64.h:55