Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session's contexts can now be updated #148

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
adamroach <[email protected]>
Adrian Cable <[email protected]>
Agniva De Sarker <[email protected]>
Antoine Baché <[email protected]>
Antoine Baché <[email protected]>
Atsushi Watanabe <[email protected]>
backkem <[email protected]>
chenkaiC4 <[email protected]>
Expand Down
40 changes: 32 additions & 8 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"io"
"net"
"sync"
"sync/atomic"

"github.com/pion/logging"
"github.com/pion/transport/packetio"
Expand All @@ -18,7 +19,8 @@

type session struct {
localContextMutex sync.Mutex
localContext, remoteContext *Context
localContext *Context
remoteContext atomic.Value // *Context
localOptions, remoteOptions []ContextOption

newStream chan readStream
Expand Down Expand Up @@ -107,13 +109,15 @@
}

func (s *session) start(localMasterKey, localMasterSalt, remoteMasterKey, remoteMasterSalt []byte, profile ProtectionProfile, child streamSession) error {
var err error
s.localContext, err = CreateContext(localMasterKey, localMasterSalt, profile, s.localOptions...)
if err != nil {
return err
}

s.remoteContext, err = CreateContext(remoteMasterKey, remoteMasterSalt, profile, s.remoteOptions...)
err := s.UpdateContext(&Config{
Keys: SessionKeys{
LocalMasterKey: localMasterKey,
LocalMasterSalt: localMasterSalt,
RemoteMasterKey: remoteMasterKey,
RemoteMasterSalt: remoteMasterSalt,
},
Profile: profile,
})
if err != nil {
return err
}
Expand Down Expand Up @@ -149,3 +153,23 @@

return nil
}

// UpdateContext updates the local and remote context of the session.
func (s *session) UpdateContext(config *Config) error {
localContext, err := CreateContext(config.Keys.LocalMasterKey, config.Keys.LocalMasterSalt, config.Profile, s.localOptions...)
if err != nil {
return err
}

Check warning on line 162 in session.go

View check run for this annotation

Codecov / codecov/patch

session.go#L161-L162

Added lines #L161 - L162 were not covered by tests
remoteContext, err := CreateContext(config.Keys.RemoteMasterKey, config.Keys.RemoteMasterSalt, config.Profile, s.remoteOptions...)
if err != nil {
return err
}

Check warning on line 166 in session.go

View check run for this annotation

Codecov / codecov/patch

session.go#L165-L166

Added lines #L165 - L166 were not covered by tests

s.localContextMutex.Lock()
s.localContext = localContext
s.localContextMutex.Unlock()

s.remoteContext.Store(remoteContext)

return nil
}
4 changes: 3 additions & 1 deletion session_srtcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ func destinationSSRC(pkts []rtcp.Packet) []uint32 {
}

func (s *SessionSRTCP) decrypt(buf []byte) error {
decrypted, err := s.remoteContext.DecryptRTCP(buf, buf, nil)
// Safe since remoteContext always contains a *Context.
remoteContext := s.remoteContext.Load().(*Context)
decrypted, err := remoteContext.DecryptRTCP(buf, buf, nil)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion session_srtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ func (s *SessionSRTP) decrypt(buf []byte) error {
return errFailedTypeAssertion
}

decrypted, err := s.remoteContext.decryptRTP(buf, buf, h, headerLen)
// Safe since remoteContext always contains a *Context.
remoteContext := s.remoteContext.Load().(*Context)
decrypted, err := remoteContext.decryptRTP(buf, buf, h, headerLen)
if err != nil {
return err
}
Expand Down