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 support for optional parameters (#22)
- Loading branch information
Showing
17 changed files
with
389 additions
and
113 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package ie | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
type SourceIPAddress struct { | ||
IEtype uint16 | ||
Length uint16 | ||
MPL bool | ||
V4 bool | ||
V6 bool | ||
IPv4Address []byte | ||
IPv6Address []byte | ||
MaskPrefixLength uint8 | ||
} | ||
|
||
func NewSourceIPAddress(cidr string) (SourceIPAddress, error) { | ||
sourceIPAddress := SourceIPAddress{ | ||
IEtype: 192, | ||
} | ||
|
||
ip, ipnet, err := net.ParseCIDR(cidr) | ||
if err != nil { | ||
return sourceIPAddress, fmt.Errorf("invalid CIDR") | ||
} | ||
|
||
if ip.To4() != nil { | ||
sourceIPAddress.V4 = true | ||
sourceIPAddress.IPv4Address = ip.To4() | ||
sourceIPAddress.Length = 6 | ||
} else { | ||
sourceIPAddress.V6 = true | ||
sourceIPAddress.IPv6Address = ip.To16() | ||
sourceIPAddress.Length = 18 | ||
} | ||
|
||
if ipnet != nil { | ||
sourceIPAddress.MPL = true | ||
ones, _ := ipnet.Mask.Size() | ||
sourceIPAddress.MaskPrefixLength = uint8(ones) | ||
} | ||
return sourceIPAddress, nil | ||
} | ||
|
||
func (sourceIPAddress SourceIPAddress) IsZeroValue() bool { | ||
return sourceIPAddress.Length == 0 | ||
} | ||
|
||
func (sourceIPAddress SourceIPAddress) Serialize() []byte { | ||
var length uint16 | ||
|
||
if sourceIPAddress.V4 { | ||
length = 6 | ||
} | ||
if sourceIPAddress.V6 { | ||
length = 18 | ||
} | ||
bytes := make([]byte, 4+length) | ||
bytes[0] = byte(sourceIPAddress.IEtype >> 8) | ||
bytes[1] = byte(sourceIPAddress.IEtype) | ||
bytes[2] = byte(length >> 8) | ||
bytes[3] = byte(length) | ||
if sourceIPAddress.MPL { | ||
bytes[4] = 0x80 | ||
} | ||
if sourceIPAddress.V4 { | ||
bytes[4] |= 0x40 | ||
} | ||
if sourceIPAddress.V6 { | ||
bytes[4] |= 0x20 | ||
} | ||
if sourceIPAddress.V4 { | ||
copy(bytes[5:9], sourceIPAddress.IPv4Address) | ||
bytes[9] = sourceIPAddress.MaskPrefixLength | ||
} | ||
if sourceIPAddress.V6 { | ||
copy(bytes[5:21], sourceIPAddress.IPv6Address) | ||
bytes[21] = sourceIPAddress.MaskPrefixLength | ||
} | ||
return bytes | ||
} | ||
|
||
func DeserializeSourceIPAddress(ieType uint16, ieLength uint16, ieValue []byte) (SourceIPAddress, error) { | ||
sourceIPAddress := SourceIPAddress{ | ||
IEtype: ieType, | ||
Length: ieLength, | ||
} | ||
|
||
if ieValue[0]&0x80 == 0x80 { | ||
sourceIPAddress.MPL = true | ||
} | ||
if ieValue[0]&0x40 == 0x40 { | ||
sourceIPAddress.V4 = true | ||
sourceIPAddress.IPv4Address = ieValue[1:5] | ||
if sourceIPAddress.MPL { | ||
sourceIPAddress.MaskPrefixLength = ieValue[5] | ||
} | ||
} | ||
if ieValue[0]&0x20 == 0x20 { | ||
sourceIPAddress.V6 = true | ||
sourceIPAddress.IPv6Address = ieValue[1:17] | ||
if sourceIPAddress.MPL { | ||
sourceIPAddress.MaskPrefixLength = ieValue[17] | ||
} | ||
} | ||
return sourceIPAddress, nil | ||
} |
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,111 @@ | ||
package ie_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dot-5g/pfcp/ie" | ||
) | ||
|
||
func TestGivenCorrectIPv4AddressWhenSourceIPAddressThenFieldsSetCorrectly(t *testing.T) { | ||
|
||
sourceIPAddress, err := ie.NewSourceIPAddress("1.2.3.4/24") | ||
|
||
if err != nil { | ||
t.Fatalf("Error creating SourceIPAddress: %v", err) | ||
} | ||
|
||
if sourceIPAddress.IEtype != 192 { | ||
t.Errorf("Expected NodeID, got %d", sourceIPAddress.IEtype) | ||
} | ||
|
||
if sourceIPAddress.Length != 6 { | ||
t.Errorf("Expected NodeID length 5, got %d", sourceIPAddress.Length) | ||
} | ||
|
||
if sourceIPAddress.MPL != true { | ||
t.Errorf("Expected NodeID MPL true, got %v", sourceIPAddress.MPL) | ||
} | ||
|
||
if sourceIPAddress.V4 != true { | ||
t.Errorf("Expected NodeID V4 true, got %v", sourceIPAddress.V4) | ||
} | ||
|
||
if sourceIPAddress.V6 != false { | ||
t.Errorf("Expected NodeID V6 false, got %v", sourceIPAddress.V6) | ||
} | ||
|
||
if sourceIPAddress.MaskPrefixLength != 24 { | ||
t.Errorf("Expected NodeID MaskPrefixLength 24, got %d", sourceIPAddress.MaskPrefixLength) | ||
} | ||
} | ||
|
||
func TestGivenCorrectIPv6AddressWhenSourceIPAddressThenFieldsSetCorrectly(t *testing.T) { | ||
|
||
sourceIPAddress, err := ie.NewSourceIPAddress("2001:db8::/32") | ||
|
||
if err != nil { | ||
t.Fatalf("Error creating SourceIPAddress: %v", err) | ||
} | ||
|
||
if sourceIPAddress.IEtype != 192 { | ||
t.Errorf("Expected NodeID, got %d", sourceIPAddress.IEtype) | ||
} | ||
|
||
if sourceIPAddress.Length != 18 { | ||
t.Errorf("Expected NodeID length 17, got %d", sourceIPAddress.Length) | ||
} | ||
|
||
if sourceIPAddress.MPL != true { | ||
t.Errorf("Expected NodeID MPL true, got %v", sourceIPAddress.MPL) | ||
} | ||
|
||
if sourceIPAddress.V4 != false { | ||
t.Errorf("Expected NodeID V4 false, got %v", sourceIPAddress.V4) | ||
} | ||
|
||
if sourceIPAddress.V6 != true { | ||
t.Errorf("Expected NodeID V6 true, got %v", sourceIPAddress.V6) | ||
} | ||
|
||
if sourceIPAddress.MaskPrefixLength != 32 { | ||
t.Errorf("Expected NodeID MaskPrefixLength 32, got %d", sourceIPAddress.MaskPrefixLength) | ||
} | ||
} | ||
|
||
func TestGivenSerializedIPV4AddressWhenDeserializeThenFieldsSetCorrectly(t *testing.T) { | ||
|
||
sourceIPAddress, err := ie.NewSourceIPAddress("2.2.3.1/24") | ||
|
||
if err != nil { | ||
t.Fatalf("Error creating SourceIPAddress: %v", err) | ||
} | ||
|
||
serializedSourceIPAddress := sourceIPAddress.Serialize() | ||
|
||
deserializedSourceIPAddress, err := ie.DeserializeSourceIPAddress(192, 6, serializedSourceIPAddress[4:]) | ||
|
||
if err != nil { | ||
t.Fatalf("Error deserializing SourceIPAddress: %v", err) | ||
} | ||
|
||
if deserializedSourceIPAddress.IEtype != 192 { | ||
t.Errorf("Expected NodeID, got %d", deserializedSourceIPAddress.IEtype) | ||
} | ||
|
||
if deserializedSourceIPAddress.Length != 6 { | ||
t.Errorf("Expected NodeID length 5, got %d", deserializedSourceIPAddress.Length) | ||
} | ||
|
||
if deserializedSourceIPAddress.MPL != true { | ||
t.Errorf("Expected NodeID MPL true, got %v", deserializedSourceIPAddress.MPL) | ||
} | ||
|
||
if deserializedSourceIPAddress.V4 != true { | ||
t.Errorf("Expected NodeID V4 true, got %v", deserializedSourceIPAddress.V4) | ||
} | ||
|
||
if deserializedSourceIPAddress.V6 != false { | ||
t.Errorf("Expected NodeID V6 false, got %v", deserializedSourceIPAddress.V6) | ||
} | ||
|
||
} |
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.