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

Commit b0eaaae

Browse files
committed
feat: Adds PDI UE IP address
1 parent 4421c74 commit b0eaaae

File tree

4 files changed

+148
-5
lines changed

4 files changed

+148
-5
lines changed

ie/create_pdr_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func TestGivenCorrectParametersWhenNewCreatePDRThenFieldsSetCorrectly(t *testing
5353
t.Errorf("Expected CreatePDR Precedence %v, got %v", precedence, createPDR.Precedence)
5454
}
5555

56-
if createPDR.PDI != pdi {
57-
t.Errorf("Expected CreatePDR PDI %v, got %v", pdi, createPDR.PDI)
56+
if createPDR.PDI.SourceInterface != pdi.SourceInterface {
57+
t.Errorf("Expected CreatePDR PDI SourceInterface %v, got %v", pdi.SourceInterface, createPDR.PDI.SourceInterface)
5858
}
5959
}
6060

@@ -113,7 +113,7 @@ func TestGivenSerializedWhenDeserializeCreatePDRThenFieldsSetCorrectly(t *testin
113113
t.Errorf("Expected CreatePDR Precedence %v, got %v", precedence, deserialized.Precedence)
114114
}
115115

116-
if deserialized.PDI != pdi {
117-
t.Errorf("Expected CreatePDR PDI %v, got %v", pdi, deserialized.PDI)
116+
if deserialized.PDI.SourceInterface != pdi.SourceInterface {
117+
t.Errorf("Expected CreatePDR PDI SourceInterface %v, got %v", pdi.SourceInterface, deserialized.PDI.SourceInterface)
118118
}
119119
}

ie/ie.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
PDRIDIEType IEType = 56
2121
FSEIDIEType IEType = 57
2222
NodeIDIEType IEType = 60
23+
UEIPAddressIEType IEType = 93
2324
RecoveryTimeStampIEType IEType = 96
2425
NodeReportTypeIEType IEType = 101
2526
FARIDIEType IEType = 108

ie/pdi.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88

99
type PDI struct {
1010
Header Header
11-
SourceInterface SourceInterface
11+
SourceInterface SourceInterface // Mandatory
12+
UEIPAddress UEIPAddress // Optional
1213
}
1314

1415
func NewPDI(sourceInterface SourceInterface) (PDI, error) {

ie/ue_ip_address.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package ie
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"net"
7+
)
8+
9+
type UEIPAddress struct {
10+
Header Header
11+
IP6PL bool
12+
CHV6 bool
13+
CHV4 bool
14+
IPv6D bool
15+
SD bool
16+
V4 bool
17+
V6 bool
18+
IPv4Address []byte
19+
IPv6Address []byte
20+
IPv6PrefixDelegationBits uint8
21+
IPv6PrefixLength uint8
22+
}
23+
24+
type SourceDestination struct {
25+
Source bool
26+
Destination bool
27+
}
28+
29+
func NewUEIPAddress(ipv4CIDR string, ipv6CIDR string, sd SourceDestination, ipv6PrefixDelegationBits uint8) (UEIPAddress, error) {
30+
var ipv4Address, ipv6Address []byte
31+
var ipv6PrefixLength uint8
32+
var length uint16 = 1
33+
34+
if ipv4CIDR != "" {
35+
ip, _, err := net.ParseCIDR(ipv4CIDR)
36+
if err != nil {
37+
return UEIPAddress{}, err
38+
}
39+
ipv4Address = ip.To4()
40+
if ipv4Address == nil {
41+
return UEIPAddress{}, errors.New("invalid IPv4 address")
42+
}
43+
length += 4
44+
}
45+
46+
if ipv6CIDR != "" {
47+
ip, ipv6Network, err := net.ParseCIDR(ipv6CIDR)
48+
if err != nil {
49+
return UEIPAddress{}, err
50+
}
51+
ipv6Address = ip.To16()
52+
if ipv6Address == nil {
53+
return UEIPAddress{}, errors.New("invalid IPv6 address")
54+
}
55+
ones, _ := ipv6Network.Mask.Size()
56+
ipv6PrefixLength = uint8(ones)
57+
length += 18
58+
}
59+
60+
var sourceDestination bool
61+
if sd.Source {
62+
sourceDestination = false
63+
} else if sd.Destination {
64+
sourceDestination = true
65+
}
66+
67+
ieHeader := Header{
68+
Type: UEIPAddressIEType,
69+
Length: length,
70+
}
71+
72+
return UEIPAddress{
73+
Header: ieHeader,
74+
IP6PL: ipv6PrefixLength != 0,
75+
CHV6: ipv6Address == nil,
76+
CHV4: ipv4Address == nil,
77+
IPv6D: ipv6PrefixDelegationBits != 0,
78+
SD: sourceDestination,
79+
V4: ipv4Address != nil,
80+
V6: ipv6Address != nil,
81+
IPv4Address: ipv4Address,
82+
IPv6Address: ipv6Address,
83+
IPv6PrefixDelegationBits: ipv6PrefixDelegationBits,
84+
IPv6PrefixLength: ipv6PrefixLength,
85+
}, nil
86+
}
87+
88+
func (ueIPaddress *UEIPAddress) Serialize() []byte {
89+
buf := new(bytes.Buffer)
90+
91+
// Octets 1 to 4: Header
92+
buf.Write(ueIPaddress.Header.Serialize())
93+
94+
// Octet 5: Bit 1: V6, Bit 2: V4, Bit 3: S/D, Bit 4: IPv6D, Bit 5: CHV4, Bit 6: CHV6, Bit 7: IP6PL, Bit 8: Spare
95+
var octet5 byte
96+
if ueIPaddress.IP6PL {
97+
octet5 |= 1 << 7
98+
}
99+
if ueIPaddress.CHV6 {
100+
octet5 |= 1 << 6
101+
}
102+
if ueIPaddress.CHV4 {
103+
octet5 |= 1 << 5
104+
}
105+
if ueIPaddress.IPv6D {
106+
octet5 |= 1 << 4
107+
}
108+
if ueIPaddress.SD {
109+
octet5 |= 1 << 3
110+
}
111+
if ueIPaddress.V4 {
112+
octet5 |= 1 << 2
113+
}
114+
if ueIPaddress.V6 {
115+
octet5 |= 1 << 1
116+
}
117+
buf.WriteByte(octet5)
118+
119+
// Octet m to (m+3): IPv4 Address
120+
if ueIPaddress.V4 {
121+
buf.Write(ueIPaddress.IPv4Address)
122+
}
123+
124+
// Octet p to (p+15): IPv6 Address
125+
if ueIPaddress.V6 {
126+
buf.Write(ueIPaddress.IPv6Address)
127+
}
128+
129+
// Octet r: IPv6 Delegation Bits
130+
if ueIPaddress.IPv6D {
131+
buf.WriteByte(ueIPaddress.IPv6PrefixDelegationBits)
132+
}
133+
134+
// Octet s: IPv6 Prefix Length
135+
if ueIPaddress.IPv6D {
136+
buf.WriteByte(ueIPaddress.IPv6PrefixLength)
137+
}
138+
139+
return buf.Bytes()
140+
141+
}

0 commit comments

Comments
 (0)