source: mainline/uspace/srv/net/udp/pdu.c@ 47f5a77

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 47f5a77 was 12df1f1, checked in by Martin Decky <martin@…>, 12 years ago

cherrypick trivial changes from lp:~as-s/helenos/ipv6

  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[66a272f8]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>
[02a09ed]42#include <inet/addr.h>
43#include <net/socket_codes.h>
[66a272f8]44#include "msg.h"
45#include "pdu.h"
46#include "std.h"
47#include "udp_type.h"
48
49#define UDP_CHECKSUM_INIT 0xffff
50
51/** One's complement addition.
52 *
53 * Result is a + b + carry.
54 */
55static uint16_t udp_ocadd16(uint16_t a, uint16_t b)
56{
57 uint32_t s;
58
59 s = (uint32_t)a + (uint32_t)b;
60 return (s & 0xffff) + (s >> 16);
61}
62
63static uint16_t udp_checksum_calc(uint16_t ivalue, void *data, size_t size)
64{
65 uint16_t sum;
66 uint16_t w;
67 size_t words, i;
68 uint8_t *bdata;
69
70 sum = ~ivalue;
71 words = size / 2;
72 bdata = (uint8_t *)data;
73
74 for (i = 0; i < words; i++) {
75 w = ((uint16_t)bdata[2*i] << 8) | bdata[2*i + 1];
76 sum = udp_ocadd16(sum, w);
77 }
78
79 if (size % 2 != 0) {
80 w = ((uint16_t)bdata[2*words] << 8);
81 sum = udp_ocadd16(sum, w);
82 }
83
84 return ~sum;
85}
86
[1d24ad3]87static uint16_t udp_phdr_setup(udp_pdu_t *pdu, udp_phdr_t *phdr,
88 udp_phdr6_t *phdr6)
[66a272f8]89{
[02a09ed]90 addr32_t src_v4;
91 addr128_t src_v6;
92 uint16_t src_af = inet_addr_get(&pdu->src, &src_v4, &src_v6);
93
94 addr32_t dest_v4;
95 addr128_t dest_v6;
96 uint16_t dest_af = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6);
[19a4f73]97
[02a09ed]98 assert(src_af == dest_af);
[19a4f73]99
[02a09ed]100 switch (src_af) {
101 case AF_INET:
102 phdr->src_addr = host2uint32_t_be(src_v4);
103 phdr->dest_addr = host2uint32_t_be(dest_v4);
104 phdr->zero = 0;
105 phdr->protocol = IP_PROTO_UDP;
106 phdr->udp_length = host2uint16_t_be(pdu->data_size);
107 break;
108 case AF_INET6:
[1d24ad3]109 host2addr128_t_be(src_v6, phdr6->src_addr);
110 host2addr128_t_be(dest_v6, phdr6->dest_addr);
111 phdr6->udp_length = host2uint32_t_be(pdu->data_size);
[12df1f1]112 memset(phdr6->zeroes, 0, 3);
[1d24ad3]113 phdr6->next = IP_PROTO_UDP;
114 break;
[02a09ed]115 default:
116 assert(false);
117 }
[19a4f73]118
[02a09ed]119 return src_af;
[66a272f8]120}
121
[92b42442]122udp_pdu_t *udp_pdu_new(void)
[66a272f8]123{
124 return calloc(1, sizeof(udp_pdu_t));
125}
126
127void udp_pdu_delete(udp_pdu_t *pdu)
128{
129 free(pdu->data);
130 free(pdu);
131}
132
133static uint16_t udp_pdu_checksum_calc(udp_pdu_t *pdu)
134{
135 uint16_t cs_phdr;
136 udp_phdr_t phdr;
[1d24ad3]137 udp_phdr6_t phdr6;
[02a09ed]138
[1d24ad3]139 uint16_t af = udp_phdr_setup(pdu, &phdr, &phdr6);
[02a09ed]140 switch (af) {
141 case AF_INET:
142 cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *) &phdr,
143 sizeof(udp_phdr_t));
144 break;
145 case AF_INET6:
[1d24ad3]146 cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *) &phdr6,
147 sizeof(udp_phdr6_t));
148 break;
[02a09ed]149 default:
150 assert(false);
151 }
152
153 return udp_checksum_calc(cs_phdr, pdu->data, pdu->data_size);
[66a272f8]154}
155
156static void udp_pdu_set_checksum(udp_pdu_t *pdu, uint16_t checksum)
157{
158 udp_header_t *hdr;
159
160 hdr = (udp_header_t *)pdu->data;
161 hdr->checksum = host2uint16_t_be(checksum);
162}
163
164/** Decode incoming PDU */
165int udp_pdu_decode(udp_pdu_t *pdu, udp_sockpair_t *sp, udp_msg_t **msg)
166{
167 udp_msg_t *nmsg;
168 udp_header_t *hdr;
169 void *text;
170 size_t text_size;
171 uint16_t length;
172 uint16_t checksum;
173
174 if (pdu->data_size < sizeof(udp_header_t))
175 return EINVAL;
176
177 text = pdu->data + sizeof(udp_header_t);
178 text_size = pdu->data_size - sizeof(udp_header_t);
179
180 hdr = (udp_header_t *)pdu->data;
181
182 sp->foreign.port = uint16_t_be2host(hdr->src_port);
183 sp->foreign.addr = pdu->src;
184 sp->local.port = uint16_t_be2host(hdr->dest_port);
185 sp->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 = text;
200 nmsg->data_size = length - sizeof(udp_header_t);
201
202 *msg = nmsg;
203 return EOK;
204}
205
206/** Encode outgoing PDU */
207int udp_pdu_encode(udp_sockpair_t *sp, udp_msg_t *msg, udp_pdu_t **pdu)
208{
209 udp_pdu_t *npdu;
210 udp_header_t *hdr;
211 uint16_t checksum;
212
213 npdu = udp_pdu_new();
214 if (npdu == NULL)
215 return ENOMEM;
216
217 npdu->src = sp->local.addr;
218 npdu->dest = sp->foreign.addr;
219
220 npdu->data_size = sizeof(udp_header_t) + msg->data_size;
221 npdu->data = calloc(1, npdu->data_size);
222 if (npdu->data == NULL) {
223 udp_pdu_delete(npdu);
224 return ENOMEM;
225 }
226
227 hdr = (udp_header_t *)npdu->data;
228 hdr->src_port = host2uint16_t_be(sp->local.port);
229 hdr->dest_port = host2uint16_t_be(sp->foreign.port);
230 hdr->length = host2uint16_t_be(npdu->data_size);
231 hdr->checksum = 0;
232
233 memcpy((uint8_t *)npdu->data + sizeof(udp_header_t), msg->data,
234 msg->data_size);
235
236 /* Checksum calculation */
237 checksum = udp_pdu_checksum_calc(npdu);
238 udp_pdu_set_checksum(npdu, checksum);
239
240 *pdu = npdu;
241 return EOK;
242}
243
244/**
245 * @}
246 */
Note: See TracBrowser for help on using the repository browser.