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

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since edf0d27 was a2e3ee6, checked in by Martin Decky <martin@…>, 13 years ago

use new network address infrastructure (towards IPv6 support)

  • Property mode set to 100644
File size: 5.1 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
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 void udp_phdr_setup(udp_pdu_t *pdu, udp_phdr_t *phdr)
87{
88 // FIXME: Check for correctness
89
90 uint32_t src;
91 inet_addr_pack(&pdu->src, &src);
92
93 uint32_t dest;
94 inet_addr_pack(&pdu->dest, &dest);
95
96 phdr->src_addr = host2uint32_t_be(src);
97 phdr->dest_addr = host2uint32_t_be(dest);
98 phdr->zero = 0;
99 phdr->protocol = IP_PROTO_UDP;
100 phdr->udp_length = host2uint16_t_be(pdu->data_size);
101}
102
103udp_pdu_t *udp_pdu_new(void)
104{
105 return calloc(1, sizeof(udp_pdu_t));
106}
107
108void udp_pdu_delete(udp_pdu_t *pdu)
109{
110 free(pdu->data);
111 free(pdu);
112}
113
114static uint16_t udp_pdu_checksum_calc(udp_pdu_t *pdu)
115{
116 uint16_t cs_phdr;
117 uint16_t cs_all;
118 udp_phdr_t phdr;
119
120 udp_phdr_setup(pdu, &phdr);
121 cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *)&phdr,
122 sizeof(udp_phdr_t));
123 cs_all = udp_checksum_calc(cs_phdr, pdu->data, pdu->data_size);
124
125 return cs_all;
126}
127
128static void udp_pdu_set_checksum(udp_pdu_t *pdu, uint16_t checksum)
129{
130 udp_header_t *hdr;
131
132 hdr = (udp_header_t *)pdu->data;
133 hdr->checksum = host2uint16_t_be(checksum);
134}
135
136/** Decode incoming PDU */
137int udp_pdu_decode(udp_pdu_t *pdu, udp_sockpair_t *sp, udp_msg_t **msg)
138{
139 udp_msg_t *nmsg;
140 udp_header_t *hdr;
141 void *text;
142 size_t text_size;
143 uint16_t length;
144 uint16_t checksum;
145
146 if (pdu->data_size < sizeof(udp_header_t))
147 return EINVAL;
148
149 text = pdu->data + sizeof(udp_header_t);
150 text_size = pdu->data_size - sizeof(udp_header_t);
151
152 hdr = (udp_header_t *)pdu->data;
153
154 sp->foreign.port = uint16_t_be2host(hdr->src_port);
155 sp->foreign.addr = pdu->src;
156 sp->local.port = uint16_t_be2host(hdr->dest_port);
157 sp->local.addr = pdu->dest;
158
159 length = uint16_t_be2host(hdr->length);
160 checksum = uint16_t_be2host(hdr->checksum);
161 (void) checksum;
162
163 if (length < sizeof(udp_header_t) ||
164 length > sizeof(udp_header_t) + text_size)
165 return EINVAL;
166
167 nmsg = udp_msg_new();
168 if (nmsg == NULL)
169 return ENOMEM;
170
171 nmsg->data = text;
172 nmsg->data_size = length - sizeof(udp_header_t);
173
174 *msg = nmsg;
175 return EOK;
176}
177
178/** Encode outgoing PDU */
179int udp_pdu_encode(udp_sockpair_t *sp, udp_msg_t *msg, udp_pdu_t **pdu)
180{
181 udp_pdu_t *npdu;
182 udp_header_t *hdr;
183 uint16_t checksum;
184
185 npdu = udp_pdu_new();
186 if (npdu == NULL)
187 return ENOMEM;
188
189 npdu->src = sp->local.addr;
190 npdu->dest = sp->foreign.addr;
191
192 npdu->data_size = sizeof(udp_header_t) + msg->data_size;
193 npdu->data = calloc(1, npdu->data_size);
194 if (npdu->data == NULL) {
195 udp_pdu_delete(npdu);
196 return ENOMEM;
197 }
198
199 hdr = (udp_header_t *)npdu->data;
200 hdr->src_port = host2uint16_t_be(sp->local.port);
201 hdr->dest_port = host2uint16_t_be(sp->foreign.port);
202 hdr->length = host2uint16_t_be(npdu->data_size);
203 hdr->checksum = 0;
204
205 memcpy((uint8_t *)npdu->data + sizeof(udp_header_t), msg->data,
206 msg->data_size);
207
208 /* Checksum calculation */
209 checksum = udp_pdu_checksum_calc(npdu);
210 udp_pdu_set_checksum(npdu, checksum);
211
212 *pdu = npdu;
213 return EOK;
214}
215
216/**
217 * @}
218 */
Note: See TracBrowser for help on using the repository browser.