From 4d0f25400af3b0b6e00d64529acbc5074e7714ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=80=9D=E6=95=8F?= Date: Fri, 26 Aug 2022 09:57:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 8 +++++++- tap.go | 11 +++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index 8e9a7c8..3dbfec8 100644 --- a/config.go +++ b/config.go @@ -23,7 +23,7 @@ type Config struct { } `yaml:"network"` IPv4Nodes map[uint32]string - Lock sync.Mutex + lock sync.Mutex } var config = NewConfig() @@ -56,3 +56,9 @@ func NewConfig() *Config { return cnf } + +func (cnf *Config) Lock(do func()) { + cnf.lock.Lock() + defer cnf.lock.Unlock() + do() +} diff --git a/tap.go b/tap.go index 103f49a..4035019 100644 --- a/tap.go +++ b/tap.go @@ -167,12 +167,15 @@ func (nc *NetCard) runRead() { continue } - var realAddr string - config.Lock.Lock() - if _, ok = config.IPv4Nodes[binary.LittleEndian.Uint32([]byte(waterutil.IPv4Destination(rframe)))]; !ok { + var realAddr string = "" + config.Lock(func() { + if realAddr, ok = config.IPv4Nodes[binary.LittleEndian.Uint32([]byte(waterutil.IPv4Destination(rframe)))]; !ok { + return + } + }) + if realAddr == "" { continue } - config.Lock.Unlock() // log.Printf("Payload: % x\n", rframe.Payload())