source: mainline/uspace/lib/net/tl/tl_common.c@ 069015f2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 069015f2 was 84c20da, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cleanup transport layer common functions.

  • Property mode set to 100644
File size: 9.3 KB
Line 
1/*
2 * Copyright (c) 2008 Lukas Mejdrech
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 libnet
30 * @{
31 */
32
33/** @file
34 * Transport layer common functions implementation.
35 * @see tl_common.h
36 */
37
38#include <tl_common.h>
39#include <packet_client.h>
40#include <packet_remote.h>
41#include <icmp_interface.h>
42#include <ip_remote.h>
43#include <ip_interface.h>
44#include <tl_interface.h>
45
46#include <net/socket_codes.h>
47#include <net/in.h>
48#include <net/in6.h>
49#include <net/inet.h>
50#include <net/device.h>
51#include <net/packet.h>
52
53#include <async.h>
54#include <ipc/services.h>
55#include <errno.h>
56#include <err.h>
57
58DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
59
60/** Gets the address port.
61 *
62 * Supports AF_INET and AF_INET6 address families.
63 *
64 * @param[in,out] addr The address to be updated.
65 * @param[in] addrlen The address length.
66 * @param[out] port The set port.
67 * @returns EOK on success.
68 * @returns EINVAL if the address length does not match the address
69 * family.
70 * @returns EAFNOSUPPORT if the address family is not supported.
71 */
72int
73tl_get_address_port(const struct sockaddr *addr, int addrlen, uint16_t *port)
74{
75 const struct sockaddr_in *address_in;
76 const struct sockaddr_in6 *address_in6;
77
78 if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr)))
79 return EINVAL;
80
81 switch (addr->sa_family) {
82 case AF_INET:
83 if (addrlen != sizeof(struct sockaddr_in))
84 return EINVAL;
85
86 address_in = (struct sockaddr_in *) addr;
87 *port = ntohs(address_in->sin_port);
88 break;
89
90 case AF_INET6:
91 if (addrlen != sizeof(struct sockaddr_in6))
92 return EINVAL;
93
94 address_in6 = (struct sockaddr_in6 *) addr;
95 *port = ntohs(address_in6->sin6_port);
96 break;
97
98 default:
99 return EAFNOSUPPORT;
100 }
101
102 return EOK;
103}
104
105/** Get IP packet dimensions.
106 *
107 * Try to search a cache and query the IP module if not found.
108 * The reply is cached then.
109 *
110 * @param[in] ip_phone The IP moduel phone for (semi)remote calls.
111 * @param[in] packet_dimensions The packet dimensions cache.
112 * @param[in] device_id The device identifier.
113 * @param[out] packet_dimension The IP packet dimensions.
114 * @return EOK on success.
115 * @return EBADMEM if the packet_dimension parameter is NULL.
116 * @return ENOMEM if there is not enough memory left.
117 * @return EINVAL if the packet_dimensions cache is not valid.
118 * @return Other codes as defined for the ip_packet_size_req()
119 * function.
120 */
121int
122tl_get_ip_packet_dimension(int ip_phone,
123 packet_dimensions_ref packet_dimensions, device_id_t device_id,
124 packet_dimension_ref *packet_dimension)
125{
126 ERROR_DECLARE;
127
128 if (!packet_dimension)
129 return EBADMEM;
130
131 *packet_dimension = packet_dimensions_find(packet_dimensions,
132 device_id);
133 if (!*packet_dimension) {
134 /* Ask for and remember them if not found */
135 *packet_dimension = malloc(sizeof(**packet_dimension));
136 if(!*packet_dimension)
137 return ENOMEM;
138
139 if (ERROR_OCCURRED(ip_packet_size_req(ip_phone, device_id,
140 *packet_dimension))) {
141 free(*packet_dimension);
142 return ERROR_CODE;
143 }
144
145 ERROR_CODE = packet_dimensions_add(packet_dimensions, device_id,
146 *packet_dimension);
147 if (ERROR_CODE < 0) {
148 free(*packet_dimension);
149 return ERROR_CODE;
150 }
151 }
152
153 return EOK;
154}
155
156/** Updates IP device packet dimensions cache.
157 *
158 * @param[in,out] packet_dimensions The packet dimensions cache.
159 * @param[in] device_id The device identifier.
160 * @param[in] content The new maximum content size.
161 * @returns EOK on success.
162 * @return ENOENT if the packet dimension is not cached.
163 */
164int
165tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions,
166 device_id_t device_id, size_t content)
167{
168 packet_dimension_ref packet_dimension;
169
170 packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
171 if (!packet_dimension)
172 return ENOENT;
173
174 packet_dimension->content = content;
175
176 if (device_id != DEVICE_INVALID_ID) {
177 packet_dimension = packet_dimensions_find(packet_dimensions,
178 DEVICE_INVALID_ID);
179
180 if (packet_dimension) {
181 if (packet_dimension->content >= content)
182 packet_dimension->content = content;
183 else
184 packet_dimensions_exclude(packet_dimensions,
185 DEVICE_INVALID_ID);
186 }
187 }
188
189 return EOK;
190}
191
192/** Sets the address port.
193 *
194 * Supports AF_INET and AF_INET6 address families.
195 *
196 * @param[in,out] addr The address to be updated.
197 * @param[in] addrlen The address length.
198 * @param[in] port The port to be set.
199 * @returns EOK on success.
200 * @returns EINVAL if the address length does not match the address
201 * family.
202 * @returns EAFNOSUPPORT if the address family is not supported.
203 */
204int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port)
205{
206 struct sockaddr_in *address_in;
207 struct sockaddr_in6 *address_in6;
208 size_t length;
209
210 if (addrlen < 0)
211 return EINVAL;
212
213 length = (size_t) addrlen;
214 if (length < sizeof(struct sockaddr))
215 return EINVAL;
216
217 switch (addr->sa_family) {
218 case AF_INET:
219 if (length != sizeof(struct sockaddr_in))
220 return EINVAL;
221 address_in = (struct sockaddr_in *) addr;
222 address_in->sin_port = htons(port);
223 return EOK;
224
225 case AF_INET6:
226 if (length != sizeof(struct sockaddr_in6))
227 return EINVAL;
228 address_in6 = (struct sockaddr_in6 *) addr;
229 address_in6->sin6_port = htons(port);
230 return EOK;
231
232 default:
233 return EAFNOSUPPORT;
234 }
235}
236
237/** Prepares the packet for ICMP error notification.
238 *
239 * Keeps the first packet and releases all the others.
240 * Releases all the packets on error.
241 *
242 * @param[in] packet_phone The packet server module phone.
243 * @param[in] icmp_phone The ICMP module phone.
244 * @param[in] packet The packet to be send.
245 * @param[in] error The packet error reporting service. Prefixes the
246 * received packet.
247 * @returns EOK on success.
248 * @returns ENOENT if no packet may be sent.
249 */
250int
251tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet,
252 services_t error)
253{
254 packet_t next;
255 uint8_t *src;
256 int length;
257
258 // detach the first packet and release the others
259 next = pq_detach(packet);
260 if (next)
261 pq_release_remote(packet_phone, packet_get_id(next));
262
263 length = packet_get_addr(packet, &src, NULL);
264 if ((length > 0) && (!error) && (icmp_phone >= 0) &&
265 // set both addresses to the source one (avoids the source address
266 // deletion before setting the destination one)
267 (packet_set_addr(packet, src, src, (size_t) length) == EOK)) {
268 return EOK;
269 } else
270 pq_release_remote(packet_phone, packet_get_id(packet));
271
272 return ENOENT;
273}
274
275/** Receives data from the socket into a packet.
276 *
277 * @param[in] packet_phone The packet server module phone.
278 * @param[out] packet The new created packet.
279 * @param[in] prefix Reserved packet data prefix length.
280 * @param[in] dimension The packet dimension.
281 * @param[in] addr The destination address.
282 * @param[in] addrlen The address length.
283 * @returns Number of bytes received.
284 * @returns EINVAL if the client does not send data.
285 * @returns ENOMEM if there is not enough memory left.
286 * @returns Other error codes as defined for the
287 * async_data_read_finalize() function.
288 */
289int
290tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix,
291 const packet_dimension_ref dimension, const struct sockaddr *addr,
292 socklen_t addrlen)
293{
294 ERROR_DECLARE;
295
296 ipc_callid_t callid;
297 size_t length;
298 void * data;
299
300 if (!dimension)
301 return EINVAL;
302
303 // get the data length
304 if (!async_data_write_receive(&callid, &length))
305 return EINVAL;
306
307 // get a new packet
308 *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len,
309 prefix + dimension->prefix, dimension->suffix);
310 if (!packet)
311 return ENOMEM;
312
313 // allocate space in the packet
314 data = packet_suffix(*packet, length);
315 if (!data) {
316 pq_release_remote(packet_phone, packet_get_id(*packet));
317 return ENOMEM;
318 }
319
320 // read the data into the packet
321 if (ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) ||
322 // set the packet destination address
323 ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr,
324 addrlen))) {
325 pq_release_remote(packet_phone, packet_get_id(*packet));
326 return ERROR_CODE;
327 }
328
329 return (int) length;
330}
331
332/** @}
333 */
Note: See TracBrowser for help on using the repository browser.