|
| 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