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