/
user-id.go
251 lines (210 loc) · 4.84 KB
/
user-id.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
// Copyright 2019-2021 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 (
"io"
"github.com/wmnsk/go-pfcp/internal/utils"
)
// NewUserID creates a new UserID IE.
func NewUserID(flags uint8, imsi, imei, msisdn, nai string) *IE {
fields := NewUserIDFields(flags, imsi, imei, msisdn, nai)
b, err := fields.Marshal()
if err != nil {
return nil
}
return New(UserID, b)
}
// UserID returns UserID in structured format if the type of IE matches.
func (i *IE) UserID() (*UserIDFields, error) {
if i.Type != UserID {
return nil, &InvalidTypeError{Type: i.Type}
}
s, err := ParseUserIDFields(i.Payload)
if err != nil {
return nil, err
}
return s, nil
}
// UserIDFields represents a fields contained in UserID IE.
type UserIDFields struct {
Flags uint8
IMSILength uint8
IMSI string
IMEILength uint8
IMEI string
MSISDNLength uint8
MSISDN string
NAILength uint8
NAI string
}
// NewUserIDFields creates a new NewUserIDFields.
func NewUserIDFields(flags uint8, imsi, imei, msisdn, nai string) *UserIDFields {
f := &UserIDFields{
Flags: flags,
}
if has1stBit(flags) {
v, err := utils.StrToSwappedBytes(imsi, "f")
if err != nil {
return nil
}
f.IMSILength = uint8(len(v))
f.IMSI = imsi
}
if has2ndBit(flags) {
v, err := utils.StrToSwappedBytes(imei, "f")
if err != nil {
return nil
}
f.IMEILength = uint8(len(v))
f.IMEI = imei
}
if has3rdBit(flags) {
v, err := utils.StrToSwappedBytes(msisdn, "f")
if err != nil {
return nil
}
f.MSISDNLength = uint8(len(v))
f.MSISDN = msisdn
}
if has4thBit(flags) {
f.NAILength = uint8(len([]byte(nai)))
f.NAI = nai
}
return f
}
// ParseUserIDFields parses b into UserIDFields.
func ParseUserIDFields(b []byte) (*UserIDFields, error) {
f := &UserIDFields{}
if err := f.UnmarshalBinary(b); err != nil {
return nil, err
}
return f, nil
}
// UnmarshalBinary parses b into IE.
func (f *UserIDFields) UnmarshalBinary(b []byte) error {
l := len(b)
if l < 1 {
return io.ErrUnexpectedEOF
}
f.Flags = b[0]
offset := 1
if has1stBit(f.Flags) {
if l < offset+1 {
return io.ErrUnexpectedEOF
}
f.IMSILength = b[offset]
offset++
if l < offset+int(f.IMSILength) {
return io.ErrUnexpectedEOF
}
f.IMSI = utils.SwappedBytesToStr(b[offset:offset+int(f.IMSILength)], true)
offset += int(f.IMSILength)
}
if has2ndBit(f.Flags) {
if l < offset+1 {
return io.ErrUnexpectedEOF
}
f.IMEILength = b[offset]
offset++
if l < offset+int(f.IMEILength) {
return io.ErrUnexpectedEOF
}
f.IMEI = utils.SwappedBytesToStr(b[offset:offset+int(f.IMEILength)], true)
offset += int(f.IMEILength)
}
if has3rdBit(f.Flags) {
if l < offset+1 {
return io.ErrUnexpectedEOF
}
f.MSISDNLength = b[offset]
offset++
if l < offset+int(f.MSISDNLength) {
return io.ErrUnexpectedEOF
}
f.MSISDN = utils.SwappedBytesToStr(b[offset:offset+int(f.MSISDNLength)], true)
offset += int(f.MSISDNLength)
}
if has4thBit(f.Flags) {
if l < offset+1 {
return io.ErrUnexpectedEOF
}
f.NAILength = b[offset]
offset++
if l < offset+int(f.NAILength) {
return io.ErrUnexpectedEOF
}
f.NAI = string(b[offset : offset+int(f.NAILength)])
}
return nil
}
// Marshal returns the serialized bytes of UserIDFields.
func (f *UserIDFields) 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 *UserIDFields) MarshalTo(b []byte) error {
l := len(b)
if l < 1 {
return io.ErrUnexpectedEOF
}
b[0] = f.Flags
offset := 1
if has1stBit(f.Flags) {
b[offset] = f.IMSILength
offset++
v, err := utils.StrToSwappedBytes(f.IMSI, "f")
if err != nil {
return err
}
copy(b[offset:offset+int(f.IMSILength)], v)
offset += int(f.IMSILength)
}
if has2ndBit(f.Flags) {
b[offset] = f.IMEILength
offset++
v, err := utils.StrToSwappedBytes(f.IMEI, "f")
if err != nil {
return err
}
copy(b[offset:offset+int(f.IMEILength)], v)
offset += int(f.IMEILength)
}
if has3rdBit(f.Flags) {
b[offset] = f.MSISDNLength
offset++
v, err := utils.StrToSwappedBytes(f.MSISDN, "f")
if err != nil {
return err
}
copy(b[offset:offset+int(f.MSISDNLength)], v)
offset += int(f.MSISDNLength)
}
if has1stBit(f.Flags) {
b[offset] = f.NAILength
offset++
copy(b[offset:offset+int(f.NAILength)], f.NAI)
}
return nil
}
// MarshalLen returns field length in integer.
func (f *UserIDFields) MarshalLen() int {
l := 1
if has1stBit(f.Flags) {
l += 1 + int(f.IMSILength)
}
if has2ndBit(f.Flags) {
l += 1 + int(f.IMEILength)
}
if has3rdBit(f.Flags) {
l += 1 + int(f.MSISDNLength)
}
if has4thBit(f.Flags) {
l += 1 + int(f.NAILength)
}
return l
}