source: mainline/uspace/srv/net/udp/pdu.c

Last change on this file was 3bacee1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Make ccheck-fix again and commit more good files.

  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 * Copyright (c) 2015 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#include <inet/addr.h>
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 */
54static 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
62static 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
86static ip_ver_t udp_phdr_setup(udp_pdu_t *pdu, udp_phdr_t *phdr,
87 udp_phdr6_t *phdr6)
88{
89 addr32_t src_v4;
90 addr128_t src_v6;
91 ip_ver_t src_ver = inet_addr_get(&pdu->src, &src_v4, &src_v6);
92
93 addr32_t dest_v4;
94 addr128_t dest_v6;
95 ip_ver_t dest_ver = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6);
96
97 assert(src_ver == dest_ver);
98
99 switch (src_ver) {
100 case ip_v4:
101 phdr->src_addr = host2uint32_t_be(src_v4);
102 phdr->dest_addr = host2uint32_t_be(dest_v4);
103 phdr->zero = 0;
104 phdr->protocol = IP_PROTO_UDP;
105 phdr->udp_length = host2uint16_t_be(pdu->data_size);
106 break;
107 case ip_v6:
108 host2addr128_t_be(src_v6, phdr6->src_addr);
109 host2addr128_t_be(dest_v6, phdr6->dest_addr);
110 phdr6->udp_length = host2uint32_t_be(pdu->data_size);
111 memset(phdr6->zeroes, 0, 3);
112 phdr6->next = IP_PROTO_UDP;
113 break;
114 default:
115 assert(false);
116 }
117
118 return src_ver;
119}
120
121udp_pdu_t *udp_pdu_new(void)
122{
123 return calloc(1, sizeof(udp_pdu_t));
124}
125
126void udp_pdu_delete(udp_pdu_t *pdu)
127{
128 free(pdu->data);
129 free(pdu);
130}
131
132static uint16_t udp_pdu_checksum_calc(udp_pdu_t *pdu)
133{
134 uint16_t cs_phdr;
135 udp_phdr_t phdr;
136 udp_phdr6_t phdr6;
137
138 ip_ver_t ver = udp_phdr_setup(pdu, &phdr, &phdr6);
139 switch (ver) {
140 case ip_v4:
141 cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *) &phdr,
142 sizeof(udp_phdr_t));
143 break;
144 case ip_v6:
145 cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *) &phdr6,
146 sizeof(udp_phdr6_t));
147 break;
148 default:
149 assert(false);
150 }
151
152 return udp_checksum_calc(cs_phdr, pdu->data, pdu->data_size);
153}
154
155static void udp_pdu_set_checksum(udp_pdu_t *pdu, uint16_t checksum)
156{
157 udp_header_t *hdr;
158
159 hdr = (udp_header_t *)pdu->data;
160 hdr->checksum = host2uint16_t_be(checksum);
161}
162
163/** Decode incoming PDU */
164errno_t udp_pdu_decode(udp_pdu_t *pdu, inet_ep2_t *epp, udp_msg_t **msg)
165{
166 udp_msg_t *nmsg;
167 udp_header_t *hdr;
168 void *text;
169 size_t text_size;
170 uint16_t length;
171 uint16_t checksum;
172
173 if (pdu->data_size < sizeof(udp_header_t))
174 return EINVAL;
175
176 text = pdu->data + sizeof(udp_header_t);
177 text_size = pdu->data_size - sizeof(udp_header_t);
178
179 hdr = (udp_header_t *)pdu->data;
180
181 epp->local_link = pdu->iplink;
182 epp->remote.port = uint16_t_be2host(hdr->src_port);
183 epp->remote.addr = pdu->src;
184 epp->local.port = uint16_t_be2host(hdr->dest_port);
185 epp->local.addr = pdu->dest;
186
187 length = uint16_t_be2host(hdr->length);
188 checksum = uint16_t_be2host(hdr->checksum);
189 (void) checksum;
190
191 if (length < sizeof(udp_header_t) ||
192 length > sizeof(udp_header_t) + text_size)
193 return EINVAL;
194
195 nmsg = udp_msg_new();
196 if (nmsg == NULL)
197 return ENOMEM;
198
199 nmsg->data_size = length - sizeof(udp_header_t);
200 nmsg->data = malloc(nmsg->data_size);
201 if (nmsg->data == NULL)
202 return ENOMEM;
203
204 memcpy(nmsg->data, text, nmsg->data_size);
205
206 *msg = nmsg;
207 return EOK;
208}
209
210/** Encode outgoing PDU */
211errno_t udp_pdu_encode(inet_ep2_t *epp, udp_msg_t *msg, udp_pdu_t **pdu)
212{
213 udp_pdu_t *npdu;
214 udp_header_t *hdr;
215 uint16_t checksum;
216
217 npdu = udp_pdu_new();
218 if (npdu == NULL)
219 return ENOMEM;
220
221 npdu->iplink = epp->local_link;
222 npdu->src = epp->local.addr;
223 npdu->dest = epp->remote.addr;
224
225 npdu->data_size = sizeof(udp_header_t) + msg->data_size;
226 npdu->data = calloc(1, npdu->data_size);
227 if (npdu->data == NULL) {
228 udp_pdu_delete(npdu);
229 return ENOMEM;
230 }
231
232 hdr = (udp_header_t *)npdu->data;
233 hdr->src_port = host2uint16_t_be(epp->local.port);
234 hdr->dest_port = host2uint16_t_be(epp->remote.port);
235 hdr->length = host2uint16_t_be(npdu->data_size);
236 hdr->checksum = 0;
237
238 memcpy((uint8_t *)npdu->data + sizeof(udp_header_t), msg->data,
239 msg->data_size);
240
241 /* Checksum calculation */
242 checksum = udp_pdu_checksum_calc(npdu);
243 udp_pdu_set_checksum(npdu, checksum);
244
245 *pdu = npdu;
246 return EOK;
247}
248
249/**
250 * @}
251 */
Note: See TracBrowser for help on using the repository browser.