Camel/Inap: add missing sub-trees
[metze/wireshark/wip.git] / sharkd_session.c
index 2453b1311ce2a898e7be14493d8d465462763c7b..bffacf4b3efb281be30e2e22097814b26e74d2c3 100644 (file)
@@ -6,7 +6,7 @@
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
- * SPDX-License-Identifier: GPL-2.0+
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include <config.h>
@@ -18,7 +18,7 @@
 
 #include <glib.h>
 
-#include <wsutil/wsjsmn.h>
+#include <wsutil/wsjson.h>
 #include <wsutil/ws_printf.h>
 
 #include <file.h>
 # include <codecs/speex/speex_resampler.h>
 #endif /* HAVE_SPEEXDSP */
 
-#ifdef HAVE_GEOIP
-# include <GeoIP.h>
-# include <epan/geoip_db.h>
-# include <wsutil/pint.h>
-#endif
+#include <epan/maxmind_db.h>
 
-#include <wsutil/glib-compat.h>
+#include <wsutil/pint.h>
 #include <wsutil/strtoi.h>
 
 #include "globals.h"
@@ -83,7 +79,7 @@ static GHashTable *filter_table = NULL;
 static gboolean
 json_unescape_str(char *input)
 {
-       return wsjsmn_unescape_json_string(input, input);
+       return wsjson_unescape_json_string(input, input);
 }
 
 static const char *
@@ -267,11 +263,11 @@ sharkd_rtp_match_check(const struct sharkd_rtp_match *req, const packet_info *pi
 static gboolean
 sharkd_session_process_info_nstat_cb(const void *key, void *value, void *userdata)
 {
-       stat_tap_table_ui *new_stat_tap = (stat_tap_table_ui *) value;
+       stat_tap_table_ui *stat_tap = (stat_tap_table_ui *) value;
        int *pi = (int *) userdata;
 
        printf("%s{", (*pi) ? "," : "");
-               printf("\"name\":\"%s\"", new_stat_tap->title);
+               printf("\"name\":\"%s\"", stat_tap->title);
                printf(",\"tap\":\"nstat:%s\"", (const char *) key);
        printf("}");
 
@@ -497,7 +493,7 @@ sharkd_session_process_info(void)
 
        printf(",\"nstat\":[");
        i = 0;
-       new_stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i);
+       stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i);
        printf("]");
 
        printf(",\"convs\":[");
@@ -724,13 +720,15 @@ sharkd_session_create_columns(column_info *cinfo, const char *buf, const jsmntok
                if (tok_column == NULL)
                        break;
 
+               columns_custom[i] = NULL;
+               columns_occur[i] = 0;
+
                if ((custom_sepa = strchr(tok_column, ':')))
                {
                        *custom_sepa = '\0'; /* XXX, C abuse: discarding-const */
 
                        columns_fmt[i] = COL_CUSTOM;
                        columns_custom[i] = tok_column;
-                       columns_occur[i] = 0;
 
                        if (!ws_strtoi16(custom_sepa + 1, NULL, &columns_occur[i]))
                                return NULL;
@@ -1098,16 +1096,21 @@ static gboolean
 sharkd_session_packet_tap_expert_cb(void *tapdata, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pointer)
 {
        struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
-       expert_info_t *ei             = (expert_info_t *) pointer;
+       const expert_info_t *ei       = (const expert_info_t *) pointer;
+       expert_info_t *ei_copy;
 
-       ei = (expert_info_t *) g_memdup(ei, sizeof(*ei));
        if (ei == NULL)
                return FALSE;
 
-       ei->protocol = g_string_chunk_insert_const(etd->text, ei->protocol);
-       ei->summary  = g_string_chunk_insert_const(etd->text, ei->summary);
+       ei_copy = g_new(expert_info_t, 1);
+       /* Note: this is a shallow copy */
+       *ei_copy = *ei;
 
-       etd->details = g_slist_prepend(etd->details, ei);
+       /* ei->protocol, ei->summary might be allocated in packet scope, make a copy. */
+       ei_copy->protocol = g_string_chunk_insert_const(etd->text, ei_copy->protocol);
+       ei_copy->summary  = g_string_chunk_insert_const(etd->text, ei_copy->summary);
+
+       etd->details = g_slist_prepend(etd->details, ei_copy);
 
        return TRUE;
 }
@@ -1211,126 +1214,73 @@ struct sharkd_conv_tap_data
        gboolean resolve_port;
 };
 
-static int
+static gboolean
 sharkd_session_geoip_addr(address *addr, const char *suffix)
 {
-       int with_geoip = 0;
+       const mmdb_lookup_t *lookup = NULL;
+       gboolean with_geoip = FALSE;
 
-       (void) addr;
-       (void) suffix;
-
-#ifdef HAVE_GEOIP
        if (addr->type == AT_IPv4)
        {
                guint32 ip = pntoh32(addr->data);
 
-               guint num_dbs = geoip_db_num_dbs();
-               guint dbnum;
-
-               for (dbnum = 0; dbnum < num_dbs; dbnum++)
-               {
-                       const char *geoip_key = NULL;
-                       char *geoip_val;
-
-                       int db_type = geoip_db_type(dbnum);
-
-                       switch (db_type)
-                       {
-                               case GEOIP_COUNTRY_EDITION:
-                                       geoip_key = "geoip_country";
-                                       break;
-
-                               case GEOIP_CITY_EDITION_REV0:
-                               case GEOIP_CITY_EDITION_REV1:
-                                       geoip_key = "geoip_city";
-                                       break;
-
-                               case GEOIP_ORG_EDITION:
-                                       geoip_key = "geoip_org";
-                                       break;
-
-                               case GEOIP_ISP_EDITION:
-                                       geoip_key = "geoip_isp";
-                                       break;
-
-                               case GEOIP_ASNUM_EDITION:
-                                       geoip_key = "geoip_as";
-                                       break;
-
-                               case WS_LAT_FAKE_EDITION:
-                                       geoip_key = "geoip_lat";
-                                       break;
-
-                               case WS_LON_FAKE_EDITION:
-                                       geoip_key = "geoip_lon";
-                                       break;
-                       }
-
-                       if (geoip_key && (geoip_val = geoip_db_lookup_ipv4(dbnum, ip, NULL)))
-                       {
-                               printf(",\"%s%s\":", geoip_key, suffix);
-                               json_puts_string(geoip_val);
-                               with_geoip = 1;
-                       }
-               }
+               lookup = maxmind_db_lookup_ipv4(ip);
        }
-#ifdef HAVE_GEOIP_V6
-       if (addr->type == AT_IPv6)
+       else if (addr->type == AT_IPv6)
        {
                const ws_in6_addr *ip6 = (const ws_in6_addr *) addr->data;
 
-               guint num_dbs = geoip_db_num_dbs();
-               guint dbnum;
+               lookup = maxmind_db_lookup_ipv6(ip6);
+       }
 
-               for (dbnum = 0; dbnum < num_dbs; dbnum++)
-               {
-                       const char *geoip_key = NULL;
-                       char *geoip_val;
+       if (!lookup || !lookup->found)
+               return FALSE;
 
-                       int db_type = geoip_db_type(dbnum);
+       if (lookup->country)
+       {
+               printf(",\"geoip_country%s\":", suffix);
+               json_puts_string(lookup->country);
+               with_geoip = TRUE;
+       }
 
-                       switch (db_type)
-                       {
-                               case GEOIP_COUNTRY_EDITION_V6:
-                                       geoip_key = "geoip_country";
-                                       break;
-#if NUM_DB_TYPES > 31
-                               case GEOIP_CITY_EDITION_REV0_V6:
-                               case GEOIP_CITY_EDITION_REV1_V6:
-                                       geoip_key = "geoip_city";
-                                       break;
+       if (lookup->country_iso)
+       {
+               printf(",\"geoip_country_iso%s\":", suffix);
+               json_puts_string(lookup->country_iso);
+               with_geoip = TRUE;
+       }
 
-                               case GEOIP_ORG_EDITION_V6:
-                                       geoip_key = "geoip_org";
-                                       break;
+       if (lookup->city)
+       {
+               printf(",\"geoip_city%s\":", suffix);
+               json_puts_string(lookup->city);
+               with_geoip = TRUE;
+       }
 
-                               case GEOIP_ISP_EDITION_V6:
-                                       geoip_key = "geoip_isp";
-                                       break;
+       if (lookup->as_org)
+       {
+               printf(",\"geoip_as_org%s\":", suffix);
+               json_puts_string(lookup->as_org);
+               with_geoip = TRUE;
+       }
 
-                               case GEOIP_ASNUM_EDITION_V6:
-                                       geoip_key = "geoip_as";
-                                       break;
-#endif /* DB_NUM_TYPES */
-                               case WS_LAT_FAKE_EDITION:
-                                       geoip_key = "geoip_lat";
-                                       break;
+       if (lookup->as_number > 0)
+       {
+               printf(",\"geoip_as%s\":%u", suffix, lookup->as_number);
+               with_geoip = TRUE;
+       }
 
-                               case WS_LON_FAKE_EDITION:
-                                       geoip_key = "geoip_lon";
-                                       break;
-                       }
+       if (lookup->latitude >= -90.0 && lookup->latitude <= 90.0)
+       {
+               printf(",\"geoip_lat%s\":%f", suffix, lookup->latitude);
+               with_geoip = TRUE;
+       }
 
-                       if (geoip_key && (geoip_val = geoip_db_lookup_ipv6(dbnum, *ip6, NULL)))
-                       {
-                               printf(",\"%s%s\":", geoip_key, suffix);
-                               json_puts_string(geoip_val);
-                               with_geoip = 1;
-                       }
-               }
+       if (lookup->longitude >= -180.0 && lookup->longitude <= 180.0)
+       {
+               printf(",\"geoip_lon%s\":%f", suffix, lookup->longitude);
+               with_geoip = TRUE;
        }
-#endif /* HAVE_GEOIP_V6 */
-#endif /* HAVE_GEOIP */
 
        return with_geoip;
 }
@@ -1704,7 +1654,7 @@ sharkd_session_free_tap_conv_cb(void *arg)
 static void
 sharkd_session_process_tap_nstat_cb(void *arg)
 {
-       new_stat_data_t *stat_data = (new_stat_data_t *) arg;
+       stat_data_t *stat_data = (stat_data_t *) arg;
        guint i, j, k;
 
        printf("{\"tap\":\"nstat:%s\",\"type\":\"nstat\"", stat_data->stat_tap_data->cli_string);
@@ -1745,14 +1695,14 @@ sharkd_session_process_tap_nstat_cb(void *arg)
                {
                        stat_tap_table_item_type *field_data;
 
-                       field_data = new_stat_tap_get_field_data(table, j, 0);
+                       field_data = stat_tap_get_field_data(table, j, 0);
                        if (field_data == NULL || field_data->type == TABLE_ITEM_NONE) /* Nothing for us here */
                                continue;
 
                        printf("%s[", sepa);
                        for (k = 0; k < table->num_fields; k++)
                        {
-                               field_data = new_stat_tap_get_field_data(table, j, k);
+                               field_data = stat_tap_get_field_data(table, j, k);
 
                                if (k)
                                        printf(",");
@@ -1764,7 +1714,7 @@ sharkd_session_process_tap_nstat_cb(void *arg)
                                                break;
 
                                        case TABLE_ITEM_INT:
-                                               printf("%d", field_data->value.uint_value);
+                                               printf("%d", field_data->value.int_value);
                                                break;
 
                                        case TABLE_ITEM_STRING:
@@ -1798,7 +1748,7 @@ sharkd_session_process_tap_nstat_cb(void *arg)
 static void
 sharkd_session_free_tap_nstat_cb(void *arg)
 {
-       new_stat_data_t *stat_data = (new_stat_data_t *) arg;
+       stat_data_t *stat_data = (stat_data_t *) arg;
 
        free_stat_tables(stat_data->stat_tap_data, NULL, NULL);
 }
@@ -2357,8 +2307,8 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
                }
                else if (!strncmp(tok_tap, "nstat:", 6))
                {
-                       stat_tap_table_ui *stat_tap = new_stat_tap_by_name(tok_tap + 6);
-                       new_stat_data_t *stat_data;
+                       stat_tap_table_ui *stat_tap = stat_tap_by_name(tok_tap + 6);
+                       stat_data_t *stat_data;
 
                        if (!stat_tap)
                        {
@@ -2368,7 +2318,7 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
 
                        stat_tap->stat_tap_init_cb(stat_tap, NULL, NULL);
 
-                       stat_data = g_new0(new_stat_data_t, 1);
+                       stat_data = g_new0(stat_data_t, 1);
                        stat_data->stat_tap_data = stat_tap;
                        stat_data->user_data = NULL;
 
@@ -2818,7 +2768,7 @@ sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct ep
        if (fdata->flags.has_user_comment)
                pkt_comment = sharkd_get_user_comment(fdata);
        else if (fdata->flags.has_phdr_comment)
-               pkt_comment = pi->phdr->opt_comment;
+               pkt_comment = pi->rec->opt_comment;
 
        if (pkt_comment)
        {
@@ -2842,7 +2792,7 @@ sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct ep
 
                        for (i = 0; i < count; i++)
                        {
-                               struct data_source *src = (struct data_source *) g_slist_nth_data((GSList *) data_src, i);
+                               const struct data_source *src = (const struct data_source *) g_slist_nth_data((GSList *) data_src, i);
 
                                tvbs[i] = get_data_source_tvb(src);
                        }
@@ -4058,7 +4008,7 @@ sharkd_session_main(void)
                /* every command is line seperated JSON */
                int ret;
 
-               ret = wsjsmn_parse(buf, NULL, 0);
+               ret = wsjson_parse(buf, NULL, 0);
                if (ret < 0)
                {
                        fprintf(stderr, "invalid JSON -> closing\n");
@@ -4076,7 +4026,7 @@ sharkd_session_main(void)
 
                memset(tokens, 0, ret * sizeof(jsmntok_t));
 
-               ret = wsjsmn_parse(buf, tokens, ret);
+               ret = wsjson_parse(buf, tokens, ret);
                if (ret < 0)
                {
                        fprintf(stderr, "invalid JSON(2) -> closing\n");