plgd-dev / go-coap

Implementation of CoAP Server & Client in Go

Home Page:https://coap.technology

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

/examples/dtls/cid/server crashes if client has wrong PSK

jonhelge opened this issue · comments

If I have a mismatch between the PSK in server and client I get this error:

Client's hint: Pion DTLS Client 
dtls: cannot accept connection: handshake error: context deadline exceeded
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x11c9509]

goroutine 130 [running]:
github.com/pion/dtls/v2.(*Conn).RemoteAddr(0xc000195858?)
        /Users/jhn/go/pkg/mod/github.com/pion/dtls/v2@v2.2.8-0.20231001141911-840187442335/conn.go:1144 +0x29
github.com/plgd-dev/go-coap/v3/net.(*Conn).RemoteAddr(...)
        /Users/jhn/playground/coap_trials/go-coap/net/conn.go:49
github.com/plgd-dev/go-coap/v3/dtls/server.(*Session).RemoteAddr(0x1062c53?)
        /Users/jhn/playground/coap_trials/go-coap/dtls/server/session.go:125 +0x1f
github.com/plgd-dev/go-coap/v3/udp/client.(*Conn).RemoteAddr(0x0?)
        /Users/jhn/playground/coap_trials/go-coap/udp/client/conn.go:510 +0x1c
github.com/plgd-dev/go-coap/v3/pkg/connections.(*Connections).Store(0xc000060418, {0x130ac68, 0xc0000a2d20})
        /Users/jhn/playground/coap_trials/go-coap/pkg/connections/connections.go:29 +0x26
github.com/plgd-dev/go-coap/v3/dtls/server.(*Server).serveConnection(0xc00007ee00, 0xc000060418, 0xc0000a2d20)
        /Users/jhn/playground/coap_trials/go-coap/dtls/server/server.go:119 +0x50
github.com/plgd-dev/go-coap/v3/dtls/server.(*Server).Serve.func3()
        /Users/jhn/playground/coap_trials/go-coap/dtls/server/server.go:168 +0x55
created by github.com/plgd-dev/go-coap/v3/dtls/server.(*Server).Serve in goroutine 1
        /Users/jhn/playground/coap_trials/go-coap/dtls/server/server.go:166 +0x465
exit status 2

For some reason the code below this fixes the problem. It could be by accident. When debugging it looked like the coap server did not understand that the handshake failed and tried to continue as if it had a valid connection.

diff --git a/examples/dtls/cid/server/main.go b/examples/dtls/cid/server/main.go
index a5564bf..bbff066 100644
--- a/examples/dtls/cid/server/main.go
+++ b/examples/dtls/cid/server/main.go
@@ -52,7 +52,13 @@ type wrappedListener struct {
 // AcceptWithContext disregards the passed context and calls the underlying
 // net.Listener Accept().
 func (w *wrappedListener) AcceptWithContext(_ context.Context) (net.Conn, error) {
-       return w.l.Accept()
+       conn, err := w.l.Accept()
+
+       if err == nil {
+               return conn, err
+       } else {
+               return nil, err
+       }
 }

Thanks for reporting this @jonhelge! The issue here is that the interface is non-nil because it has been assigned a type *dtls.Conn, but the value of the type is nil. I opened #489 to address this as we shouldn't attempt to serve a connection if an error is returned, even if the connection is non-nil.