Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit db505d5

Browse files
authored
chore: Restructures ie deserialization (#31)
1 parent 807650a commit db505d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1094
-897
lines changed

client/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ func New(ServerAddress string) *Pfcp {
2323

2424
func (pfcp *Pfcp) sendNodePfcpMessage(message messages.PFCPMessage, sequenceNumber uint32) error {
2525
messageType := message.GetMessageType()
26-
header := messages.NewNodePFCPHeader(messageType, sequenceNumber)
26+
header := messages.NewNodeHeader(messageType, sequenceNumber)
2727
return pfcp.sendPfcpMessage(message, header)
2828
}
2929

3030
func (pfcp *Pfcp) sendSessionPfcpMessage(message messages.PFCPMessage, seid uint64, sequenceNumber uint32) error {
3131
messageType := message.GetMessageType()
32-
header := messages.NewSessionPFCPHeader(messageType, seid, sequenceNumber)
32+
header := messages.NewSessionHeader(messageType, seid, sequenceNumber)
3333
return pfcp.sendPfcpMessage(message, header)
3434
}
3535

36-
func (pfcp *Pfcp) sendPfcpMessage(message messages.PFCPMessage, header messages.PFCPHeader) error {
36+
func (pfcp *Pfcp) sendPfcpMessage(message messages.PFCPMessage, header messages.Header) error {
3737
payload := messages.Serialize(message, header)
3838
if err := pfcp.Udp.Send(payload); err != nil {
3939
log.Printf("Failed to send PFCP: %v\n", err)

ie/apply_action.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package ie
22

33
import (
44
"bytes"
5-
"encoding/binary"
65
"fmt"
76
)
87

98
type ApplyAction struct {
10-
IEType uint16
11-
Length uint16
9+
Header Header
1210
DFRT bool
1311
IPMD bool
1412
IPMA bool
@@ -64,6 +62,11 @@ func NewApplyAction(flag ApplyActionFlag, extraFlags []ApplyActionExtraFlag) (Ap
6462
var bdpn bool
6563
var edrt bool
6664

65+
ieHeader := Header{
66+
Type: IEType(ApplyActionIEType),
67+
Length: 2,
68+
}
69+
6770
if (contains(extraFlags, NOCP) || contains(extraFlags, BDPN) || contains(extraFlags, DDPN)) && flag != BUFF {
6871
return ApplyAction{}, fmt.Errorf("the NOCP flag, BDPN and DDPN flag may only be set if the BUFF flag is set")
6972
}
@@ -121,8 +124,7 @@ func NewApplyAction(flag ApplyActionFlag, extraFlags []ApplyActionExtraFlag) (Ap
121124
}
122125

123126
return ApplyAction{
124-
IEType: uint16(ApplyActionIEType),
125-
Length: 2,
127+
Header: ieHeader,
126128
DFRT: dfrt,
127129
IPMD: ipmd,
128130
IPMA: ipma,
@@ -140,11 +142,8 @@ func NewApplyAction(flag ApplyActionFlag, extraFlags []ApplyActionExtraFlag) (Ap
140142
func (applyaction ApplyAction) Serialize() []byte {
141143
buf := new(bytes.Buffer)
142144

143-
// Octets 1 to 2: Type
144-
binary.Write(buf, binary.BigEndian, uint16(applyaction.IEType))
145-
146-
// Octets 3 to 4: Length
147-
binary.Write(buf, binary.BigEndian, uint16(applyaction.Length))
145+
// Octets 1 to 4: Header
146+
buf.Write(applyaction.Header.Serialize())
148147

149148
// Octet 5: DFRT (bit 8), IPMD (bit 7), IPMA (bit 6), DUPL (bit 5), NOCP (bit 4), BUFF (bit 3), FORW (bit 2), DROP (bit 1)
150149
var byte5 byte
@@ -191,26 +190,25 @@ func (applyaction ApplyAction) Serialize() []byte {
191190
}
192191

193192
func (applyaction ApplyAction) IsZeroValue() bool {
194-
return applyaction.Length == 0
193+
return applyaction.Header.Length == 0
195194
}
196195

197-
func DeserializeApplyAction(ieType uint16, ieLength uint16, ieValue []byte) (ApplyAction, error) {
196+
func DeserializeApplyAction(ieHeader Header, ieValue []byte) (ApplyAction, error) {
198197
var applyaction ApplyAction
199198

200-
if ieType != uint16(ApplyActionIEType) {
201-
return applyaction, fmt.Errorf("invalid IE type: expected %d, got %d", ApplyActionIEType, ieType)
199+
if ieHeader.Type != ApplyActionIEType {
200+
return applyaction, fmt.Errorf("invalid IE type: expected %d, got %d", ApplyActionIEType, ieHeader.Type)
202201
}
203202

204-
if ieLength != 2 {
205-
return applyaction, fmt.Errorf("invalid length field for ApplyAction: expected 2, got %d", ieLength)
203+
if ieHeader.Length != 2 {
204+
return applyaction, fmt.Errorf("invalid length field for ApplyAction: expected 2, got %d", ieHeader.Length)
206205
}
207206

208207
if len(ieValue) != 2 {
209208
return applyaction, fmt.Errorf("invalid length for ApplyAction: got %d bytes, want 2", len(ieValue))
210209
}
211210

212-
applyaction.IEType = ieType
213-
applyaction.Length = ieLength
211+
applyaction.Header = ieHeader
214212

215213
// Deserialize the first byte (Octet 5)
216214
byte5 := ieValue[0]

ie/apply_action_test.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ func TestGivenCorrectValuesWhenNewApplyActionThenFieldsSetCorrectly(t *testing.T
1414
t.Fatalf("Error creating ApplyAction: %v", err)
1515
}
1616

17-
if applyAction.IEType != 44 {
18-
t.Errorf("Expected IEType 44, got %d", applyAction.IEType)
17+
if applyAction.Header.Type != 44 {
18+
t.Errorf("Expected IEType 44, got %d", applyAction.Header.Type)
1919
}
2020

21-
if applyAction.Length != 2 {
22-
t.Errorf("Expected Length 2, got %d", applyAction.Length)
21+
if applyAction.Header.Length != 2 {
22+
t.Errorf("Expected Length 2, got %d", applyAction.Header.Length)
2323
}
2424

2525
if applyAction.FORW != true {
@@ -114,18 +114,23 @@ func TestGivenApplyActionSerializedWhenDeserializeThenFieldsSetCorrectly(t *test
114114

115115
serialized := applyAction.Serialize()
116116

117-
deserialized, err := ie.DeserializeApplyAction(44, 2, serialized[4:])
117+
ieHeader := ie.Header{
118+
Type: 44,
119+
Length: 2,
120+
}
121+
122+
deserialized, err := ie.DeserializeApplyAction(ieHeader, serialized[4:])
118123

119124
if err != nil {
120125
t.Fatalf("Error deserializing ApplyAction: %v", err)
121126
}
122127

123-
if deserialized.IEType != 44 {
124-
t.Errorf("Expected IEType 44, got %d", deserialized.IEType)
128+
if deserialized.Header.Type != 44 {
129+
t.Errorf("Expected IEType 44, got %d", deserialized.Header.Type)
125130
}
126131

127-
if deserialized.Length != 2 {
128-
t.Errorf("Expected Length 2, got %d", deserialized.Length)
132+
if deserialized.Header.Length != 2 {
133+
t.Errorf("Expected Length 2, got %d", deserialized.Header.Length)
129134
}
130135

131136
if deserialized.DFRT != false {

ie/cause.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package ie
22

33
import (
44
"bytes"
5-
"encoding/binary"
65
"fmt"
76
)
87

98
type Cause struct {
10-
IEType uint16
11-
Length uint16
9+
Header Header
1210
Value CauseValue
1311
}
1412

@@ -40,21 +38,22 @@ func NewCause(value CauseValue) (Cause, error) {
4038
return Cause{}, fmt.Errorf("invalid value for Cause: %d", value)
4139
}
4240

43-
return Cause{
44-
IEType: uint16(CauseIEType),
41+
header := Header{
42+
Type: CauseIEType,
4543
Length: 1,
44+
}
45+
46+
return Cause{
47+
Header: header,
4648
Value: value,
4749
}, nil
4850
}
4951

5052
func (cause Cause) Serialize() []byte {
5153
buf := new(bytes.Buffer)
5254

53-
// Octets 1 to 2: Type
54-
binary.Write(buf, binary.BigEndian, uint16(cause.IEType))
55-
56-
// Octets 3 to 4: Length
57-
binary.Write(buf, binary.BigEndian, uint16(cause.Length))
55+
// Octets 1 to 4: Header
56+
buf.Write(cause.Header.Serialize())
5857

5958
// Octet 5: Value (1 byte)
6059
buf.WriteByte(uint8(cause.Value))
@@ -63,27 +62,18 @@ func (cause Cause) Serialize() []byte {
6362
}
6463

6564
func (cause Cause) IsZeroValue() bool {
66-
return cause.Length == 0
65+
return cause.Value == 0
6766
}
6867

69-
func DeserializeCause(ieType uint16, ieLength uint16, ieValue []byte) (Cause, error) {
68+
func DeserializeCause(ieHeader Header, ieValue []byte) (Cause, error) {
7069
var cause Cause
7170

7271
if len(ieValue) != 1 {
7372
return cause, fmt.Errorf("invalid length for Cause: got %d bytes, want 1", len(ieValue))
7473
}
7574

76-
if ieType != uint16(CauseIEType) {
77-
return cause, fmt.Errorf("invalid IE type: expected %d, got %d", CauseIEType, ieType)
78-
}
79-
80-
if ieLength != 1 {
81-
return cause, fmt.Errorf("invalid length field for Cause: expected 1, got %d", ieLength)
82-
}
83-
8475
return Cause{
85-
IEType: ieType,
86-
Length: ieLength,
76+
Header: ieHeader,
8777
Value: CauseValue(ieValue[0]),
8878
}, nil
8979
}

ie/create_far.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,29 @@ import (
77
)
88

99
type CreateFAR struct {
10-
IEType uint16
11-
Length uint16
10+
Header Header
1211
FARID FARID
1312
ApplyAction ApplyAction
1413
}
1514

1615
func NewCreateFAR(farid FARID, applyaction ApplyAction) (CreateFAR, error) {
16+
ieHeader := Header{
17+
Type: IEType(CreateFARIEType),
18+
Length: farid.Header.Length + applyaction.Header.Length + 8,
19+
}
20+
1721
return CreateFAR{
18-
IEType: uint16(CreateFARIEType),
19-
Length: farid.Length + applyaction.Length + 8,
22+
Header: ieHeader,
2023
FARID: farid,
2124
ApplyAction: applyaction,
2225
}, nil
2326
}
2427

2528
func (createfar CreateFAR) Serialize() []byte {
26-
2729
buf := new(bytes.Buffer)
2830

29-
// Octets 1 to 2: Type
30-
binary.Write(buf, binary.BigEndian, uint16(createfar.IEType))
31-
32-
// Octets 3 to 4: Length
33-
binary.Write(buf, binary.BigEndian, uint16(createfar.Length))
31+
// Octets 1 to 4: Header
32+
buf.Write(createfar.Header.Serialize())
3433

3534
// Octets 5 to n: FAR ID
3635
serializedFARID := createfar.FARID.Serialize()
@@ -45,18 +44,17 @@ func (createfar CreateFAR) Serialize() []byte {
4544
}
4645

4746
func (createfar CreateFAR) IsZeroValue() bool {
48-
return createfar.Length == 0
47+
return createfar.Header.Length == 0
4948
}
5049

51-
func DeserializeCreateFAR(ieType uint16, length uint16, value []byte) (CreateFAR, error) {
50+
func DeserializeCreateFAR(ieHeader Header, value []byte) (CreateFAR, error) {
5251
var createfar CreateFAR
5352

54-
if len(value) < IEHeaderLength {
55-
return createfar, fmt.Errorf("invalid length for CreateFAR: got %d bytes, want at least %d", len(value), IEHeaderLength)
53+
if len(value) < HeaderLength {
54+
return createfar, fmt.Errorf("invalid length for CreateFAR: got %d bytes, want at least %d", len(value), HeaderLength)
5655
}
5756

58-
createfar.IEType = ieType
59-
createfar.Length = length
57+
createfar.Header = ieHeader
6058

6159
buffer := bytes.NewBuffer(value)
6260

@@ -73,7 +71,12 @@ func DeserializeCreateFAR(ieType uint16, length uint16, value []byte) (CreateFAR
7371
faridIELength := binary.BigEndian.Uint16(buffer.Next(2))
7472
faridIEValue := buffer.Next(int(faridIELength))
7573

76-
farid, err := DeserializeFARID(faridIEType, faridIELength, faridIEValue)
74+
faridIEHEader := Header{
75+
Type: IEType(faridIEType),
76+
Length: faridIELength,
77+
}
78+
79+
farid, err := DeserializeFARID(faridIEHEader, faridIEValue)
7780
if err != nil {
7881
return createfar, fmt.Errorf("failed to deserialize FARID: %v", err)
7982
}
@@ -94,7 +97,12 @@ func DeserializeCreateFAR(ieType uint16, length uint16, value []byte) (CreateFAR
9497
}
9598
applyactionIEValue := buffer.Next(int(applyactionIELength))
9699

97-
applyaction, err := DeserializeApplyAction(applyactionIEType, applyactionIELength, applyactionIEValue)
100+
applyActionHeader := Header{
101+
Type: IEType(applyactionIEType),
102+
Length: applyactionIELength,
103+
}
104+
105+
applyaction, err := DeserializeApplyAction(applyActionHeader, applyactionIEValue)
98106
if err != nil {
99107
return createfar, fmt.Errorf("failed to deserialize ApplyAction: %v", err)
100108
}

ie/create_far_test.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ func TestGivenCorrectValuesWhenNewFarThenFieldsSetCorrectly(t *testing.T) {
2626
t.Fatalf("Error creating CreateFAR: %v", err)
2727
}
2828

29-
if createFar.IEType != 3 {
30-
t.Errorf("Expected IEType 3, got %d", createFar.IEType)
29+
if createFar.Header.Type != 3 {
30+
t.Errorf("Expected IEType 3, got %d", createFar.Header.Type)
3131
}
3232

33-
if createFar.Length != 14 {
34-
t.Errorf("Expected Length 14, got %d", createFar.Length)
33+
if createFar.Header.Length != 14 {
34+
t.Errorf("Expected Length 14, got %d", createFar.Header.Length)
3535
}
3636

3737
if createFar.FARID.Value != 1 {
@@ -82,12 +82,12 @@ func TestGivenCorrectValuesWhenNewFarThenFieldsSetCorrectly(t *testing.T) {
8282
t.Errorf("Expected BDPN false, got %v", createFar.ApplyAction.BDPN)
8383
}
8484

85-
if createFar.ApplyAction.Length != 2 {
86-
t.Errorf("Expected Length 2, got %d", createFar.ApplyAction.Length)
85+
if createFar.ApplyAction.Header.Length != 2 {
86+
t.Errorf("Expected Length 2, got %d", createFar.ApplyAction.Header.Length)
8787
}
8888

89-
if createFar.ApplyAction.IEType != 44 {
90-
t.Errorf("Expected IEType 44, got %d", createFar.ApplyAction.IEType)
89+
if createFar.ApplyAction.Header.Type != 44 {
90+
t.Errorf("Expected IEType 44, got %d", createFar.ApplyAction.Header.Type)
9191
}
9292

9393
}
@@ -113,18 +113,23 @@ func TestGivenSerializedWhenDeserializeCreateFarThenFieldsSetCorrectly(t *testin
113113

114114
serialized := createFar.Serialize()
115115

116-
deserialized, err := ie.DeserializeCreateFAR(3, 14, serialized[4:])
116+
ieHeader := ie.Header{
117+
Type: 3,
118+
Length: 14,
119+
}
120+
121+
deserialized, err := ie.DeserializeCreateFAR(ieHeader, serialized[4:])
117122

118123
if err != nil {
119124
t.Fatalf("Error deserializing CreateFAR: %v", err)
120125
}
121126

122-
if deserialized.IEType != 3 {
123-
t.Errorf("Expected IEType 3, got %d", deserialized.IEType)
127+
if deserialized.Header.Type != 3 {
128+
t.Errorf("Expected IEType 3, got %d", deserialized.Header.Type)
124129
}
125130

126-
if deserialized.Length != 14 {
127-
t.Errorf("Expected Length 14, got %d", deserialized.Length)
131+
if deserialized.Header.Length != 14 {
132+
t.Errorf("Expected Length 14, got %d", deserialized.Header.Length)
128133
}
129134

130135
if deserialized.FARID != farId {

0 commit comments

Comments
 (0)