This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds pfcp session establishment
- Loading branch information
Showing
14 changed files
with
260 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package ie | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"net" | ||
) | ||
|
||
type FSEID struct { | ||
IEType uint16 | ||
Length uint16 | ||
V4 bool | ||
V6 bool | ||
SEID uint64 | ||
IPv4 []byte | ||
IPv6 []byte | ||
} | ||
|
||
func NewFSEID(seid uint64, ipv4Address string, ipv6Address string) (FSEID, error) { | ||
fseid := FSEID{ | ||
IEType: 57, | ||
SEID: seid, | ||
} | ||
var length uint16 = 9 | ||
|
||
ipv4 := net.ParseIP(ipv4Address) | ||
ipv6 := net.ParseIP(ipv6Address) | ||
|
||
if ipv4.To4() != nil { | ||
fseid.V4 = true | ||
fseid.IPv4 = ipv4.To4() | ||
length += 4 | ||
} | ||
if ipv6.To16() != nil { | ||
fseid.V6 = true | ||
fseid.IPv6 = ipv6.To16() | ||
length += 16 | ||
} | ||
fseid.Length = length | ||
return fseid, nil | ||
} | ||
|
||
func (fseid FSEID) Serialize() []byte { | ||
buf := new(bytes.Buffer) | ||
|
||
// Octets 1 to 2: Type (57) | ||
binary.Write(buf, binary.BigEndian, uint16(fseid.IEType)) | ||
|
||
// Octets 3 to 4: Length | ||
binary.Write(buf, binary.BigEndian, uint16(fseid.Length)) | ||
|
||
// Octet 5: Spare (6 bits) + V4 (1 bit) + V6 (1 bit) | ||
var flags byte | ||
if fseid.V4 { | ||
flags |= 1 << 1 // Set the second bit from the right if V4 is true | ||
} | ||
if fseid.V6 { | ||
flags |= 1 << 0 // Set the first bit from the right if V6 is true | ||
} | ||
buf.WriteByte(flags) | ||
|
||
// Octets 6 13: SEID | ||
binary.Write(buf, binary.BigEndian, fseid.SEID) | ||
|
||
// Octet m to (m+3) IPv4 address | ||
if fseid.V4 { | ||
buf.Write(fseid.IPv4) | ||
} | ||
|
||
// Octet p to (p+15): IPv6 address | ||
if fseid.V6 { | ||
buf.Write(fseid.IPv6) | ||
} | ||
|
||
return buf.Bytes() | ||
} | ||
|
||
func DeserializeFSEID(ieType uint16, ieLength uint16, ieValue []byte) FSEID { | ||
v4 := ieValue[0]&0x02 > 0 | ||
v6 := ieValue[0]&0x01 > 0 | ||
seid := binary.BigEndian.Uint64(ieValue[1:9]) | ||
ipv4 := ieValue[9:13] | ||
ipv6 := ieValue[13:29] | ||
|
||
return FSEID{ | ||
IEType: ieType, | ||
Length: ieLength, | ||
V4: v4, | ||
V6: v6, | ||
SEID: seid, | ||
IPv4: ipv4, | ||
IPv6: ipv6, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package ie_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/dot-5g/pfcp/ie" | ||
) | ||
|
||
func TestGivenValidIPAddressWhenNewFSEIDThenFieldsAreSetCorrectly(t *testing.T) { | ||
seid := uint64(0x1234567890ABCDEF) | ||
|
||
fseid, err := ie.NewFSEID(seid, "1.2.3.4", "") | ||
|
||
if err != nil { | ||
t.Fatalf("Error creating FSEID: %v", err) | ||
} | ||
|
||
if fseid.IEType != 57 { | ||
t.Errorf("Expected FSEID IEType 97, got %d", fseid.IEType) | ||
} | ||
|
||
if fseid.Length != 13 { | ||
t.Errorf("Expected FSEID length 12, got %d", fseid.Length) | ||
} | ||
|
||
if fseid.V4 != true { | ||
t.Errorf("Expected FSEID V4 true, got %v", fseid.V4) | ||
} | ||
|
||
if fseid.V6 != false { | ||
t.Errorf("Expected FSEID V6 false, got %v", fseid.V6) | ||
} | ||
|
||
if fseid.SEID != seid { | ||
t.Errorf("Expected FSEID SEID %d, got %d", seid, fseid.SEID) | ||
} | ||
|
||
expectedIPv4 := []byte{1, 2, 3, 4} | ||
for i := range fseid.IPv4 { | ||
if fseid.IPv4[i] != expectedIPv4[i] { | ||
t.Errorf("Expected FSEID IPv4 %v, got %v", expectedIPv4, fseid.IPv4) | ||
} | ||
} | ||
|
||
} | ||
|
||
func TestGivenSerializedWhenDeserializeThenFieldsSetCorrectly(t *testing.T) { | ||
seid := uint64(0x1234567890ABCDEF) | ||
ipv4 := "2.3.4.5" | ||
ipv6 := "2001:db8::68" | ||
|
||
fseid, err := ie.NewFSEID(seid, ipv4, ipv6) | ||
|
||
if err != nil { | ||
t.Fatalf("Error creating FSEID: %v", err) | ||
} | ||
|
||
serialized := fseid.Serialize() | ||
|
||
fmt.Printf("Serialized FSEID: %v\n", serialized) | ||
|
||
deserialized := ie.DeserializeFSEID(57, 12, serialized[4:]) | ||
|
||
if deserialized.IEType != 57 { | ||
t.Errorf("Expected FSEID IEType 57, got %d", deserialized.IEType) | ||
} | ||
|
||
if deserialized.Length != 12 { | ||
t.Errorf("Expected FSEID length 12, got %d", deserialized.Length) | ||
} | ||
|
||
if deserialized.V4 != true { | ||
t.Errorf("Expected FSEID V4 true, got %v", deserialized.V4) | ||
} | ||
|
||
if deserialized.V6 != true { | ||
t.Errorf("Expected FSEID V6 true, got %v", deserialized.V6) | ||
} | ||
|
||
if deserialized.SEID != seid { | ||
t.Errorf("Expected FSEID SEID %d, got %d", seid, deserialized.SEID) | ||
} | ||
|
||
expectedIPv4 := []byte{2, 3, 4, 5} | ||
for i := range deserialized.IPv4 { | ||
if deserialized.IPv4[i] != expectedIPv4[i] { | ||
t.Errorf("Expected FSEID IPv4 %v, got %v", expectedIPv4, deserialized.IPv4) | ||
} | ||
} | ||
|
||
expectedIPv6 := []byte{32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0x00, 0x68} | ||
for i := range deserialized.IPv6 { | ||
if deserialized.IPv6[i] != expectedIPv6[i] { | ||
t.Errorf("Expected FSEID IPv6 %v, got %v", expectedIPv6, deserialized.IPv6) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.