4 * libtcp-portmon.h: tcp port monitoring library.
6 * Copyright (C) 2005-2007 Philip Kovacs pkovacs@users.sourceforge.net
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 #ifndef LIBTCP_PORTMON_H
26 #define LIBTCP_PORTMON_H
28 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
43 #define TCP_CONNECTION_STARTING_AGE 1 /* connection deleted if unseen again after this # of refreshes */
44 #define TCP_CONNECTION_HASH_KEY_SIZE 28
45 #define TCP_PORT_MONITOR_HASH_KEY_SIZE 12
47 /* -------------------------------------------------------------------
48 * IMPLEMENTATION INTERFACE
50 * Implementation-specific interface begins here. Clients should not
51 * manipulate these structures directly, nor call the defined helper
52 * functions. Use the "Client interface" functions defined at bottom.
53 * ------------------------------------------------------------------- */
55 /* The inventory of peekable items within the port monitor. */
56 enum tcp_port_monitor_peekables {
68 /* ------------------------------------------------------------------------
69 * A single tcp connection
71 * The age variable provides the mechanism for removing connections if they
72 * are not seen again in subsequent update cycles.
73 * ------------------------------------------------------------------------ */
74 typedef struct _tcp_connection_t {
75 gchar key[TCP_CONNECTION_HASH_KEY_SIZE]; /* connection's key in monitor hash */
78 in_addr_t remote_addr;
79 in_port_t remote_port;
83 /* ----------------------------------
86 * Returns 0 on success, -1 otherwise
87 * ----------------------------------*/
88 int copy_tcp_connection(
89 tcp_connection_t * /* p_dest_connection */,
90 const tcp_connection_t * /* p_source_connection */
93 /* ------------------------------------------------------------------------
94 * A tcp connection node/list
96 * Connections within each monitor are stored in a double-linked list.
97 * ------------------------------------------------------------------------ */
98 typedef struct _tcp_connection_node_t {
99 tcp_connection_t connection;
100 struct _tcp_connection_node_t * p_prev;
101 struct _tcp_connection_node_t * p_next;
102 } tcp_connection_node_t;
104 typedef struct _tcp_connection_list_t {
105 tcp_connection_node_t * p_head;
106 tcp_connection_node_t * p_tail;
107 } tcp_connection_list_t;
112 typedef struct _tcp_port_monitor_t {
113 gchar key[TCP_PORT_MONITOR_HASH_KEY_SIZE]; /* monitor's key in collection hash */
114 in_port_t port_range_begin; /* start of monitor port range */
115 in_port_t port_range_end; /* begin = end to monitor a single port */
116 tcp_connection_list_t connection_list; /* list of connections for this monitor */
117 GHashTable *hash; /* hash table of pointers into connection list */
118 tcp_connection_t **p_peek; /* array of connection pointers for O(1) peeking */
119 unsigned int max_port_monitor_connections; /* max number of connections */
120 } tcp_port_monitor_t;
122 /* ------------------------
123 * A port monitor node/list
124 * ------------------------ */
125 typedef struct _tcp_port_monitor_node_t {
126 tcp_port_monitor_t * p_monitor;
127 struct _tcp_port_monitor_node_t *p_next;
128 } tcp_port_monitor_node_t;
130 typedef struct __tcp_port_monitor_list_t {
131 tcp_port_monitor_node_t * p_head;
132 tcp_port_monitor_node_t * p_tail;
133 } tcp_port_monitor_list_t;
135 /* ---------------------------------------
136 * A port monitor utility function typedef
137 * ---------------------------------------*/
138 typedef void (*tcp_port_monitor_function_ptr_t)( tcp_port_monitor_t * /* p_monitor */, void * /* p_void */ );
140 /* ---------------------------------------------------------------------------
141 * Port monitor utility functions implementing tcp_port_monitor_function_ptr_t
142 * ---------------------------------------------------------------------------*/
143 void destroy_tcp_port_monitor(
144 tcp_port_monitor_t * /* p_monitor */,
145 void * /* p_void (use NULL for this function) */
148 void age_tcp_port_monitor(
149 tcp_port_monitor_t * /* p_monitor */,
150 void * /* p_void (use NULL for this function) */
153 void rebuild_tcp_port_monitor_peek_table(
154 tcp_port_monitor_t * /* p_monitor */,
155 void * /* p_void (use NULL for this function) */
158 void show_connection_to_tcp_port_monitor(
159 tcp_port_monitor_t * /* p_monitor */,
160 void * /* p_connection (client should cast) */
163 /* -----------------------------
164 * A tcp port monitor collection
165 * -----------------------------*/
166 typedef struct _tcp_port_monitor_collection_t {
167 tcp_port_monitor_list_t monitor_list; /* list of monitors for this collection */
168 GHashTable *hash; /* hash table of pointers into collection's monitor list */
169 } tcp_port_monitor_collection_t;
171 /* ---------------------------------------------------------------------------------------
172 * Apply a tcp_port_monitor_function_ptr_t function to each port monitor in the collection.
173 * ---------------------------------------------------------------------------------------*/
174 void for_each_tcp_port_monitor_in_collection(
175 tcp_port_monitor_collection_t * /* p_collection */,
176 tcp_port_monitor_function_ptr_t /* p_function */,
177 void * /* p_function_args (for user arguments) */
180 /* ----------------------------------------------------------------------
183 * Clients should call only those functions below this line.
184 * ---------------------------------------------------------------------- */
186 /* struct to hold monitor creation arguments */
187 typedef struct _tcp_port_monitor_args_t {
188 int max_port_monitor_connections; /* monitor supports tracking at most this many connections */
189 } tcp_port_monitor_args_t;
192 /* ----------------------------------
193 * Client operations on port monitors
194 * ---------------------------------- */
196 /* Clients should first try to "find_tcp_port_monitor" before creating one
197 so that there are no redundant monitors. */
198 tcp_port_monitor_t * create_tcp_port_monitor(
199 in_port_t /* port_range_begin */,
200 in_port_t /* port_range_end */,
201 tcp_port_monitor_args_t * /* p_creation_args */
204 /* Clients use this function to get connection data from the indicated port monitor.
205 The requested monitor value is copied into a client-supplied char buffer.
206 Returns 0 on success, -1 otherwise. */
207 int peek_tcp_port_monitor(
208 const tcp_port_monitor_t * /* p_monitor */,
209 int /* item, ( item of interest, from tcp_port_monitor_peekables enum ) */,
210 int /* connection_index, ( 0 to number of connections in monitor - 1 )*/,
211 char * /* p_buffer, buffer to receive requested value */,
212 size_t /* buffer_size, size of p_buffer */
215 /* --------------------------------
216 * Client operations on collections
217 * -------------------------------- */
219 /* Create a monitor collection. Do this one first. */
220 tcp_port_monitor_collection_t * create_tcp_port_monitor_collection (void);
222 /* Destroy the monitor collection (and everything it contains). Do this one last. */
223 void destroy_tcp_port_monitor_collection(
224 tcp_port_monitor_collection_t * /* p_collection */
227 /* Updates the tcp statitics for all monitors within a collection */
228 void update_tcp_port_monitor_collection(
229 tcp_port_monitor_collection_t * /* p_collection */
232 /* After clients create a monitor, use this to add it to the collection.
233 Returns 0 on success, -1 otherwise. */
234 int insert_tcp_port_monitor_into_collection(
235 tcp_port_monitor_collection_t * /* p_collection */,
236 tcp_port_monitor_t * /* p_monitor */
239 /* Clients need a way to find monitors */
240 tcp_port_monitor_t * find_tcp_port_monitor(
241 const tcp_port_monitor_collection_t * /* p_collection */,
242 in_port_t /* port_range_begin */,
243 in_port_t /* port_range_end */