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