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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/* traffic_table_ui.c
* Copied from gtk/conversations_table.c 2003 Ronnie Sahlberg
* Helper routines common to all conversations taps.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <glib.h>
#include "traffic_table_ui.h"
#include <wsutil/utf8_entities.h>
#ifdef HAVE_GEOIP
#include <GeoIP.h>
#include "epan/address.h"
#include "epan/addr_resolv.h"
#include "epan/geoip_db.h"
#include "epan/strutil.h"
#include "wsutil/pint.h"
#include "wsutil/str_util.h"
#include "epan/packet_info.h"
#include "epan/conversation_table.h"
#include <errno.h>
#include <stdio.h>
#include "wsutil/filesystem.h"
#include "wsutil/file_util.h"
#include "wsutil/tempfile.h"
#endif
const char *conv_column_titles[CONV_NUM_COLUMNS] = {
"Address A",
"Port A",
"Address B",
"Port B",
"Packets",
"Bytes",
"Packets A " UTF8_RIGHTWARDS_ARROW " B",
"Bytes A " UTF8_RIGHTWARDS_ARROW " B",
"Packets B " UTF8_RIGHTWARDS_ARROW " A",
"Bytes B " UTF8_RIGHTWARDS_ARROW " A",
"Rel Start",
"Duration",
"Bits/s A " UTF8_RIGHTWARDS_ARROW " B",
"Bits/s B " UTF8_RIGHTWARDS_ARROW " A"
};
const char *conv_conn_a_title = "Connection A";
const char *conv_conn_b_title = "Connection B";
const char *conv_abs_start_title = "Abs Start";
const char *endp_column_titles[ENDP_NUM_COLUMNS] = {
"Address",
"Port",
"Packets",
"Bytes",
"Packets A " UTF8_RIGHTWARDS_ARROW " B",
"Bytes A " UTF8_RIGHTWARDS_ARROW " B",
"Packets B " UTF8_RIGHTWARDS_ARROW " A",
"Bytes B " UTF8_RIGHTWARDS_ARROW " A"
};
const char *endp_conn_title = "Connection";
#ifdef HAVE_GEOIP
#define MAX_TPL_LINE_LEN 4096
gchar *
create_endpoint_geoip_map(const GArray *endp_array, gchar **err_str) {
char *tpl_filename, *tpl_line;
FILE *tpl_file, *out_file;
char *map_path;
gchar *map_filename = NULL;
guint i;
GString *tpl_entry;
gchar *esc_entry;
int db_lon, db_lat, db_country4, db_country6, db_city4, db_city6, db_asn4, db_asn6;
guint cur_db;
const char *map_endpoint_opener = "{\n";
db_lon = db_lat = db_country4 = db_country6 = db_city4 = db_city6 = db_asn4 = db_asn6 = -1;
/* Create a location map HTML file from a template */
/* XXX - add error handling */
tpl_filename = get_datafile_path("ipmap.html");
tpl_file = ws_fopen(tpl_filename, "r");
if(tpl_file == NULL) {
if (err_str) {
GString *err_descr = g_string_new("");
g_string_printf(err_descr, file_open_error_message(errno, FALSE), tpl_filename);
*err_str = g_string_free(err_descr, FALSE);
}
g_free(tpl_filename);
return NULL;
}
g_free(tpl_filename);
#if 1
/* We should probably create a file with a temporary name and a .html extension instead */
if (! create_tempdir(&map_path, "Wireshark IP Map ")) {
if (err_str) {
GString *err_descr = g_string_new("");
g_string_printf(err_descr, "Could not create temporary directory\n%s",
map_path);
*err_str = g_string_free(err_descr, FALSE);
}
fclose(tpl_file);
return NULL;
}
#else
/* Debugging only */
map_path = "/tmp";
#endif
map_filename = g_strdup_printf("%s%cipmap.html", map_path, G_DIR_SEPARATOR);
out_file = ws_fopen(map_filename, "w");
if(out_file == NULL) {
if (err_str) {
GString *err_descr = g_string_new("");
g_string_printf(err_descr, file_open_error_message(errno, FALSE), map_filename);
*err_str = g_string_free(err_descr, FALSE);
}
g_free(map_filename);
fclose(tpl_file);
return NULL;
}
tpl_line = (char *)g_malloc(MAX_TPL_LINE_LEN);
while (fgets(tpl_line, MAX_TPL_LINE_LEN, tpl_file) != NULL) {
fputs(tpl_line, out_file);
/* MUST match ipmap.html */
if (strstr(tpl_line, "// Start endpoint list")) {
break;
}
}
for (cur_db = 0; cur_db < geoip_db_num_dbs(); cur_db++) {
switch (geoip_db_type(cur_db)) {
case WS_LON_FAKE_EDITION:
db_lon = cur_db;
break;
case WS_LAT_FAKE_EDITION:
db_lat = cur_db;
break;
case GEOIP_COUNTRY_EDITION:
db_country4 = cur_db;
break;
case GEOIP_COUNTRY_EDITION_V6:
db_country6 = cur_db;
break;
case GEOIP_CITY_EDITION_REV0:
case GEOIP_CITY_EDITION_REV1:
db_city4 = cur_db;
break;
case GEOIP_CITY_EDITION_REV0_V6:
case GEOIP_CITY_EDITION_REV1_V6:
db_city6 = cur_db;
break;
}
}
if(db_lon < 0 || db_lat < 0) {
if (err_str) {
*err_str = g_strdup("Unable to open GeoIP database");
}
/* We can't write the map file, so close it and get rid of it */
fclose(out_file);
ws_unlink(map_filename);
g_free(map_filename);
fclose(tpl_file);
return NULL;
}
/* Fill in our map data */
tpl_entry = g_string_new("");
for (i = 0; i < endp_array->len; i++) {
char *lat = NULL, *lon = NULL, *country = NULL, *city = NULL, *asn = NULL;
hostlist_talker_t *endp_item = &g_array_index(endp_array, hostlist_talker_t, i);
if (endp_item->myaddress.type == AT_IPv4) {
lon = geoip_db_lookup_ipv4(db_lon, pntoh32(endp_item->myaddress.data), NULL);
lat = geoip_db_lookup_ipv4(db_lat, pntoh32(endp_item->myaddress.data), NULL);
country = geoip_db_lookup_ipv4(db_country4, pntoh32(endp_item->myaddress.data), "-");
city = geoip_db_lookup_ipv4(db_city4, pntoh32(endp_item->myaddress.data), "-");
asn = geoip_db_lookup_ipv4(db_asn4, pntoh32(endp_item->myaddress.data), "-");
} else if (endp_item->myaddress.type == AT_IPv6) {
const struct e_in6_addr *addr = (const struct e_in6_addr *) endp_item->myaddress.data;
lon = geoip_db_lookup_ipv6(db_lon, *addr, NULL);
lat = geoip_db_lookup_ipv6(db_lat, *addr, NULL);
country = geoip_db_lookup_ipv6(db_country6, *addr, "-");
city = geoip_db_lookup_ipv6(db_city6, *addr, "-");
asn = geoip_db_lookup_ipv6(db_asn6, *addr, "-");
} else {
continue;
}
/*
{
'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [-122.583889, 37.898889] },
'properties': { 'title': 'host.example.com', 'description': 'AS: AS12345 Ewok Holdings, Inc.<br/>Country: US<br/>City: Muir Woods, CA<br/>Packets: 6<br/>Bytes: 980' }
},
*/
if (lon && lat) {
char* addr_str;
g_string_printf(tpl_entry, "%s", map_endpoint_opener);
/* Longitude + latitude */
g_string_append_printf(tpl_entry, " 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [%s, %s] },\n", lon, lat);
/* Address */
addr_str = address_to_display(NULL, &endp_item->myaddress);
g_string_append_printf(tpl_entry, " 'properties': { 'title': '%s', ", addr_str);
wmem_free(NULL, addr_str);
/* Description */
/* City */
esc_entry = string_replace(city, "'", "'");
g_string_append_printf(tpl_entry, "'description': '<div class=\"geoip_property\">City: %s</div>", esc_entry);
g_free(esc_entry);
/* Country */
esc_entry = string_replace(country, "'", "'");
g_string_append_printf(tpl_entry, "<div class=\"geoip_property\">Country: %s</div>", esc_entry);
g_free(esc_entry);
/* Packets */
esc_entry = format_size(endp_item->tx_frames + endp_item->rx_frames,
(format_size_flags_e)(format_size_unit_none|format_size_prefix_si));
g_string_append_printf(tpl_entry, "<div class=\"geoip_property\">Packets: %s</div>", esc_entry);
g_free(esc_entry);
/* Bytes */
esc_entry = format_size(endp_item->tx_bytes + endp_item->rx_bytes,
(format_size_flags_e)(format_size_unit_none|format_size_prefix_si));
g_string_append_printf(tpl_entry, "<div class=\"geoip_property\">Bytes: %s</div>", esc_entry);
g_free(esc_entry);
/* ASN */
esc_entry = string_replace(asn, "'", "'");
g_string_append_printf(tpl_entry, "<div class=\"geoip_property\">AS Number: %s</div>", esc_entry);
g_free(esc_entry);
/* XXX - We could add specific icons, e.g. depending on the amount of packets or bytes */
g_string_append(tpl_entry, "' }\n");
g_string_append(tpl_entry, "}");
fputs(tpl_entry->str, out_file);
map_endpoint_opener = ",\n{\n";
}
wmem_free(NULL, lat);
wmem_free(NULL, lon);
wmem_free(NULL, country);
wmem_free(NULL, city);
wmem_free(NULL, asn);
/* XXX Display an error if we we have no entries */
}
while (fgets(tpl_line, MAX_TPL_LINE_LEN, tpl_file) != NULL) {
fputs(tpl_line, out_file);
}
g_free(tpl_line);
fclose(tpl_file);
fclose(out_file);
return map_filename;
}
#endif
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/
|