| 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 libnet
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file
|
|---|
| 34 | * Socket common core.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #ifndef LIBNET_SOCKET_CORE_H_
|
|---|
| 38 | #define LIBNET_SOCKET_CORE_H_
|
|---|
| 39 |
|
|---|
| 40 | #include <sys/types.h>
|
|---|
| 41 | #include <adt/generic_char_map.h>
|
|---|
| 42 | #include <adt/dynamic_fifo.h>
|
|---|
| 43 | #include <adt/int_map.h>
|
|---|
| 44 | #include <net/in.h>
|
|---|
| 45 | #include <async.h>
|
|---|
| 46 |
|
|---|
| 47 | /** Initial size of the received packet queue. */
|
|---|
| 48 | #define SOCKET_INITIAL_RECEIVED_SIZE 4
|
|---|
| 49 |
|
|---|
| 50 | /** Maximum size of the received packet queue. */
|
|---|
| 51 | #define SOCKET_MAX_RECEIVED_SIZE 0
|
|---|
| 52 |
|
|---|
| 53 | /** Initial size of the sockets for acceptance queue. */
|
|---|
| 54 | #define SOCKET_INITIAL_ACCEPTED_SIZE 1
|
|---|
| 55 |
|
|---|
| 56 | /** Maximum size of the sockets for acceptance queue. */
|
|---|
| 57 | #define SOCKET_MAX_ACCEPTEDED_SIZE 0
|
|---|
| 58 |
|
|---|
| 59 | /** Listening sockets' port map key. */
|
|---|
| 60 | #define SOCKET_MAP_KEY_LISTENING "L"
|
|---|
| 61 |
|
|---|
| 62 | /** Type definition of the socket core.
|
|---|
| 63 | * @see socket_core
|
|---|
| 64 | */
|
|---|
| 65 | typedef struct socket_core socket_core_t;
|
|---|
| 66 |
|
|---|
| 67 | /** Type definition of the socket port.
|
|---|
| 68 | * @see socket_port
|
|---|
| 69 | */
|
|---|
| 70 | typedef struct socket_port socket_port_t;
|
|---|
| 71 |
|
|---|
| 72 | /** Socket core. */
|
|---|
| 73 | struct socket_core {
|
|---|
| 74 | /** Socket identifier. */
|
|---|
| 75 | int socket_id;
|
|---|
| 76 | /** Client application session. */
|
|---|
| 77 | async_sess_t *sess;
|
|---|
| 78 | /** Bound port. */
|
|---|
| 79 | int port;
|
|---|
| 80 | /** Sockets for acceptance queue. */
|
|---|
| 81 | dyn_fifo_t accepted;
|
|---|
| 82 | /** Protocol specific data. */
|
|---|
| 83 | void *specific_data;
|
|---|
| 84 | /** Socket ports map key. */
|
|---|
| 85 | const uint8_t *key;
|
|---|
| 86 | /** Length of the Socket ports map key. */
|
|---|
| 87 | size_t key_length;
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | /** Sockets map.
|
|---|
| 91 | * The key is the socket identifier.
|
|---|
| 92 | */
|
|---|
| 93 | INT_MAP_DECLARE(socket_cores, socket_core_t);
|
|---|
| 94 |
|
|---|
| 95 | /** Bount port sockets map.
|
|---|
| 96 | *
|
|---|
| 97 | * The listening socket has the SOCKET_MAP_KEY_LISTENING key identifier whereas
|
|---|
| 98 | * the other use the remote addresses.
|
|---|
| 99 | */
|
|---|
| 100 | GENERIC_CHAR_MAP_DECLARE(socket_port_map, socket_core_t *);
|
|---|
| 101 |
|
|---|
| 102 | /** Ports map.
|
|---|
| 103 | * The key is the port number.
|
|---|
| 104 | */
|
|---|
| 105 | INT_MAP_DECLARE(socket_ports, socket_port_t);
|
|---|
| 106 |
|
|---|
| 107 | extern void socket_cores_release(async_sess_t *, socket_cores_t *,
|
|---|
| 108 | socket_ports_t *, void (*)(socket_core_t *));
|
|---|
| 109 | extern int socket_bind(socket_cores_t *, socket_ports_t *, int, void *, size_t,
|
|---|
| 110 | int, int, int);
|
|---|
| 111 | extern int socket_bind_free_port(socket_ports_t *, socket_core_t *, int, int,
|
|---|
| 112 | int);
|
|---|
| 113 | extern int socket_create(socket_cores_t *, async_sess_t *, void *, int *);
|
|---|
| 114 | extern int socket_destroy(async_sess_t *, int, socket_cores_t *,
|
|---|
| 115 | socket_ports_t *, void (*)(socket_core_t *));
|
|---|
| 116 | extern socket_core_t *socket_port_find(socket_ports_t *, int, const uint8_t *,
|
|---|
| 117 | size_t);
|
|---|
| 118 | extern void socket_port_release(socket_ports_t *, socket_core_t *);
|
|---|
| 119 | extern int socket_port_add(socket_ports_t *, int, socket_core_t *,
|
|---|
| 120 | const uint8_t *, size_t);
|
|---|
| 121 |
|
|---|
| 122 | #endif
|
|---|
| 123 |
|
|---|
| 124 | /** @}
|
|---|
| 125 | */
|
|---|