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