/
f-teid.go
261 lines (220 loc) · 4.81 KB
/
f-teid.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2019-2020 go-pfcp authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package ie
import (
"encoding/binary"
"io"
"net"
)
// NewFTEID creates a new FTEID IE.
func NewFTEID(teid uint32, v4, v6 net.IP, chid []byte) *IE {
fields := NewFTEIDFields(teid, v4, v6, chid)
b, err := fields.Marshal()
if err != nil {
return nil
}
return New(FTEID, b)
}
// FTEID returns FTEID in structured format if the type of IE matches.
func (i *IE) FTEID() (*FTEIDFields, error) {
if i.Type != FTEID {
return nil, &InvalidTypeError{Type: i.Type}
}
fields, err := ParseFTEIDFields(i.Payload)
if err != nil {
return nil, err
}
return fields, nil
}
// FTEIDFields represents a fields contained in FTEID IE.
type FTEIDFields struct {
Flags uint8
TEID uint32
IPv4Address net.IP
IPv6Address net.IP
ChooseID []byte
}
// NewFTEIDFields creates a new NewFTEIDFields.
func NewFTEIDFields(teid uint32, v4, v6 net.IP, chid []byte) *FTEIDFields {
var fields *FTEIDFields
if chid != nil {
fields = &FTEIDFields{
IPv4Address: v4,
IPv6Address: v6,
ChooseID: chid,
}
fields.SetChIDFlag()
} else {
fields = &FTEIDFields{
TEID: teid,
IPv4Address: v4,
IPv6Address: v6,
}
}
if v4 != nil {
fields.SetIPv4Flag()
}
if v6 != nil {
fields.SetIPv6Flag()
}
return fields
}
// HasChID reports whether CHID flag is set.
func (f *FTEIDFields) HasChID() bool {
return has4thBit(f.Flags)
}
// SetChIDFlag sets CHID flag in FTEID.
func (f *FTEIDFields) SetChIDFlag() {
f.Flags |= 0x08
}
// HasCh reports whether CH flag is set.
func (f *FTEIDFields) HasCh() bool {
return has3rdBit(f.Flags)
}
// SetChFlag sets CH flag in FTEID.
func (f *FTEIDFields) SetChFlag() {
f.Flags |= 0x04
}
// HasIPv4 reports whether IPv4 flag is set.
func (f *FTEIDFields) HasIPv4() bool {
return has2ndBit(f.Flags)
}
// SetIPv4Flag sets CHID flag in FTEID.
func (f *FTEIDFields) SetIPv4Flag() {
f.Flags |= 0x02
}
// HasIPv6 reports whether IPv6 flag is set.
func (f *FTEIDFields) HasIPv6() bool {
return has1stBit(f.Flags)
}
// SetIPv6Flag sets CHID flag in FTEID.
func (f *FTEIDFields) SetIPv6Flag() {
f.Flags |= 0x01
}
// ParseFTEIDFields parses b into FTEIDFields.
func ParseFTEIDFields(b []byte) (*FTEIDFields, error) {
f := &FTEIDFields{}
if err := f.UnmarshalBinary(b); err != nil {
return nil, err
}
return f, nil
}
// UnmarshalBinary parses b into IE.
func (f *FTEIDFields) UnmarshalBinary(b []byte) error {
l := len(b)
if l < 2 {
return io.ErrUnexpectedEOF
}
f.Flags = b[0]
offset := 1
if f.HasChID() || f.HasCh() {
if f.HasIPv4() {
if l < offset+4 {
return io.ErrUnexpectedEOF
}
f.IPv4Address = net.IP(b[offset : offset+4])
offset += 4
}
if f.HasIPv6() {
if l < offset+16 {
return io.ErrUnexpectedEOF
}
f.IPv6Address = net.IP(b[offset : offset+16])
offset += 16
}
if l <= offset {
return nil
}
f.ChooseID = b[offset:]
return nil
}
if l < offset+4 {
return nil
}
f.TEID = binary.BigEndian.Uint32(b[offset : offset+4])
offset += 4
if f.HasIPv4() {
if l < offset+4 {
return io.ErrUnexpectedEOF
}
f.IPv4Address = net.IP(b[offset : offset+4])
offset += 4
}
if f.HasIPv6() {
if l < offset+16 {
return io.ErrUnexpectedEOF
}
f.IPv6Address = net.IP(b[offset : offset+16])
}
return nil
}
// Marshal returns the serialized bytes of FTEIDFields.
func (f *FTEIDFields) Marshal() ([]byte, error) {
b := make([]byte, f.MarshalLen())
if err := f.MarshalTo(b); err != nil {
return nil, err
}
return b, nil
}
// MarshalTo puts the byte sequence in the byte array given as b.
func (f *FTEIDFields) MarshalTo(b []byte) error {
l := len(b)
if l < 1 {
return io.ErrUnexpectedEOF
}
b[0] = f.Flags
offset := 1
if f.HasChID() || f.HasCh() {
if f.IPv4Address != nil {
if l < offset+4 {
return io.ErrUnexpectedEOF
}
copy(b[offset:offset+4], f.IPv4Address.To4())
offset += 4
}
if f.IPv6Address != nil {
if l < offset+16 {
return io.ErrUnexpectedEOF
}
copy(b[offset:offset+16], f.IPv6Address.To16())
offset += 16
}
copy(b[offset:], f.ChooseID)
return nil
}
if l < offset+4 {
return io.ErrUnexpectedEOF
}
binary.BigEndian.PutUint32(b[offset:offset+4], f.TEID)
offset += 4
if f.IPv4Address != nil {
if l < offset+4 {
return io.ErrUnexpectedEOF
}
copy(b[offset:offset+4], f.IPv4Address.To4())
offset += 4
}
if f.IPv6Address != nil {
if l < offset+16 {
return io.ErrUnexpectedEOF
}
copy(b[offset:offset+16], f.IPv6Address.To16())
}
return nil
}
// MarshalLen returns field length in integer.
func (f *FTEIDFields) MarshalLen() int {
l := 1
if f.IPv4Address != nil {
l += 4
}
if f.IPv6Address != nil {
l += 16
}
if f.HasChID() || f.HasCh() {
l += len(f.ChooseID)
return l
}
return l + 4
}