Rev 4708 | Rev 4726 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4708 | Rev 4712 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Lukas Mejdrech |
2 | * Copyright (c) 2009 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 socket |
29 | /** @addtogroup socket |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
34 | * \todo |
34 | * \todo |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include "../err.h" |
37 | #include "../err.h" |
38 | 38 | ||
39 | #include "../include/in.h" |
39 | #include "../include/in.h" |
40 | #include "../include/inet.h" |
40 | #include "../include/inet.h" |
41 | 41 | ||
42 | #include "../include/socket.h" |
42 | #include "../include/socket_codes.h" |
43 | #include "../include/socket_errno.h" |
43 | #include "../include/socket_errno.h" |
44 | 44 | ||
45 | #include "../structures/dynamic_fifo.h" |
45 | #include "../structures/dynamic_fifo.h" |
46 | #include "../structures/int_map.h" |
46 | #include "../structures/int_map.h" |
47 | #include "../structures/packet/packet.h" |
47 | #include "../structures/packet/packet.h" |
48 | #include "../structures/packet/packet_client.h" |
48 | #include "../structures/packet/packet_client.h" |
49 | 49 | ||
50 | #include "socket_core.h" |
50 | #include "socket_core.h" |
51 | 51 | ||
52 | int socket_bind_insert( socket_ports_ref global_sockets, socket_core_ref socket, int port ); |
52 | int socket_bind_insert( socket_ports_ref global_sockets, socket_core_ref socket, int port ); |
53 | 53 | ||
54 | INT_MAP_IMPLEMENT( socket_cores, socket_core_t ); |
54 | INT_MAP_IMPLEMENT( socket_cores, socket_core_t ); |
55 | 55 | ||
56 | INT_MAP_IMPLEMENT( socket_ports, socket_core_ref ); |
56 | INT_MAP_IMPLEMENT( socket_ports, socket_core_ref ); |
57 | 57 | ||
58 | int socket_bind( socket_cores_ref local_sockets, socket_ports_ref global_sockets, int socket_id, void * addr, size_t addrlen, int free_ports_start, int free_ports_end, int last_used_port ){ |
58 | int socket_bind( socket_cores_ref local_sockets, socket_ports_ref global_sockets, int socket_id, void * addr, size_t addrlen, int free_ports_start, int free_ports_end, int last_used_port ){ |
59 | socket_core_ref socket; |
59 | socket_core_ref socket; |
60 | socket_core_ref * socket_pointer; |
60 | socket_core_ref * socket_pointer; |
61 | struct sockaddr * address; |
61 | struct sockaddr * address; |
62 | struct sockaddr_in * address_in; |
62 | struct sockaddr_in * address_in; |
63 | 63 | ||
64 | if( addrlen < sizeof( struct sockaddr )) return EINVAL; |
64 | if( addrlen < sizeof( struct sockaddr )) return EINVAL; |
65 | address = ( struct sockaddr * ) addr; |
65 | address = ( struct sockaddr * ) addr; |
66 | switch( address->sa_family ){ |
66 | switch( address->sa_family ){ |
67 | case AF_INET: |
67 | case AF_INET: |
68 | if( addrlen != sizeof( struct sockaddr_in )) return EINVAL; |
68 | if( addrlen != sizeof( struct sockaddr_in )) return EINVAL; |
69 | address_in = ( struct sockaddr_in * ) addr; |
69 | address_in = ( struct sockaddr_in * ) addr; |
70 | // find the socket |
70 | // find the socket |
71 | socket = socket_cores_find( local_sockets, socket_id ); |
71 | socket = socket_cores_find( local_sockets, socket_id ); |
72 | if( ! socket ) return ENOTSOCK; |
72 | if( ! socket ) return ENOTSOCK; |
73 | // bind a free port? |
73 | // bind a free port? |
74 | if( address_in->sin_port <= 0 ){ |
74 | if( address_in->sin_port <= 0 ){ |
75 | return socket_bind_free_port( global_sockets, socket, free_ports_start, free_ports_end, last_used_port ); |
75 | return socket_bind_free_port( global_sockets, socket, free_ports_start, free_ports_end, last_used_port ); |
76 | } |
76 | } |
77 | // try to find the port |
77 | // try to find the port |
78 | socket_pointer = socket_ports_find( global_sockets, address_in->sin_port ); |
78 | socket_pointer = socket_ports_find( global_sockets, address_in->sin_port ); |
79 | if( socket_pointer ){ |
79 | if( socket_pointer ){ |
80 | // already used |
80 | // already used |
81 | return EADDRINUSE; |
81 | return EADDRINUSE; |
82 | } |
82 | } |
83 | // disbind if bound |
83 | // disbind if bound |
84 | socket_ports_exclude( global_sockets, socket->port ); |
84 | socket_ports_exclude( global_sockets, socket->port ); |
85 | socket->port = -1; |
85 | socket->port = -1; |
86 | return socket_bind_insert( global_sockets, socket, address_in->sin_port ); |
86 | return socket_bind_insert( global_sockets, socket, address_in->sin_port ); |
87 | break; |
87 | break; |
88 | // TODO IPv6 |
88 | // TODO IPv6 |
89 | } |
89 | } |
90 | return EAFNOSUPPORT; |
90 | return EAFNOSUPPORT; |
91 | } |
91 | } |
92 | 92 | ||
93 | int socket_bind_free_port( socket_ports_ref global_sockets, socket_core_ref socket, int free_ports_start, int free_ports_end, int last_used_port ){ |
93 | int socket_bind_free_port( socket_ports_ref global_sockets, socket_core_ref socket, int free_ports_start, int free_ports_end, int last_used_port ){ |
94 | int index; |
94 | int index; |
95 | 95 | ||
96 | // from the last used one |
96 | // from the last used one |
97 | index = last_used_port; |
97 | index = last_used_port; |
98 | do{ |
98 | do{ |
99 | ++ index; |
99 | ++ index; |
100 | // til the range end |
100 | // til the range end |
101 | if( index >= free_ports_end ){ |
101 | if( index >= free_ports_end ){ |
102 | // start from the range beginning |
102 | // start from the range beginning |
103 | index = free_ports_start - 1; |
103 | index = free_ports_start - 1; |
104 | do{ |
104 | do{ |
105 | ++ index; |
105 | ++ index; |
106 | // til the last used one |
106 | // til the last used one |
107 | if( index >= last_used_port ){ |
107 | if( index >= last_used_port ){ |
108 | // none found |
108 | // none found |
109 | return ENOTCONN; |
109 | return ENOTCONN; |
110 | } |
110 | } |
111 | }while( socket_ports_find( global_sockets, index ) != NULL ); |
111 | }while( socket_ports_find( global_sockets, index ) != NULL ); |
112 | // found, break immediately |
112 | // found, break immediately |
113 | break; |
113 | break; |
114 | } |
114 | } |
115 | }while( socket_ports_find( global_sockets, index ) != NULL ); |
115 | }while( socket_ports_find( global_sockets, index ) != NULL ); |
116 | return socket_bind_insert( global_sockets, socket, index ); |
116 | return socket_bind_insert( global_sockets, socket, index ); |
117 | } |
117 | } |
118 | 118 | ||
119 | int socket_bind_insert( socket_ports_ref global_sockets, socket_core_ref socket, int port ){ |
119 | int socket_bind_insert( socket_ports_ref global_sockets, socket_core_ref socket, int port ){ |
120 | ERROR_DECLARE; |
120 | ERROR_DECLARE; |
121 | 121 | ||
122 | socket_core_ref * socket_pointer; |
122 | socket_core_ref * socket_pointer; |
123 | 123 | ||
124 | // create a wrapper |
124 | // create a wrapper |
125 | socket_pointer = ( socket_core_ref * ) malloc( sizeof( socket_core_ref )); |
125 | socket_pointer = ( socket_core_ref * ) malloc( sizeof( socket_core_ref )); |
126 | if( ! socket_pointer ) return ENOMEM; |
126 | if( ! socket_pointer ) return ENOMEM; |
127 | * socket_pointer = socket; |
127 | * socket_pointer = socket; |
128 | // register the incomming port |
128 | // register the incomming port |
129 | ERROR_CODE = socket_ports_add( global_sockets, port, socket_pointer ); |
129 | ERROR_CODE = socket_ports_add( global_sockets, port, socket_pointer ); |
130 | if( ERROR_CODE < 0 ){ |
130 | if( ERROR_CODE < 0 ){ |
131 | free( socket_pointer ); |
131 | free( socket_pointer ); |
132 | return ERROR_CODE; |
132 | return ERROR_CODE; |
133 | } |
133 | } |
134 | socket->port = port; |
134 | socket->port = port; |
135 | return EOK; |
135 | return EOK; |
136 | } |
136 | } |
137 | 137 | ||
138 | int socket_create( socket_cores_ref local_sockets, int app_phone, int * socket_id ){ |
138 | int socket_create( socket_cores_ref local_sockets, int app_phone, int * socket_id ){ |
139 | ERROR_DECLARE; |
139 | ERROR_DECLARE; |
140 | 140 | ||
141 | socket_core_ref socket; |
141 | socket_core_ref socket; |
142 | int res; |
142 | int res; |
143 | 143 | ||
144 | if( ! socket_id ) return EBADMEM; |
144 | if( ! socket_id ) return EBADMEM; |
145 | socket = ( socket_core_ref ) malloc( sizeof( * socket )); |
145 | socket = ( socket_core_ref ) malloc( sizeof( * socket )); |
146 | if( ! socket ) return ENOMEM; |
146 | if( ! socket ) return ENOMEM; |
147 | // initialize |
147 | // initialize |
148 | socket->phone = app_phone; |
148 | socket->phone = app_phone; |
149 | socket->port = -1; |
149 | socket->port = -1; |
150 | socket->device_id = -1; |
150 | socket->device_id = -1; |
151 | socket->peer_addr = NULL; |
151 | socket->peer_addr = NULL; |
152 | if( ERROR_OCCURRED( dyn_fifo_initialize( & socket->received, SOCKET_INITIAL_RECEIVED_SIZE ))){ |
152 | if( ERROR_OCCURRED( dyn_fifo_initialize( & socket->received, SOCKET_INITIAL_RECEIVED_SIZE ))){ |
153 | free( socket ); |
153 | free( socket ); |
154 | return ERROR_CODE; |
154 | return ERROR_CODE; |
155 | } |
155 | } |
156 | if( ERROR_OCCURRED( dyn_fifo_initialize( & socket->accepted, SOCKET_INITIAL_ACCEPTED_SIZE ))){ |
156 | if( ERROR_OCCURRED( dyn_fifo_initialize( & socket->accepted, SOCKET_INITIAL_ACCEPTED_SIZE ))){ |
157 | dyn_fifo_destroy( & socket->received ); |
157 | dyn_fifo_destroy( & socket->received ); |
158 | free( socket ); |
158 | free( socket ); |
159 | return ERROR_CODE; |
159 | return ERROR_CODE; |
160 | } |
160 | } |
161 | // get a next free socket number |
161 | // get a next free socket number |
162 | socket->socket_id = socket_cores_count( local_sockets ) + 1; |
162 | socket->socket_id = socket_cores_count( local_sockets ) + 1; |
163 | // store the socket |
163 | // store the socket |
164 | res = socket_cores_add( local_sockets, socket->socket_id, socket ); |
164 | res = socket_cores_add( local_sockets, socket->socket_id, socket ); |
165 | if( res < 0 ){ |
165 | if( res < 0 ){ |
166 | dyn_fifo_destroy( & socket->received ); |
166 | dyn_fifo_destroy( & socket->received ); |
167 | dyn_fifo_destroy( & socket->accepted ); |
167 | dyn_fifo_destroy( & socket->accepted ); |
168 | free( socket ); |
168 | free( socket ); |
169 | return res; |
169 | return res; |
170 | } |
170 | } |
171 | // return the socket identifier |
171 | // return the socket identifier |
172 | * socket_id = socket->socket_id; |
172 | * socket_id = socket->socket_id; |
173 | return EOK; |
173 | return EOK; |
174 | } |
174 | } |
175 | 175 | ||
176 | int socket_destroy( int packet_phone, int socket_id, socket_cores_ref local_sockets, socket_ports_ref global_sockets ){ |
176 | int socket_destroy( int packet_phone, int socket_id, socket_cores_ref local_sockets, socket_ports_ref global_sockets ){ |
177 | socket_core_ref socket; |
177 | socket_core_ref socket; |
178 | int accepted_id; |
178 | int accepted_id; |
179 | int packet_id; |
179 | int packet_id; |
180 | 180 | ||
181 | // find the socket |
181 | // find the socket |
182 | socket = socket_cores_find( local_sockets, socket_id ); |
182 | socket = socket_cores_find( local_sockets, socket_id ); |
183 | if( ! socket ) return ENOTSOCK; |
183 | if( ! socket ) return ENOTSOCK; |
184 | socket_ports_exclude( global_sockets, socket->port ); |
184 | socket_ports_exclude( global_sockets, socket->port ); |
185 | // destroy all accepted sockets |
185 | // destroy all accepted sockets |
186 | while(( accepted_id = dyn_fifo_pop( & socket->accepted )) >= 0 ){ |
186 | while(( accepted_id = dyn_fifo_pop( & socket->accepted )) >= 0 ){ |
187 | socket_destroy( packet_phone, accepted_id, local_sockets, global_sockets ); |
187 | socket_destroy( packet_phone, accepted_id, local_sockets, global_sockets ); |
188 | } |
188 | } |
189 | // release all received packets |
189 | // release all received packets |
190 | while(( packet_id = dyn_fifo_pop( & socket->received )) >= 0 ){ |
190 | while(( packet_id = dyn_fifo_pop( & socket->received )) >= 0 ){ |
191 | pq_release( packet_phone, packet_id ); |
191 | pq_release( packet_phone, packet_id ); |
192 | } |
192 | } |
193 | dyn_fifo_destroy( & socket->received ); |
193 | dyn_fifo_destroy( & socket->received ); |
194 | dyn_fifo_destroy( & socket->accepted ); |
194 | dyn_fifo_destroy( & socket->accepted ); |
195 | socket_cores_exclude( local_sockets, socket_id ); |
195 | socket_cores_exclude( local_sockets, socket_id ); |
196 | return EOK; |
196 | return EOK; |
197 | } |
197 | } |
198 | 198 | ||
199 | /** @} |
199 | /** @} |
200 | */ |
200 | */ |
201 | 201 |