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 
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 
62 #define GNRC_NETIF_HDR_FLAGS_BROADCAST (0x80)
63 
75 #define GNRC_NETIF_HDR_FLAGS_MULTICAST (0x40)
76 
90 #define GNRC_NETIF_HDR_FLAGS_MORE_DATA (0x10)
91 
101 typedef struct {
102  uint8_t src_l2addr_len;
103  uint8_t dst_l2addr_len;
105  uint8_t flags;
106  uint8_t lqi;
107  int16_t rssi;
109 
117 static inline void gnrc_netif_hdr_init(gnrc_netif_hdr_t *hdr, uint8_t src_l2addr_len,
118  uint8_t dst_l2addr_len)
119 {
120  hdr->src_l2addr_len = src_l2addr_len;
121  hdr->dst_l2addr_len = dst_l2addr_len;
122  hdr->if_pid = KERNEL_PID_UNDEF;
123  hdr->rssi = 0;
124  hdr->lqi = 0;
125  hdr->flags = 0;
126 }
127 
136 static inline size_t gnrc_netif_hdr_sizeof(const gnrc_netif_hdr_t *hdr)
137 {
138  return sizeof(gnrc_netif_hdr_t) + hdr->src_l2addr_len + hdr->dst_l2addr_len;
139 }
140 
149 static inline uint8_t *gnrc_netif_hdr_get_src_addr(const gnrc_netif_hdr_t *hdr)
150 {
151  return ((uint8_t *)(hdr + 1));
152 }
153 
162  const uint8_t *addr,
163  uint8_t addr_len)
164 {
165  if (addr_len != hdr->src_l2addr_len) {
166  return;
167  }
168 
169  memcpy(((uint8_t *)(hdr + 1)), addr, addr_len);
170 }
171 
172 
181 static inline uint8_t *gnrc_netif_hdr_get_dst_addr(const gnrc_netif_hdr_t *hdr)
182 {
183  return (((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len);
184 }
185 
194  const uint8_t *addr,
195  uint8_t addr_len)
196 {
197  if (addr_len != hdr->dst_l2addr_len) {
198  return;
199  }
200 
201  memcpy(((uint8_t *)(hdr + 1)) + hdr->src_l2addr_len, addr, addr_len);
202 }
203 
204 #if defined(MODULE_GNRC_IPV6) || defined(DOXYGEN)
205 
222 static inline int gnrc_netif_hdr_ipv6_iid_from_src(const gnrc_netif_t *netif,
223  const gnrc_netif_hdr_t *hdr,
224  eui64_t *iid)
225 {
226  return gnrc_netif_ipv6_iid_from_addr(netif,
228  hdr->src_l2addr_len,
229  iid);
230 }
231 
249 static inline int gnrc_netif_hdr_ipv6_iid_from_dst(const gnrc_netif_t *netif,
250  const gnrc_netif_hdr_t *hdr,
251  eui64_t *iid)
252 {
253  return gnrc_netif_ipv6_iid_from_addr(netif,
255  hdr->dst_l2addr_len,
256  iid);
257 }
258 #else /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
259 #define gnrc_netif_hdr_ipv6_iid_from_src(netif, hdr, iid) (-ENOTSUP);
260 #define gnrc_netif_hdr_ipv6_iid_from_dst(netif, hdr, iid) (-ENOTSUP);
261 #endif /* defined(MODULE_GNRC_IPV6) || defined(DOXYGEN) */
262 
277 gnrc_pktsnip_t *gnrc_netif_hdr_build(const uint8_t *src, uint8_t src_len,
278  const uint8_t *dst, uint8_t dst_len);
279 
292 {
293  assert(hdr != NULL);
294  return gnrc_netif_get_by_pid(hdr->if_pid);
295 }
296 
305  const gnrc_netif_t *netif)
306 {
307  hdr->if_pid = (netif != NULL) ? netif->pid : KERNEL_PID_UNDEF;
308 }
309 
316 
326 
337 int gnrc_netif_hdr_get_dstaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
338 
349 int gnrc_netif_hdr_get_srcaddr(gnrc_pktsnip_t* pkt, uint8_t** pointer_to_addr);
350 
351 #ifdef __cplusplus
352 }
353 #endif
354 
355 #endif /* NET_GNRC_NETIF_HDR_H */
356 
kernel_pid_t
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:125
gnrc_netif_hdr_set_netif
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:304
gnrc_netif_hdr_get_src_addr
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:149
gnrc_netif_hdr_t
Generic network interface header.
Definition: hdr.h:101
assert
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:104
gnrc_netif_hdr_t::lqi
uint8_t lqi
lqi of received packet (optional)
Definition: hdr.h:106
gnrc_netif_hdr_sizeof
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:136
gnrc_netif_hdr_t::if_pid
kernel_pid_t if_pid
PID of network interface.
Definition: hdr.h:104
gnrc_netif_hdr_get_srcaddr
int gnrc_netif_hdr_get_srcaddr(gnrc_pktsnip_t *pkt, uint8_t **pointer_to_addr)
Extract the source address out of a gnrc packet.
gnrc_netif_hdr_get_netif
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:291
gnrc_netif_hdr_init
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:117
gnrc_netif_hdr_set_dst_addr
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:193
gnrc_netif_t
Representation of a network interface.
Definition: netif.h:115
gnrc_netif_hdr_get_flag
uint8_t gnrc_netif_hdr_get_flag(gnrc_pktsnip_t *pkt)
Fetch the netif header flags of a gnrc packet.
pkt.h
General definitions for network packets and their helper functions.
gnrc_netif_t::pid
kernel_pid_t pid
PID of the network interface's thread.
Definition: netif.h:188
gnrc_netif_hdr_get_dst_addr
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:181
gnrc_netif_hdr_get_dstaddr
int gnrc_netif_hdr_get_dstaddr(gnrc_pktsnip_t *pkt, uint8_t **pointer_to_addr)
Extract the destination address out of a gnrc packet.
gnrc_netif_hdr_build
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.
gnrc_netif_get_by_pid
gnrc_netif_t * gnrc_netif_get_by_pid(kernel_pid_t pid)
Get network interface by PID.
pktbuf.h
Interface definition for the global network buffer. Network devices and layers can allocate space for...
gnrc_netif_hdr_ipv6_iid_from_dst
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:249
gnrc_netif_hdr_t::rssi
int16_t rssi
rssi of received packet in dBm (optional)
Definition: hdr.h:107
gnrc_netif_hdr_t::src_l2addr_len
uint8_t src_l2addr_len
length of l2 source address in byte
Definition: hdr.h:102
eui64_t
Data type to represent an EUI-64.
Definition: eui64.h:55
netif.h
Definition for GNRC's network interfaces.
gnrc_netif_hdr_print
void gnrc_netif_hdr_print(gnrc_netif_hdr_t *hdr)
Outputs a generic interface header to stdout.
gnrc_netif_hdr_t::flags
uint8_t flags
flags as defined above
Definition: hdr.h:105
gnrc_netif_hdr_t::dst_l2addr_len
uint8_t dst_l2addr_len
length of l2 destination address in byte
Definition: hdr.h:103
gnrc_netif_hdr_ipv6_iid_from_src
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:222
gnrc_pktsnip
Type to represent parts (either headers or payload) of a packet, called snips.
Definition: pkt.h:108
errno.h
KERNEL_PID_UNDEF
#define KERNEL_PID_UNDEF
Canonical identifier for an invalid PID.
Definition: sched.h:105
gnrc_netif_hdr_set_src_addr
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:161