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

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

cstyle

  • 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, nic_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 tl_update_ip_packet_dimension(packet_dimensions_t *packet_dimensions,
161 nic_device_id_t device_id, size_t content)
162{
163 packet_dimension_t *packet_dimension;
164
165 packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
166 if (!packet_dimension)
167 return ENOENT;
168
169 packet_dimension->content = content;
170
171 if (device_id != NIC_DEVICE_INVALID_ID) {
172 packet_dimension = packet_dimensions_find(packet_dimensions,
173 NIC_DEVICE_INVALID_ID);
174
175 if (packet_dimension) {
176 if (packet_dimension->content >= content)
177 packet_dimension->content = content;
178 else
179 packet_dimensions_exclude(packet_dimensions,
180 NIC_DEVICE_INVALID_ID, free);
181 }
182 }
183
184 return EOK;
185}
186
187/** Sets the address port.
188 *
189 * Supports AF_INET and AF_INET6 address families.
190 *
191 * @param[in,out] addr The address to be updated.
192 * @param[in] addrlen The address length.
193 * @param[in] port The port to be set.
194 * @return EOK on success.
195 * @return EINVAL if the address length does not match the address
196 * family.
197 * @return EAFNOSUPPORT if the address family is not supported.
198 */
199int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port)
200{
201 struct sockaddr_in *address_in;
202 struct sockaddr_in6 *address_in6;
203 size_t length;
204
205 if (addrlen < 0)
206 return EINVAL;
207
208 length = (size_t) addrlen;
209 if (length < sizeof(struct sockaddr))
210 return EINVAL;
211
212 switch (addr->sa_family) {
213 case AF_INET:
214 if (length != sizeof(struct sockaddr_in))
215 return EINVAL;
216 address_in = (struct sockaddr_in *) addr;
217 address_in->sin_port = htons(port);
218 return EOK;
219
220 case AF_INET6:
221 if (length != sizeof(struct sockaddr_in6))
222 return EINVAL;
223 address_in6 = (struct sockaddr_in6 *) addr;
224 address_in6->sin6_port = htons(port);
225 return EOK;
226
227 default:
228 return EAFNOSUPPORT;
229 }
230}
231
232/** Prepares the packet for ICMP error notification.
233 *
234 * Keep the first packet and release all the others.
235 * Release all the packets on error.
236 *
237 * @param[in] packet_sess Packet server module session.
238 * @param[in] icmp_sess ICMP module phone.
239 * @param[in] packet Packet to be send.
240 * @param[in] error Packet error reporting service. Prefixes the
241 * received packet.
242 *
243 * @return EOK on success.
244 * @return ENOENT if no packet may be sent.
245 *
246 */
247int tl_prepare_icmp_packet(async_sess_t *packet_sess, async_sess_t *icmp_sess,
248 packet_t *packet, services_t error)
249{
250 /* Detach the first packet and release the others */
251 packet_t *next = pq_detach(packet);
252 if (next)
253 pq_release_remote(packet_sess, packet_get_id(next));
254
255 uint8_t *src;
256 int length = packet_get_addr(packet, &src, NULL);
257 if ((length > 0) && (!error) && (icmp_sess) &&
258 /*
259 * Set both addresses to the source one (avoids the source address
260 * deletion before setting the destination one)
261 */
262 (packet_set_addr(packet, src, src, (size_t) length) == EOK)) {
263 return EOK;
264 } else
265 pq_release_remote(packet_sess, packet_get_id(packet));
266
267 return ENOENT;
268}
269
270/** Receives data from the socket into a packet.
271 *
272 * @param[in] sess Packet server module session.
273 * @param[out] packet New created packet.
274 * @param[in] prefix Reserved packet data prefix length.
275 * @param[in] dimension Packet dimension.
276 * @param[in] addr Destination address.
277 * @param[in] addrlen Address length.
278 *
279 * @return Number of bytes received.
280 * @return EINVAL if the client does not send data.
281 * @return ENOMEM if there is not enough memory left.
282 * @return Other error codes as defined for the
283 * async_data_read_finalize() function.
284 *
285 */
286int tl_socket_read_packet_data(async_sess_t *sess, packet_t **packet,
287 size_t prefix, const packet_dimension_t *dimension,
288 const struct sockaddr *addr, socklen_t addrlen)
289{
290 ipc_callid_t callid;
291 size_t length;
292 void *data;
293 int rc;
294
295 if (!dimension)
296 return EINVAL;
297
298 /* Get the data length */
299 if (!async_data_write_receive(&callid, &length))
300 return EINVAL;
301
302 /* Get a new packet */
303 *packet = packet_get_4_remote(sess, length, dimension->addr_len,
304 prefix + dimension->prefix, dimension->suffix);
305 if (!packet)
306 return ENOMEM;
307
308 /* Allocate space in the packet */
309 data = packet_suffix(*packet, length);
310 if (!data) {
311 pq_release_remote(sess, packet_get_id(*packet));
312 return ENOMEM;
313 }
314
315 /* Read the data into the packet */
316 rc = async_data_write_finalize(callid, data, length);
317 if (rc != EOK) {
318 pq_release_remote(sess, packet_get_id(*packet));
319 return rc;
320 }
321
322 /* Set the packet destination address */
323 rc = packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen);
324 if (rc != EOK) {
325 pq_release_remote(sess, packet_get_id(*packet));
326 return rc;
327 }
328
329 return (int) length;
330}
331
332/** @}
333 */
Note: See TracBrowser for help on using the repository browser.