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 "../err.h"
|
---|
42 |
|
---|
43 | #include "../structures/packet/packet.h"
|
---|
44 | #include "../structures/packet/packet_client.h"
|
---|
45 |
|
---|
46 | #include "../include/device.h"
|
---|
47 | #include "../include/icmp_interface.h"
|
---|
48 | #include "../include/in.h"
|
---|
49 | #include "../include/in6.h"
|
---|
50 | #include "../include/inet.h"
|
---|
51 | #include "../include/ip_interface.h"
|
---|
52 | #include "../include/socket_codes.h"
|
---|
53 | #include "../include/socket_errno.h"
|
---|
54 |
|
---|
55 | #include "tl_common.h"
|
---|
56 |
|
---|
57 | DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
|
---|
58 |
|
---|
59 | int tl_get_address_port(const struct sockaddr * addr, int addrlen, uint16_t * port){
|
---|
60 | const struct sockaddr_in * address_in;
|
---|
61 | const struct sockaddr_in6 * address_in6;
|
---|
62 |
|
---|
63 | if((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))){
|
---|
64 | return EINVAL;
|
---|
65 | }
|
---|
66 | switch(addr->sa_family){
|
---|
67 | case AF_INET:
|
---|
68 | if(addrlen != sizeof(struct sockaddr_in)){
|
---|
69 | return EINVAL;
|
---|
70 | }
|
---|
71 | address_in = (struct sockaddr_in *) addr;
|
---|
72 | *port = ntohs(address_in->sin_port);
|
---|
73 | break;
|
---|
74 | case AF_INET6:
|
---|
75 | if(addrlen != sizeof(struct sockaddr_in6)){
|
---|
76 | return EINVAL;
|
---|
77 | }
|
---|
78 | address_in6 = (struct sockaddr_in6 *) addr;
|
---|
79 | *port = ntohs(address_in6->sin6_port);
|
---|
80 | break;
|
---|
81 | default:
|
---|
82 | return EAFNOSUPPORT;
|
---|
83 | }
|
---|
84 | return EOK;
|
---|
85 | }
|
---|
86 |
|
---|
87 | int tl_get_ip_packet_dimension(int ip_phone, packet_dimensions_ref packet_dimensions, device_id_t device_id, packet_dimension_ref * packet_dimension){
|
---|
88 | ERROR_DECLARE;
|
---|
89 |
|
---|
90 | if(! packet_dimension){
|
---|
91 | return EBADMEM;
|
---|
92 | }
|
---|
93 |
|
---|
94 | *packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
|
---|
95 | if(! * packet_dimension){
|
---|
96 | // ask for and remember them if not found
|
---|
97 | *packet_dimension = malloc(sizeof(** packet_dimension));
|
---|
98 | if(! * packet_dimension){
|
---|
99 | return ENOMEM;
|
---|
100 | }
|
---|
101 | if(ERROR_OCCURRED(ip_packet_size_req(ip_phone, device_id, * packet_dimension))){
|
---|
102 | free(*packet_dimension);
|
---|
103 | return ERROR_CODE;
|
---|
104 | }
|
---|
105 | ERROR_CODE = packet_dimensions_add(packet_dimensions, device_id, * packet_dimension);
|
---|
106 | if(ERROR_CODE < 0){
|
---|
107 | free(*packet_dimension);
|
---|
108 | return ERROR_CODE;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | return EOK;
|
---|
112 | }
|
---|
113 |
|
---|
114 | int tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content){
|
---|
115 | packet_dimension_ref packet_dimension;
|
---|
116 |
|
---|
117 | packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
|
---|
118 | if(! packet_dimension){
|
---|
119 | return ENOENT;
|
---|
120 | }
|
---|
121 | packet_dimension->content = content;
|
---|
122 | if(device_id != DEVICE_INVALID_ID){
|
---|
123 | packet_dimension = packet_dimensions_find(packet_dimensions, DEVICE_INVALID_ID);
|
---|
124 | if(packet_dimension){
|
---|
125 | if(packet_dimension->content >= content){
|
---|
126 | packet_dimension->content = content;
|
---|
127 | }else{
|
---|
128 | packet_dimensions_exclude(packet_dimensions, DEVICE_INVALID_ID);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | return EOK;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port){
|
---|
136 | struct sockaddr_in * address_in;
|
---|
137 | struct sockaddr_in6 * address_in6;
|
---|
138 | size_t length;
|
---|
139 |
|
---|
140 | if(addrlen < 0){
|
---|
141 | return EINVAL;
|
---|
142 | }
|
---|
143 | length = (size_t) addrlen;
|
---|
144 | if(length < sizeof(struct sockaddr)){
|
---|
145 | return EINVAL;
|
---|
146 | }
|
---|
147 | switch(addr->sa_family){
|
---|
148 | case AF_INET:
|
---|
149 | if(length != sizeof(struct sockaddr_in)){
|
---|
150 | return EINVAL;
|
---|
151 | }
|
---|
152 | address_in = (struct sockaddr_in *) addr;
|
---|
153 | address_in->sin_port = htons(port);
|
---|
154 | return EOK;
|
---|
155 | case AF_INET6:
|
---|
156 | if(length != sizeof(struct sockaddr_in6)){
|
---|
157 | return EINVAL;
|
---|
158 | }
|
---|
159 | address_in6 = (struct sockaddr_in6 *) addr;
|
---|
160 | address_in6->sin6_port = htons(port);
|
---|
161 | return EOK;
|
---|
162 | default:
|
---|
163 | return EAFNOSUPPORT;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | int tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, services_t error){
|
---|
168 | packet_t next;
|
---|
169 | uint8_t * src;
|
---|
170 | int length;
|
---|
171 |
|
---|
172 | // detach the first packet and release the others
|
---|
173 | next = pq_detach(packet);
|
---|
174 | if(next){
|
---|
175 | pq_release(packet_phone, packet_get_id(next));
|
---|
176 | }
|
---|
177 | length = packet_get_addr(packet, &src, NULL);
|
---|
178 | if((length > 0)
|
---|
179 | && (! error)
|
---|
180 | && (icmp_phone >= 0)
|
---|
181 | // set both addresses to the source one (avoids the source address deletion before setting the destination one)
|
---|
182 | && (packet_set_addr(packet, src, src, (size_t) length) == EOK)){
|
---|
183 | return EOK;
|
---|
184 | }else{
|
---|
185 | pq_release(packet_phone, packet_get_id(packet));
|
---|
186 | }
|
---|
187 | return ENOENT;
|
---|
188 | }
|
---|
189 |
|
---|
190 | int tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen){
|
---|
191 | ERROR_DECLARE;
|
---|
192 |
|
---|
193 | ipc_callid_t callid;
|
---|
194 | size_t length;
|
---|
195 | void * data;
|
---|
196 |
|
---|
197 | if(! dimension){
|
---|
198 | return EINVAL;
|
---|
199 | }
|
---|
200 | // get the data length
|
---|
201 | if(! async_data_write_receive(&callid, &length)){
|
---|
202 | return EINVAL;
|
---|
203 | }
|
---|
204 | // get a new packet
|
---|
205 | *packet = packet_get_4(packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix);
|
---|
206 | if(! packet){
|
---|
207 | return ENOMEM;
|
---|
208 | }
|
---|
209 | // allocate space in the packet
|
---|
210 | data = packet_suffix(*packet, length);
|
---|
211 | if(! data){
|
---|
212 | pq_release(packet_phone, packet_get_id(*packet));
|
---|
213 | return ENOMEM;
|
---|
214 | }
|
---|
215 | // read the data into the packet
|
---|
216 | if(ERROR_OCCURRED(async_data_write_finalize(callid, data, length))
|
---|
217 | // set the packet destination address
|
---|
218 | || ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen))){
|
---|
219 | pq_release(packet_phone, packet_get_id(*packet));
|
---|
220 | return ERROR_CODE;
|
---|
221 | }
|
---|
222 | return (int) length;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /** @}
|
---|
226 | */
|
---|