aboutsummaryrefslogtreecommitdiffstats
path: root/epan/conversation_filter.c
blob: 0f29c6569ad0245a60ccb844d2f83abdd983e359 (plain)
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
/* conversation_filter.c
 * Routines for dissector-generated conversation filters for use as
 * display and color filters
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <glib.h>
#include "packet.h"

#include "conversation_filter.h"


GList *packet_conv_filter_list = NULL;
GList *log_conv_filter_list = NULL;

static GSList *conversation_proto_names = NULL;

void conversation_filters_init(void)
{
    // add_conversation_filter_protocol prepends entries to the list. Add
    // lower layers first so that upper-layer conversations take precedence.
    add_conversation_filter_protocol("eth");
    add_conversation_filter_protocol("ipv6");
    add_conversation_filter_protocol("ip");
    add_conversation_filter_protocol("udp");
    add_conversation_filter_protocol("tcp");
}

static void do_register_conversation_filter(GList **conv_filter_list, const char *proto_name, const char *display_name,
                                        is_filter_valid_func is_filter_valid, build_filter_string_func build_filter_string) {
    conversation_filter_t *entry;

    entry = g_new(conversation_filter_t, 1);

    entry->proto_name           = proto_name;
    entry->display_name         = display_name;
    entry->is_filter_valid      = is_filter_valid;
    entry->build_filter_string  = build_filter_string;

    *conv_filter_list = g_list_append(*conv_filter_list, entry);
}

void register_conversation_filter(const char *proto_name, const char *display_name,
                                  is_filter_valid_func is_filter_valid, build_filter_string_func build_filter_string) {
    do_register_conversation_filter(&packet_conv_filter_list,
                                        proto_name,
                                        display_name,
                                        is_filter_valid,
                                        build_filter_string);
}

void register_log_conversation_filter(const char *proto_name, const char *display_name,
                                  is_filter_valid_func is_filter_valid, build_filter_string_func build_filter_string) {
    do_register_conversation_filter(&log_conv_filter_list,
                                        proto_name,
                                        display_name,
                                        is_filter_valid,
                                        build_filter_string);
}

void add_conversation_filter_protocol(const char *proto_name)
{
    for (GSList *cur_entry = conversation_proto_names; cur_entry; cur_entry = g_slist_next(cur_entry)) {
        if (strcmp(proto_name, cur_entry->data) == 0) {
            return;
        }
    }
    conversation_proto_names = g_slist_prepend(conversation_proto_names, (void *)proto_name);
}

static struct conversation_filter_s* find_conversation_filter(GList *conv_filter_list, const char *name)
{
    GList *list_entry = conv_filter_list;
    conversation_filter_t* filter;

    while (list_entry != NULL) {
        filter = (conversation_filter_t*)list_entry->data;
        if (!strcmp(filter->proto_name, name))
            return filter;

        list_entry = g_list_next(list_entry);
    }

    return NULL;
}

static void conversation_filter_free(gpointer p, gpointer user_data _U_)
{
    g_free(p);
}

void conversation_filters_cleanup(void)
{
    g_list_foreach(packet_conv_filter_list, conversation_filter_free, NULL);
    g_list_free(packet_conv_filter_list);
    g_list_foreach(log_conv_filter_list, conversation_filter_free, NULL);
    g_list_free(log_conv_filter_list);

    g_slist_free(conversation_proto_names);
}

static gchar *conversation_filter_from_pinfo(GList *conv_filter_list, struct _packet_info *pinfo)
{
    conversation_filter_t *conv_filter;
    gchar *filter;

    for (GSList *cur_entry = conversation_proto_names; cur_entry; cur_entry = g_slist_next(cur_entry)) {
        conv_filter = find_conversation_filter(conv_filter_list, (const char *) cur_entry->data);
        if (conv_filter && conv_filter->is_filter_valid(pinfo)) {
            if ((filter = conv_filter->build_filter_string(pinfo)) != NULL)
                return filter;
        }
    }

    return NULL;
}

gchar *conversation_filter_from_packet(struct _packet_info *pinfo)
{
    return conversation_filter_from_pinfo(packet_conv_filter_list, pinfo);
}

gchar *conversation_filter_from_log(struct _packet_info *pinfo)
{
    return conversation_filter_from_pinfo(log_conv_filter_list, pinfo);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */