Rev 4731 | Rev 4743 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4731 | Rev 4738 | ||
---|---|---|---|
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/socket_codes.h" |
50 | #include "../include/socket_codes.h" |
51 | #include "../include/socket_errno.h" |
51 | #include "../include/socket_errno.h" |
52 | 52 | ||
53 | #include "tl_common.h" |
53 | #include "tl_common.h" |
54 | 54 | ||
55 | int tl_get_address_port( const struct sockaddr * addr, int addrlen, uint16_t * port ){ |
55 | int tl_get_address_port( const struct sockaddr * addr, int addrlen, uint16_t * port ){ |
56 | const struct sockaddr_in * address_in; |
56 | const struct sockaddr_in * address_in; |
57 | const struct sockaddr_in6 * address_in6; |
57 | const struct sockaddr_in6 * address_in6; |
58 | 58 | ||
59 | if( addrlen < sizeof( struct sockaddr )) return EINVAL; |
59 | if( addrlen < sizeof( struct sockaddr )) return EINVAL; |
60 | switch( addr->sa_family ){ |
60 | switch( addr->sa_family ){ |
61 | case AF_INET: |
61 | case AF_INET: |
62 | if( addrlen != sizeof( struct sockaddr_in )) return EINVAL; |
62 | if( addrlen != sizeof( struct sockaddr_in )) return EINVAL; |
63 | address_in = ( struct sockaddr_in * ) addr; |
63 | address_in = ( struct sockaddr_in * ) addr; |
64 | * port = address_in->sin_port; |
64 | * port = ntohs( address_in->sin_port ); |
65 | break; |
65 | break; |
66 | case AF_INET6: |
66 | case AF_INET6: |
67 | if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL; |
67 | if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL; |
68 | address_in6 = ( struct sockaddr_in6 * ) addr; |
68 | address_in6 = ( struct sockaddr_in6 * ) addr; |
69 | * port = address_in6->sin6_port; |
69 | * port = ntohs( address_in6->sin6_port ); |
70 | break; |
70 | break; |
71 | default: |
71 | default: |
72 | return EAFNOSUPPORT; |
72 | return EAFNOSUPPORT; |
73 | } |
73 | } |
74 | return EOK; |
74 | return EOK; |
75 | } |
75 | } |
76 | 76 | ||
77 | - | ||
78 | int tl_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){ |
77 | int tl_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){ |
79 | struct sockaddr_in * address_in; |
78 | struct sockaddr_in * address_in; |
80 | struct sockaddr_in6 * address_in6; |
79 | struct sockaddr_in6 * address_in6; |
81 | size_t length; |
80 | size_t length; |
82 | 81 | ||
83 | if( addrlen < 0 ) return EINVAL; |
82 | if( addrlen < 0 ) return EINVAL; |
84 | length = ( size_t ) addrlen; |
83 | length = ( size_t ) addrlen; |
85 | if( length < sizeof( struct sockaddr )) return EINVAL; |
84 | if( length < sizeof( struct sockaddr )) return EINVAL; |
86 | switch( addr->sa_family ){ |
85 | switch( addr->sa_family ){ |
87 | case AF_INET: |
86 | case AF_INET: |
88 | if( length != sizeof( struct sockaddr_in )) return EINVAL; |
87 | if( length != sizeof( struct sockaddr_in )) return EINVAL; |
89 | address_in = ( struct sockaddr_in * ) addr; |
88 | address_in = ( struct sockaddr_in * ) addr; |
90 | address_in->sin_port = port; |
89 | address_in->sin_port = htons( port ); |
91 | return EOK; |
90 | return EOK; |
92 | case AF_INET6: |
91 | case AF_INET6: |
93 | if( length != sizeof( struct sockaddr_in6 )) return EINVAL; |
92 | if( length != sizeof( struct sockaddr_in6 )) return EINVAL; |
94 | address_in6 = ( struct sockaddr_in6 * ) addr; |
93 | address_in6 = ( struct sockaddr_in6 * ) addr; |
95 | address_in6->sin6_port = port; |
94 | address_in6->sin6_port = htons( port ); |
96 | return EOK; |
95 | return EOK; |
97 | default: |
96 | default: |
98 | return EAFNOSUPPORT; |
97 | return EAFNOSUPPORT; |
99 | } |
98 | } |
100 | } |
99 | } |
101 | 100 | ||
102 | int tl_prepare_icmp_packet( int packet_phone, int icmp_phone, packet_t packet, services_t error ){ |
101 | int tl_prepare_icmp_packet( int packet_phone, int icmp_phone, packet_t packet, services_t error ){ |
103 | packet_t next; |
102 | packet_t next; |
104 | uint8_t * src; |
103 | uint8_t * src; |
105 | int length; |
104 | int length; |
106 | 105 | ||
107 | // detach the first packet and release the others |
106 | // detach the first packet and release the others |
108 | next = pq_detach( packet ); |
107 | next = pq_detach( packet ); |
109 | if( next ){ |
108 | if( next ){ |
110 | pq_release( packet_phone, packet_get_id( next )); |
109 | pq_release( packet_phone, packet_get_id( next )); |
111 | } |
110 | } |
112 | length = packet_get_addr( packet, & src, NULL ); |
111 | length = packet_get_addr( packet, & src, NULL ); |
113 | if(( length > 0 ) |
112 | if(( length > 0 ) |
114 | && ( ! error ) |
113 | && ( ! error ) |
115 | && ( icmp_phone >= 0 ) |
114 | && ( icmp_phone >= 0 ) |
116 | // set both addresses to the source one (avoids the source address deletion before setting the destination one) |
115 | // set both addresses to the source one (avoids the source address deletion before setting the destination one) |
117 | && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){ |
116 | && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){ |
118 | return EOK; |
117 | return EOK; |
119 | }else{ |
118 | }else{ |
120 | pq_release( packet_phone, packet_get_id( packet )); |
119 | pq_release( packet_phone, packet_get_id( packet )); |
121 | } |
120 | } |
122 | return ENOENT; |
121 | return ENOENT; |
123 | } |
122 | } |
124 | 123 | ||
125 | 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 ){ |
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 ){ |
126 | ERROR_DECLARE; |
125 | ERROR_DECLARE; |
127 | 126 | ||
128 | ipc_callid_t callid; |
127 | ipc_callid_t callid; |
129 | size_t length; |
128 | size_t length; |
130 | void * data; |
129 | void * data; |
131 | 130 | ||
132 | if( ! dimension ) return EINVAL; |
131 | if( ! dimension ) return EINVAL; |
133 | // get the data length |
132 | // get the data length |
134 | if( ! ipc_data_write_receive( & callid, & length )) return EINVAL; |
133 | if( ! ipc_data_write_receive( & callid, & length )) return EINVAL; |
135 | // get a new packet |
134 | // get a new packet |
136 | * packet = packet_get_4( packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix ); |
135 | * packet = packet_get_4( packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix ); |
137 | if( ! packet ) return ENOMEM; |
136 | if( ! packet ) return ENOMEM; |
138 | // allocate space in the packet |
137 | // allocate space in the packet |
139 | data = packet_suffix( * packet, length ); |
138 | data = packet_suffix( * packet, length ); |
140 | if( ! data ){ |
139 | if( ! data ){ |
141 | pq_release( packet_phone, packet_get_id( * packet )); |
140 | pq_release( packet_phone, packet_get_id( * packet )); |
142 | return ENOMEM; |
141 | return ENOMEM; |
143 | } |
142 | } |
144 | // read the data into the packet |
143 | // read the data into the packet |
145 | if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length )) |
144 | if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length )) |
146 | // set the packet destination address |
145 | // set the packet destination address |
147 | || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen ))){ |
146 | || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen ))){ |
148 | pq_release( packet_phone, packet_get_id( * packet )); |
147 | pq_release( packet_phone, packet_get_id( * packet )); |
149 | return ERROR_CODE; |
148 | return ERROR_CODE; |
150 | } |
149 | } |
151 | return ( int ) length; |
150 | return ( int ) length; |
152 | } |
151 | } |
153 | 152 | ||
154 | /** @} |
153 | /** @} |
155 | */ |
154 | */ |
156 | 155 |