| 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/jutil.c,v 1.7 2006/04/12 07:47:01 merunka Exp $ |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "jbase.h" |
| 24 |
#include "jutil.h" |
| 25 |
|
| 26 |
const char * jutil_ValidateBPFFilter(char *filter) { |
| 27 |
const char *ret = NULL; |
| 28 |
struct bpf_program program; |
| 29 |
pcap_t *pcap; |
| 30 |
pcap = pcap_open_dead(DLT_EN10MB, 1500); |
| 31 |
if (pcap_compile(pcap, &program, filter, 0, 0xFFFFFFFF) == -1) { |
| 32 |
ret = pcap_geterr(pcap); |
| 33 |
} else { |
| 34 |
JBASE_PCAP_FREECODE(pcap, &program); |
| 35 |
} |
| 36 |
pcap_close(pcap); |
| 37 |
return ret; |
| 38 |
} |
| 39 |
|
| 40 |
int jutil_IsHostAggregation(int af, const jbase_mutableaddress *addr) { |
| 41 |
switch (af) { |
| 42 |
case AF_INET: |
| 43 |
return addr->addr4.s_addr == htonl(0x01000000); |
| 44 |
case AF_INET6: |
| 45 |
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); |
| 46 |
} |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
const char * jutil_StorageAddress2String(const struct sockaddr_storage *hwaddr0, char *dst, size_t cnt) { |
| 51 |
int i; |
| 52 |
char *buf; |
| 53 |
const struct sockaddr *hwaddr = (const struct sockaddr *) hwaddr0; |
| 54 |
|
| 55 |
if (cnt < 20) { |
| 56 |
return dst; |
| 57 |
} |
| 58 |
|
| 59 |
buf = dst; |
| 60 |
for (i=0; i<6; i++) { |
| 61 |
if (i > 0) |
| 62 |
*(buf++) = ':'; |
| 63 |
sprintf(buf, "%02x", hwaddr->sa_data[i]); |
| 64 |
buf += 2; |
| 65 |
} |
| 66 |
*buf = '\0'; |
| 67 |
return dst; |
| 68 |
} |
| 69 |
|
| 70 |
const char * jutil_Address2String(int af, const jbase_mutableaddress *src, char *dst, size_t cnt) { |
| 71 |
if (jutil_IsHostAggregation(af, src)) { |
| 72 |
*dst = '\0'; |
| 73 |
return dst; |
| 74 |
} |
| 75 |
#if HAVE_INET_NTOP |
| 76 |
return inet_ntop(af, (const void *)src, dst, cnt); |
| 77 |
#elif HAVE_INET_NTOA |
| 78 |
static GStaticMutex mutex = G_STATIC_MUTEX_INIT; |
| 79 |
char *tmp, *ret = NULL; |
| 80 |
g_static_mutex_lock(&mutex); |
| 81 |
switch (af) { |
| 82 |
case AF_INET: |
| 83 |
tmp = inet_ntoa(src->addr4); |
| 84 |
break; |
| 85 |
case AF_INET6: |
| 86 |
g_snprintf(dst, cnt, "ipv6-res.-n/a"); //TODO: find an alternative way to resolve IPv6 |
| 87 |
return dst; |
| 88 |
} |
| 89 |
if (tmp && strlen(tmp)<cnt-1) { |
| 90 |
strcpy(dst, tmp); |
| 91 |
ret = dst; |
| 92 |
} |
| 93 |
g_static_mutex_unlock(&mutex); |
| 94 |
return ret; |
| 95 |
#else |
| 96 |
# error "no funtion to convert internet address to string found by configure" |
| 97 |
#endif |
| 98 |
} |
| 99 |
|
| 100 |
guint jutil_ParseAggregation(const char *agg) { |
| 101 |
if (strcmp(agg, "none") && strcmp(agg,"host") && strcmp(agg,"port")) { |
| 102 |
return AGG_UNKNOWN; |
| 103 |
} |
| 104 |
switch (*agg) { |
| 105 |
case 'n': return AGG_NONE; |
| 106 |
case 'h': return AGG_HOST; |
| 107 |
case 'p': return AGG_PORT; |
| 108 |
} |
| 109 |
return AGG_UNKNOWN; |
| 110 |
} |
| 111 |
|
| 112 |
void memand(char *buf1, const char *buf2, int length) { |
| 113 |
int i; |
| 114 |
for (i=0; i<length; i++) { |
| 115 |
buf1[i] &= buf2[i]; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
gboolean jutil_String2Address(const char *address, jbase_mutableaddress *dest, int *af) { |
| 120 |
memset(dest, '\0', sizeof(jbase_mutableaddress)); |
| 121 |
#ifdef INET_ATON |
| 122 |
if (inet_aton(address, &dest->addr4)) { |
| 123 |
*af = AF_INET; |
| 124 |
return TRUE; |
| 125 |
} |
| 126 |
return FALSE; |
| 127 |
#else |
| 128 |
unsigned long int tmpaddr; |
| 129 |
tmpaddr = inet_addr(address); |
| 130 |
if (tmpaddr == -1 && strcmp(address, "255.255.255.255")) |
| 131 |
return FALSE; |
| 132 |
memcpy(&dest->addr4, &tmpaddr, sizeof(struct in_addr)); |
| 133 |
*af = AF_INET; |
| 134 |
return TRUE; |
| 135 |
#endif |
| 136 |
} |
| 137 |
|
| 138 |
void jutil_formatNumber(guint32 n, gboolean onoffPackets, gchar *buf, int len) { |
| 139 |
gchar suffixes[] = {'b','k','m','g','t'}; |
| 140 |
gchar fmt[64]; |
| 141 |
int mag = 0; |
| 142 |
int ipart,fpart = 0; |
| 143 |
gdouble f = (gdouble)n; |
| 144 |
while (mag<4 && f>1000.0) { |
| 145 |
mag ++; |
| 146 |
f /= 1024.0; |
| 147 |
} |
| 148 |
sprintf(fmt, "%.0f", f); |
| 149 |
ipart = strlen(fmt); |
| 150 |
while (ipart+1+fpart+2 < len && mag > 0) |
| 151 |
fpart ++; |
| 152 |
if (ipart+1+fpart+2 > len) { |
| 153 |
sprintf(buf, "ERR"); |
| 154 |
return; |
| 155 |
} |
| 156 |
sprintf(fmt, "%%%d.%df%c", ipart, fpart, !mag && onoffPackets ? 'p' : suffixes[mag]); |
| 157 |
sprintf(buf, fmt, f); |
| 158 |
} |
| 159 |
|
| 160 |
gboolean jutil_IsInNetwork(const jbase_mutableaddress *address, int address_af, const jbase_mutableaddress *network, const jbase_mutableaddress *netmask, int network_af) { |
| 161 |
jbase_mutableaddress addr; |
| 162 |
if (address_af != network_af) |
| 163 |
return FALSE; |
| 164 |
memcpy(&addr, address, JBASE_AF_SIZE(address_af)); |
| 165 |
memand((char *) &addr, (const char *) netmask, JBASE_AF_SIZE(address_af)); |
| 166 |
return !memcmp(&addr, network, JBASE_AF_SIZE(address_af)); |
| 167 |
} |
| 168 |
|
| 169 |
void jutil_InterpretStreamFormat(GString *str, const char *formatString, const jbase_stream *s) { |
| 170 |
const char *fmt; |
| 171 |
gchar addr[INET6_ADDRSTRLEN + 1]; |
| 172 |
gchar *eitem; |
| 173 |
|
| 174 |
fmt = formatString; |
| 175 |
|
| 176 |
#define PRINT_ADDRESS(id,fld) if (!strcmp(fmt, id)) { \ |
| 177 |
jutil_Address2String(JBASE_AF(s->proto), &(fld), addr, INET6_ADDRSTRLEN); \ |
| 178 |
g_string_append_printf(str, "%s", addr); \ |
| 179 |
goto nexteitem; \ |
| 180 |
} |
| 181 |
|
| 182 |
#define PRINT_STRING_OR_NULL(id,snull,fld) if (!strcmp(fmt, id)) { \ |
| 183 |
g_string_append_printf(str, "%s", (snull) ? "" : fld); \ |
| 184 |
goto nexteitem; \ |
| 185 |
} |
| 186 |
|
| 187 |
#define PRINT_GUINT(id,fld) if (!strcmp(fmt, id)) { \ |
| 188 |
g_string_append_printf(str, "%d", fld); \ |
| 189 |
goto nexteitem; \ |
| 190 |
} |
| 191 |
|
| 192 |
#define PRINT_GUINT64(id,fld) if (!strcmp(fmt, id)) { \ |
| 193 |
g_string_append_printf(str, "%08x%08x", (unsigned int)(fld>>32), (unsigned int)(fld&0xffffffff)); \ |
| 194 |
goto nexteitem; \ |
| 195 |
} |
| 196 |
|
| 197 |
#define PRINT_PORT(id,fld) if (!strcmp(fmt, id)) { \ |
| 198 |
if ((fld)==-1) g_string_append(str, "AGGR."); else g_string_append_printf(str, "%d", fld); \ |
| 199 |
goto nexteitem; \ |
| 200 |
} |
| 201 |
|
| 202 |
do { |
| 203 |
eitem = strchr(fmt, '$'); |
| 204 |
if (eitem) *eitem = '\0'; |
| 205 |
g_string_append(str, fmt); |
| 206 |
if (eitem) |
| 207 |
*eitem = '$'; |
| 208 |
else |
| 209 |
break; |
| 210 |
|
| 211 |
fmt = eitem + 1; |
| 212 |
eitem = strchr(fmt, '$'); |
| 213 |
if (eitem) *eitem = '\0'; |
| 214 |
PRINT_GUINT64("uid", s->uid); |
| 215 |
PRINT_ADDRESS("src", s->src); |
| 216 |
PRINT_ADDRESS("dst", s->dst); |
| 217 |
PRINT_STRING_OR_NULL("srcname", s->srcresolv == NULL || s->srcresolv->name == NULL, s->srcresolv->name); |
| 218 |
PRINT_STRING_OR_NULL("dstname", s->dstresolv == NULL || s->dstresolv->name == NULL, s->dstresolv->name); |
| 219 |
PRINT_STRING_OR_NULL("proto", FALSE, JBASE_PROTOCOLS[s->proto]); |
| 220 |
PRINT_PORT("srcport", s->srcport); |
| 221 |
PRINT_PORT("dstport", s->dstport); |
| 222 |
PRINT_GUINT("srcbytes", s->srcbytes); |
| 223 |
PRINT_GUINT("dstbytes", s->dstbytes); |
| 224 |
PRINT_GUINT("srcpackets", s->srcpackets); |
| 225 |
PRINT_GUINT("dstpackets", s->dstpackets); |
| 226 |
PRINT_GUINT("totalbytes", s->totalbytes); |
| 227 |
PRINT_GUINT("totalpackets", s->totalpackets); |
| 228 |
PRINT_GUINT("srcbps", s->srcbps); |
| 229 |
PRINT_GUINT("dstbps", s->dstbps); |
| 230 |
PRINT_GUINT("totalbps", s->totalbps); |
| 231 |
PRINT_GUINT("srcpps", s->srcpps); |
| 232 |
PRINT_GUINT("dstpps", s->dstpps); |
| 233 |
PRINT_GUINT("totalpps", s->totalpps); |
| 234 |
PRINT_STRING_OR_NULL("filterdata", s->filterDataString == NULL, s->filterDataString); |
| 235 |
PRINT_STRING_OR_NULL("filterdataifchanged", s->filterDataString == NULL || s->filterDataLastDisplayChangeCount == s->filterDataChangeCount, s->filterDataString); |
| 236 |
g_string_append_printf(str, "?%s?", fmt); |
| 237 |
nexteitem: |
| 238 |
if (eitem) |
| 239 |
*eitem = '$'; |
| 240 |
else |
| 241 |
break; |
| 242 |
fmt = eitem + 1; |
| 243 |
} while (TRUE); |
| 244 |
} |