Skip to content

Commit

Permalink
Simplify the natmap a little.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Dec 11, 2024
1 parent f80294c commit 36d4b27
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions service/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func PacketServe(clientConn net.PacketConn, handle AssocationHandleFunc, metrics
}
metrics.AddNATEntry()
deleteEntry := nm.Add(addr, conn)
go func(conn *natconn) {
go func(conn net.Conn) {
defer func() {
conn.Close()
deleteEntry()
Expand Down Expand Up @@ -239,6 +239,7 @@ func (c *natconn) Close() error {
close(c.bytesReadCh)
return c.PacketConn.Close()
}

func (c *natconn) RemoteAddr() net.Addr {
return c.raddr
}
Expand Down Expand Up @@ -437,33 +438,18 @@ func (m *natmap) Get(key string) *natconn {
return m.keyConn[key]
}

func (m *natmap) set(key string, pc *natconn) {
m.Lock()
defer m.Unlock()

m.keyConn[key] = pc
return
}

func (m *natmap) del(key string) *natconn {
m.Lock()
defer m.Unlock()

entry, ok := m.keyConn[key]
if ok {
delete(m.keyConn, key)
return entry
}
return nil
}

// Add adds a new UDP NAT entry to the natmap and returns a closure to delete
// the entry.
func (m *natmap) Add(addr net.Addr, pc *natconn) func() {
m.Lock()
defer m.Unlock()

key := addr.String()
m.set(key, pc)
m.keyConn[key] = pc
return func() {
m.del(key)
m.Lock()
defer m.Unlock()
delete(m.keyConn, key)
}
}

Expand Down

0 comments on commit 36d4b27

Please sign in to comment.