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