Contiki 2.5
hc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
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. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$Id: hc.c,v 1.5 2010/10/19 18:29:04 adamdunkels Exp $
32  */
33 
34 /**
35  * \file
36  * TCP/IP header compression implementation
37  * \author Adam Dunkels <adam@sics.se>
38  *
39  */
40 
41 #include "net/hc.h"
42 
43 #include "net/uip.h"
44 
45 #include <string.h>
46 
47 #define FLAGS_COMPRESSED 0x8000
48 #define FLAGS_BROADCASTDATA 0x4000
49 
50 struct hc_hdr {
51  u16_t flagsport;
52  uip_ipaddr_t srcipaddr;
53 };
54 
55 struct udpip_hdr {
56  /* IP header. */
57  u8_t vhl,
58  tos,
59  len[2],
60  ipid[2],
61  ipoffset[2],
62  ttl,
63  proto;
64  u16_t ipchksum;
65  uip_ipaddr_t srcipaddr, destipaddr;
66 
67  /* UDP header. */
68  u16_t srcport,
69  destport;
70  u16_t udplen;
71  u16_t udpchksum;
72 };
73 
74 #include <stdio.h>
75 
76 /*---------------------------------------------------------------------------*/
77 /**
78  * Initialize the header compression module.
79  */
80 /*---------------------------------------------------------------------------*/
81 void
82 hc_init(void)
83 {
84 
85 }
86 /*---------------------------------------------------------------------------*/
87 /**
88  * Compress a header
89  *
90  * This function compresses the TCP/IP headers in a buffer and
91  * should be called just before sending out data on the network. A
92  * pointer to the compressed header is returned, and len is
93  * adjusted.
94  *
95  * If the header could not be compressed, the function does nothing
96  * and returns a NULL pointer.
97  *
98  * \return A pointer to the start of the compressed header or NULL if
99  * the header could not be compressed.
100  */
101 /*---------------------------------------------------------------------------*/
102 int
103 hc_compress(u8_t *buf, int len)
104 {
105  struct hc_hdr *hdr;
106  struct udpip_hdr *uhdr;
107 
108  hdr = (struct hc_hdr *)buf;
109  uhdr = (struct udpip_hdr *)buf;
110 
111  /* Check the original TCP/IP header to see if it matches our
112  pattern, and compress if it does. */
113 
114  if(uhdr->vhl == 0x45 && /* Only IPv4 without
115  options. */
116  uhdr->len[0] == 0x00 && /* Only packets < 256
117  bytes long. */
118  uhdr->ipoffset[0] == 0x00 && /* No fragmented IP
119  packets. */
120  uhdr->ipoffset[1] == 0x00 && /* No fragmented IP
121  packets. */
122  uhdr->proto == UIP_PROTO_UDP && /* Only UDP packets. */
123  uip_ipaddr_cmp(&uhdr->destipaddr, &uip_broadcast_addr) && /* Only
124  link-local broadcast
125  packets. */
126  uhdr->destport == uhdr->srcport && /* Only packets with
127  the same destination
128  and source port
129  number. */
130  (uhdr->destport & UIP_HTONS(0xc000)) == 0) { /* Only packets with the two
131  highest bits in the port
132  number equal to zero. */
133 
134  hdr->flagsport = uip_htons(
135  FLAGS_COMPRESSED | /* Compressed header. */
136  FLAGS_BROADCASTDATA | /* Broadcast data. */
137  (uip_htons(uhdr->destport) & 0x3fff));
138  uip_ipaddr_copy(&hdr->srcipaddr, &uhdr->srcipaddr);
139 
140  /* Move the packet data to the end of the compressed header. */
141  memcpy((char *)hdr + HC_HLEN,
142  &buf[UIP_IPUDPH_LEN],
143  len - UIP_IPUDPH_LEN);
144 
145  /* Return the new packet length. */
146  return len - (UIP_IPUDPH_LEN - HC_HLEN);
147  }
148 
149  /* No compression possible, return NULL pointer. */
150  return len;
151 
152 }
153 /*---------------------------------------------------------------------------*/
154 /**
155  * Inflate (decompress) a header
156  *
157  * This function should be called to inflate a possibly compressed
158  * packet header just after a packet has been received from the
159  * network. The function will copy the packet data so that the
160  * original header fits and adjusts uip_len.
161  *
162  */
163 /*---------------------------------------------------------------------------*/
164 int
165 hc_inflate(u8_t *buf, int len)
166 {
167  struct udpip_hdr *uhdr;
168  struct hc_hdr *hdr;
169 
170  hdr = (struct hc_hdr *)buf;
171 
172  /* First, check if the header in buf is compressed or not. */
173  if((hdr->flagsport & UIP_HTONS(FLAGS_COMPRESSED)) != 0 &&
174  (hdr->flagsport & UIP_HTONS(FLAGS_BROADCASTDATA)) != 0) {
175 
176  /* Move packet data in memory to make room for the uncompressed header. */
177  memmove(&buf[UIP_IPUDPH_LEN - HC_HLEN],
178  buf, len);
179  uhdr = (struct udpip_hdr *)buf;
180  hdr = (struct hc_hdr *)&buf[UIP_IPUDPH_LEN - HC_HLEN];
181 
182  uip_ipaddr_copy(&uhdr->srcipaddr, &hdr->srcipaddr);
183  uhdr->srcport = hdr->flagsport & UIP_HTONS(0x3fff);
184  uhdr->destport = hdr->flagsport & UIP_HTONS(0x3fff);
185 
186  uhdr->udplen = len;
187 
188  len += UIP_IPUDPH_LEN - HC_HLEN;
189 
190 
191  uhdr->vhl = 0x45;
192  uhdr->tos = 0;
193  uhdr->len[0] = 0;
194  uhdr->len[1] = len;
195  uhdr->ipid[0] = uhdr->ipid[1] = 0xAD;
196  uhdr->ipoffset[0] = uhdr->ipoffset[1] = 0;
197  uhdr->ttl = 2;
198  uhdr->proto = UIP_PROTO_UDP;
199  uip_ipaddr_copy(&uhdr->destipaddr, &uip_broadcast_addr);
200  uhdr->udpchksum = 0;
201 
202  uhdr->ipchksum = 0;
203  uhdr->ipchksum = ~(uip_ipchksum());
204 
205  }
206 
207  return len;
208 }
209 /*---------------------------------------------------------------------------*/