Skip to content

Commit

Permalink
[FIX]Add a global lock to the packet parsing feature to prevent c-mem…
Browse files Browse the repository at this point in the history
…ory errors
  • Loading branch information
randolphcyg committed Apr 24, 2024
1 parent a1e0d1a commit f71bb58
Show file tree
Hide file tree
Showing 273 changed files with 332 additions and 288 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/randolphcyg/gowireshark

go 1.22.1
go 1.22.2

require (
github.com/pkg/errors v0.9.1
Expand Down
24 changes: 24 additions & 0 deletions gowireshark.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"log/slog"
"os"
"strconv"
"sync"
"time"

"github.com/pkg/errors"
Expand All @@ -38,6 +39,8 @@ var (
// DissectResChans dissect result chan map
var DissectResChans = make(map[string]chan FrameDissectRes)

var EpanMutex = &sync.Mutex{}

// Init policies、WTAP mod、EPAN mod.
func init() {
initEnvRes := C.init_env()
Expand Down Expand Up @@ -91,6 +94,9 @@ func initCapFile(inputFilepath string) (err error) {

// DissectPrintFirstFrame Dissect and print the first frame
func DissectPrintFirstFrame(inputFilepath string) (err error) {
EpanMutex.Lock()
defer EpanMutex.Unlock()

err = initCapFile(inputFilepath)
if err != nil {
return
Expand All @@ -103,6 +109,9 @@ func DissectPrintFirstFrame(inputFilepath string) (err error) {

// DissectPrintAllFrame Dissect and print all frames
func DissectPrintAllFrame(inputFilepath string) (err error) {
EpanMutex.Lock()
defer EpanMutex.Unlock()

err = initCapFile(inputFilepath)
if err != nil {
return
Expand All @@ -119,6 +128,9 @@ func DissectPrintAllFrame(inputFilepath string) (err error) {
// @param inputFilepath: Pcap src file path
// @param count: The index of the first several frame you want to dissect
func DissectPrintFirstSeveralFrame(inputFilepath string, count int) (err error) {
EpanMutex.Lock()
defer EpanMutex.Unlock()

err = initCapFile(inputFilepath)
if err != nil {
return
Expand All @@ -140,6 +152,9 @@ func DissectPrintFirstSeveralFrame(inputFilepath string, count int) (err error)
// @param inputFilepath: Pcap src file path
// @param num: The index value of the specific frame you want to dissect
func DissectPrintSpecificFrame(inputFilepath string, num int) (err error) {
EpanMutex.Lock()
defer EpanMutex.Unlock()

err = initCapFile(inputFilepath)
if err != nil {
return
Expand Down Expand Up @@ -179,6 +194,9 @@ func UnmarshalHexData(src string) (res HexData, err error) {

// GetSpecificFrameHexData Get hex data of specific frame
func GetSpecificFrameHexData(inputFilepath string, num int) (hexData HexData, err error) {
EpanMutex.Lock()
defer EpanMutex.Unlock()

err = initCapFile(inputFilepath)
if err != nil {
return
Expand Down Expand Up @@ -688,6 +706,9 @@ func UnmarshalDissectResult(src string) (res FrameDissectRes, err error) {
// @param isDebug: Whether to print JSON result in C logic
// @return res: Contains specific frame's JSON dissect result
func GetSpecificFrameProtoTreeInJson(inputFilepath string, num int, isDescriptive, isDebug bool) (frameDissectRes FrameDissectRes, err error) {
EpanMutex.Lock()
defer EpanMutex.Unlock()

err = initCapFile(inputFilepath)
if err != nil {
return
Expand Down Expand Up @@ -737,6 +758,9 @@ func GetSpecificFrameProtoTreeInJson(inputFilepath string, num int, isDescriptiv
// @param isDebug: Whether to print JSON result in C logic
// @return res: Contains all frame's JSON dissect result
func GetAllFrameProtoTreeInJson(inputFilepath string, isDescriptive bool, isDebug bool) (res []FrameDissectRes, err error) {
EpanMutex.Lock()
defer EpanMutex.Unlock()

err = initCapFile(inputFilepath)
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,6 @@
#define HAVE_REMOTE
#endif

#include <ws_log_defs.h>
#include <include/ws_log_defs.h>

#endif /* __CONFIG_H__ */
2 changes: 1 addition & 1 deletion include/wireshark/epan/addr_resolv.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <epan/ipv6.h>
#include <epan/to_str.h>
#include <wiretap/wtap.h>
#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/app_mem_usage.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef __APP_MEM_USAGE_H__
#define __APP_MEM_USAGE_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

typedef struct {
const char *name;
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/asn1.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#ifndef __ASN1_H__
#define __ASN1_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

typedef enum {
ASN1_ENC_BER, /* X.690 - BER, CER, DER */
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/capture_dissectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef __CAPTURE_DISSECTORS_H__
#define __CAPTURE_DISSECTORS_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"
#include <wiretap/wtap.h>

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/charsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef __CHARSETS_H__
#define __CHARSETS_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/color_filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef __COLOR_FILTERS_H__
#define __COLOR_FILTERS_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#include <wsutil/color.h>

Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/column-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <glib.h>

#include "packet_info.h"
#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#ifndef __COLUMN_H__
#define __COLUMN_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"
#include <epan/column-utils.h>

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/conversation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef __CONVERSATION_H__
#define __CONVERSATION_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#include "packet.h" /* for conversation dissector type */
#include <epan/wmem_scopes.h>
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/conversation_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef __DISSECTOR_FILTERS_H__
#define __DISSECTOR_FILTERS_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/crc10-tvb.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef __CRC10_TVB_H__
#define __CRC10_TVB_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/crc16-tvb.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#ifndef __CRC16_TVB_H__
#define __CRC16_TVB_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/crc32-tvb.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef __CRC32_TVB_H__
#define __CRC32_TVB_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/crc6-tvb.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef __CRC6_TVB_H__
#define __CRC6_TVB_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/crypt/dot11decrypt_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "dot11decrypt_system.h"

#include "ws_attributes.h"
#include "include/ws_attributes.h"
#include <wsutil/wsgcrypt.h>

/****************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/crypt/dot11decrypt_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
#include <stdbool.h>

#include "dot11decrypt_user.h"
#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

/************************************************************************/
/* Macro definitions */
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/crypt/dot11decrypt_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/* */
#include <glib.h>

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

/* */
/* */
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/crypt/dot11decrypt_ws.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define _DOT11DECRYPT_WS_H

#include "dot11decrypt_system.h"
#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/decode_as.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef __DECODE_AS_H__
#define __DECODE_AS_H__

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#include "ftypes/ftypes.h"
#include "packet_info.h"
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/dfilter/dfilter-macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef _DFILTER_MACRO_H
#define _DFILTER_MACRO_H

#include <wireshark.h>
#include <include/wireshark.h>
#include "dfilter.h"

#define DFILTER_MACRO_FILENAME "dfilter_macros"
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/dfilter/dfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#ifndef DFILTER_H
#define DFILTER_H

#include <wireshark.h>
#include <include/wireshark.h>

#include "dfilter-loc.h"
#include <epan/proto.h>
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/dfilter/drange.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#ifndef __DRANGE_H__
#define __DRANGE_H__

#include <wireshark.h>
#include <include/wireshark.h>

/* Please don't directly manipulate these structs. Please use
* the methods provided. If you REALLY can't do what you need to
Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/dfilter/sttype-set.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef STTYPE_SET_H
#define STTYPE_SET_H

#include <wireshark.h>
#include <include/wireshark.h>

#include "syntax-tree.h"

Expand Down
2 changes: 1 addition & 1 deletion include/wireshark/epan/disabled_protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern "C" {
#endif /* __cplusplus */

#include <glib.h>
#include <ws_symbol_export.h>
#include <include/ws_symbol_export.h>

/*
* Tell if protocols have been enabled/disabled since
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef PACKET_ANSI_MAP_H
#define PACKET_ANSI_MAP_H

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#define ANSI_MAP_MAX_NUM_MESSAGE_TYPES 256

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#ifndef PACKET_CAMEL_H
#define PACKET_CAMEL_H

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

void proto_reg_handoff_camel(void);
void proto_register_camel(void);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef PACKET_GSM_MAP_H
#define PACKET_GSM_MAP_H

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

/* Defines for the GSM MAP taps */
#define GSM_MAP_MAX_NUM_OPR_CODES 256
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef PACKET_H245_H
#define PACKET_H245_H

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

typedef enum _h245_msg_type {
H245_TermCapSet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#ifndef PACKET_H248_H
#define PACKET_H248_H

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#include <epan/packet.h>
#include <epan/expert.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef _IEEE1609DOT2_H_
#define _IEEE1609DOT2_H_

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#include "packet-ieee1609dot2-val.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef __PACKET_KERBEROS_H
#define __PACKET_KERBEROS_H

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#ifdef __cplusplus
extern "C" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define __PACKET_LDAP_H__

# include <epan/packet.h> /* for dissector_*_t types */
#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

/*
* These are all APPLICATION types; the value is the type tag.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "ws_symbol_export.h"
#include "include/ws_symbol_export.h"

#define MAX_T38_DATA_ITEMS 4
#define MAX_T38_DESC 128
Expand Down
Loading

0 comments on commit f71bb58

Please sign in to comment.