-
Notifications
You must be signed in to change notification settings - Fork 37
/
ip_helpers.go
112 lines (99 loc) · 2.77 KB
/
ip_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Code generated by vpplink DO NOT EDIT.
// Copyright (C) 2019 Cisco Systems Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package generated
import (
"net"
types "github.com/calico-vpp/vpplink/api/v0"
"github.com/projectcalico/vpp-dataplane/v3/vpplink/generated/bindings/ip_types"
)
func toVppIPProto(proto types.IPProto) ip_types.IPProto {
switch proto {
case types.UDP:
return ip_types.IP_API_PROTO_UDP
case types.TCP:
return ip_types.IP_API_PROTO_TCP
case types.SCTP:
return ip_types.IP_API_PROTO_SCTP
case types.ICMP:
return ip_types.IP_API_PROTO_ICMP
case types.ICMP6:
return ip_types.IP_API_PROTO_ICMP6
}
return ip_types.IP_API_PROTO_RESERVED
}
// Make sure you really call this with an IPv4 address...
func toVppIP4Address(addr net.IP) ip_types.IP4Address {
ip := [4]uint8{}
copy(ip[:], addr.To4())
return ip
}
func toVppIP6Address(addr net.IP) ip_types.IP6Address {
ip := [16]uint8{}
copy(ip[:], addr)
return ip
}
func toVppAddress(addr net.IP) ip_types.Address {
return ip_types.NewAddress(addr)
}
func fromVppIpAddressUnion(Un ip_types.AddressUnion, isv6 bool) net.IP {
if isv6 {
a := Un.GetIP6()
return net.IP(a[:])
} else {
a := Un.GetIP4()
return net.IP(a[:])
}
}
func toVppAddressWithPrefix(prefix *net.IPNet) ip_types.AddressWithPrefix {
return ip_types.AddressWithPrefix(toVppPrefix(prefix))
}
func toVppPrefix(prefix *net.IPNet) ip_types.Prefix {
len, _ := prefix.Mask.Size()
r := ip_types.Prefix{
Address: toVppAddress(prefix.IP),
Len: uint8(len),
}
return r
}
func toVppIp4WithPrefix(prefix *net.IPNet) ip_types.IP4AddressWithPrefix {
return ip_types.IP4AddressWithPrefix(toVppIP4Prefix(prefix))
}
func toVppIP4Prefix(prefix *net.IPNet) ip_types.IP4Prefix {
len, _ := prefix.Mask.Size()
r := ip_types.IP4Prefix{
Address: toVppIP4Address(prefix.IP),
Len: uint8(len),
}
return r
}
func ToVppPrefix(prefix *net.IPNet) ip_types.Prefix {
len, _ := prefix.Mask.Size()
r := ip_types.Prefix{
Address: ip_types.NewAddress(prefix.IP),
Len: uint8(len),
}
return r
}
func FromVppPrefix(prefix ip_types.Prefix) *net.IPNet {
addressSize := 32
if prefix.Address.Af == ip_types.ADDRESS_IP6 {
addressSize = 128
}
return &net.IPNet{
IP: prefix.Address.ToIP(),
Mask: net.CIDRMask(int(prefix.Len), addressSize),
}
}