gnrc_pktbuf: add gnrc_pktbuf_reverse_snips() helper function
This allows for a) testing the packet reversal properly in unittests b) use it in other places than `gnrc_ipv6`'s receive function
This commit is contained in:
parent
85733a7568
commit
ac54a2d2b2
@ -225,6 +225,24 @@ gnrc_pktsnip_t *gnrc_pktbuf_remove_snip(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *sni
|
|||||||
*/
|
*/
|
||||||
gnrc_pktsnip_t *gnrc_pktbuf_replace_snip(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *old, gnrc_pktsnip_t *add);
|
gnrc_pktsnip_t *gnrc_pktbuf_replace_snip(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *old, gnrc_pktsnip_t *add);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reverses snip order of a packet in a write-protected manner.
|
||||||
|
*
|
||||||
|
* This can be used to change the send/receive order of a packet (see
|
||||||
|
* @ref gnrc_pktsnip_t)
|
||||||
|
*
|
||||||
|
* @note @p pkt is released on failure.
|
||||||
|
*
|
||||||
|
* @param[in] pkt A packet. When this function fails (due to a full packet
|
||||||
|
* packet buffer) @p pkt will be released.
|
||||||
|
*
|
||||||
|
* @return The reversed version of @p pkt on success
|
||||||
|
* @return NULL, when there is not enough space in the packet buffer to reverse
|
||||||
|
* the packet in a write-protected manner. @p pkt is released in that
|
||||||
|
* case.
|
||||||
|
*/
|
||||||
|
gnrc_pktsnip_t *gnrc_pktbuf_reverse_snips(gnrc_pktsnip_t *pkt);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Duplicates pktsnip chain upto (including) a snip with the given type
|
* @brief Duplicates pktsnip chain upto (including) a snip with the given type
|
||||||
* as a continuous snip.
|
* as a continuous snip.
|
||||||
|
|||||||
@ -86,5 +86,28 @@ gnrc_pktsnip_t *gnrc_pktbuf_replace_snip(gnrc_pktsnip_t *pkt,
|
|||||||
return pkt;
|
return pkt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gnrc_pktsnip_t *gnrc_pktbuf_reverse_snips(gnrc_pktsnip_t *pkt)
|
||||||
|
{
|
||||||
|
gnrc_pktsnip_t *reversed = NULL, *ptr = pkt;
|
||||||
|
|
||||||
|
while (ptr != NULL) {
|
||||||
|
gnrc_pktsnip_t *next;
|
||||||
|
|
||||||
|
/* try to write-protect snip as its next-pointer is changed below */
|
||||||
|
pkt = gnrc_pktbuf_start_write(ptr); /* use pkt as temporary variable */
|
||||||
|
if (pkt == NULL) {
|
||||||
|
gnrc_pktbuf_release(reversed);
|
||||||
|
gnrc_pktbuf_release(ptr);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
/* switch around pointers */
|
||||||
|
next = pkt->next;
|
||||||
|
pkt->next = reversed;
|
||||||
|
reversed = pkt;
|
||||||
|
ptr = next;
|
||||||
|
}
|
||||||
|
return reversed;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user