-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.go
181 lines (156 loc) · 5.59 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// -----------------------------------------------------------------------------
// github.com/balacode/udpt /[config.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
package udpt
import (
"io"
"os"
"time"
)
// Config contains UDP and other default configuration settings.
// These settings normally don't need to be changed.
type Configuration struct {
// -------------------------------------------------------------------------
// Components:
// Cipher is the object that handles encryption and decryption.
//
// It must implement the SymmetricCipher interface which is defined
// in this package. If you don't specify Cipher, then encryption will
// be done using the default AES-256 cipher used in this package.
//
Cipher SymmetricCipher
// Compressor handles compression and uncompression.
Compressor Compression
// -------------------------------------------------------------------------
// Limits:
// PacketSizeLimit is the maximum size of a datagram in bytes,
// including the headers, metadata and data payload.
//
// Maximum Transmission Unit (MTU):
//
// Internet Protocol requires hosts to process IP datagrams
// of at least 576 bytes for IPv4 (or 1280 bytes for IPv6).
// The IPv4 header is 20 bytes (or up to 60 with options).
// UDP header is 8 bytes. 576 - 60 - 8 = 508 bytes available.
//
// The maximum Ethernet (v2) frame size is 1518 bytes, 18
// of which are overhead, giving a usable size of 1500.
// (To be on the safe side, we further reduce this by 50 bytes)
//
PacketSizeLimit int
// PacketPayloadSize is the size of a single packet's payload, in bytes.
// That is the part of the packet that contains actual useful data.
// PacketPayloadSize must always be smaller that PacketSizeLimit.
PacketPayloadSize int
// SendBufferSize is size of the write buffer used by Send(), in bytes.
SendBufferSize int
// SendRetries is the number of times for
// Send() to retry sending lost packets.
SendRetries int
// -------------------------------------------------------------------------
// Timeouts and Intervals:
// ReplyTimeout is the maximum time to wait for reply
// datagram(s) to arrive in a UDP connection.
ReplyTimeout time.Duration
// SendPacketInterval is the time to wait between sending packets.
SendPacketInterval time.Duration
// SendRetryInterval is the time for Sender.Send() to
// wait before retrying to send undelivered packets.
SendRetryInterval time.Duration
// SendWaitInterval is the amount of time Sender() should sleep
// in the loop, before checking if a confirmation has arrived.
SendWaitInterval time.Duration
// WriteTimeout is the maximum time to
// wait for writing to a UDP connection.
WriteTimeout time.Duration
// -------------------------------------------------------------------------
// Logging:
// LogWriter is the writer to which logError() and logInfo() output.
// If you leave it nil, no logging will be done.
LogWriter io.Writer
// VerboseReceiver specifies if Receiver should
// write informational log messages to LogWriter.
VerboseReceiver bool
// VerboseSender specifies if Sender should write
// informational log messages to LogWriter.
VerboseSender bool
} // Configuration
// NewDebugConfig returns configuration settings for debugging.
//
// You can specify an optional writer used for logging. If you omit it,
// logError() and logInfo() will print to standard output.
//
func NewDebugConfig(logWriter ...io.Writer) *Configuration {
cf := NewDefaultConfig()
cf.VerboseSender = true
cf.VerboseReceiver = true
if len(logWriter) > 0 {
cf.LogWriter = logWriter[0]
} else {
cf.LogWriter = os.Stdout
}
return cf
} // NewDebugConfig
// NewDefaultConfig returns default configuration settings.
func NewDefaultConfig() *Configuration {
return &Configuration{
//
// Components:
Cipher: &aesCipher{},
Compressor: &zlibCompressor{},
//
// Limits:
PacketSizeLimit: 1450,
PacketPayloadSize: 1024,
SendBufferSize: 16 * 1024 * 2014, // 16 MiB
SendRetries: 10,
//
// Timeouts and Intervals:
ReplyTimeout: 10 * time.Second,
SendPacketInterval: 1 * time.Millisecond,
SendRetryInterval: 250 * time.Millisecond,
SendWaitInterval: 25 * time.Millisecond,
WriteTimeout: 10 * time.Second,
//
// Logging: (default nil/zero values)
}
} // NewDefaultConfig
// Validate checks if all configuration parameters
// are set within acceptable limits.
//
// Returns nil if there is no problem, or the error instance.
//
func (cf *Configuration) Validate() error {
//
// Components:
if cf.Cipher == nil {
return makeError(0xE16FB9, "nil Configuration.Cipher")
}
if cf.Compressor == nil {
return makeError(0xE5B3C1, "nil Configuration.Compressor")
}
// Limits:
n := cf.PacketSizeLimit
if n < 8 || n > (65535-8) {
return makeError(0xE86C2A,
"invalid Configuration.PacketSizeLimit:", n)
}
n = cf.PacketPayloadSize
if n < 1 || n > (cf.PacketSizeLimit-200) {
return makeError(0xE54BF4,
"invalid Configuration.PacketPayloadSize:", n)
}
n = cf.SendBufferSize
if n < 0 {
return makeError(0xE27C2B,
"invalid Configuration.SendBufferSize:", n)
}
n = cf.SendRetries
if n < 0 {
return makeError(0xE47C83,
"invalid Configuration.SendRetries:", n)
}
return nil
} // Validate
// end