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