File: jutil.c

package info (click to toggle)
jnettop 0.13.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: ansic: 4,012; sh: 775; makefile: 20
file content (245 lines) | stat: -rw-r--r-- 7,392 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 *    jnettop, network online traffic visualiser
 *    Copyright (C) 2002-2005 Jakub Skopal
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    $Header: /cvsroot/jnettop/jnettop/jutil.c,v 1.7 2006/04/12 07:47:01 merunka Exp $
 *
 */

#include "jbase.h"
#include "jutil.h"

const char * jutil_ValidateBPFFilter(char *filter) {
	const char *ret = NULL;
	struct bpf_program program;
	pcap_t *pcap;
	pcap = pcap_open_dead(DLT_EN10MB, 1500);
	if (pcap_compile(pcap, &program, filter, 0, 0xFFFFFFFF) == -1) {
		ret = pcap_geterr(pcap);
	} else {
		JBASE_PCAP_FREECODE(pcap, &program);
	}
	pcap_close(pcap);
	return ret;
}

int	jutil_IsHostAggregation(int af, const jbase_mutableaddress *addr) {
	switch (af) {
		case AF_INET:
			return addr->addr4.s_addr == htonl(0x01000000);
		case AF_INET6:
			return addr->addr6.ntop_s6_addr32[0] == 0x0 && addr->addr6.ntop_s6_addr32[1] == 0x0 && addr->addr6.ntop_s6_addr32[2] == 0x0  && addr->addr6.ntop_s6_addr32[3] == htonl(0x01000000);
	}
	return 0;
}

const char * jutil_StorageAddress2String(const struct sockaddr_storage *hwaddr0, char *dst, size_t cnt) {
	int i;
	char *buf;
	const struct sockaddr *hwaddr = (const struct sockaddr *) hwaddr0;

	if (cnt < 20) {
		return dst;
	}

	buf = dst;
	for (i=0; i<6; i++) {
		if (i > 0)
			*(buf++) = ':';
		sprintf(buf, "%02x", hwaddr->sa_data[i]);
		buf += 2;
	}
	*buf = '\0';
	return dst;
}

const char * jutil_Address2String(int af, const jbase_mutableaddress *src, char *dst, size_t cnt) {
	if (jutil_IsHostAggregation(af, src)) {
		*dst = '\0';
		return dst;
	}
#if HAVE_INET_NTOP
	return inet_ntop(af, (const void *)src, dst, cnt);
#elif HAVE_INET_NTOA
	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
	char *tmp, *ret = NULL;
	g_static_mutex_lock(&mutex);
	switch (af) {
	case AF_INET:
		tmp = inet_ntoa(src->addr4);
		break;
	case AF_INET6:
		g_snprintf(dst, cnt, "ipv6-res.-n/a"); //TODO: find an alternative way to resolve IPv6
		return dst;
	}
	if (tmp && strlen(tmp)<cnt-1) {
		strcpy(dst, tmp);
		ret = dst;
	}
	g_static_mutex_unlock(&mutex);
	return ret;
#else
# error "no funtion to convert internet address to string found by configure"
#endif
}

guint     jutil_ParseAggregation(const char *agg) {
	if (strcmp(agg, "none") == 0)
		return AGG_NONE;
	else if (strcmp(agg,"host") == 0)
		return AGG_HOST;
	else if (strcmp(agg,"port") == 0)
		return AGG_PORT;
	else if (strcmp(agg,"host+port") == 0)
		return AGG_BOTH;
	else
		return AGG_UNKNOWN;
}

void memand(char *buf1, const char *buf2, int length) {
	int i;
	for (i=0; i<length; i++) {
		buf1[i] &= buf2[i];
	}
}

gboolean jutil_String2Address(const char *address, jbase_mutableaddress *dest, int *af) {
	memset(dest, '\0', sizeof(jbase_mutableaddress));
#ifdef INET_ATON
	if (inet_aton(address, &dest->addr4)) {
		*af = AF_INET;
		return TRUE;
	}
	return FALSE;
#else
	unsigned long int tmpaddr;
	tmpaddr = inet_addr(address);
	if (tmpaddr == -1 && strcmp(address, "255.255.255.255"))
		return FALSE;
	memcpy(&dest->addr4, &tmpaddr, sizeof(struct in_addr));
	*af = AF_INET;
	return TRUE;
#endif
}

void jutil_formatNumber(guint64 n, gboolean onoffPackets, gchar *buf, int len) {
	gchar suffixes[] = {'b','K','M','G','T','P','E','Z'};
	gchar fmt[64];
	int  mag = 0;
	int  ipart,fpart = 0;
	gdouble f = (gdouble)n;
	while (mag<4 && f>1000.0) {
		mag ++;
		f /= 1024.0;
	}
	sprintf(fmt, "%.0f", f);
	ipart = strlen(fmt);
	while (ipart+1+fpart+2 < len && mag > 0)
		fpart ++;
	if (ipart+1+fpart+2 > len) {
		sprintf(buf, "ERR");
		return;
	}
	sprintf(fmt, "%%%d.%df%c", ipart, fpart, !mag && onoffPackets ? 'p' : suffixes[mag]);
	sprintf(buf, fmt, f);
}

gboolean jutil_IsInNetwork(const jbase_mutableaddress *address, int address_af, const jbase_mutableaddress *network, const jbase_mutableaddress *netmask, int network_af) {
	jbase_mutableaddress	addr;
	if (address_af != network_af)
		return FALSE;
	memcpy(&addr, address, JBASE_AF_SIZE(address_af));
	memand((char *) &addr, (const char *) netmask, JBASE_AF_SIZE(address_af));
	return !memcmp(&addr, network, JBASE_AF_SIZE(address_af));
}

void jutil_InterpretStreamFormat(GString *str, const char *formatString, const jbase_stream *s) {
	const char *fmt;
	gchar addr[INET6_ADDRSTRLEN + 1];
	gchar *eitem;

	fmt = formatString;

#define PRINT_ADDRESS(id,fld) if (!strcmp(fmt, id)) { \
				jutil_Address2String(JBASE_AF(s->proto), &(fld), addr, INET6_ADDRSTRLEN); \
				g_string_append_printf(str, "%s", addr); \
				goto nexteitem; \
			}
				
#define PRINT_STRING_OR_NULL(id,snull,fld) if (!strcmp(fmt, id)) { \
				g_string_append_printf(str, "%s", (snull) ? "" : fld); \
				goto nexteitem; \
			}

#define PRINT_GUINT(id,fld) if (!strcmp(fmt, id)) { \
				g_string_append_printf(str, "%lld", fld); \
				goto nexteitem; \
			}

#define PRINT_GUINT64(id,fld) if (!strcmp(fmt, id)) { \
				g_string_append_printf(str, "%08x%08x", (unsigned int)(fld>>32), (unsigned int)(fld&0xffffffff)); \
				goto nexteitem; \
			}

#define PRINT_PORT(id,fld) if (!strcmp(fmt, id)) { \
				if ((fld)==-1) g_string_append(str, "AGGR."); else g_string_append_printf(str, "%d", fld); \
				goto nexteitem; \
			}

		do {
			eitem = strchr(fmt, '$');
			if (eitem) *eitem = '\0';
			g_string_append(str, fmt);
			if (eitem)
				*eitem = '$';
			else
				break;
			
			fmt = eitem + 1;
			eitem = strchr(fmt, '$');
			if (eitem) *eitem = '\0';
			PRINT_GUINT64("uid", s->uid);
			PRINT_ADDRESS("src", s->src);
			PRINT_ADDRESS("dst", s->dst);
			PRINT_STRING_OR_NULL("srcname", s->srcresolv == NULL || s->srcresolv->name == NULL, s->srcresolv->name);
			PRINT_STRING_OR_NULL("dstname", s->dstresolv == NULL || s->dstresolv->name == NULL, s->dstresolv->name);
			PRINT_STRING_OR_NULL("proto", FALSE, JBASE_PROTOCOLS[s->proto]);
			PRINT_PORT("srcport", s->srcport);
			PRINT_PORT("dstport", s->dstport);
			PRINT_GUINT("srcbytes", s->srcbytes);
			PRINT_GUINT("dstbytes", s->dstbytes);
			PRINT_GUINT("srcpackets", s->srcpackets);
			PRINT_GUINT("dstpackets", s->dstpackets);
			PRINT_GUINT("totalbytes", s->totalbytes);
			PRINT_GUINT("totalpackets", s->totalpackets);
			PRINT_GUINT("srcbps", s->srcbps);
			PRINT_GUINT("dstbps", s->dstbps);
			PRINT_GUINT("totalbps", s->totalbps);
			PRINT_GUINT("srcpps", s->srcpps);
			PRINT_GUINT("dstpps", s->dstpps);
			PRINT_GUINT("totalpps", s->totalpps);
			PRINT_STRING_OR_NULL("filterdata", s->filterDataString == NULL, s->filterDataString);
			PRINT_STRING_OR_NULL("filterdataifchanged", s->filterDataString == NULL || s->filterDataLastDisplayChangeCount == s->filterDataChangeCount, s->filterDataString);
			g_string_append_printf(str, "?%s?", fmt);
nexteitem:
			if (eitem)
				*eitem = '$';
			else
				break;
			fmt = eitem + 1;
		} while (TRUE); 
}