1 /*
2  *    jnettop, network online traffic visualiser
3  *    Copyright (C) 2002-2005 Jakub Skopal
4  *
5  *    This program is free software; you can redistribute it and/or modify
6  *    it under the terms of the GNU General Public License as published by
7  *    the Free Software Foundation; either version 2 of the License, or
8  *    (at your option) any later version.
9  *
10  *    This program is distributed in the hope that it will be useful,
11  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *    GNU General Public License for more details.
14  *
15  *    You should have received a copy of the GNU General Public License
16  *    along with this program; if not, write to the Free Software
17  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *    $Header: /cvsroot/jnettop/jnettop/jbase.h,v 1.6 2006/04/12 07:47:01 merunka Exp $
20  *
21  */
22 
23 #ifndef __JBASE_H__
24 #define __JBASE_H__
25 
26 #include "config.h"
27 #if NEED_REENTRANT
28 # define _REENTRANT
29 #endif
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #if HAVE_STRING_H
33 # include <string.h>
34 #elif HAVE_STRINGS_H
35 # include <strings.h>
36 #else
37 # error "No string.h nor strings.h found"
38 #endif
39 #include <sys/time.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42 #include <sys/socket.h>
43 #if HAVE_SYS_SOCKIO_H
44 # include <sys/sockio.h>
45 #endif
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <pcap.h>
49 #include <glib.h>
50 #include <errno.h>
51 #include <sys/wait.h>
52 #include "ether.h"
53 #include "ethertype.h"
54 #include "ip.h"
55 #include "tcp.h"
56 #include "udp.h"
57 #include "icmp.h"
58 #include "icmp6.h"
59 #include "sll.h"
60 #include "ieee8021q.h"
61 #include <net/if.h>
62 #include <netinet/if_ether.h>
63 #if WITH_NCURSES && HAVE_LIBNCURSES
64 # if HAVE_NCURSES_H
65 #  include <ncurses.h>
66 #  define SUPPORT_NCURSES
67 # elif HAVE_NCURSES_NCURSES_H
68 #  include <ncurses/ncurses.h>
69 #  define SUPPORT_NCURSES
70 # endif
71 #endif
72 #include <time.h>
73 #include <netdb.h>
74 #include <sys/ioctl.h>
75 #include <netinet/ip6.h>
76 #include <ctype.h>
77 #if WITH_SYSLOG
78 # if HAVE_SYSLOG_H
79 #  include <syslog.h>
80 #  define SUPPORT_SYSLOG
81 # endif
82 #endif
83 #if WITH_DB4
84 # if HAVE_DB_H && HAVE_LIBDB
85 #  include <db.h>
86 #  define SUPPORT_DB4
87 # endif
88 #endif
89 
90 
91 #define HISTORY_LENGTH			5
92 #define FREEPACKETSTACK_PEEK		50
93 #define FILTER_DATA_STRING_LENGTH	256
94 #define FILTER_DATA_STRING_LENGTH_S	"255"
95 
96 #ifdef HAVE_IP6_S6_ADDR32
97 # define ntop_s6_addr32	s6_addr32
98 #elif HAVE_IP6___U6_ADDR___U6_ADDR32
99 # define ntop_s6_addr32 __u6_addr.__u6_addr32
100 #elif HAVE_IP6__S6_UN__S6_U32
101 # define ntop_s6_addr32	_S6_un._S6_u32
102 #else
103 # error "Configure did not find the insides of struct in6_addr."
104 #endif
105 
106 #if HAVE_PCAP_FREECODE_1
107 # define JBASE_PCAP_FREECODE(a,b) pcap_freecode(b)
108 #elif HAVE_PCAP_FREECODE_2
109 # define JBASE_PCAP_FREECODE(a,b) pcap_freecode(a,b)
110 #endif
111 
112 extern char	pcap_errbuf[PCAP_ERRBUF_SIZE];
113 extern volatile int	threadCount;
114 
115 void	jbase_cb_DrawStatus(const char *statusMesage);
116 void	debug(int priority, const char *format, ...);
117 
118 typedef union __jbase_mutableaddress {
119 	struct in_addr addr4;
120 	struct in6_addr addr6;
121 } jbase_mutableaddress;
122 
123 typedef struct __jbase_resolv_entry {
124 	jbase_mutableaddress	addr;
125 	int			af;
126 	const gchar  *		name;
127 } jbase_resolv_entry;
128 
129 typedef struct __jbase_payload_info {
130 	const gchar *		data;
131 	guint			len;
132 } jbase_payload_info;
133 
134 typedef struct __jbase_device {
135         gchar                   *name;
136         struct sockaddr_storage hwaddr;
137 } jbase_device;
138 
139 typedef struct __jbase_packet {
140 	const jbase_device	* device;
141 	struct pcap_pkthdr	header;
142 	guint			dataLink;
143 	gchar 			data[BUFSIZ];
144 } jbase_packet;
145 
146 struct __jbase_stream;
147 struct __jbase_payload_info;
148 
149 #define	RXTX_RX		1
150 #define	RXTX_UNKNOWN	0
151 #define	RXTX_TX		(-1)
152 
153 typedef void (*FilterDataFunc) (struct __jbase_stream *stream, const struct __jbase_packet *packet, gboolean direction, const struct __jbase_payload_info *pi);
154 typedef void (*FilterDataFreeFunc) (struct __jbase_stream *stream);
155 
156 typedef struct __jbase_stream {
157 	// stream header information
158 	jbase_mutableaddress	src;
159 	jbase_mutableaddress	dst;
160 	guint			proto;
161 	gint			srcport;
162 	gint			dstport;
163 	struct __jbase_resolv_entry	*srcresolv;
164 	struct __jbase_resolv_entry	*dstresolv;
165 
166 	// uid
167 	guint64			uid;
168 
169 	// stream classification data
170 	gboolean		direction;
171 	int			rxtx;
172 
173 	// stream statistics information
174 	guint32			srcbytes, dstbytes, totalbytes;
175 	guint32			srcpackets, dstpackets, totalpackets;
176 	GTimeVal		firstSeen;
177 	GTimeVal		lastSeen;
178 	guint			hsrcbytes[HISTORY_LENGTH], hdstbytes[HISTORY_LENGTH];
179 	guint			hsrcpackets[HISTORY_LENGTH], hdstpackets[HISTORY_LENGTH];
180 	guint			hsrcbytessum, hdstbytessum;
181 	guint			hsrcpacketssum, hdstpacketssum;
182 	guint			srcbps, dstbps, totalbps;
183 	guint			srcpps, dstpps, totalpps;
184 
185 	// stream state information
186 	guint			dead;
187 	guint			displayed;
188 
189 	// filter data information
190 	guint			filterDataLastDisplayChangeCount;
191 	guint			filterDataChangeCount;
192 	gchar			filterDataString[FILTER_DATA_STRING_LENGTH];
193 	FilterDataFunc		filterDataFunc;
194 	FilterDataFreeFunc	filterDataFreeFunc;
195 	guchar			*filterData;
196 } jbase_stream;
197 
198 #define	SET_FILTER_DATA_STRING(stream, string) { \
199 		memset((stream)->filterDataString, 0, FILTER_DATA_STRING_LENGTH); \
200 		g_strlcpy((stream)->filterDataString, string, FILTER_DATA_STRING_LENGTH); \
201 		(stream)->filterDataChangeCount ++; \
202 	}
203 
204 #define SET_FILTER_DATA_STRING_2(stream, format, arg0, arg1) { \
205 		memset((stream)->filterDataString, 0, FILTER_DATA_STRING_LENGTH); \
206 		g_snprintf((stream)->filterDataString, FILTER_DATA_STRING_LENGTH, format, arg0, arg1); \
207 		(stream)->filterDataChangeCount ++; \
208 	}
209 
210 typedef struct __jbase_display {
211 	gboolean	supported;
212 	gboolean	(*presetup)();
213 	void		(*setup)();
214 	gboolean	(*prerunsetup)();
215 	void		(*prerun)();
216 	gboolean	(*run)();
217 	void		(*shutdown)();
218 	void		(*drawstatus)(const gchar *msg);
219 	int		(*processargument)(const gchar **arg, int cnt);
220 } jbase_display;
221 
222 typedef struct _jbase_network_mask_list {
223 	jbase_mutableaddress	network;
224 	jbase_mutableaddress	netmask;
225 	int			af;
226 	struct _jbase_network_mask_list * next;
227 } jbase_network_mask_list;
228 
229 #define	JBASE_PROTO_UNKNOWN	0
230 #define	JBASE_PROTO_IP		1
231 #define	JBASE_PROTO_TCP		2
232 #define	JBASE_PROTO_UDP		3
233 #define	JBASE_PROTO_ARP		4
234 #define JBASE_PROTO_ETHER	5
235 #define JBASE_PROTO_SLL		6
236 #define JBASE_PROTO_AGGR	7
237 #define JBASE_PROTO_ICMP	8
238 
239 #define JBASE_PROTO_IPv6_BEGIN	9
240 #define JBASE_PROTO_IP6		9
241 #define JBASE_PROTO_TCP6	10
242 #define JBASE_PROTO_UDP6	11
243 #define JBASE_PROTO_ICMP6	12
244 #define JBASE_PROTO_IPv6_END	12
245 
246 #define JBASE_PROTO_MAX		16
247 
248 #define JBASE_IS_IPV6(a)	((a) >= JBASE_PROTO_IPv6_BEGIN && (a) <= JBASE_PROTO_IPv6_END)
249 #define JBASE_AF(a)		(JBASE_IS_IPV6(a) ? AF_INET6 : AF_INET)
250 
251 #define JBASE_AF_SIZE(a)	(a == AF_INET6 ? sizeof(struct in6_addr) : sizeof(struct in_addr))
252 
253 extern gchar  *JBASE_PROTOCOLS[];
254 
255 #define AGG_UNKNOWN		(-1)
256 #define AGG_NONE		0
257 #define AGG_PORT		1
258 #define AGG_HOST		2
259 
260 extern gchar *JBASE_PROTOCOLS[];
261 extern gchar *JBASE_AGGREGATION[];
262 
263 #ifndef LOG_NOTICE
264 #define	LOG_NOTICE	5	/* normal but significant condition */
265 #endif
266 #ifndef LOG_WARNING
267 #define LOG_WARNING     4       /* warning conditions */
268 #endif
269 #ifndef LOG_ERR
270 #define LOG_ERR         3       /* error conditions */
271 #endif
272 #ifndef LOG_DEBUG
273 #define LOG_DEBUG       7       /* debug-level messages */
274 #endif
275 
276 #endif
277