Rev 4743 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4743 | Rev 4750 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Lukas Mejdrech |
2 | * Copyright (c) 2008 Lukas Mejdrech |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup net_tl |
29 | /** @addtogroup net_tl |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
34 | * Transport layer common functions implementation. |
34 | * Transport layer common functions implementation. |
35 | * @see tl_common.h |
35 | * @see tl_common.h |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <async.h> |
38 | #include <async.h> |
39 | #include <ipc/services.h> |
39 | #include <ipc/services.h> |
40 | 40 | ||
41 | #include "../err.h" |
41 | #include "../err.h" |
42 | 42 | ||
43 | #include "../structures/packet/packet.h" |
43 | #include "../structures/packet/packet.h" |
44 | #include "../structures/packet/packet_client.h" |
44 | #include "../structures/packet/packet_client.h" |
45 | 45 | ||
46 | #include "../include/icmp_interface.h" |
46 | #include "../include/icmp_interface.h" |
47 | #include "../include/in.h" |
47 | #include "../include/in.h" |
48 | #include "../include/in6.h" |
48 | #include "../include/in6.h" |
49 | #include "../include/inet.h" |
49 | #include "../include/inet.h" |
- | 50 | #include "../include/ip_interface.h" |
|
50 | #include "../include/socket_codes.h" |
51 | #include "../include/socket_codes.h" |
51 | #include "../include/socket_errno.h" |
52 | #include "../include/socket_errno.h" |
52 | 53 | ||
53 | #include "tl_common.h" |
54 | #include "tl_common.h" |
54 | 55 | ||
- | 56 | DEVICE_MAP_IMPLEMENT( packet_dimensions, packet_dimension_t ); |
|
- | 57 | ||
55 | int tl_get_address_port( const struct sockaddr * addr, int addrlen, uint16_t * port ){ |
58 | int tl_get_address_port( const struct sockaddr * addr, int addrlen, uint16_t * port ){ |
56 | const struct sockaddr_in * address_in; |
59 | const struct sockaddr_in * address_in; |
57 | const struct sockaddr_in6 * address_in6; |
60 | const struct sockaddr_in6 * address_in6; |
58 | 61 | ||
59 | if(( addrlen <= 0 ) || (( size_t ) addrlen < sizeof( struct sockaddr ))) return EINVAL; |
62 | if(( addrlen <= 0 ) || (( size_t ) addrlen < sizeof( struct sockaddr ))) return EINVAL; |
60 | switch( addr->sa_family ){ |
63 | switch( addr->sa_family ){ |
61 | case AF_INET: |
64 | case AF_INET: |
62 | if( addrlen != sizeof( struct sockaddr_in )) return EINVAL; |
65 | if( addrlen != sizeof( struct sockaddr_in )) return EINVAL; |
63 | address_in = ( struct sockaddr_in * ) addr; |
66 | address_in = ( struct sockaddr_in * ) addr; |
64 | * port = ntohs( address_in->sin_port ); |
67 | * port = ntohs( address_in->sin_port ); |
65 | break; |
68 | break; |
66 | case AF_INET6: |
69 | case AF_INET6: |
67 | if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL; |
70 | if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL; |
68 | address_in6 = ( struct sockaddr_in6 * ) addr; |
71 | address_in6 = ( struct sockaddr_in6 * ) addr; |
69 | * port = ntohs( address_in6->sin6_port ); |
72 | * port = ntohs( address_in6->sin6_port ); |
70 | break; |
73 | break; |
71 | default: |
74 | default: |
72 | return EAFNOSUPPORT; |
75 | return EAFNOSUPPORT; |
73 | } |
76 | } |
- | 77 | return EOK; |
|
- | 78 | } |
|
- | 79 | ||
- | 80 | int tl_get_ip_packet_dimension( int ip_phone, packet_dimensions_ref packet_dimensions, device_id_t device_id, packet_dimension_ref * packet_dimension ){ |
|
- | 81 | ERROR_DECLARE; |
|
- | 82 | ||
- | 83 | if( ! packet_dimension ) return EBADMEM; |
|
- | 84 | ||
- | 85 | * packet_dimension = packet_dimensions_find( packet_dimensions, device_id ); |
|
- | 86 | if( ! * packet_dimension ){ |
|
- | 87 | // ask for and remember them if not found |
|
- | 88 | * packet_dimension = malloc( sizeof( ** packet_dimension )); |
|
- | 89 | if( ! * packet_dimension ) return ENOMEM; |
|
- | 90 | if( ERROR_OCCURRED( ip_packet_size_req( ip_phone, device_id, & ( ** packet_dimension ).addr_len, & ( ** packet_dimension ).prefix, & ( ** packet_dimension ).content, & ( ** packet_dimension ).suffix ))){ |
|
- | 91 | free( * packet_dimension ); |
|
- | 92 | return ERROR_CODE; |
|
- | 93 | } |
|
- | 94 | ERROR_CODE = packet_dimensions_add( packet_dimensions, device_id, * packet_dimension ); |
|
- | 95 | if( ERROR_CODE < 0 ){ |
|
- | 96 | free( * packet_dimension ); |
|
- | 97 | return ERROR_CODE; |
|
- | 98 | } |
|
- | 99 | } |
|
74 | return EOK; |
100 | return EOK; |
75 | } |
101 | } |
76 | 102 | ||
77 | int tl_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){ |
103 | int tl_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){ |
78 | struct sockaddr_in * address_in; |
104 | struct sockaddr_in * address_in; |
79 | struct sockaddr_in6 * address_in6; |
105 | struct sockaddr_in6 * address_in6; |
80 | size_t length; |
106 | size_t length; |
81 | 107 | ||
82 | if( addrlen < 0 ) return EINVAL; |
108 | if( addrlen < 0 ) return EINVAL; |
83 | length = ( size_t ) addrlen; |
109 | length = ( size_t ) addrlen; |
84 | if( length < sizeof( struct sockaddr )) return EINVAL; |
110 | if( length < sizeof( struct sockaddr )) return EINVAL; |
85 | switch( addr->sa_family ){ |
111 | switch( addr->sa_family ){ |
86 | case AF_INET: |
112 | case AF_INET: |
87 | if( length != sizeof( struct sockaddr_in )) return EINVAL; |
113 | if( length != sizeof( struct sockaddr_in )) return EINVAL; |
88 | address_in = ( struct sockaddr_in * ) addr; |
114 | address_in = ( struct sockaddr_in * ) addr; |
89 | address_in->sin_port = htons( port ); |
115 | address_in->sin_port = htons( port ); |
90 | return EOK; |
116 | return EOK; |
91 | case AF_INET6: |
117 | case AF_INET6: |
92 | if( length != sizeof( struct sockaddr_in6 )) return EINVAL; |
118 | if( length != sizeof( struct sockaddr_in6 )) return EINVAL; |
93 | address_in6 = ( struct sockaddr_in6 * ) addr; |
119 | address_in6 = ( struct sockaddr_in6 * ) addr; |
94 | address_in6->sin6_port = htons( port ); |
120 | address_in6->sin6_port = htons( port ); |
95 | return EOK; |
121 | return EOK; |
96 | default: |
122 | default: |
97 | return EAFNOSUPPORT; |
123 | return EAFNOSUPPORT; |
98 | } |
124 | } |
99 | } |
125 | } |
100 | 126 | ||
101 | int tl_prepare_icmp_packet( int packet_phone, int icmp_phone, packet_t packet, services_t error ){ |
127 | int tl_prepare_icmp_packet( int packet_phone, int icmp_phone, packet_t packet, services_t error ){ |
102 | packet_t next; |
128 | packet_t next; |
103 | uint8_t * src; |
129 | uint8_t * src; |
104 | int length; |
130 | int length; |
105 | 131 | ||
106 | // detach the first packet and release the others |
132 | // detach the first packet and release the others |
107 | next = pq_detach( packet ); |
133 | next = pq_detach( packet ); |
108 | if( next ){ |
134 | if( next ){ |
109 | pq_release( packet_phone, packet_get_id( next )); |
135 | pq_release( packet_phone, packet_get_id( next )); |
110 | } |
136 | } |
111 | length = packet_get_addr( packet, & src, NULL ); |
137 | length = packet_get_addr( packet, & src, NULL ); |
112 | if(( length > 0 ) |
138 | if(( length > 0 ) |
113 | && ( ! error ) |
139 | && ( ! error ) |
114 | && ( icmp_phone >= 0 ) |
140 | && ( icmp_phone >= 0 ) |
115 | // set both addresses to the source one (avoids the source address deletion before setting the destination one) |
141 | // set both addresses to the source one (avoids the source address deletion before setting the destination one) |
116 | && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){ |
142 | && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){ |
117 | return EOK; |
143 | return EOK; |
118 | }else{ |
144 | }else{ |
119 | pq_release( packet_phone, packet_get_id( packet )); |
145 | pq_release( packet_phone, packet_get_id( packet )); |
120 | } |
146 | } |
121 | return ENOENT; |
147 | return ENOENT; |
122 | } |
148 | } |
123 | 149 | ||
124 | 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 ){ |
150 | 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 ){ |
125 | ERROR_DECLARE; |
151 | ERROR_DECLARE; |
126 | 152 | ||
127 | ipc_callid_t callid; |
153 | ipc_callid_t callid; |
128 | size_t length; |
154 | size_t length; |
129 | void * data; |
155 | void * data; |
130 | 156 | ||
131 | if( ! dimension ) return EINVAL; |
157 | if( ! dimension ) return EINVAL; |
132 | // get the data length |
158 | // get the data length |
133 | if( ! ipc_data_write_receive( & callid, & length )) return EINVAL; |
159 | if( ! ipc_data_write_receive( & callid, & length )) return EINVAL; |
134 | // get a new packet |
160 | // get a new packet |
135 | * packet = packet_get_4( packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix ); |
161 | * packet = packet_get_4( packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix ); |
136 | if( ! packet ) return ENOMEM; |
162 | if( ! packet ) return ENOMEM; |
137 | // allocate space in the packet |
163 | // allocate space in the packet |
138 | data = packet_suffix( * packet, length ); |
164 | data = packet_suffix( * packet, length ); |
139 | if( ! data ){ |
165 | if( ! data ){ |
140 | pq_release( packet_phone, packet_get_id( * packet )); |
166 | pq_release( packet_phone, packet_get_id( * packet )); |
141 | return ENOMEM; |
167 | return ENOMEM; |
142 | } |
168 | } |
143 | // read the data into the packet |
169 | // read the data into the packet |
144 | if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length )) |
170 | if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length )) |
145 | // set the packet destination address |
171 | // set the packet destination address |
146 | || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen ))){ |
172 | || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen ))){ |
147 | pq_release( packet_phone, packet_get_id( * packet )); |
173 | pq_release( packet_phone, packet_get_id( * packet )); |
148 | return ERROR_CODE; |
174 | return ERROR_CODE; |
149 | } |
175 | } |
150 | return ( int ) length; |
176 | return ( int ) length; |
151 | } |
177 | } |
152 | 178 | ||
153 | /** @} |
179 | /** @} |
154 | */ |
180 | */ |
155 | 181 |