Skip to content

Commit

Permalink
Begin cleanup of make-dissector-reg.py
Browse files Browse the repository at this point in the history
Move registration code to a new register.c file so it is readable.
Dissector load points are stored in a generated function pointer
array instead.

Simplify python script somewhat by not interleaving the plugin and
dissector logic.

Change-Id: I5ec21270f4e1550a5c911efa7f0dc4fc7fcb13a5
Reviewed-on: https://code.wireshark.org/review/24474
Reviewed-by: João Valverde <j@v6e.pt>
Petri-Dish: João Valverde <j@v6e.pt>
Tested-by: Petri Dish Buildbot
Reviewed-by: Michael Mann <mmann78@netscape.net>
  • Loading branch information
randstr authored and mmann78 committed Nov 18, 2017
1 parent d8e2bee commit b695b3e
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 175 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Expand Up @@ -1552,6 +1552,7 @@ endif()
set(SHARK_COMMON_SRC
cfile.c
frame_tvbuff.c
register.c
sync_pipe_write.c
version_info.c
)
Expand Down Expand Up @@ -2523,6 +2524,7 @@ if(BUILD_dftest)
)
set(dftest_FILES
dftest.c
register.c
)
add_executable(dftest ${dftest_FILES})
add_dependencies(dftest version)
Expand Down Expand Up @@ -3003,7 +3005,6 @@ set(SHARK_PUBLIC_HEADERS
file.h
globals.h
log.h
register.h
ws_attributes.h
ws_compiler_tests.h
ws_diag_control.h
Expand Down
6 changes: 4 additions & 2 deletions Makefile.am
Expand Up @@ -395,6 +395,7 @@ EXTCAP_COMMON_SRC = \
SHARK_COMMON_SRC = \
cfile.c \
frame_tvbuff.c \
register.c \
sync_pipe_write.c

# wireshark specifics
Expand Down Expand Up @@ -677,7 +678,8 @@ randpkt_LDADD = \
@C_ARES_LIBS@

dftest_SOURCES = \
dftest.c
dftest.c \
register.c

dftest_CPPFLAGS = $(AM_CPPFLAGS) $(GLIB_CFLAGS)

Expand Down Expand Up @@ -723,7 +725,6 @@ pkginclude_HEADERS = \
file.h \
globals.h \
log.h \
register.h \
ws_attributes.h \
ws_compiler_tests.h \
ws_diag_control.h \
Expand All @@ -740,6 +741,7 @@ noinst_HEADERS = \
extcap_spawn.h \
fileset.h \
frame_tvbuff.h \
register.h \
ringbuffer.h \
summary.h \
sync_pipe.h \
Expand Down
7 changes: 4 additions & 3 deletions debian/libwireshark0.symbols
Expand Up @@ -451,6 +451,10 @@ libwireshark.so.0 libwireshark0 #MINVER#
dissector_handle_get_protocol_index@Base 1.9.1
dissector_handle_get_short_name@Base 1.9.1
dissector_hostlist_init@Base 1.99.0
dissector_reg_handoff@Base 2.5.0
dissector_reg_handoff_count@Base 2.5.0
dissector_reg_proto@Base 2.5.0
dissector_reg_proto_count@Base 2.5.0
dissector_reset_payload@Base 2.5.0
dissector_reset_string@Base 1.9.1
dissector_reset_uint@Base 1.9.1
Expand Down Expand Up @@ -1280,8 +1284,6 @@ libwireshark.so.0 libwireshark0 #MINVER#
reassembly_table_destroy@Base 1.9.1
reassembly_table_init@Base 1.9.1
register_all_plugin_tap_listeners@Base 1.9.1
register_all_protocol_handoffs@Base 1.9.1
register_all_protocols@Base 1.9.1
register_ber_oid_dissector@Base 2.1.0
register_ber_oid_dissector_handle@Base 1.9.1
register_ber_oid_syntax@Base 1.9.1
Expand All @@ -1291,7 +1293,6 @@ libwireshark.so.0 libwireshark0 #MINVER#
register_cleanup_routine@Base 1.99.8
register_conversation_filter@Base 2.0.0
register_conversation_table@Base 2.5.0
register_count@Base 1.9.1
register_custom_dissector_table@Base 1.99.8
register_custom_dissector_table@Base 1.99.8
register_decode_as@Base 1.12.0~rc1
Expand Down
5 changes: 3 additions & 2 deletions epan/dissectors/CMakeLists.txt
Expand Up @@ -210,6 +210,7 @@ set(NCP2222_DISSECTOR_SRC
source_group(dissectors-ncp2222 FILES ${NCP2222_DISSECTOR_SRC})

set(DISSECTOR_PUBLIC_HEADERS
dissectors.h
packet-6lowpan.h
packet-a21.h
packet-acp133.h
Expand Down Expand Up @@ -1814,7 +1815,7 @@ set(DISSECTOR_SUPPORT_SRC
packet-dcerpc-nt.c
usb.c
snort-config.c
register.c
dissectors.c
)
source_group(dissector-support FILES ${DISSECTOR_SUPPORT_SRC})

Expand All @@ -1834,7 +1835,7 @@ set(ALL_DISSECTOR_SRC
${CORBA_IDL_DISSECTOR_SRC}
)

register_dissector_files(register.c
register_dissector_files(dissectors.c
dissectors
${ALL_DISSECTOR_SRC}
)
Expand Down
30 changes: 12 additions & 18 deletions epan/dissectors/Makefile.am
Expand Up @@ -28,14 +28,14 @@ include Custom.common

# "BUILT_SOURCES" are built before any "make all" or "make check" targets.
BUILT_SOURCES = \
register.c
dissectors.c

# Header files generated from source files.
NODIST_GENERATED_HEADER_FILES =

# C source files generated from source files.
NODIST_GENERATED_C_FILES = \
register.c
dissectors.c

# All the generated files.
NODIST_GENERATED_FILES = \
Expand Down Expand Up @@ -1474,6 +1474,7 @@ FILE_DISSECTOR_INCLUDES = \
#
DISSECTOR_INCLUDES = \
$(PIDL_DISSECTOR_INCLUDES) \
dissectors.h \
packet-6lowpan.h \
packet-a21.h \
packet-acp133.h \
Expand Down Expand Up @@ -1853,7 +1854,7 @@ DISSECTOR_INCLUDES = \

# Dissector helpers. They're included in the source files in this
# directory, but they're not dissectors themselves, i.e. they're not
# used to generate "register.c").
# used to generate "dissectors.c").
DISSECTOR_SUPPORT_SRC = \
errno.c \
packet-dcerpc-nt.c \
Expand Down Expand Up @@ -1924,10 +1925,9 @@ x11-dissector: $(top_srcdir)/tools/process-x11-fields.pl $(srcdir)/x11-fields $(
$(PERL) $(top_srcdir)/tools/process-x11-xcb.pl $(srcdir)

#
# Build register.c, which contains a function register_all_protocols()
# that calls the register routines for all protocols and a function
# register_all_protocol_handoffs() that calls the handoff registration
# routines for all protocols.
# Build dissectors.c, which contains a function pointer array
# with the register routines for all protocols and a function pointer array
# with the handoff registration routines for all protocols.
#
# We do this by scanning through sources. If that turns out to be too slow,
# maybe we could just require every .o file to have an register routine
Expand All @@ -1940,20 +1940,15 @@ x11-dissector: $(top_srcdir)/tools/process-x11-fields.pl $(srcdir)/x11-fields $(
#
# ALL_DISSECTORS_SRC is assumed to have all the files that need to be scanned.
#
# For some unknown reason, having a big "for" loop in the Makefile
# to scan all the files doesn't work with some "make"s; they seem to
# pass only the first few names in the list to the shell, for some
# reason.
#
# Therefore, we use a script to generate the register.c file.
# We use a script to generate the dissectors.c file.
# The first argument is the directory in which the source files live.
# The second argument is "dissectors", to indicate that we should build
# a register.c file for libwireshark.
# a dissectors.c file for libwireshark.
# All subsequent arguments are the files to scan.
#
register.c: $(plugin_src) $(ALL_DISSECTORS_SRC) Custom.common \
dissectors.c: $(plugin_src) $(ALL_DISSECTORS_SRC) Custom.common \
$(top_srcdir)/tools/make-dissector-reg.py
@echo Making register.c ; \
@echo Making dissectors.c ; \
$(PYTHON) $(top_srcdir)/tools/make-dissector-reg.py $(srcdir) \
dissectors $(ALL_DISSECTORS_SRC) ;

Expand All @@ -1962,8 +1957,7 @@ CLEANFILES = \
libdissectors.la \
libdirtydissectors.a \
libdirtydissectors.la \
register.c-tmp \
register-cache.pkl \
dissectors-cache.pkl \
*~

DISTCLEANFILES = \
Expand Down
51 changes: 51 additions & 0 deletions epan/dissectors/dissectors.h
@@ -0,0 +1,51 @@
/* dissectors.h
* Definitions for protocol registration
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0+
*/

#ifndef __DISSECTOR_REGISTER_H__
#define __DISSECTOR_REGISTER_H__

#include "ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <glib.h>

typedef struct _dissector_reg {
const char *cb_name;
void (*cb_func)(void);
} dissector_reg_t;

WS_DLL_PUBLIC dissector_reg_t dissector_reg_proto[];
WS_DLL_PUBLIC dissector_reg_t dissector_reg_handoff[];

WS_DLL_PUBLIC gulong dissector_reg_proto_count(void);

WS_DLL_PUBLIC gulong dissector_reg_handoff_count(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __DISSECTOR_REGISTER_H__ */

/*
* 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=2 tabstop=8 expandtab:
* :indentSize=2:tabSize=8:noTabs=true:
*/
123 changes: 123 additions & 0 deletions register.c
@@ -0,0 +1,123 @@
/* 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+
*/

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

#include <glib.h>
#include "epan/dissectors/dissectors.h"

static const char *cur_cb_name = NULL;
//static GMutex register_cb_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(register_cb_mtx);
cur_cb_name = proto;
// g_mutex_unlock(register_cb_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;

#if GLIB_CHECK_VERSION(2,31,0)
g_thread_new("register_all_protocols_worker", &register_all_protocols_worker, NULL);
#else
g_thread_create(&register_all_protocols_worker, TRUE, FALSE, NULL);
#endif
while (!g_async_queue_timeout_pop(register_cb_done_q, CB_WAIT_TIME)) {
// g_mutex_lock(register_cb_mtx);
cb_name = cur_cb_name;
// g_mutex_unlock(register_cb_mtx);
if (cb && cb_name) {
cb(RA_REGISTER, cb_name, cb_data);
called_back = TRUE;
}
}
if (cb && !called_back) {
cb(RA_REGISTER, "Registration 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;

#if GLIB_CHECK_VERSION(2,31,0)
g_thread_new("register_all_protocol_handoffs_worker", &register_all_protocol_handoffs_worker, NULL);
#else
g_thread_create(&register_all_protocol_handoffs_worker, TRUE, FALSE, NULL);
#endif
while (!g_async_queue_timeout_pop(register_cb_done_q, CB_WAIT_TIME)) {
// g_mutex_lock(register_cb_mtx);
cb_name = cur_cb_name;
// g_mutex_unlock(register_cb_mtx);
if (cb && cb_name) {
cb(RA_HANDOFF, cb_name, cb_data);
called_back = TRUE;
}
}
if (cb && !called_back) {
cb(RA_HANDOFF, "Registration finished", cb_data);
}

g_async_queue_unref(register_cb_done_q);
}

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: 2
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=2 tabstop=8 expandtab:
* :indentSize=2:tabSize=8:noTabs=true:
*/
8 changes: 5 additions & 3 deletions register.h
Expand Up @@ -49,7 +49,7 @@ typedef void (*register_cb)(register_action_e action, const char *message, gpoin
* Messages have the format "proto_register_XXX".
* @param client_data Data pointer for the callback.
*/
WS_DLL_PUBLIC void register_all_protocols(register_cb cb, gpointer client_data);
void register_all_protocols(register_cb cb, gpointer client_data);

/** Call each dissector's protocol handoff routine.
*
Expand All @@ -61,9 +61,11 @@ WS_DLL_PUBLIC void register_all_protocols(register_cb cb, gpointer client_data);
* Messages have the format "proto_reg_handoff_XXX".
* @param client_data Data pointer for the callback.
*/
WS_DLL_PUBLIC void register_all_protocol_handoffs(register_cb cb, gpointer client_data);
void register_all_protocol_handoffs(register_cb cb, gpointer client_data);

extern void register_all_tap_listeners(void);
WS_DLL_PUBLIC gulong register_count(void);

gulong register_count(void);

#ifdef __cplusplus
}
Expand Down

0 comments on commit b695b3e

Please sign in to comment.