option.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package srtp
  4. import (
  5. "github.com/pion/transport/v2/replaydetector"
  6. )
  7. // ContextOption represents option of Context using the functional options pattern.
  8. type ContextOption func(*Context) error
  9. // SRTPReplayProtection sets SRTP replay protection window size.
  10. func SRTPReplayProtection(windowSize uint) ContextOption { // nolint:revive
  11. return func(c *Context) error {
  12. c.newSRTPReplayDetector = func() replaydetector.ReplayDetector {
  13. return replaydetector.New(windowSize, maxROC<<16|maxSequenceNumber)
  14. }
  15. return nil
  16. }
  17. }
  18. // SRTCPReplayProtection sets SRTCP replay protection window size.
  19. func SRTCPReplayProtection(windowSize uint) ContextOption {
  20. return func(c *Context) error {
  21. c.newSRTCPReplayDetector = func() replaydetector.ReplayDetector {
  22. return replaydetector.New(windowSize, maxSRTCPIndex)
  23. }
  24. return nil
  25. }
  26. }
  27. // SRTPNoReplayProtection disables SRTP replay protection.
  28. func SRTPNoReplayProtection() ContextOption { // nolint:revive
  29. return func(c *Context) error {
  30. c.newSRTPReplayDetector = func() replaydetector.ReplayDetector {
  31. return &nopReplayDetector{}
  32. }
  33. return nil
  34. }
  35. }
  36. // SRTCPNoReplayProtection disables SRTCP replay protection.
  37. func SRTCPNoReplayProtection() ContextOption {
  38. return func(c *Context) error {
  39. c.newSRTCPReplayDetector = func() replaydetector.ReplayDetector {
  40. return &nopReplayDetector{}
  41. }
  42. return nil
  43. }
  44. }
  45. // SRTPReplayDetectorFactory sets custom SRTP replay detector.
  46. func SRTPReplayDetectorFactory(fn func() replaydetector.ReplayDetector) ContextOption { // nolint:revive
  47. return func(c *Context) error {
  48. c.newSRTPReplayDetector = fn
  49. return nil
  50. }
  51. }
  52. // SRTCPReplayDetectorFactory sets custom SRTCP replay detector.
  53. func SRTCPReplayDetectorFactory(fn func() replaydetector.ReplayDetector) ContextOption {
  54. return func(c *Context) error {
  55. c.newSRTCPReplayDetector = fn
  56. return nil
  57. }
  58. }
  59. type nopReplayDetector struct{}
  60. func (s *nopReplayDetector) Check(uint64) (func(), bool) {
  61. return func() {}, true
  62. }