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
RevLine 
[21580dd]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
[f1938c6]29/** @addtogroup libnet
[84c20da]30 * @{
[21580dd]31 */
32
33/** @file
[84c20da]34 * Transport layer common functions implementation.
35 * @see tl_common.h
[21580dd]36 */
37
[84c20da]38#include <tl_common.h>
39#include <packet_client.h>
40#include <packet_remote.h>
[f1938c6]41#include <icmp_remote.h>
[84c20da]42#include <ip_remote.h>
43#include <ip_interface.h>
[014dd57b]44#include <tl_remote.h>
[058edb6]45#include <net/socket_codes.h>
[e4554d4]46#include <net/in.h>
47#include <net/in6.h>
48#include <net/inet.h>
[84c20da]49#include <net/device.h>
50#include <net/packet.h>
[21580dd]51#include <async.h>
52#include <ipc/services.h>
[e98b1d5]53#include <errno.h>
[21580dd]54
[aadf01e]55DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
[21580dd]56
[84c20da]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.
[1bfd3d3]64 * @return EOK on success.
65 * @return EINVAL if the address length does not match the address
[84c20da]66 * family.
[1bfd3d3]67 * @return EAFNOSUPPORT if the address family is not supported.
[84c20da]68 */
[2fa0ad9]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;
[21580dd]74
[2fa0ad9]75 if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr)))
[aadf01e]76 return EINVAL;
[2fa0ad9]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;
[84c20da]86
[2fa0ad9]87 case AF_INET6:
88 if (addrlen != sizeof(struct sockaddr_in6))
[84c20da]89 return EINVAL;
[2fa0ad9]90
91 address_in6 = (struct sockaddr_in6 *) addr;
92 *port = ntohs(address_in6->sin6_port);
93 break;
[84c20da]94
[2fa0ad9]95 default:
96 return EAFNOSUPPORT;
[21580dd]97 }
[2fa0ad9]98
[21580dd]99 return EOK;
100}
101
[14f1db0]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 *
[6b82009]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 *
[14f1db0]119 */
[6b82009]120int tl_get_ip_packet_dimension(async_sess_t *sess,
[609243f4]121 packet_dimensions_t *packet_dimensions, nic_device_id_t device_id,
[f772bc55]122 packet_dimension_t **packet_dimension)
[14f1db0]123{
124 if (!packet_dimension)
[aadf01e]125 return EBADMEM;
[14f1db0]126
[2fa0ad9]127 *packet_dimension = packet_dimensions_find(packet_dimensions,
128 device_id);
[14f1db0]129 if (!*packet_dimension) {
130 /* Ask for and remember them if not found */
131 *packet_dimension = malloc(sizeof(**packet_dimension));
[6b82009]132 if (!*packet_dimension)
[aadf01e]133 return ENOMEM;
[14f1db0]134
[6b82009]135 int rc = ip_packet_size_req(sess, device_id, *packet_dimension);
[0ab68f6]136 if (rc != EOK) {
[aadf01e]137 free(*packet_dimension);
[0ab68f6]138 return rc;
[21580dd]139 }
[14f1db0]140
[0ab68f6]141 rc = packet_dimensions_add(packet_dimensions, device_id,
[14f1db0]142 *packet_dimension);
[0ab68f6]143 if (rc < 0) {
[aadf01e]144 free(*packet_dimension);
[0ab68f6]145 return rc;
[21580dd]146 }
147 }
[14f1db0]148
[21580dd]149 return EOK;
150}
151
[84c20da]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.
[1bfd3d3]157 * @return EOK on success.
[84c20da]158 * @return ENOENT if the packet dimension is not cached.
159 */
[9b6be51]160int tl_update_ip_packet_dimension(packet_dimensions_t *packet_dimensions,
[609243f4]161 nic_device_id_t device_id, size_t content)
[2fa0ad9]162{
[f772bc55]163 packet_dimension_t *packet_dimension;
[b4ff8a6d]164
[aadf01e]165 packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
[2fa0ad9]166 if (!packet_dimension)
[aadf01e]167 return ENOENT;
[84c20da]168
[b4ff8a6d]169 packet_dimension->content = content;
[2fa0ad9]170
[609243f4]171 if (device_id != NIC_DEVICE_INVALID_ID) {
[2fa0ad9]172 packet_dimension = packet_dimensions_find(packet_dimensions,
[609243f4]173 NIC_DEVICE_INVALID_ID);
[2fa0ad9]174
175 if (packet_dimension) {
176 if (packet_dimension->content >= content)
[b4ff8a6d]177 packet_dimension->content = content;
[2fa0ad9]178 else
179 packet_dimensions_exclude(packet_dimensions,
[609243f4]180 NIC_DEVICE_INVALID_ID, free);
[b4ff8a6d]181 }
182 }
[2fa0ad9]183
[b4ff8a6d]184 return EOK;
185}
186
[84c20da]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.
[1bfd3d3]194 * @return EOK on success.
195 * @return EINVAL if the address length does not match the address
[84c20da]196 * family.
[1bfd3d3]197 * @return EAFNOSUPPORT if the address family is not supported.
[84c20da]198 */
[7c8267b]199int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port)
200{
[2fa0ad9]201 struct sockaddr_in *address_in;
202 struct sockaddr_in6 *address_in6;
[aadf01e]203 size_t length;
[21580dd]204
[7c8267b]205 if (addrlen < 0)
[aadf01e]206 return EINVAL;
[7c8267b]207
[aadf01e]208 length = (size_t) addrlen;
[7c8267b]209 if (length < sizeof(struct sockaddr))
[aadf01e]210 return EINVAL;
[7c8267b]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;
[84c20da]219
[7c8267b]220 case AF_INET6:
221 if (length != sizeof(struct sockaddr_in6))
[aadf01e]222 return EINVAL;
[7c8267b]223 address_in6 = (struct sockaddr_in6 *) addr;
224 address_in6->sin6_port = htons(port);
225 return EOK;
[84c20da]226
[7c8267b]227 default:
228 return EAFNOSUPPORT;
[21580dd]229 }
230}
231
[84c20da]232/** Prepares the packet for ICMP error notification.
233 *
[6b82009]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.
[84c20da]245 *
246 */
[6b82009]247int tl_prepare_icmp_packet(async_sess_t *packet_sess, async_sess_t *icmp_sess,
248 packet_t *packet, services_t error)
[2fa0ad9]249{
[28a3e74]250 /* Detach the first packet and release the others */
[6b82009]251 packet_t *next = pq_detach(packet);
[14f1db0]252 if (next)
[6b82009]253 pq_release_remote(packet_sess, packet_get_id(next));
[14f1db0]254
[6b82009]255 uint8_t *src;
256 int length = packet_get_addr(packet, &src, NULL);
257 if ((length > 0) && (!error) && (icmp_sess) &&
[28a3e74]258 /*
259 * Set both addresses to the source one (avoids the source address
260 * deletion before setting the destination one)
261 */
[2fa0ad9]262 (packet_set_addr(packet, src, src, (size_t) length) == EOK)) {
[21580dd]263 return EOK;
[2fa0ad9]264 } else
[6b82009]265 pq_release_remote(packet_sess, packet_get_id(packet));
266
[21580dd]267 return ENOENT;
268}
269
[84c20da]270/** Receives data from the socket into a packet.
271 *
[6b82009]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 *
[84c20da]285 */
[6b82009]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)
[2fa0ad9]289{
[aadf01e]290 ipc_callid_t callid;
291 size_t length;
[0ab68f6]292 void *data;
293 int rc;
[21580dd]294
[2fa0ad9]295 if (!dimension)
[aadf01e]296 return EINVAL;
[2fa0ad9]297
[28a3e74]298 /* Get the data length */
[2fa0ad9]299 if (!async_data_write_receive(&callid, &length))
[aadf01e]300 return EINVAL;
[2fa0ad9]301
[28a3e74]302 /* Get a new packet */
[6b82009]303 *packet = packet_get_4_remote(sess, length, dimension->addr_len,
[2fa0ad9]304 prefix + dimension->prefix, dimension->suffix);
305 if (!packet)
[aadf01e]306 return ENOMEM;
[2fa0ad9]307
[28a3e74]308 /* Allocate space in the packet */
[aadf01e]309 data = packet_suffix(*packet, length);
[2fa0ad9]310 if (!data) {
[6b82009]311 pq_release_remote(sess, packet_get_id(*packet));
[21580dd]312 return ENOMEM;
313 }
[2fa0ad9]314
[28a3e74]315 /* Read the data into the packet */
[0ab68f6]316 rc = async_data_write_finalize(callid, data, length);
317 if (rc != EOK) {
[6b82009]318 pq_release_remote(sess, packet_get_id(*packet));
[0ab68f6]319 return rc;
320 }
321
[28a3e74]322 /* Set the packet destination address */
[0ab68f6]323 rc = packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen);
324 if (rc != EOK) {
[6b82009]325 pq_release_remote(sess, packet_get_id(*packet));
[0ab68f6]326 return rc;
[21580dd]327 }
[2fa0ad9]328
[aadf01e]329 return (int) length;
[21580dd]330}
331
332/** @}
333 */
Note: See TracBrowser for help on using the repository browser.