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