| 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
|
|---|
| 34 | * Socket common core.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #ifndef __NET_SOCKET_CORE_H__
|
|---|
| 38 | #define __NET_SOCKET_CORE_H__
|
|---|
| 39 |
|
|---|
| 40 | #include <sys/types.h>
|
|---|
| 41 |
|
|---|
| 42 | #include <in.h>
|
|---|
| 43 | #include <net_device.h>
|
|---|
| 44 | #include <adt/generic_char_map.h>
|
|---|
| 45 | #include <adt/dynamic_fifo.h>
|
|---|
| 46 | #include <adt/int_map.h>
|
|---|
| 47 | #include <packet/packet.h>
|
|---|
| 48 |
|
|---|
| 49 | /** Initial size of the received packet queue.
|
|---|
| 50 | */
|
|---|
| 51 | #define SOCKET_INITIAL_RECEIVED_SIZE 4
|
|---|
| 52 |
|
|---|
| 53 | /** Maximum size of the received packet queue.
|
|---|
| 54 | */
|
|---|
| 55 | #define SOCKET_MAX_RECEIVED_SIZE 0
|
|---|
| 56 |
|
|---|
| 57 | /** Initial size of the sockets for acceptance queue.
|
|---|
| 58 | */
|
|---|
| 59 | #define SOCKET_INITIAL_ACCEPTED_SIZE 1
|
|---|
| 60 |
|
|---|
| 61 | /** Maximum size of the sockets for acceptance queue.
|
|---|
| 62 | */
|
|---|
| 63 | #define SOCKET_MAX_ACCEPTEDED_SIZE 0
|
|---|
| 64 |
|
|---|
| 65 | /** Listening sockets' port map key.
|
|---|
| 66 | */
|
|---|
| 67 | #define SOCKET_MAP_KEY_LISTENING "L"
|
|---|
| 68 |
|
|---|
| 69 | /** Type definition of the socket core.
|
|---|
| 70 | * @see socket_core
|
|---|
| 71 | */
|
|---|
| 72 | typedef struct socket_core socket_core_t;
|
|---|
| 73 |
|
|---|
| 74 | /** Type definition of the socket core pointer.
|
|---|
| 75 | * @see socket_core
|
|---|
| 76 | */
|
|---|
| 77 | typedef socket_core_t * socket_core_ref;
|
|---|
| 78 |
|
|---|
| 79 | /** Type definition of the socket port.
|
|---|
| 80 | * @see socket_port
|
|---|
| 81 | */
|
|---|
| 82 | typedef struct socket_port socket_port_t;
|
|---|
| 83 |
|
|---|
| 84 | /** Type definition of the socket port pointer.
|
|---|
| 85 | * @see socket_port
|
|---|
| 86 | */
|
|---|
| 87 | typedef socket_port_t * socket_port_ref;
|
|---|
| 88 |
|
|---|
| 89 | /** Socket core.
|
|---|
| 90 | */
|
|---|
| 91 | struct socket_core{
|
|---|
| 92 | /** Socket identifier.
|
|---|
| 93 | */
|
|---|
| 94 | int socket_id;
|
|---|
| 95 | /** Client application phone.
|
|---|
| 96 | */
|
|---|
| 97 | int phone;
|
|---|
| 98 | /** Bound port.
|
|---|
| 99 | */
|
|---|
| 100 | int port;
|
|---|
| 101 | /** Received packets queue.
|
|---|
| 102 | */
|
|---|
| 103 | dyn_fifo_t received;
|
|---|
| 104 | /** Sockets for acceptance queue.
|
|---|
| 105 | */
|
|---|
| 106 | dyn_fifo_t accepted;
|
|---|
| 107 | /** Protocol specific data.
|
|---|
| 108 | */
|
|---|
| 109 | void * specific_data;
|
|---|
| 110 | /** Socket ports map key.
|
|---|
| 111 | */
|
|---|
| 112 | const char * key;
|
|---|
| 113 | /** Length of the Socket ports map key.
|
|---|
| 114 | */
|
|---|
| 115 | size_t key_length;
|
|---|
| 116 | };
|
|---|
| 117 |
|
|---|
| 118 | /** Sockets map.
|
|---|
| 119 | * The key is the socket identifier.
|
|---|
| 120 | */
|
|---|
| 121 | INT_MAP_DECLARE(socket_cores, socket_core_t);
|
|---|
| 122 |
|
|---|
| 123 | /** Bount port sockets map.
|
|---|
| 124 | * The listening socket has the SOCKET_MAP_KEY_LISTENING key identifier whereas the other use the remote addresses.
|
|---|
| 125 | */
|
|---|
| 126 | GENERIC_CHAR_MAP_DECLARE(socket_port_map, socket_core_ref);
|
|---|
| 127 |
|
|---|
| 128 | /** Ports map.
|
|---|
| 129 | * The key is the port number.
|
|---|
| 130 | */
|
|---|
| 131 | INT_MAP_DECLARE(socket_ports, socket_port_t);
|
|---|
| 132 |
|
|---|
| 133 | /** Destroys local sockets.
|
|---|
| 134 | * Releases all buffered packets and calls the release function for each of the sockets.
|
|---|
| 135 | * @param[in] packet_phone The packet server phone to release buffered packets.
|
|---|
| 136 | * @param[in] local_sockets The local sockets to be destroyed.
|
|---|
| 137 | * @param[in,out] global_sockets The global sockets to be updated.
|
|---|
| 138 | * @param[in] socket_release The client release callback function.
|
|---|
| 139 | */
|
|---|
| 140 | extern void socket_cores_release(int packet_phone, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket));
|
|---|
| 141 |
|
|---|
| 142 | /** Binds the socket to the port.
|
|---|
| 143 | * The address port is used if set, a free port is used if not.
|
|---|
| 144 | * @param[in] local_sockets The local sockets to be searched.
|
|---|
| 145 | * @param[in,out] global_sockets The global sockets to be updated.
|
|---|
| 146 | * @param[in] socket_id The new socket identifier.
|
|---|
| 147 | * @param[in] addr The address to be bound to.
|
|---|
| 148 | * @param[in] addrlen The address length.
|
|---|
| 149 | * @param[in] free_ports_start The minimum free port.
|
|---|
| 150 | * @param[in] free_ports_end The maximum free port.
|
|---|
| 151 | * @param[in] last_used_port The last used free port.
|
|---|
| 152 | * @returns EOK on success.
|
|---|
| 153 | * @returns ENOTSOCK if the socket was not found.
|
|---|
| 154 | * @returns EAFNOSUPPORT if the address family is not supported.
|
|---|
| 155 | * @returns EADDRINUSE if the port is already in use.
|
|---|
| 156 | * @returns Other error codes as defined for the socket_bind_free_port() function.
|
|---|
| 157 | * @returns Other error codes as defined for the socket_bind_insert() function.
|
|---|
| 158 | */
|
|---|
| 159 | extern 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);
|
|---|
| 160 |
|
|---|
| 161 | /** Binds the socket to a free port.
|
|---|
| 162 | * The first free port is used.
|
|---|
| 163 | * @param[in,out] global_sockets The global sockets to be updated.
|
|---|
| 164 | * @param[in,out] socket The socket to be bound.
|
|---|
| 165 | * @param[in] free_ports_start The minimum free port.
|
|---|
| 166 | * @param[in] free_ports_end The maximum free port.
|
|---|
| 167 | * @param[in] last_used_port The last used free port.
|
|---|
| 168 | * @returns EOK on success.
|
|---|
| 169 | * @returns ENOTCONN if no free port was found.
|
|---|
| 170 | * @returns Other error codes as defined for the socket_bind_insert() function.
|
|---|
| 171 | */
|
|---|
| 172 | extern 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);
|
|---|
| 173 |
|
|---|
| 174 | /** Creates a new socket.
|
|---|
| 175 | * @param[in,out] local_sockets The local sockets to be updated.
|
|---|
| 176 | * @param[in] app_phone The application phone.
|
|---|
| 177 | * @param[in] specific_data The socket specific data.
|
|---|
| 178 | * @param[in,out] socket_id The new socket identifier. A new identifier is chosen if set to zero (0) or negative. A negative identifier is chosen if set to negative.
|
|---|
| 179 | * @returns EOK on success.
|
|---|
| 180 | * @returns EINVAL if the socket_id parameter is NULL.
|
|---|
| 181 | * @returns ENOMEM if there is not enough memory left.
|
|---|
| 182 | */
|
|---|
| 183 | extern int socket_create(socket_cores_ref local_sockets, int app_phone, void * specific_data, int * socket_id);
|
|---|
| 184 |
|
|---|
| 185 | /** Destroys the socket.
|
|---|
| 186 | * If the socket is bound, the port is released.
|
|---|
| 187 | * Releases all buffered packets, calls the release function and removes the socket from the local sockets.
|
|---|
| 188 | * @param[in] packet_phone The packet server phone to release buffered packets.
|
|---|
| 189 | * @param[in] socket_id The socket identifier.
|
|---|
| 190 | * @param[in,out] local_sockets The local sockets to be updated.
|
|---|
| 191 | * @param[in,out] global_sockets The global sockets to be updated.
|
|---|
| 192 | * @param[in] socket_release The client release callback function.
|
|---|
| 193 | * @returns EOK on success.
|
|---|
| 194 | * @returns ENOTSOCK if the socket is not found.
|
|---|
| 195 | */
|
|---|
| 196 | extern int socket_destroy(int packet_phone, int socket_id, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void (*socket_release)(socket_core_ref socket));
|
|---|
| 197 |
|
|---|
| 198 | /** Replies the packet or the packet queue data to the application via the socket.
|
|---|
| 199 | * Uses the current message processing fibril.
|
|---|
| 200 | * @param[in] packet The packet to be transfered.
|
|---|
| 201 | * @param[out] length The total data length.
|
|---|
| 202 | * @returns EOK on success.
|
|---|
| 203 | * @returns EBADMEM if the length parameter is NULL.
|
|---|
| 204 | * @returns ENOMEM if there is not enough memory left.
|
|---|
| 205 | * @returns Other error codes as defined for the data_reply() function.
|
|---|
| 206 | */
|
|---|
| 207 | extern int socket_reply_packets(packet_t packet, size_t * length);
|
|---|
| 208 |
|
|---|
| 209 | /** Finds the bound port socket.
|
|---|
| 210 | * @param[in] global_sockets The global sockets to be searched.
|
|---|
| 211 | * @param[in] port The port number.
|
|---|
| 212 | * @param[in] key The socket key identifier.
|
|---|
| 213 | * @param[in] key_length The socket key length.
|
|---|
| 214 | * @returns The found socket.
|
|---|
| 215 | * @returns NULL if no socket was found.
|
|---|
| 216 | */
|
|---|
| 217 | extern socket_core_ref socket_port_find(socket_ports_ref global_sockets, int port, const char * key, size_t key_length);
|
|---|
| 218 |
|
|---|
| 219 | /** Releases the socket port.
|
|---|
| 220 | * If the socket is bound the port entry is released.
|
|---|
| 221 | * If there are no more port entries the port is release.
|
|---|
| 222 | * @param[in] global_sockets The global sockets to be updated.
|
|---|
| 223 | * @param[in] socket The socket to be unbound.
|
|---|
| 224 | */
|
|---|
| 225 | extern void socket_port_release(socket_ports_ref global_sockets, socket_core_ref socket);
|
|---|
| 226 |
|
|---|
| 227 | /** Adds the socket to an already bound port.
|
|---|
| 228 | * @param[in] global_sockets The global sockets to be updated.
|
|---|
| 229 | * @param[in] port The port number to be bound to.
|
|---|
| 230 | * @param[in] socket The socket to be added.
|
|---|
| 231 | * @param[in] key The socket key identifier.
|
|---|
| 232 | * @param[in] key_length The socket key length.
|
|---|
| 233 | * @returns EOK on success.
|
|---|
| 234 | * @returns ENOENT if the port is not already used.
|
|---|
| 235 | * @returns Other error codes as defined for the socket_port_add_core() function.
|
|---|
| 236 | */
|
|---|
| 237 | extern int socket_port_add(socket_ports_ref global_sockets, int port, socket_core_ref socket, const char * key, size_t key_length);
|
|---|
| 238 |
|
|---|
| 239 | #endif
|
|---|
| 240 |
|
|---|
| 241 | /** @}
|
|---|
| 242 | */
|
|---|