Skip to content

Commit

Permalink
Remove pessimistic String calls for low level logs
Browse files Browse the repository at this point in the history
  • Loading branch information
paulwe committed Apr 20, 2024
1 parent c8be609 commit 4c00345
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 30 deletions.
10 changes: 5 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (c *Client) PerformTransaction(msg *stun.Message, to net.Addr, ignoreResult

c.trMap.Insert(trKey, tr)

c.log.Tracef("Start %s transaction %s to %s", msg.Type, trKey, tr.To.String())
c.log.Tracef("Start %s transaction %s to %s", msg.Type, trKey, tr.To)
_, err := c.conn.WriteTo(tr.Raw, to)
if err != nil {
return client.TransactionResult{}, err
Expand Down Expand Up @@ -524,7 +524,7 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
return err
}

c.log.Tracef("Data indication received from %s", from.String())
c.log.Tracef("Data indication received from %s", from)

relayedConn := c.relayedUDPConn()
if relayedConn == nil {
Expand All @@ -548,7 +548,7 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
return err
}

c.log.Debugf("Connection attempt from %s", addr.String())
c.log.Debugf("Connection attempt from %s", addr)

allocation := c.getTCPAllocation()
if allocation == nil {
Expand All @@ -575,7 +575,7 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
if !ok {
c.mutexTrMap.Unlock()
// Silently discard
c.log.Debugf("No transaction for %s", msg.String())
c.log.Debugf("No transaction for %s", msg)
return nil
}

Expand All @@ -589,7 +589,7 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
From: from,
Retries: tr.Retries(),
}) {
c.log.Debugf("No listener for %s", msg.String())
c.log.Debugf("No listener for %s", msg)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestClientWithSTUN(t *testing.T) {

resp, err := c.SendBindingRequest()
assert.NoError(t, err, "should succeed")
log.Debugf("mapped-addr: %s", resp.String())
log.Debugf("mapped-addr: %s", resp)
assert.Equal(t, 0, c.trMap.Size(), "should be no transaction left")
assert.NoError(t, pc.Close())
})
Expand Down
10 changes: 5 additions & 5 deletions internal/allocation/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ func (a *Allocation) packetHandler(m *Manager) {
}

a.log.Debugf("Relay socket %s received %d bytes from %s",
a.RelaySocket.LocalAddr().String(),
a.RelaySocket.LocalAddr(),
n,
srcAddr.String())
srcAddr)

if channel := a.GetChannelByAddr(srcAddr); channel != nil {
channelData := &proto.ChannelData{
Expand Down Expand Up @@ -274,13 +274,13 @@ func (a *Allocation) packetHandler(m *Manager) {
return
}
a.log.Debugf("Relaying message from %s to client at %s",
srcAddr.String(),
a.fiveTuple.SrcAddr.String())
srcAddr,
a.fiveTuple.SrcAddr)
if _, err = a.TurnSocket.WriteTo(msg.Raw, a.fiveTuple.SrcAddr); err != nil {
a.log.Errorf("Failed to send DataIndication from allocation %v %v", srcAddr, err)
}
} else {
a.log.Infof("No Permission or Channel exists for %v on allocation %v", srcAddr, a.RelayAddr.String())
a.log.Infof("No Permission or Channel exists for %v on allocation %v", srcAddr, a.RelayAddr)
}
}
}
2 changes: 1 addition & 1 deletion internal/allocation/allocation_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (m *Manager) CreateAllocation(fiveTuple *FiveTuple, turnSocket net.PacketCo
a.RelaySocket = conn
a.RelayAddr = relayAddr

m.log.Debugf("Listening on relay address: %s", a.RelayAddr.String())
m.log.Debugf("Listening on relay address: %s", a.RelayAddr)

a.lifetimeTimer = time.AfterFunc(lifetime, func() {
m.DeleteAllocation(a.fiveTuple)
Expand Down
2 changes: 1 addition & 1 deletion internal/client/udp_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func (c *UDPConn) bind(b *binding) error {
return fmt.Errorf("unexpected response type %s", res.Type) //nolint:goerr113
}

c.log.Debugf("Channel binding successful: %s %d", b.addr.String(), b.number)
c.log.Debugf("Channel binding successful: %s %d", b.addr, b.number)

// Success.
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Request struct {

// HandleRequest processes the give Request
func HandleRequest(r Request) error {
r.Log.Debugf("Received %d bytes of udp from %s on %s", len(r.Buff), r.SrcAddr.String(), r.Conn.LocalAddr().String())
r.Log.Debugf("Received %d bytes of udp from %s on %s", len(r.Buff), r.SrcAddr, r.Conn.LocalAddr())

if proto.IsChannelData(r.Buff) {
return handleDataPacket(r)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/stun.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func handleBindingRequest(r Request, m *stun.Message) error {
r.Log.Debugf("Received BindingRequest from %s", r.SrcAddr.String())
r.Log.Debugf("Received BindingRequest from %s", r.SrcAddr)

ip, port, err := ipnet.AddrIPPort(r.SrcAddr)
if err != nil {
Expand Down
24 changes: 10 additions & 14 deletions internal/server/turn.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// See: https://tools.ietf.org/html/rfc5766#section-6.2
func handleAllocateRequest(r Request, m *stun.Message) error {
r.Log.Debugf("Received AllocateRequest from %s", r.SrcAddr.String())
r.Log.Debugf("Received AllocateRequest from %s", r.SrcAddr)

// 1. The server MUST require that the request be authenticated. This
// authentication MUST be done using the long-term credential
Expand Down Expand Up @@ -177,7 +177,7 @@ func handleAllocateRequest(r Request, m *stun.Message) error {
}

func handleRefreshRequest(r Request, m *stun.Message) error {
r.Log.Debugf("Received RefreshRequest from %s", r.SrcAddr.String())
r.Log.Debugf("Received RefreshRequest from %s", r.SrcAddr)

messageIntegrity, hasAuth, err := authenticateRequest(r, m, stun.MethodRefresh)
if !hasAuth {
Expand Down Expand Up @@ -211,7 +211,7 @@ func handleRefreshRequest(r Request, m *stun.Message) error {
}

func handleCreatePermissionRequest(r Request, m *stun.Message) error {
r.Log.Debugf("Received CreatePermission from %s", r.SrcAddr.String())
r.Log.Debugf("Received CreatePermission from %s", r.SrcAddr)

a := r.AllocationManager.GetAllocation(&allocation.FiveTuple{
SrcAddr: r.SrcAddr,
Expand All @@ -236,13 +236,12 @@ func handleCreatePermissionRequest(r Request, m *stun.Message) error {
}

if err := r.AllocationManager.GrantPermission(r.SrcAddr, peerAddress.IP); err != nil {
r.Log.Infof("permission denied for client %s to peer %s", r.SrcAddr.String(),
peerAddress.IP.String())
r.Log.Infof("permission denied for client %s to peer %s", r.SrcAddr, peerAddress.IP)
return err
}

r.Log.Debugf("Adding permission for %s", fmt.Sprintf("%s:%d",
peerAddress.IP.String(), peerAddress.Port))
peerAddress.IP, peerAddress.Port))

a.AddPermission(allocation.NewPermission(
&net.UDPAddr{
Expand All @@ -266,7 +265,7 @@ func handleCreatePermissionRequest(r Request, m *stun.Message) error {
}

func handleSendIndication(r Request, m *stun.Message) error {
r.Log.Debugf("Received SendIndication from %s", r.SrcAddr.String())
r.Log.Debugf("Received SendIndication from %s", r.SrcAddr)
a := r.AllocationManager.GetAllocation(&allocation.FiveTuple{
SrcAddr: r.SrcAddr,
DstAddr: r.Conn.LocalAddr(),
Expand Down Expand Up @@ -299,7 +298,7 @@ func handleSendIndication(r Request, m *stun.Message) error {
}

func handleChannelBindRequest(r Request, m *stun.Message) error {
r.Log.Debugf("Received ChannelBindRequest from %s", r.SrcAddr.String())
r.Log.Debugf("Received ChannelBindRequest from %s", r.SrcAddr)

a := r.AllocationManager.GetAllocation(&allocation.FiveTuple{
SrcAddr: r.SrcAddr,
Expand Down Expand Up @@ -328,18 +327,15 @@ func handleChannelBindRequest(r Request, m *stun.Message) error {
}

if err = r.AllocationManager.GrantPermission(r.SrcAddr, peerAddr.IP); err != nil {
r.Log.Infof("permission denied for client %s to peer %s", r.SrcAddr.String(),
peerAddr.IP.String())
r.Log.Infof("permission denied for client %s to peer %s", r.SrcAddr, peerAddr.IP)

unauthorizedRequestMsg := buildMsg(m.TransactionID,
stun.NewType(stun.MethodChannelBind, stun.ClassErrorResponse),
&stun.ErrorCodeAttribute{Code: stun.CodeUnauthorized})
return buildAndSendErr(r.Conn, r.SrcAddr, err, unauthorizedRequestMsg...)
}

r.Log.Debugf("Binding channel %d to %s",
channel,
fmt.Sprintf("%s:%d", peerAddr.IP.String(), peerAddr.Port))
r.Log.Debugf("Binding channel %d to %s", channel, peerAddr)
err = a.AddChannelBind(allocation.NewChannelBind(
channel,
&net.UDPAddr{IP: peerAddr.IP, Port: peerAddr.Port},
Expand All @@ -353,7 +349,7 @@ func handleChannelBindRequest(r Request, m *stun.Message) error {
}

func handleChannelData(r Request, c *proto.ChannelData) error {
r.Log.Debugf("Received ChannelData from %s", r.SrcAddr.String())
r.Log.Debugf("Received ChannelData from %s", r.SrcAddr)

a := r.AllocationManager.GetAllocation(&allocation.FiveTuple{
SrcAddr: r.SrcAddr,
Expand Down
2 changes: 1 addition & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func TestServerVNet(t *testing.T) {
log.Debug("sending a binding request.")
reflAddr, err := client.SendBindingRequest()
assert.NoError(t, err)
log.Debugf("mapped-address: %v", reflAddr.String())
log.Debugf("mapped-address: %s", reflAddr)
udpAddr, ok := reflAddr.(*net.UDPAddr)
assert.True(t, ok)

Expand Down

0 comments on commit 4c00345

Please sign in to comment.