-
Notifications
You must be signed in to change notification settings - Fork 2k
How to build a packet in GNRC (to be continued)
This document briefly describes how to build packet snips and construct a packet in the GNRC network stack of RIOT. Also, it briefly shows the packet structure in GNRC, i.e., how a packet is build by linking up several packet snips.
Assuming that you are in a network stack that has 3 layers, as shown below, and you are now at the top layer (LAYER_3) for creating a new packet for sending to another node.
For instance, for creating a new packet snip that has a content of ''AAA'' at layer_3, you can use gnrc_pktbuf_add() function as below:
gnrc_pktsnip_t* pkt; /* the packet snip pointer */
char raw_data[3] = {'A', 'A', 'A'};
pkt = gnrc_pktbuf_add(NULL, raw_data, sizeof(raw_data) , GNRC_NETTYPE_LAYER_3);
When you want to create a new packet, you need to set the first input-parameter as NULL; the second and the third input-parameters are the pointer to the data and the size of the data content; the final input-parameter is the network type. The return of the function is the pointer to the packet part that represents the new gnrc_pktsnip_t. Now, the packet is composed of only one packet snip, as shown in the following figure.
Assuming that the packet created at LAYER_3 has been pushed to LAYER_2 (i.e., the pointer pkt has been relayed to LAYER_2), and, in case that you are now at LAYER_2 for adding a LAYER_2 header to the packet for continuing constructing the packet. To doing this, you use gnrc_pktbuf_add() as below:
char ly2head[4] = {'B','B','B','B'}; /* the layer_2 header */
pkt = gnrc_pktbuf_add(pkt, ly2head, sizeof(ly2head) , GNRC_NETTYPE_LAYER_2);
Now, the packet is composed of two packet snips, as shown in the following figure.
Similarly, when this packet is further pushed to layer_1, the protocol located at layer_1 will use gnrc_pktbuf_add() to add its own header to the packet, then, the complete packet may look like in the following figure,
char ly1head[2] = {'C','C'}; /* the layer_1 header */
pkt = gnrc_pktbuf_add(pkt, ly1head, sizeof(ly1head) , GNRC_NETTYPE_LAYER_1);
When the packet is sent out and if everything is correct, the receiver will receive the packet that has a content of "CCBBBBAAA".