source: mainline/uspace/srv/net/udp/pdu.c@ 02a09ed

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

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

  • Property mode set to 100644
File size: 5.5 KB
Line 
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#include <inet/addr.h>
43#include <net/socket_codes.h>
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
87static uint16_t udp_phdr_setup(udp_pdu_t *pdu, udp_phdr_t *phdr)
88{
89 addr32_t src_v4;
90 addr128_t src_v6;
91 uint16_t src_af = inet_addr_get(&pdu->src, &src_v4, &src_v6);
92
93 addr32_t dest_v4;
94 addr128_t dest_v6;
95 uint16_t dest_af = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6);
96
97 assert(src_af == dest_af);
98
99 switch (src_af) {
100 case AF_INET:
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 AF_INET6:
108 // FIXME TODO
109 assert(false);
110 default:
111 assert(false);
112 }
113
114 return src_af;
115}
116
117udp_pdu_t *udp_pdu_new(void)
118{
119 return calloc(1, sizeof(udp_pdu_t));
120}
121
122void udp_pdu_delete(udp_pdu_t *pdu)
123{
124 free(pdu->data);
125 free(pdu);
126}
127
128static uint16_t udp_pdu_checksum_calc(udp_pdu_t *pdu)
129{
130 uint16_t cs_phdr;
131 udp_phdr_t phdr;
132
133 uint16_t af = udp_phdr_setup(pdu, &phdr);
134 switch (af) {
135 case AF_INET:
136 cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *) &phdr,
137 sizeof(udp_phdr_t));
138 break;
139 case AF_INET6:
140 // FIXME TODO
141 assert(false);
142 default:
143 assert(false);
144 }
145
146 return udp_checksum_calc(cs_phdr, pdu->data, pdu->data_size);
147}
148
149static void udp_pdu_set_checksum(udp_pdu_t *pdu, uint16_t checksum)
150{
151 udp_header_t *hdr;
152
153 hdr = (udp_header_t *)pdu->data;
154 hdr->checksum = host2uint16_t_be(checksum);
155}
156
157/** Decode incoming PDU */
158int udp_pdu_decode(udp_pdu_t *pdu, udp_sockpair_t *sp, udp_msg_t **msg)
159{
160 udp_msg_t *nmsg;
161 udp_header_t *hdr;
162 void *text;
163 size_t text_size;
164 uint16_t length;
165 uint16_t checksum;
166
167 if (pdu->data_size < sizeof(udp_header_t))
168 return EINVAL;
169
170 text = pdu->data + sizeof(udp_header_t);
171 text_size = pdu->data_size - sizeof(udp_header_t);
172
173 hdr = (udp_header_t *)pdu->data;
174
175 sp->foreign.port = uint16_t_be2host(hdr->src_port);
176 sp->foreign.addr = pdu->src;
177 sp->local.port = uint16_t_be2host(hdr->dest_port);
178 sp->local.addr = pdu->dest;
179
180 length = uint16_t_be2host(hdr->length);
181 checksum = uint16_t_be2host(hdr->checksum);
182 (void) checksum;
183
184 if (length < sizeof(udp_header_t) ||
185 length > sizeof(udp_header_t) + text_size)
186 return EINVAL;
187
188 nmsg = udp_msg_new();
189 if (nmsg == NULL)
190 return ENOMEM;
191
192 nmsg->data = text;
193 nmsg->data_size = length - sizeof(udp_header_t);
194
195 *msg = nmsg;
196 return EOK;
197}
198
199/** Encode outgoing PDU */
200int udp_pdu_encode(udp_sockpair_t *sp, udp_msg_t *msg, udp_pdu_t **pdu)
201{
202 udp_pdu_t *npdu;
203 udp_header_t *hdr;
204 uint16_t checksum;
205
206 npdu = udp_pdu_new();
207 if (npdu == NULL)
208 return ENOMEM;
209
210 npdu->src = sp->local.addr;
211 npdu->dest = sp->foreign.addr;
212
213 npdu->data_size = sizeof(udp_header_t) + msg->data_size;
214 npdu->data = calloc(1, npdu->data_size);
215 if (npdu->data == NULL) {
216 udp_pdu_delete(npdu);
217 return ENOMEM;
218 }
219
220 hdr = (udp_header_t *)npdu->data;
221 hdr->src_port = host2uint16_t_be(sp->local.port);
222 hdr->dest_port = host2uint16_t_be(sp->foreign.port);
223 hdr->length = host2uint16_t_be(npdu->data_size);
224 hdr->checksum = 0;
225
226 memcpy((uint8_t *)npdu->data + sizeof(udp_header_t), msg->data,
227 msg->data_size);
228
229 /* Checksum calculation */
230 checksum = udp_pdu_checksum_calc(npdu);
231 udp_pdu_set_checksum(npdu, checksum);
232
233 *pdu = npdu;
234 return EOK;
235}
236
237/**
238 * @}
239 */
Note: See TracBrowser for help on using the repository browser.