1 | /*
|
---|
2 | * Copyright (c) 2012 Jiri Svoboda
|
---|
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 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup udp
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file UDP PDU encoding and decoding
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <bitops.h>
|
---|
38 | #include <byteorder.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <mem.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 |
|
---|
43 | #include "msg.h"
|
---|
44 | #include "pdu.h"
|
---|
45 | #include "std.h"
|
---|
46 | #include "udp_type.h"
|
---|
47 |
|
---|
48 | #define UDP_CHECKSUM_INIT 0xffff
|
---|
49 |
|
---|
50 | /** One's complement addition.
|
---|
51 | *
|
---|
52 | * Result is a + b + carry.
|
---|
53 | */
|
---|
54 | static uint16_t udp_ocadd16(uint16_t a, uint16_t b)
|
---|
55 | {
|
---|
56 | uint32_t s;
|
---|
57 |
|
---|
58 | s = (uint32_t)a + (uint32_t)b;
|
---|
59 | return (s & 0xffff) + (s >> 16);
|
---|
60 | }
|
---|
61 |
|
---|
62 | static uint16_t udp_checksum_calc(uint16_t ivalue, void *data, size_t size)
|
---|
63 | {
|
---|
64 | uint16_t sum;
|
---|
65 | uint16_t w;
|
---|
66 | size_t words, i;
|
---|
67 | uint8_t *bdata;
|
---|
68 |
|
---|
69 | sum = ~ivalue;
|
---|
70 | words = size / 2;
|
---|
71 | bdata = (uint8_t *)data;
|
---|
72 |
|
---|
73 | for (i = 0; i < words; i++) {
|
---|
74 | w = ((uint16_t)bdata[2*i] << 8) | bdata[2*i + 1];
|
---|
75 | sum = udp_ocadd16(sum, w);
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (size % 2 != 0) {
|
---|
79 | w = ((uint16_t)bdata[2*words] << 8);
|
---|
80 | sum = udp_ocadd16(sum, w);
|
---|
81 | }
|
---|
82 |
|
---|
83 | return ~sum;
|
---|
84 | }
|
---|
85 |
|
---|
86 | static void udp_phdr_setup(udp_pdu_t *pdu, udp_phdr_t *phdr)
|
---|
87 | {
|
---|
88 | phdr->src_addr = host2uint32_t_be(pdu->src.ipv4);
|
---|
89 | phdr->dest_addr = host2uint32_t_be(pdu->dest.ipv4);
|
---|
90 | phdr->zero = 0;
|
---|
91 | phdr->protocol = IP_PROTO_UDP;
|
---|
92 | phdr->udp_length = host2uint16_t_be(pdu->data_size);
|
---|
93 | }
|
---|
94 |
|
---|
95 | udp_pdu_t *udp_pdu_new(void)
|
---|
96 | {
|
---|
97 | return calloc(1, sizeof(udp_pdu_t));
|
---|
98 | }
|
---|
99 |
|
---|
100 | void udp_pdu_delete(udp_pdu_t *pdu)
|
---|
101 | {
|
---|
102 | free(pdu->data);
|
---|
103 | free(pdu);
|
---|
104 | }
|
---|
105 |
|
---|
106 | static uint16_t udp_pdu_checksum_calc(udp_pdu_t *pdu)
|
---|
107 | {
|
---|
108 | uint16_t cs_phdr;
|
---|
109 | uint16_t cs_all;
|
---|
110 | udp_phdr_t phdr;
|
---|
111 |
|
---|
112 | udp_phdr_setup(pdu, &phdr);
|
---|
113 | cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *)&phdr,
|
---|
114 | sizeof(udp_phdr_t));
|
---|
115 | cs_all = udp_checksum_calc(cs_phdr, pdu->data, pdu->data_size);
|
---|
116 |
|
---|
117 | return cs_all;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static void udp_pdu_set_checksum(udp_pdu_t *pdu, uint16_t checksum)
|
---|
121 | {
|
---|
122 | udp_header_t *hdr;
|
---|
123 |
|
---|
124 | hdr = (udp_header_t *)pdu->data;
|
---|
125 | hdr->checksum = host2uint16_t_be(checksum);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Decode incoming PDU */
|
---|
129 | int udp_pdu_decode(udp_pdu_t *pdu, udp_sockpair_t *sp, udp_msg_t **msg)
|
---|
130 | {
|
---|
131 | udp_msg_t *nmsg;
|
---|
132 | udp_header_t *hdr;
|
---|
133 | void *text;
|
---|
134 | size_t text_size;
|
---|
135 | uint16_t length;
|
---|
136 | uint16_t checksum;
|
---|
137 |
|
---|
138 | if (pdu->data_size < sizeof(udp_header_t))
|
---|
139 | return EINVAL;
|
---|
140 |
|
---|
141 | text = pdu->data + sizeof(udp_header_t);
|
---|
142 | text_size = pdu->data_size - sizeof(udp_header_t);
|
---|
143 |
|
---|
144 | hdr = (udp_header_t *)pdu->data;
|
---|
145 |
|
---|
146 | sp->foreign.port = uint16_t_be2host(hdr->src_port);
|
---|
147 | sp->foreign.addr = pdu->src;
|
---|
148 | sp->local.port = uint16_t_be2host(hdr->dest_port);
|
---|
149 | sp->local.addr = pdu->dest;
|
---|
150 |
|
---|
151 | length = uint16_t_be2host(hdr->length);
|
---|
152 | checksum = uint16_t_be2host(hdr->checksum);
|
---|
153 | (void) checksum;
|
---|
154 |
|
---|
155 | if (length < sizeof(udp_header_t) ||
|
---|
156 | length > sizeof(udp_header_t) + text_size)
|
---|
157 | return EINVAL;
|
---|
158 |
|
---|
159 | nmsg = udp_msg_new();
|
---|
160 | if (nmsg == NULL)
|
---|
161 | return ENOMEM;
|
---|
162 |
|
---|
163 | nmsg->data = text;
|
---|
164 | nmsg->data_size = length - sizeof(udp_header_t);
|
---|
165 |
|
---|
166 | *msg = nmsg;
|
---|
167 | return EOK;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /** Encode outgoing PDU */
|
---|
171 | int udp_pdu_encode(udp_sockpair_t *sp, udp_msg_t *msg, udp_pdu_t **pdu)
|
---|
172 | {
|
---|
173 | udp_pdu_t *npdu;
|
---|
174 | udp_header_t *hdr;
|
---|
175 | uint16_t checksum;
|
---|
176 |
|
---|
177 | npdu = udp_pdu_new();
|
---|
178 | if (npdu == NULL)
|
---|
179 | return ENOMEM;
|
---|
180 |
|
---|
181 | npdu->src = sp->local.addr;
|
---|
182 | npdu->dest = sp->foreign.addr;
|
---|
183 |
|
---|
184 | npdu->data_size = sizeof(udp_header_t) + msg->data_size;
|
---|
185 | npdu->data = calloc(1, npdu->data_size);
|
---|
186 | if (npdu->data == NULL) {
|
---|
187 | udp_pdu_delete(npdu);
|
---|
188 | return ENOMEM;
|
---|
189 | }
|
---|
190 |
|
---|
191 | hdr = (udp_header_t *)npdu->data;
|
---|
192 | hdr->src_port = host2uint16_t_be(sp->local.port);
|
---|
193 | hdr->dest_port = host2uint16_t_be(sp->foreign.port);
|
---|
194 | hdr->length = host2uint16_t_be(npdu->data_size);
|
---|
195 | hdr->checksum = 0;
|
---|
196 |
|
---|
197 | memcpy((uint8_t *)npdu->data + sizeof(udp_header_t), msg->data,
|
---|
198 | msg->data_size);
|
---|
199 |
|
---|
200 | /* Checksum calculation */
|
---|
201 | checksum = udp_pdu_checksum_calc(npdu);
|
---|
202 | udp_pdu_set_checksum(npdu, checksum);
|
---|
203 |
|
---|
204 | *pdu = npdu;
|
---|
205 | return EOK;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * @}
|
---|
210 | */
|
---|