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

Commit

Permalink
chore: Restructures ie deserialization (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
gruyaume authored Jan 8, 2024
1 parent 807650a commit db505d5
Show file tree
Hide file tree
Showing 45 changed files with 1,094 additions and 897 deletions.
6 changes: 3 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ func New(ServerAddress string) *Pfcp {

func (pfcp *Pfcp) sendNodePfcpMessage(message messages.PFCPMessage, sequenceNumber uint32) error {
messageType := message.GetMessageType()
header := messages.NewNodePFCPHeader(messageType, sequenceNumber)
header := messages.NewNodeHeader(messageType, sequenceNumber)
return pfcp.sendPfcpMessage(message, header)
}

func (pfcp *Pfcp) sendSessionPfcpMessage(message messages.PFCPMessage, seid uint64, sequenceNumber uint32) error {
messageType := message.GetMessageType()
header := messages.NewSessionPFCPHeader(messageType, seid, sequenceNumber)
header := messages.NewSessionHeader(messageType, seid, sequenceNumber)
return pfcp.sendPfcpMessage(message, header)
}

func (pfcp *Pfcp) sendPfcpMessage(message messages.PFCPMessage, header messages.PFCPHeader) error {
func (pfcp *Pfcp) sendPfcpMessage(message messages.PFCPMessage, header messages.Header) error {
payload := messages.Serialize(message, header)
if err := pfcp.Udp.Send(payload); err != nil {
log.Printf("Failed to send PFCP: %v\n", err)
Expand Down
34 changes: 16 additions & 18 deletions ie/apply_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package ie

import (
"bytes"
"encoding/binary"
"fmt"
)

type ApplyAction struct {
IEType uint16
Length uint16
Header Header
DFRT bool
IPMD bool
IPMA bool
Expand Down Expand Up @@ -64,6 +62,11 @@ func NewApplyAction(flag ApplyActionFlag, extraFlags []ApplyActionExtraFlag) (Ap
var bdpn bool
var edrt bool

ieHeader := Header{
Type: IEType(ApplyActionIEType),
Length: 2,
}

if (contains(extraFlags, NOCP) || contains(extraFlags, BDPN) || contains(extraFlags, DDPN)) && flag != BUFF {
return ApplyAction{}, fmt.Errorf("the NOCP flag, BDPN and DDPN flag may only be set if the BUFF flag is set")
}
Expand Down Expand Up @@ -121,8 +124,7 @@ func NewApplyAction(flag ApplyActionFlag, extraFlags []ApplyActionExtraFlag) (Ap
}

return ApplyAction{
IEType: uint16(ApplyActionIEType),
Length: 2,
Header: ieHeader,
DFRT: dfrt,
IPMD: ipmd,
IPMA: ipma,
Expand All @@ -140,11 +142,8 @@ func NewApplyAction(flag ApplyActionFlag, extraFlags []ApplyActionExtraFlag) (Ap
func (applyaction ApplyAction) Serialize() []byte {
buf := new(bytes.Buffer)

// Octets 1 to 2: Type
binary.Write(buf, binary.BigEndian, uint16(applyaction.IEType))

// Octets 3 to 4: Length
binary.Write(buf, binary.BigEndian, uint16(applyaction.Length))
// Octets 1 to 4: Header
buf.Write(applyaction.Header.Serialize())

// 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)
var byte5 byte
Expand Down Expand Up @@ -191,26 +190,25 @@ func (applyaction ApplyAction) Serialize() []byte {
}

func (applyaction ApplyAction) IsZeroValue() bool {
return applyaction.Length == 0
return applyaction.Header.Length == 0
}

func DeserializeApplyAction(ieType uint16, ieLength uint16, ieValue []byte) (ApplyAction, error) {
func DeserializeApplyAction(ieHeader Header, ieValue []byte) (ApplyAction, error) {
var applyaction ApplyAction

if ieType != uint16(ApplyActionIEType) {
return applyaction, fmt.Errorf("invalid IE type: expected %d, got %d", ApplyActionIEType, ieType)
if ieHeader.Type != ApplyActionIEType {
return applyaction, fmt.Errorf("invalid IE type: expected %d, got %d", ApplyActionIEType, ieHeader.Type)
}

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

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

applyaction.IEType = ieType
applyaction.Length = ieLength
applyaction.Header = ieHeader

// Deserialize the first byte (Octet 5)
byte5 := ieValue[0]
Expand Down
23 changes: 14 additions & 9 deletions ie/apply_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func TestGivenCorrectValuesWhenNewApplyActionThenFieldsSetCorrectly(t *testing.T
t.Fatalf("Error creating ApplyAction: %v", err)
}

if applyAction.IEType != 44 {
t.Errorf("Expected IEType 44, got %d", applyAction.IEType)
if applyAction.Header.Type != 44 {
t.Errorf("Expected IEType 44, got %d", applyAction.Header.Type)
}

if applyAction.Length != 2 {
t.Errorf("Expected Length 2, got %d", applyAction.Length)
if applyAction.Header.Length != 2 {
t.Errorf("Expected Length 2, got %d", applyAction.Header.Length)
}

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

serialized := applyAction.Serialize()

deserialized, err := ie.DeserializeApplyAction(44, 2, serialized[4:])
ieHeader := ie.Header{
Type: 44,
Length: 2,
}

deserialized, err := ie.DeserializeApplyAction(ieHeader, serialized[4:])

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

if deserialized.IEType != 44 {
t.Errorf("Expected IEType 44, got %d", deserialized.IEType)
if deserialized.Header.Type != 44 {
t.Errorf("Expected IEType 44, got %d", deserialized.Header.Type)
}

if deserialized.Length != 2 {
t.Errorf("Expected Length 2, got %d", deserialized.Length)
if deserialized.Header.Length != 2 {
t.Errorf("Expected Length 2, got %d", deserialized.Header.Length)
}

if deserialized.DFRT != false {
Expand Down
34 changes: 12 additions & 22 deletions ie/cause.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package ie

import (
"bytes"
"encoding/binary"
"fmt"
)

type Cause struct {
IEType uint16
Length uint16
Header Header
Value CauseValue
}

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

return Cause{
IEType: uint16(CauseIEType),
header := Header{
Type: CauseIEType,
Length: 1,
}

return Cause{
Header: header,
Value: value,
}, nil
}

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

// Octets 1 to 2: Type
binary.Write(buf, binary.BigEndian, uint16(cause.IEType))

// Octets 3 to 4: Length
binary.Write(buf, binary.BigEndian, uint16(cause.Length))
// Octets 1 to 4: Header
buf.Write(cause.Header.Serialize())

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

func (cause Cause) IsZeroValue() bool {
return cause.Length == 0
return cause.Value == 0
}

func DeserializeCause(ieType uint16, ieLength uint16, ieValue []byte) (Cause, error) {
func DeserializeCause(ieHeader Header, ieValue []byte) (Cause, error) {
var cause Cause

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

if ieType != uint16(CauseIEType) {
return cause, fmt.Errorf("invalid IE type: expected %d, got %d", CauseIEType, ieType)
}

if ieLength != 1 {
return cause, fmt.Errorf("invalid length field for Cause: expected 1, got %d", ieLength)
}

return Cause{
IEType: ieType,
Length: ieLength,
Header: ieHeader,
Value: CauseValue(ieValue[0]),
}, nil
}
44 changes: 26 additions & 18 deletions ie/create_far.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,29 @@ import (
)

type CreateFAR struct {
IEType uint16
Length uint16
Header Header
FARID FARID
ApplyAction ApplyAction
}

func NewCreateFAR(farid FARID, applyaction ApplyAction) (CreateFAR, error) {
ieHeader := Header{
Type: IEType(CreateFARIEType),
Length: farid.Header.Length + applyaction.Header.Length + 8,
}

return CreateFAR{
IEType: uint16(CreateFARIEType),
Length: farid.Length + applyaction.Length + 8,
Header: ieHeader,
FARID: farid,
ApplyAction: applyaction,
}, nil
}

func (createfar CreateFAR) Serialize() []byte {

buf := new(bytes.Buffer)

// Octets 1 to 2: Type
binary.Write(buf, binary.BigEndian, uint16(createfar.IEType))

// Octets 3 to 4: Length
binary.Write(buf, binary.BigEndian, uint16(createfar.Length))
// Octets 1 to 4: Header
buf.Write(createfar.Header.Serialize())

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

func (createfar CreateFAR) IsZeroValue() bool {
return createfar.Length == 0
return createfar.Header.Length == 0
}

func DeserializeCreateFAR(ieType uint16, length uint16, value []byte) (CreateFAR, error) {
func DeserializeCreateFAR(ieHeader Header, value []byte) (CreateFAR, error) {
var createfar CreateFAR

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

createfar.IEType = ieType
createfar.Length = length
createfar.Header = ieHeader

buffer := bytes.NewBuffer(value)

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

farid, err := DeserializeFARID(faridIEType, faridIELength, faridIEValue)
faridIEHEader := Header{
Type: IEType(faridIEType),
Length: faridIELength,
}

farid, err := DeserializeFARID(faridIEHEader, faridIEValue)
if err != nil {
return createfar, fmt.Errorf("failed to deserialize FARID: %v", err)
}
Expand All @@ -94,7 +97,12 @@ func DeserializeCreateFAR(ieType uint16, length uint16, value []byte) (CreateFAR
}
applyactionIEValue := buffer.Next(int(applyactionIELength))

applyaction, err := DeserializeApplyAction(applyactionIEType, applyactionIELength, applyactionIEValue)
applyActionHeader := Header{
Type: IEType(applyactionIEType),
Length: applyactionIELength,
}

applyaction, err := DeserializeApplyAction(applyActionHeader, applyactionIEValue)
if err != nil {
return createfar, fmt.Errorf("failed to deserialize ApplyAction: %v", err)
}
Expand Down
31 changes: 18 additions & 13 deletions ie/create_far_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func TestGivenCorrectValuesWhenNewFarThenFieldsSetCorrectly(t *testing.T) {
t.Fatalf("Error creating CreateFAR: %v", err)
}

if createFar.IEType != 3 {
t.Errorf("Expected IEType 3, got %d", createFar.IEType)
if createFar.Header.Type != 3 {
t.Errorf("Expected IEType 3, got %d", createFar.Header.Type)
}

if createFar.Length != 14 {
t.Errorf("Expected Length 14, got %d", createFar.Length)
if createFar.Header.Length != 14 {
t.Errorf("Expected Length 14, got %d", createFar.Header.Length)
}

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

if createFar.ApplyAction.Length != 2 {
t.Errorf("Expected Length 2, got %d", createFar.ApplyAction.Length)
if createFar.ApplyAction.Header.Length != 2 {
t.Errorf("Expected Length 2, got %d", createFar.ApplyAction.Header.Length)
}

if createFar.ApplyAction.IEType != 44 {
t.Errorf("Expected IEType 44, got %d", createFar.ApplyAction.IEType)
if createFar.ApplyAction.Header.Type != 44 {
t.Errorf("Expected IEType 44, got %d", createFar.ApplyAction.Header.Type)
}

}
Expand All @@ -113,18 +113,23 @@ func TestGivenSerializedWhenDeserializeCreateFarThenFieldsSetCorrectly(t *testin

serialized := createFar.Serialize()

deserialized, err := ie.DeserializeCreateFAR(3, 14, serialized[4:])
ieHeader := ie.Header{
Type: 3,
Length: 14,
}

deserialized, err := ie.DeserializeCreateFAR(ieHeader, serialized[4:])

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

if deserialized.IEType != 3 {
t.Errorf("Expected IEType 3, got %d", deserialized.IEType)
if deserialized.Header.Type != 3 {
t.Errorf("Expected IEType 3, got %d", deserialized.Header.Type)
}

if deserialized.Length != 14 {
t.Errorf("Expected Length 14, got %d", deserialized.Length)
if deserialized.Header.Length != 14 {
t.Errorf("Expected Length 14, got %d", deserialized.Header.Length)
}

if deserialized.FARID != farId {
Expand Down
Loading

0 comments on commit db505d5

Please sign in to comment.