last commit i forgot to save changes in x11.c ; this fixes that file
[monky] / src / libtcp-portmon.h
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * libtcp-portmon.h:  tcp port monitoring library.
4  *
5  * Copyright (C) 2005-2007 Philip Kovacs pkovacs@users.sourceforge.net
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
20  * USA. */
21
22 #ifndef LIBTCP_PORTMON_H
23 #define LIBTCP_PORTMON_H
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <netinet/tcp.h>
31
32 #include <math.h>
33 #include <netdb.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <glib.h>
39
40 /* connection deleted if unseen again after this # of refreshes */
41 #define TCP_CONNECTION_STARTING_AGE 1
42 #define TCP_PORT_MONITOR_HASH_KEY_SIZE 12
43 #define MAX_PORT_MONITOR_CONNECTIONS_DEFAULT 256
44
45 /* -------------------------------------------------------------------
46  * IMPLEMENTATION INTERFACE
47  *
48  * Implementation-specific interface begins here.  Clients should not
49  * manipulate these structures directly, nor call the defined helper
50  * functions.  Use the "Client interface" functions defined at bottom.
51  * ------------------------------------------------------------------- */
52
53 /* The inventory of peekable items within the port monitor. */
54 enum tcp_port_monitor_peekables {
55         COUNT = 0,
56         REMOTEIP,
57         REMOTEHOST,
58         REMOTEPORT,
59         REMOTESERVICE,
60         LOCALIP,
61         LOCALHOST,
62         LOCALPORT,
63         LOCALSERVICE
64 };
65
66 /* ------------------------------------------------------------------------
67  * A single tcp connection
68  *
69  * The age variable provides the mechanism for removing connections if they
70  * are not seen again in subsequent update cycles.
71  * ------------------------------------------------------------------------ */
72 typedef struct _tcp_connection_t {
73         /* connection's key in monitor hash */
74         struct in6_addr local_addr;
75         struct in6_addr remote_addr;
76         in_port_t local_port;
77         in_port_t remote_port;
78         int age;
79 } tcp_connection_t;
80
81 /* ----------------------------------
82  * Copy a connection
83  *
84  * Returns 0 on success, -1 otherwise
85  * ---------------------------------- */
86 int copy_tcp_connection(tcp_connection_t *p_dest_connection,
87         const tcp_connection_t *p_source_connection);
88
89 /* -------------------------------------------------------------------
90  * A tcp connection node/list
91  *
92  * Connections within each monitor are stored in a double-linked list.
93  * ------------------------------------------------------------------- */
94 typedef struct _tcp_connection_node_t {
95         tcp_connection_t connection;
96         struct _tcp_connection_node_t *p_prev;
97         struct _tcp_connection_node_t *p_next;
98 } tcp_connection_node_t;
99
100 typedef struct _tcp_connection_list_t {
101         tcp_connection_node_t *p_head;
102         tcp_connection_node_t *p_tail;
103 } tcp_connection_list_t;
104
105 /* --------------
106  * A port monitor
107  * -------------- */
108 typedef struct _tcp_port_monitor_t {
109         /* monitor's key in collection hash */
110         gchar key[TCP_PORT_MONITOR_HASH_KEY_SIZE];
111         /* start of monitor port range */
112         in_port_t port_range_begin;
113         /* begin = end to monitor a single port */
114         in_port_t port_range_end;
115         /* list of connections for this monitor */
116         tcp_connection_list_t connection_list;
117         /* hash table of pointers into connection list */
118         GHashTable *hash;
119         /* array of connection pointers for O(1) peeking */
120         tcp_connection_t **p_peek;
121         /* max number of connections */
122         unsigned int max_port_monitor_connections;
123 } tcp_port_monitor_t;
124
125 /* ------------------------
126  * A port monitor node/list
127  * ------------------------ */
128 typedef struct _tcp_port_monitor_node_t {
129         tcp_port_monitor_t *p_monitor;
130         struct _tcp_port_monitor_node_t *p_next;
131 } tcp_port_monitor_node_t;
132
133 typedef struct __tcp_port_monitor_list_t {
134         tcp_port_monitor_node_t *p_head;
135         tcp_port_monitor_node_t *p_tail;
136 } tcp_port_monitor_list_t;
137
138 /* ---------------------------------------
139  * A port monitor utility function typedef
140  * --------------------------------------- */
141 typedef void (*tcp_port_monitor_function_ptr_t)(tcp_port_monitor_t *p_monitor,
142         void *p_void);
143
144 /* -------------------------------------------
145  * Port monitor utility functions implementing
146  * tcp_port_monitor_function_ptr_t
147  * ------------------------------------------- */
148 void destroy_tcp_port_monitor(tcp_port_monitor_t *p_monitor,
149         void *p_void /* (use NULL for this function) */);
150
151 void age_tcp_port_monitor(tcp_port_monitor_t *p_monitor,
152         void *p_void /* (use NULL for this function) */);
153
154 void rebuild_tcp_port_monitor_peek_table(tcp_port_monitor_t *p_monitor,
155         void *p_void /* (use NULL for this function) */);
156
157 void show_connection_to_tcp_port_monitor(tcp_port_monitor_t *p_monitor,
158         void *p_connection /* (client should cast) */);
159
160 /* -----------------------------
161  * A tcp port monitor collection
162  * ----------------------------- */
163 typedef struct _tcp_port_monitor_collection_t {
164         /* list of monitors for this collection */
165         tcp_port_monitor_list_t monitor_list;
166         /* hash table of pointers into collection's monitor list */
167         GHashTable *hash;
168 } tcp_port_monitor_collection_t;
169
170 /* --------------------------------------------------------
171  * Apply a tcp_port_monitor_function_ptr_t function to each
172  * 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) */);
178
179 /* ----------------------------------------------------------------------
180  * CLIENT INTERFACE
181  *
182  * Clients should call only those functions below this line.
183  * ---------------------------------------------------------------------- */
184
185 /* struct to hold monitor creation arguments */
186 typedef struct _tcp_port_monitor_args_t {
187         /* monitor supports tracking at most this many connections */
188         int max_port_monitor_connections;
189 } tcp_port_monitor_args_t;
190
191 /* ----------------------------------
192  * Client operations on port monitors
193  * ---------------------------------- */
194
195 /* Clients should first try to "find_tcp_port_monitor" before creating one,
196  * so that there are no redundant monitors. */
197 tcp_port_monitor_t *create_tcp_port_monitor(in_port_t port_range_begin,
198         in_port_t port_range_end, tcp_port_monitor_args_t *p_creation_args);
199
200 /* Clients use this function to get connection data from
201  * the indicated port monitor.
202  * The requested monitor value is copied into a client-supplied char buffer.
203  * Returns 0 on success, -1 otherwise. */
204 int peek_tcp_port_monitor(const tcp_port_monitor_t *p_monitor,
205         /* (item of interest, from tcp_port_monitor_peekables enum) */
206         int item,
207         /* (0 to number of connections in monitor - 1) */
208         int connection_index,
209         /* buffer to receive requested value */
210         char *p_buffer,
211         /* size of p_buffer */
212         size_t buffer_size);
213
214 /* --------------------------------
215  * Client operations on collections
216  * -------------------------------- */
217
218 /* Create a monitor collection.  Do this one first. */
219 tcp_port_monitor_collection_t *create_tcp_port_monitor_collection(void);
220
221 /* Destroy the monitor collection (and everything it contains).
222  * Do this one last. */
223 void destroy_tcp_port_monitor_collection(
224         tcp_port_monitor_collection_t *p_collection);
225
226 /* Updates the tcp statitics for all monitors within a collection */
227 void update_tcp_port_monitor_collection(
228         tcp_port_monitor_collection_t *p_collection);
229
230 /* After clients create a monitor, use this to add it to the collection.
231  * Returns 0 on success, -1 otherwise. */
232 int insert_tcp_port_monitor_into_collection(
233         tcp_port_monitor_collection_t *p_collection, tcp_port_monitor_t *p_monitor);
234
235 /* Clients need a way to find monitors */
236 tcp_port_monitor_t *find_tcp_port_monitor(
237         const tcp_port_monitor_collection_t *p_collection,
238         in_port_t port_range_begin, in_port_t port_range_end);
239
240 #endif