Skip to content

Commit c17cf2f

Browse files
committed
Session's contexts can now be updated
This is needed for DTLS restart, as mentioned in pion/webrtc#1636.
1 parent d9aae44 commit c17cf2f

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Check out the **[contributing wiki](https://github.com/pion/webrtc/wiki/Contribu
4949
* [Mission Liao](https://github.com/mission-liao)
5050
* [Orlando](https://github.com/OrlandoCo)
5151
* [Tarrence van As](https://github.com/tarrencev)
52+
* [Antoine Baché](https://github.com/Antonito)
5253

5354
### License
5455
MIT License - see [LICENSE](LICENSE) for full text

session.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io"
55
"net"
66
"sync"
7+
"sync/atomic"
78

89
"github.com/pion/logging"
910
"github.com/pion/transport/packetio"
@@ -17,7 +18,8 @@ type streamSession interface {
1718

1819
type session struct {
1920
localContextMutex sync.Mutex
20-
localContext, remoteContext *Context
21+
localContext *Context
22+
remoteContext atomic.Value // *Context
2123
localOptions, remoteOptions []ContextOption
2224

2325
newStream chan readStream
@@ -106,13 +108,15 @@ func (s *session) close() error {
106108
}
107109

108110
func (s *session) start(localMasterKey, localMasterSalt, remoteMasterKey, remoteMasterSalt []byte, profile ProtectionProfile, child streamSession) error {
109-
var err error
110-
s.localContext, err = CreateContext(localMasterKey, localMasterSalt, profile, s.localOptions...)
111-
if err != nil {
112-
return err
113-
}
114-
115-
s.remoteContext, err = CreateContext(remoteMasterKey, remoteMasterSalt, profile, s.remoteOptions...)
111+
err := s.UpdateContext(&Config{
112+
Keys: SessionKeys{
113+
LocalMasterKey: localMasterKey,
114+
LocalMasterSalt: localMasterSalt,
115+
RemoteMasterKey: remoteMasterKey,
116+
RemoteMasterSalt: remoteMasterSalt,
117+
},
118+
Profile: profile,
119+
})
116120
if err != nil {
117121
return err
118122
}
@@ -148,3 +152,23 @@ func (s *session) start(localMasterKey, localMasterSalt, remoteMasterKey, remote
148152

149153
return nil
150154
}
155+
156+
// UpdateContext updates the local and remote context of the session.
157+
func (s *session) UpdateContext(config *Config) error {
158+
localContext, err := CreateContext(config.Keys.LocalMasterKey, config.Keys.LocalMasterSalt, config.Profile, s.localOptions...)
159+
if err != nil {
160+
return err
161+
}
162+
remoteContext, err := CreateContext(config.Keys.RemoteMasterKey, config.Keys.RemoteMasterSalt, config.Profile, s.remoteOptions...)
163+
if err != nil {
164+
return err
165+
}
166+
167+
s.localContextMutex.Lock()
168+
s.localContext = localContext
169+
s.localContextMutex.Unlock()
170+
171+
s.remoteContext.Store(remoteContext)
172+
173+
return nil
174+
}

session_srtcp.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ func destinationSSRC(pkts []rtcp.Packet) []uint32 {
147147
}
148148

149149
func (s *SessionSRTCP) decrypt(buf []byte) error {
150-
decrypted, err := s.remoteContext.DecryptRTCP(buf, buf, nil)
150+
// Safe since remoteContext always contains a *Context.
151+
remoteContext := s.remoteContext.Load().(*Context)
152+
decrypted, err := remoteContext.DecryptRTCP(buf, buf, nil)
151153
if err != nil {
152154
return err
153155
}

session_srtp.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ func (s *SessionSRTP) decrypt(buf []byte) error {
157157
return errFailedTypeAssertion
158158
}
159159

160-
decrypted, err := s.remoteContext.decryptRTP(buf, buf, h)
160+
// Safe since remoteContext always contains a *Context.
161+
remoteContext := s.remoteContext.Load().(*Context)
162+
decrypted, err := remoteContext.decryptRTP(buf, buf, h)
161163
if err != nil {
162164
return err
163165
}

0 commit comments

Comments
 (0)