forked from Lukasa/gopcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_sctp_parameters.go
175 lines (141 loc) · 5.08 KB
/
transport_sctp_parameters.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
package gopcap
import (
"encoding/binary"
"io"
"io/ioutil"
)
// Parse the supplied data as a sequence of SCTP Chunk parameters
func readSCTPChunkParameters(src io.Reader, getParameter SCTPChunkParameterFactory) ([]SCTPChunkParameter, error) {
parameters := make([]SCTPChunkParameter, 0)
var err error = nil
// Parse the parameters one at a time until there is no data left
for err != nil {
// Parse the common header so we know the type and length of the parameter.
header := SCTPChunkParameterHeader{}
err := header.ReadFrom(src)
if err != nil {
return nil, err
}
chunkReader := io.LimitReader(src, int64(header.Length)-int64(binary.Size(header)))
// Parse this chunk.
parameter := getParameter(&header)
parameter.setHeader(&header)
err = parameter.readBodyFrom(src)
if err != nil && err != io.EOF {
return nil, err
}
// Read any remaining data that the chunk didn't read.
ioutil.ReadAll(chunkReader)
parameters = append(parameters, parameter)
}
return parameters, nil
}
// Function type for building an SCTP Chunk parameter.
type SCTPChunkParameterFactory func(header *SCTPChunkParameterHeader) SCTPChunkParameter
// SCTPChunkParameter represents a single parameter in and SCTP Chunk
type SCTPChunkParameter interface {
ParameterType() SCTPChunkParameterType
ParameterLength() uint16
ReadFrom(src io.Reader) error
readBodyFrom(src io.Reader) error
setHeader(header *SCTPChunkParameterHeader)
}
// The common header for parameters in SCTP Chunks.
type SCTPChunkParameterHeader struct {
Type SCTPChunkParameterType
Length uint16
}
func (h *SCTPChunkParameterHeader) ParameterType() SCTPChunkParameterType {
return h.Type
}
func (h *SCTPChunkParameterHeader) ParameterLength() uint16 {
return h.Length
}
func (h *SCTPChunkParameterHeader) ReadFrom(src io.Reader) error {
err := readFields(src, networkByteOrder, []interface{}{
&h.Type,
&h.Length,
})
if err != nil {
return err
}
return h.readBodyFrom(src)
}
func (h *SCTPChunkParameterHeader) readBodyFrom(rc io.Reader) error {
return nil
}
func (h *SCTPChunkParameterHeader) setHeader(header *SCTPChunkParameterHeader) {
h.Type = header.Type
h.Length = header.Length
}
//-----------------------------------------------------------------------------
// SCTPChunkParameterUnknown
//-----------------------------------------------------------------------------
// SCTPChunkParameterUnknown represents a parameter for a parameter we don't understand. The data
// consists of all data after the header.
type SCTPChunkParameterUnknown struct {
SCTPChunkParameterHeader
Data []byte
}
func (p *SCTPChunkParameterUnknown) readBodyFrom(src io.Reader) error {
p.Data = make([]byte, p.Length-uint16(binary.Size(p.SCTPChunkParameterHeader)))
_, err := src.Read(p.Data)
return err
}
//-----------------------------------------------------------------------------
// SCTPChunkParameterIPv4Sender
//-----------------------------------------------------------------------------
// SCTPChunkParameterIPv4Sender represents the parameter in an SCTP INIT chunk containing the IPv4
// address of the sending endpoint.
type SCTPChunkParameterIPv4Sender struct {
SCTPChunkParameterHeader
Address [4]byte
}
func (p *SCTPChunkParameterIPv4Sender) readBodyFrom(src io.Reader) error {
return readFields(src, networkByteOrder, []interface{}{
&p.Address,
})
}
//-----------------------------------------------------------------------------
// SCTPChunkParameterIPv6Sender
//-----------------------------------------------------------------------------
// SCTPChunkParameterIPv6Sender represents the parameter in an SCTP INIT chunk containing the IPv6
// address of the sending endpoint.
type SCTPChunkParameterIPv6Sender struct {
SCTPChunkParameterHeader
Address [16]byte
}
func (p *SCTPChunkParameterIPv6Sender) readBodyFrom(src io.Reader) error {
return readFields(src, networkByteOrder, []interface{}{
&p.Address,
})
}
//-----------------------------------------------------------------------------
// SCTPChunkParameterCookieLifespanInc
//-----------------------------------------------------------------------------
// SCTPChunkParameterCookieLifespanInc represents the parameter in an SCTP INIT chunk containing the
// suggested cookie lifespan increment.
type SCTPChunkParameterCookieLifespanInc struct {
SCTPChunkParameterHeader
Increment uint32
}
func (p *SCTPChunkParameterCookieLifespanInc) readBodyFrom(src io.Reader) error {
return readFields(src, networkByteOrder, []interface{}{
&p.Increment,
})
}
//-----------------------------------------------------------------------------
// SCTPChunkParameterHeartbeatInfo
//-----------------------------------------------------------------------------
// SCTPChunkParameterHeartbeatInfo represents the parameter in a HEARTBEAT or HEARTBEAT ACK
// chunk.
type SCTPChunkParameterHeartbeatInfo struct {
SCTPChunkParameterHeader
Info []byte
}
func (p *SCTPChunkParameterHeartbeatInfo) readBodyFrom(src io.Reader) error {
p.Info = make([]byte, p.Length-uint16(binary.Size(p.SCTPChunkParameterHeader)))
_, err := src.Read(p.Info)
return err
}
// TODO: Add support for the remaining parameter types.