aboutsummaryrefslogtreecommitdiffstats
path: root/epan/register.c
blob: 8e54c8c76c1ee7b5bca2c8f3d6302106d6254b04 (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
/* register.c
 * Definitions for protocol registration
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "register.h"
#include "ws_attributes.h"

#include <glib.h>
#include <wsutil/glib-compat.h>
#include "epan/dissectors/dissectors.h"

static const char *cur_cb_name = NULL;
// We could use g_atomic_pointer_set/get instead of a mutex, but that's
// currently (early 2018) invisible to TSAN.
static GMutex *cur_cb_name_mtx;
static GAsyncQueue *register_cb_done_q;

#define CB_WAIT_TIME (150 * 1000) // microseconds

static void set_cb_name(const char *proto) {
    g_mutex_lock(cur_cb_name_mtx);
    cur_cb_name = proto;
    g_mutex_unlock(cur_cb_name_mtx);
}

static void *
register_all_protocols_worker(void *arg _U_)
{
    for (gulong i = 0; i < dissector_reg_proto_count; i++) {
        set_cb_name(dissector_reg_proto[i].cb_name);
        dissector_reg_proto[i].cb_func();
    }

    g_async_queue_push(register_cb_done_q, GINT_TO_POINTER(TRUE));
    return NULL;
}

void
register_all_protocols(register_cb cb, gpointer cb_data)
{
    const char *cb_name;
    register_cb_done_q = g_async_queue_new();
    gboolean called_back = FALSE;
    GThread *rapw_thread;

#if GLIB_CHECK_VERSION(2,32,0)
    cur_cb_name_mtx = (GMutex *)g_malloc(sizeof(GMutex));
    g_mutex_init(cur_cb_name_mtx);
#else
    cur_cb_name_mtx = g_mutex_new();
#endif

    rapw_thread = g_thread_new("register_all_protocols_worker", &register_all_protocols_worker, NULL);
    while (!g_async_queue_timeout_pop(register_cb_done_q, CB_WAIT_TIME)) {
        g_mutex_lock(cur_cb_name_mtx);
        cb_name = cur_cb_name;
        g_mutex_unlock(cur_cb_name_mtx);
        if (cb && cb_name) {
            cb(RA_REGISTER, cb_name, cb_data);
            called_back = TRUE;
        }
    }
    g_thread_join(rapw_thread);
    if (cb && !called_back) {
        cb(RA_REGISTER, "finished", cb_data);
    }
}

static void *
register_all_protocol_handoffs_worker(void *arg _U_)
{
    for (gulong i = 0; i < dissector_reg_handoff_count; i++) {
        set_cb_name(dissector_reg_handoff[i].cb_name);
        dissector_reg_handoff[i].cb_func();
    }

    g_async_queue_push(register_cb_done_q, GINT_TO_POINTER(TRUE));
    return NULL;
}

void
register_all_protocol_handoffs(register_cb cb, gpointer cb_data)
{
    cur_cb_name = NULL;
    const char *cb_name;
    gboolean called_back = FALSE;
    GThread *raphw_thread;

    raphw_thread = g_thread_new("register_all_protocol_handoffs_worker", &register_all_protocol_handoffs_worker, NULL);
    while (!g_async_queue_timeout_pop(register_cb_done_q, CB_WAIT_TIME)) {
        g_mutex_lock(cur_cb_name_mtx);
        cb_name = cur_cb_name;
        g_mutex_unlock(cur_cb_name_mtx);
        if (cb && cb_name) {
            cb(RA_HANDOFF, cb_name, cb_data);
            called_back = TRUE;
        }
    }
    g_thread_join(raphw_thread);
    if (cb && !called_back) {
        cb(RA_HANDOFF, "finished", cb_data);
    }
    g_async_queue_unref(register_cb_done_q);

#if GLIB_CHECK_VERSION(2,32,0)
    g_free(cur_cb_name_mtx);
#else
    g_mutex_free(cur_cb_name_mtx);
#endif
    cur_cb_name_mtx = NULL;
}

gulong register_count(void)
{
    return dissector_reg_proto_count + dissector_reg_handoff_count;
}

/*
 * Editor modelines  -  http://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:
 */