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 
23 #include <errno.h>
24 #include <string.h>
25 #include <stdint.h>
26 
27 #include "net/gnrc/netif/internal.h"
28 #include "net/gnrc/pkt.h"
29 #include "net/gnrc/pktbuf.h"
30 #include "net/gnrc/netif.h"
31 #include "time_units.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
41 #define GNRC_NETIF_HDR_L2ADDR_MAX_LEN (8)
42 
47 #define GNRC_NETIF_HDR_L2ADDR_PRINT_LEN (GNRC_NETIF_HDR_L2ADDR_MAX_LEN * 3)
48 
54 #define GNRC_NETIF_HDR_NO_RSSI (INT16_MIN)
60 #define GNRC_NETIF_HDR_NO_LQI (0)
61 
75 #define GNRC_NETIF_HDR_FLAGS_BROADCAST (0x80)
76 
88 #define GNRC_NETIF_HDR_FLAGS_MULTICAST (0x40)
89 
103 #define GNRC_NETIF_HDR_FLAGS_MORE_DATA (0x10)
104 
114 #define GNRC_NETIF_HDR_FLAGS_TIMESTAMP (0x08)
125 typedef struct {
126  uint8_t src_l2addr_len;
127  uint8_t dst_l2addr_len;
129  uint8_t flags;
133  uint8_t lqi;
137  int16_t rssi;
138 #if IS_USED(MODULE_GNRC_NETIF_TIMESTAMP) || defined(DOXYGEN)
154  uint64_t timestamp;
155 #endif /* MODULE_GNRC_NETIF_TIMESTAMP */
157 
165 static inline void gnrc_netif_hdr_init(gnrc_netif_hdr_t *hdr, uint8_t src_l2addr_len,
166  uint8_t dst_l2addr_len)
167 {
168  hdr->src_l2addr_len = src_l2addr_len;
169  hdr->dst_l2addr_len = dst_l2addr_len;
170  hdr->if_pid = KERNEL_PID_UNDEF;
172  hdr->lqi = GNRC_NETIF_HDR_NO_LQI;
173  hdr->flags = 0;
174 }
175 
184 static inline size_t gnrc_netif_hdr_sizeof(const gnrc_netif_hdr_t *hdr)
185 {
186  return sizeof(gnrc_netif_hdr_t) + hdr->src_l2addr_len + hdr->dst_l2addr_len;
187 }
188 
197 static inline uint8_t *gnrc_netif_hdr_get_src_addr(const gnrc_netif_hdr_t *hdr)
198 {
199  return ((uint8_t *)(hdr + 1));
200 }
201 
210  const uint8_t *addr,
211  uint8_t addr_len)
212 {
213  if (addr_len != hdr->src_l2addr_len) {
214  return;
215  }
216 
217  memcpy(((uint8_t *)(hdr + 1)), addr, addr_len);
218 }
219 
228 static inline uint8_t *gnrc_netif_hdr_get_dst_addr(const gnrc_netif_hdr_t *hdr)
229 {
230  return (((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len);
231 }
232 
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 
281 static inline int gnrc_netif_hdr_get_timestamp(const gnrc_netif_hdr_t *hdr,
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)
313 static 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 
340 static 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 
368 gnrc_pktsnip_t *gnrc_netif_hdr_build(const uint8_t *src, uint8_t src_len,
369  const uint8_t *dst, uint8_t dst_len);
370 
383 {
384  assert(hdr != NULL);
385  return gnrc_netif_get_by_pid(hdr->if_pid);
386 }
387 
396  const gnrc_netif_t *netif)
397 {
398  hdr->if_pid = (netif != NULL) ? netif->pid : KERNEL_PID_UNDEF;
399 }
400 
407 
417 
428 int gnrc_netif_hdr_get_dstaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
429 
440 int gnrc_netif_hdr_get_srcaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
441 
442 #ifdef __cplusplus
443 }
444 #endif
445 
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:135
Definition for GNRC's network interfaces.
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:138
#define KERNEL_PID_UNDEF
Canonical identifier for an invalid PID.
Definition: sched.h:109
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:184
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:165
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:209
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 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
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:228
#define GNRC_NETIF_HDR_FLAGS_TIMESTAMP
Indicate presence of a valid timestamp.
Definition: hdr.h:114
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:395
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:197
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
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:382
#define GNRC_NETIF_HDR_NO_RSSI
Special value to indicate that no RSSI value is present.
Definition: hdr.h:54
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:60
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.
General definitions for network packets and their helper functions.
Interface definition for the global network buffer.
Generic network interface header.
Definition: hdr.h:125
kernel_pid_t if_pid
PID of network interface.
Definition: hdr.h:128
int16_t rssi
RSSI of received packet or GNRC_NETIF_HDR_NO_RSSI.
Definition: hdr.h:137
uint8_t dst_l2addr_len
length of l2 destination address in byte
Definition: hdr.h:127
uint8_t src_l2addr_len
length of l2 source address in byte
Definition: hdr.h:126
uint64_t timestamp
Timestamp of reception in nanoseconds since epoch.
Definition: hdr.h:154
uint8_t flags
flags as defined above
Definition: hdr.h:129
uint8_t lqi
LQI of received packet or GNRC_NETIF_HDR_NO_LQI.
Definition: hdr.h:133
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