Subversion Repositories HelenOS

Rev

Rev 4700 | Rev 4708 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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