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

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

Move net_device.h to standard library.

  • Property mode set to 100644
File size: 7.2 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 net_tl
30 * @{
31 */
32
33/** @file
34 * Transport layer common functions implementation.
35 * @see tl_common.h
36 */
37
38#include <net/socket_codes.h>
39#include <net/in.h>
40#include <net/in6.h>
41#include <net/inet.h>
42#include <async.h>
43#include <ipc/services.h>
44#include <errno.h>
45#include <err.h>
46
47#include <net/packet.h>
48#include <packet_client.h>
49#include <packet_remote.h>
50#include <net/device.h>
51#include <icmp_interface.h>
52#include <ip_remote.h>
53#include <ip_interface.h>
54#include <tl_interface.h>
55#include <tl_common.h>
56
57DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
58
59int
60tl_get_address_port(const struct sockaddr *addr, int addrlen, uint16_t *port)
61{
62 const struct sockaddr_in *address_in;
63 const struct sockaddr_in6 *address_in6;
64
65 if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr)))
66 return EINVAL;
67
68 switch (addr->sa_family) {
69 case AF_INET:
70 if (addrlen != sizeof(struct sockaddr_in))
71 return EINVAL;
72
73 address_in = (struct sockaddr_in *) addr;
74 *port = ntohs(address_in->sin_port);
75 break;
76 case AF_INET6:
77 if (addrlen != sizeof(struct sockaddr_in6))
78 return EINVAL;
79
80 address_in6 = (struct sockaddr_in6 *) addr;
81 *port = ntohs(address_in6->sin6_port);
82 break;
83 default:
84 return EAFNOSUPPORT;
85 }
86
87 return EOK;
88}
89
90/** Get IP packet dimensions.
91 *
92 * Try to search a cache and query the IP module if not found.
93 * The reply is cached then.
94 *
95 * @param[in] ip_phone The IP moduel phone for (semi)remote calls.
96 * @param[in] packet_dimensions The packet dimensions cache.
97 * @param[in] device_id The device identifier.
98 * @param[out] packet_dimension The IP packet dimensions.
99 *
100 * @return EOK on success.
101 * @return EBADMEM if the packet_dimension parameter is NULL.
102 * @return ENOMEM if there is not enough memory left.
103 * @return EINVAL if the packet_dimensions cache is not valid.
104 * @return Other codes as defined for the ip_packet_size_req() function.
105 *
106 */
107int tl_get_ip_packet_dimension(int ip_phone,
108 packet_dimensions_ref packet_dimensions, device_id_t device_id,
109 packet_dimension_ref *packet_dimension)
110{
111 ERROR_DECLARE;
112
113 if (!packet_dimension)
114 return EBADMEM;
115
116 *packet_dimension = packet_dimensions_find(packet_dimensions,
117 device_id);
118 if (!*packet_dimension) {
119 /* Ask for and remember them if not found */
120 *packet_dimension = malloc(sizeof(**packet_dimension));
121 if(!*packet_dimension)
122 return ENOMEM;
123
124 if (ERROR_OCCURRED(ip_packet_size_req(ip_phone, device_id,
125 *packet_dimension))) {
126 free(*packet_dimension);
127 return ERROR_CODE;
128 }
129
130 ERROR_CODE = packet_dimensions_add(packet_dimensions, device_id,
131 *packet_dimension);
132 if (ERROR_CODE < 0) {
133 free(*packet_dimension);
134 return ERROR_CODE;
135 }
136 }
137
138 return EOK;
139}
140
141int
142tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions,
143 device_id_t device_id, size_t content)
144{
145 packet_dimension_ref packet_dimension;
146
147 packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
148 if (!packet_dimension)
149 return ENOENT;
150 packet_dimension->content = content;
151
152 if (device_id != DEVICE_INVALID_ID) {
153 packet_dimension = packet_dimensions_find(packet_dimensions,
154 DEVICE_INVALID_ID);
155
156 if (packet_dimension) {
157 if (packet_dimension->content >= content)
158 packet_dimension->content = content;
159 else
160 packet_dimensions_exclude(packet_dimensions,
161 DEVICE_INVALID_ID);
162
163 }
164 }
165
166 return EOK;
167}
168
169int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port)
170{
171 struct sockaddr_in *address_in;
172 struct sockaddr_in6 *address_in6;
173 size_t length;
174
175 if (addrlen < 0)
176 return EINVAL;
177
178 length = (size_t) addrlen;
179 if (length < sizeof(struct sockaddr))
180 return EINVAL;
181
182 switch (addr->sa_family) {
183 case AF_INET:
184 if (length != sizeof(struct sockaddr_in))
185 return EINVAL;
186 address_in = (struct sockaddr_in *) addr;
187 address_in->sin_port = htons(port);
188 return EOK;
189 case AF_INET6:
190 if (length != sizeof(struct sockaddr_in6))
191 return EINVAL;
192 address_in6 = (struct sockaddr_in6 *) addr;
193 address_in6->sin6_port = htons(port);
194 return EOK;
195 default:
196 return EAFNOSUPPORT;
197 }
198}
199
200int
201tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet,
202 services_t error)
203{
204 packet_t next;
205 uint8_t *src;
206 int length;
207
208 // detach the first packet and release the others
209 next = pq_detach(packet);
210 if (next)
211 pq_release_remote(packet_phone, packet_get_id(next));
212
213 length = packet_get_addr(packet, &src, NULL);
214 if ((length > 0) && (!error) && (icmp_phone >= 0) &&
215 // set both addresses to the source one (avoids the source address
216 // deletion before setting the destination one)
217 (packet_set_addr(packet, src, src, (size_t) length) == EOK)) {
218 return EOK;
219 } else
220 pq_release_remote(packet_phone, packet_get_id(packet));
221
222 return ENOENT;
223}
224
225int
226tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix,
227 const packet_dimension_ref dimension, const struct sockaddr *addr,
228 socklen_t addrlen)
229{
230 ERROR_DECLARE;
231
232 ipc_callid_t callid;
233 size_t length;
234 void * data;
235
236 if (!dimension)
237 return EINVAL;
238
239 // get the data length
240 if (!async_data_write_receive(&callid, &length))
241 return EINVAL;
242
243 // get a new packet
244 *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len,
245 prefix + dimension->prefix, dimension->suffix);
246 if (!packet)
247 return ENOMEM;
248
249 // allocate space in the packet
250 data = packet_suffix(*packet, length);
251 if (!data) {
252 pq_release_remote(packet_phone, packet_get_id(*packet));
253 return ENOMEM;
254 }
255
256 // read the data into the packet
257 if (ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) ||
258 // set the packet destination address
259 ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr,
260 addrlen))) {
261 pq_release_remote(packet_phone, packet_get_id(*packet));
262 return ERROR_CODE;
263 }
264
265 return (int) length;
266}
267
268/** @}
269 */
Note: See TracBrowser for help on using the repository browser.