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

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

networking stack: convert to the new async framework

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