Blame view

RIOT/sys/net/gnrc/pktbuf/gnrc_pktbuf.c 2.15 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  /*
   * Copyright (C) 2017 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @{
   *
   * @file
   * @author  Martine Lenders <m.lenders@fu-berlin.de>
   */
  
  #include <sys/uio.h>
  
  #include "net/gnrc/pktbuf.h"
  
  gnrc_pktsnip_t *gnrc_pktbuf_get_iovec(gnrc_pktsnip_t *pkt, size_t *len)
  {
      size_t length;
      gnrc_pktsnip_t *head;
      struct iovec *vec;
  
      assert(len != NULL);
      if (pkt == NULL) {
          *len = 0;
          return NULL;
      }
  
      /* count the number of snips in the packet and allocate the IOVEC */
      length = gnrc_pkt_count(pkt);
      head = gnrc_pktbuf_add(pkt, NULL, (length * sizeof(struct iovec)),
                             GNRC_NETTYPE_IOVEC);
      if (head == NULL) {
          *len = 0;
          return NULL;
      }
  
      assert(head->data != NULL);
      vec = (struct iovec *)(head->data);
      /* fill the IOVEC */
      while (pkt != NULL) {
          vec->iov_base = pkt->data;
          vec->iov_len = pkt->size;
          ++vec;
          pkt = pkt->next;
      }
      *len = length;
      return head;
  }
  
  gnrc_pktsnip_t *gnrc_pktbuf_remove_snip(gnrc_pktsnip_t *pkt,
                                          gnrc_pktsnip_t *snip)
  {
      LL_DELETE(pkt, snip);
      snip->next = NULL;
      gnrc_pktbuf_release(snip);
  
      return pkt;
  }
  
  gnrc_pktsnip_t *gnrc_pktbuf_replace_snip(gnrc_pktsnip_t *pkt,
                                           gnrc_pktsnip_t *old,
                                           gnrc_pktsnip_t *add)
  {
      /* If add is a list we need to preserve its tail */
      if (add->next != NULL) {
          gnrc_pktsnip_t *tail = add->next;
          gnrc_pktsnip_t *back;
          LL_SEARCH_SCALAR(tail, back, next, NULL); /* find the last snip in add */
          /* Replace old */
          LL_REPLACE_ELEM(pkt, old, add);
          /* and wire in the tail between */
          back->next = add->next;
          add->next = tail;
      }
      else {
          /* add is a single element, has no tail, simply replace */
          LL_REPLACE_ELEM(pkt, old, add);
      }
      old->next = NULL;
      gnrc_pktbuf_release(old);
  
      return pkt;
  }
  
  
  /** @} */