Contiki-NG
uipbuf.c
1 /*
2  * Copyright (c) 2017, RISE SICS, Yanzi Networks
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the authors may not be used to endorse or promote
14  * products derived from this software without specific prior
15  * written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28  * DAMAGE.
29  *
30  *
31  */
32 #include "contiki.h"
33 #include "uip.h"
34 #include "net/ipv6/uipbuf.h"
35 #include <string.h>
36 
37 /*---------------------------------------------------------------------------*/
38 
39 static uint16_t uipbuf_attrs[UIPBUF_ATTR_MAX];
40 
41 /*---------------------------------------------------------------------------*/
42 /* Get the next header given the buffer - start indicates that this is
43  start of the IPv6 header - needs to be set to 0 when in an ext hdr */
44 uint8_t*
45 uipbuf_get_next_header(uint8_t *buffer, uint16_t size, uint8_t *protocol, uint8_t start)
46 {
47  int ext_len = 0;
48  struct uip_ip_hdr *ipbuf = (struct uip_ip_hdr *) buffer;
49  struct uip_ext_hdr *ext = NULL;
50 
51  if(start) {
52  /* protocol in the IP buffer */
53  *protocol = ipbuf->proto;
54  return buffer + UIP_IPH_LEN;
55  } else {
56  /* protocol in the Ext hdr */
57  ext = (struct uip_ext_hdr *) buffer;
58  *protocol = ext->next;
59  /* This is just an ext header */
60  ext_len = (ext->len << 3) + 8;
61  return buffer + ext_len;
62  }
63 }
64 /*---------------------------------------------------------------------------*/
65 /* Get the final header given the buffer - that is assumed to be at start
66  of an IPv6 header */
67 uint8_t*
68 uipbuf_get_last_header(uint8_t *buffer, uint16_t size, uint8_t *protocol)
69 {
70  uint8_t *nbuf;
71 
72  nbuf = uipbuf_get_next_header(buffer, size, protocol, 1);
73  while(uip_is_proto_ext_hdr(*protocol)) {
74  /* send in and move beyond the ext hdr */
75  nbuf = uipbuf_get_next_header(nbuf, size - (nbuf - buffer), protocol, 0);
76  }
77  return nbuf;
78 }
79 /*---------------------------------------------------------------------------*/
80 /**
81  * Common functions for uipbuf (attributes, etc).
82  *
83  */
84 /*---------------------------------------------------------------------------*/
85 uint16_t
86 uipbuf_get_attr(uint8_t type)
87 {
88  if(type < UIPBUF_ATTR_MAX) {
89  return uipbuf_attrs[type];
90  }
91  return 0;
92 }
93 /*---------------------------------------------------------------------------*/
94 int
95 uipbuf_set_attr(uint8_t type, uint16_t value)
96 {
97  if(type < UIPBUF_ATTR_MAX) {
98  uipbuf_attrs[type] = value;
99  return 1;
100  }
101  return 0;
102 }
103 /*---------------------------------------------------------------------------*/
104 void
105 uipbuf_clear_attr(void)
106 {
107  /* set everything to "zero" */
108  memset(uipbuf_attrs, 0, sizeof(uipbuf_attrs));
109 
110  /* And initialize anything that should be initialized */
111  uipbuf_set_attr(UIPBUF_ATTR_MAX_MAC_TRANSMISSIONS,
113 }
114 /*---------------------------------------------------------------------------*/
115 void
116 uipbuf_set_attr_flag(uint16_t flag)
117 {
118  /* Assume only 16-bits for flags now */
119  uipbuf_attrs[UIPBUF_ATTR_FLAGS] |= flag;
120 }
121 /*---------------------------------------------------------------------------*/
122 void
123 uipbuf_clr_attr_flag(uint16_t flag)
124 {
125  uipbuf_attrs[UIPBUF_ATTR_FLAGS] &= ~flag;
126 }
127 /*---------------------------------------------------------------------------*/
128 uint16_t
129 uipbuf_is_attr_flag(uint16_t flag)
130 {
131  return (uipbuf_attrs[UIPBUF_ATTR_FLAGS] & flag) == flag;
132 }
133 /*---------------------------------------------------------------------------*/
static bool start(void)
Start measurement.
#define UIP_MAX_MAC_TRANSMISSIONS_UNDEFINED
This is the default value of MAC-layer transmissons for uIPv6.
Definition: uipopt.h:545
Header file for the uIP TCP/IP stack.