Debian Codegraph @

association_options.go

raw
1 // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
2 // SPDX-License-Identifier: MIT
3
4 package sctp
5
6 import (
7 "net"
8
9 "github.com/pion/logging"
10 )
11
12 // ServerOption configures a Server.
13 type ServerOption interface {
14 applyServer(*Config) error
15 }
16
17 // ClientOption configures a Client.
18 type ClientOption interface {
19 applyClient(*Config) error
20 }
21
22 // AssociationOption applies to both client and server.
23 type AssociationOption interface {
24 ServerOption
25 ClientOption
26 }
27
28 // sharedOption wraps an apply function that works for both client and server.
29 type sharedOption func(*Config) error
30
31 func (o sharedOption) applyServer(c *Config) error { return o(c) }
32 func (o sharedOption) applyClient(c *Config) error { return o(c) }
33
34 // WithLoggerFactory sets the logger factory for the association.
35 func WithLoggerFactory(loggerFactory logging.LoggerFactory) AssociationOption {
36 return sharedOption(func(c *Config) error {
37 if loggerFactory == nil {
38 return errNilLoggerFactory // add if you don't already have it
39 }
40 c.LoggerFactory = loggerFactory
41
42 return nil
43 })
44 }
45
46 // WithName sets the name of the association.
47 func WithName(name string) AssociationOption {
48 return sharedOption(func(c *Config) error {
49 c.Name = name
50
51 return nil
52 })
53 }
54
55 // WithNetConn sets the net.Conn used by the association.
56 func WithNetConn(conn net.Conn) AssociationOption {
57 return sharedOption(func(c *Config) error {
58 if conn == nil {
59 return errNilNetConn
60 }
61 c.NetConn = conn
62
63 return nil
64 })
65 }
66
67 // WithBlockWrite sets whether the association should use blocking writes.
68 // By default this is false.
69 func WithBlockWrite(b bool) AssociationOption {
70 return sharedOption(func(c *Config) error {
71 c.BlockWrite = b
72
73 return nil
74 })
75 }
76
77 // WithEnableZeroChecksum sets whether the association should accept zero as a valid checksum.
78 // By default this is false.
79 func WithEnableZeroChecksum(b bool) AssociationOption {
80 return sharedOption(func(c *Config) error {
81 c.EnableZeroChecksum = b
82
83 return nil
84 })
85 }
86
87 // WithEnableInterleaving sets whether the association should negotiate message interleaving.
88 // By default this is true.
89 func WithEnableInterleaving(b bool) AssociationOption {
90 return sharedOption(func(c *Config) error {
91 c.enableInterleaving = b
92 c.enableInterleavingSet = true
93
94 return nil
95 })
96 }
97
98 // WithMTU sets the MTU size for the association.
99 // By default this is 1191.
100 func WithMTU(size uint32) AssociationOption {
101 return sharedOption(func(c *Config) error {
102 if err := validateMTU(size); err != nil {
103 return err
104 }
105 c.MTU = size
106
107 return nil
108 })
109 }
110
111 // Congestion control options //
112
113 // WithMaxReceiveBufferSize sets the maximum receive buffer size for the association.
114 // By default this is 1024 * 1024 = 1048576.
115 func WithMaxReceiveBufferSize(size uint32) AssociationOption {
116 return sharedOption(func(c *Config) error {
117 if size == 0 {
118 return errZeroMaxReceiveBufferOption
119 }
120 c.MaxReceiveBufferSize = size
121
122 return nil
123 })
124 }
125
126 // WithMaxMessageSize sets the maximum message size for the association.
127 // By default this is 65536.
128 func WithMaxMessageSize(size uint32) AssociationOption {
129 return sharedOption(func(c *Config) error {
130 if size == 0 {
131 return errZeroMaxMessageSize
132 }
133 c.MaxMessageSize = size
134
135 return nil
136 })
137 }
138
139 // WithMaxReassemblyQueueEntries caps the number of DATA entries and I-DATA MIDs queued per stream.
140 // A value of 0 disables the cap. By default this cap is disabled.
141 func WithMaxReassemblyQueueEntries(maxEntries uint32) AssociationOption {
142 return sharedOption(func(c *Config) error {
143 c.maxReassemblyQueueEntries = maxEntries
144
145 return nil
146 })
147 }
148
149 // WithRTOMax sets the max retransmission timeout in ms for the association.
150 func WithRTOMax(rtoMax float64) AssociationOption {
151 return sharedOption(func(c *Config) error {
152 if rtoMax <= 0 {
153 return errInvalidRTOMax
154 }
155 c.RTOMax = rtoMax
156
157 return nil
158 })
159 }
160
161 // WithMinCwnd sets the minimum congestion window for the association.
162 func WithMinCwnd(minCwnd uint32) AssociationOption {
163 return sharedOption(func(c *Config) error {
164 c.MinCwnd = minCwnd
165
166 return nil
167 })
168 }
169
170 // WithFastRtxWnd sets the fast retransmission window for the association.
171 func WithFastRtxWnd(fastRtxWnd uint32) AssociationOption {
172 return sharedOption(func(c *Config) error {
173 c.FastRtxWnd = fastRtxWnd
174
175 return nil
176 })
177 }
178
179 // WithCwndCAStep sets the congestion window congestion avoidance step for the association.
180 func WithCwndCAStep(cwndCAStep uint32) AssociationOption {
181 return sharedOption(func(c *Config) error {
182 c.CwndCAStep = cwndCAStep
183
184 return nil
185 })
186 }
187
188 // WithSNAP enables SNAP, https://datatracker.ietf.org/doc/draft-hancke-tsvwg-snap/.
189 func WithSNAP(localSctpInit []byte, remoteSctpInit []byte) AssociationOption {
190 return sharedOption(func(c *Config) error {
191 if len(localSctpInit) == 0 || len(remoteSctpInit) == 0 {
192 return errInvalidSnapToken
193 }
194 c.snapConfig = &snapConfig{
195 localInit: localSctpInit,
196 remoteInit: remoteSctpInit,
197 }
198
199 return nil
200 })
201 }