Skip to content

Commit 4d3c4b1

Browse files
lisay-yanSean-Der
authored andcommitted
Add SetDTLSConnectContextMaker to SettingEngine
Allows a user to set the context used during the DTLS Handshake. This can be used to extend or reduce the timeout on the DTLS Handshake. Resolves pion#2477
1 parent c1bec49 commit 4d3c4b1

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

dtlstransport.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ func (t *DTLSTransport) Start(remoteParameters DTLSParameters) error {
334334
dtlsConfig.FlightInterval = t.api.settingEngine.dtls.retransmissionInterval
335335
dtlsConfig.InsecureSkipVerifyHello = t.api.settingEngine.dtls.insecureSkipHelloVerify
336336
dtlsConfig.EllipticCurves = t.api.settingEngine.dtls.ellipticCurves
337+
dtlsConfig.ConnectContextMaker = t.api.settingEngine.dtls.connectContextMaker
337338

338339
// Connect as DTLS Client/Server, function is blocking and we
339340
// must not hold the DTLSTransport lock

settingengine.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package webrtc
88

99
import (
10+
"context"
1011
"io"
1112
"net"
1213
"time"
@@ -63,6 +64,7 @@ type SettingEngine struct {
6364
insecureSkipHelloVerify bool
6465
retransmissionInterval time.Duration
6566
ellipticCurves []dtlsElliptic.Curve
67+
connectContextMaker func() (context.Context, func())
6668
}
6769
sctp struct {
6870
maxReceiveBufferSize uint32
@@ -368,6 +370,17 @@ func (e *SettingEngine) SetDTLSEllipticCurves(ellipticCurves ...dtlsElliptic.Cur
368370
e.dtls.ellipticCurves = ellipticCurves
369371
}
370372

373+
// SetDTLSConnectContextMaker sets the context used during the DTLS Handshake.
374+
// It can be used to extend or reduce the timeout on the DTLS Handshake.
375+
// If nil, the default dtls.ConnectContextMaker is used. It can be implemented as following.
376+
//
377+
// func ConnectContextMaker() (context.Context, func()) {
378+
// return context.WithTimeout(context.Background(), 30*time.Second)
379+
// }
380+
func (e *SettingEngine) SetDTLSConnectContextMaker(connectContextMaker func() (context.Context, func())) {
381+
e.dtls.connectContextMaker = connectContextMaker
382+
}
383+
371384
// SetSCTPMaxReceiveBufferSize sets the maximum receive buffer size.
372385
// Leave this 0 for the default maxReceiveBufferSize.
373386
func (e *SettingEngine) SetSCTPMaxReceiveBufferSize(maxReceiveBufferSize uint32) {

settingengine_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package webrtc
88

99
import (
10+
"context"
1011
"net"
1112
"testing"
1213
"time"
@@ -252,6 +253,14 @@ func TestSetDTLSEllipticCurves(t *testing.T) {
252253
}
253254
}
254255

256+
func TestSetDTLSHandShakeTimeout(*testing.T) {
257+
s := SettingEngine{}
258+
259+
s.SetDTLSConnectContextMaker(func() (context.Context, func()) {
260+
return context.WithTimeout(context.Background(), 60*time.Second)
261+
})
262+
}
263+
255264
func TestSetSCTPMaxReceiverBufferSize(t *testing.T) {
256265
s := SettingEngine{}
257266
assert.Equal(t, uint32(0), s.sctp.maxReceiveBufferSize)

0 commit comments

Comments
 (0)