Binary API generator improvements
[govpp.git] / examples / binapi / ip_types / ip_types.ba.go
1 // Code generated by GoVPP's binapi-generator. DO NOT EDIT.
2 // versions:
3 //  binapi-generator: v0.4.0-dev
4 //  VPP:              20.05-release
5 // source: /usr/share/vpp/api/core/ip_types.api.json
6
7 /*
8 Package ip_types contains generated code for VPP API file ip_types.api (3.0.0).
9
10 It consists of:
11           5 aliases
12           4 enums
13           6 types
14           1 union
15 */
16 package ip_types
17
18 import (
19         "bytes"
20         "context"
21         "encoding/binary"
22         "fmt"
23         "io"
24         "math"
25         "net"
26         "strconv"
27         "strings"
28
29         api "git.fd.io/govpp.git/api"
30         codec "git.fd.io/govpp.git/codec"
31         struc "github.com/lunixbochs/struc"
32 )
33
34 // This is a compile-time assertion to ensure that this generated file
35 // is compatible with the GoVPP api package it is being compiled against.
36 // A compilation error at this line likely means your copy of the
37 // GoVPP api package needs to be updated.
38 const _ = api.GoVppAPIPackageIsVersion2 // please upgrade the GoVPP api package
39
40 const (
41         // ModuleName is the name of this module.
42         ModuleName = "ip_types"
43         // APIVersion is the API version of this module.
44         APIVersion = "3.0.0"
45         // VersionCrc is the CRC of this module.
46         VersionCrc = 0x9cb1c2e2
47 )
48
49 // AddressFamily represents VPP binary API enum 'address_family'.
50 type AddressFamily uint8
51
52 const (
53         ADDRESS_IP4 AddressFamily = 0
54         ADDRESS_IP6 AddressFamily = 1
55 )
56
57 var (
58         AddressFamily_name = map[uint8]string{
59                 0: "ADDRESS_IP4",
60                 1: "ADDRESS_IP6",
61         }
62         AddressFamily_value = map[string]uint8{
63                 "ADDRESS_IP4": 0,
64                 "ADDRESS_IP6": 1,
65         }
66 )
67
68 func (x AddressFamily) String() string {
69         s, ok := AddressFamily_name[uint8(x)]
70         if ok {
71                 return s
72         }
73         return "AddressFamily(" + strconv.Itoa(int(x)) + ")"
74 }
75
76 // IPDscp represents VPP binary API enum 'ip_dscp'.
77 type IPDscp uint8
78
79 const (
80         IP_API_DSCP_CS0  IPDscp = 0
81         IP_API_DSCP_CS1  IPDscp = 8
82         IP_API_DSCP_AF11 IPDscp = 10
83         IP_API_DSCP_AF12 IPDscp = 12
84         IP_API_DSCP_AF13 IPDscp = 14
85         IP_API_DSCP_CS2  IPDscp = 16
86         IP_API_DSCP_AF21 IPDscp = 18
87         IP_API_DSCP_AF22 IPDscp = 20
88         IP_API_DSCP_AF23 IPDscp = 22
89         IP_API_DSCP_CS3  IPDscp = 24
90         IP_API_DSCP_AF31 IPDscp = 26
91         IP_API_DSCP_AF32 IPDscp = 28
92         IP_API_DSCP_AF33 IPDscp = 30
93         IP_API_DSCP_CS4  IPDscp = 32
94         IP_API_DSCP_AF41 IPDscp = 34
95         IP_API_DSCP_AF42 IPDscp = 36
96         IP_API_DSCP_AF43 IPDscp = 38
97         IP_API_DSCP_CS5  IPDscp = 40
98         IP_API_DSCP_EF   IPDscp = 46
99         IP_API_DSCP_CS6  IPDscp = 48
100         IP_API_DSCP_CS7  IPDscp = 50
101 )
102
103 var (
104         IPDscp_name = map[uint8]string{
105                 0:  "IP_API_DSCP_CS0",
106                 8:  "IP_API_DSCP_CS1",
107                 10: "IP_API_DSCP_AF11",
108                 12: "IP_API_DSCP_AF12",
109                 14: "IP_API_DSCP_AF13",
110                 16: "IP_API_DSCP_CS2",
111                 18: "IP_API_DSCP_AF21",
112                 20: "IP_API_DSCP_AF22",
113                 22: "IP_API_DSCP_AF23",
114                 24: "IP_API_DSCP_CS3",
115                 26: "IP_API_DSCP_AF31",
116                 28: "IP_API_DSCP_AF32",
117                 30: "IP_API_DSCP_AF33",
118                 32: "IP_API_DSCP_CS4",
119                 34: "IP_API_DSCP_AF41",
120                 36: "IP_API_DSCP_AF42",
121                 38: "IP_API_DSCP_AF43",
122                 40: "IP_API_DSCP_CS5",
123                 46: "IP_API_DSCP_EF",
124                 48: "IP_API_DSCP_CS6",
125                 50: "IP_API_DSCP_CS7",
126         }
127         IPDscp_value = map[string]uint8{
128                 "IP_API_DSCP_CS0":  0,
129                 "IP_API_DSCP_CS1":  8,
130                 "IP_API_DSCP_AF11": 10,
131                 "IP_API_DSCP_AF12": 12,
132                 "IP_API_DSCP_AF13": 14,
133                 "IP_API_DSCP_CS2":  16,
134                 "IP_API_DSCP_AF21": 18,
135                 "IP_API_DSCP_AF22": 20,
136                 "IP_API_DSCP_AF23": 22,
137                 "IP_API_DSCP_CS3":  24,
138                 "IP_API_DSCP_AF31": 26,
139                 "IP_API_DSCP_AF32": 28,
140                 "IP_API_DSCP_AF33": 30,
141                 "IP_API_DSCP_CS4":  32,
142                 "IP_API_DSCP_AF41": 34,
143                 "IP_API_DSCP_AF42": 36,
144                 "IP_API_DSCP_AF43": 38,
145                 "IP_API_DSCP_CS5":  40,
146                 "IP_API_DSCP_EF":   46,
147                 "IP_API_DSCP_CS6":  48,
148                 "IP_API_DSCP_CS7":  50,
149         }
150 )
151
152 func (x IPDscp) String() string {
153         s, ok := IPDscp_name[uint8(x)]
154         if ok {
155                 return s
156         }
157         return "IPDscp(" + strconv.Itoa(int(x)) + ")"
158 }
159
160 // IPEcn represents VPP binary API enum 'ip_ecn'.
161 type IPEcn uint8
162
163 const (
164         IP_API_ECN_NONE IPEcn = 0
165         IP_API_ECN_ECT0 IPEcn = 1
166         IP_API_ECN_ECT1 IPEcn = 2
167         IP_API_ECN_CE   IPEcn = 3
168 )
169
170 var (
171         IPEcn_name = map[uint8]string{
172                 0: "IP_API_ECN_NONE",
173                 1: "IP_API_ECN_ECT0",
174                 2: "IP_API_ECN_ECT1",
175                 3: "IP_API_ECN_CE",
176         }
177         IPEcn_value = map[string]uint8{
178                 "IP_API_ECN_NONE": 0,
179                 "IP_API_ECN_ECT0": 1,
180                 "IP_API_ECN_ECT1": 2,
181                 "IP_API_ECN_CE":   3,
182         }
183 )
184
185 func (x IPEcn) String() string {
186         s, ok := IPEcn_name[uint8(x)]
187         if ok {
188                 return s
189         }
190         return "IPEcn(" + strconv.Itoa(int(x)) + ")"
191 }
192
193 // IPProto represents VPP binary API enum 'ip_proto'.
194 type IPProto uint8
195
196 const (
197         IP_API_PROTO_HOPOPT   IPProto = 0
198         IP_API_PROTO_ICMP     IPProto = 1
199         IP_API_PROTO_IGMP     IPProto = 2
200         IP_API_PROTO_TCP      IPProto = 6
201         IP_API_PROTO_UDP      IPProto = 17
202         IP_API_PROTO_GRE      IPProto = 47
203         IP_API_PROTO_ESP      IPProto = 50
204         IP_API_PROTO_AH       IPProto = 51
205         IP_API_PROTO_ICMP6    IPProto = 58
206         IP_API_PROTO_EIGRP    IPProto = 88
207         IP_API_PROTO_OSPF     IPProto = 89
208         IP_API_PROTO_SCTP     IPProto = 132
209         IP_API_PROTO_RESERVED IPProto = 255
210 )
211
212 var (
213         IPProto_name = map[uint8]string{
214                 0:   "IP_API_PROTO_HOPOPT",
215                 1:   "IP_API_PROTO_ICMP",
216                 2:   "IP_API_PROTO_IGMP",
217                 6:   "IP_API_PROTO_TCP",
218                 17:  "IP_API_PROTO_UDP",
219                 47:  "IP_API_PROTO_GRE",
220                 50:  "IP_API_PROTO_ESP",
221                 51:  "IP_API_PROTO_AH",
222                 58:  "IP_API_PROTO_ICMP6",
223                 88:  "IP_API_PROTO_EIGRP",
224                 89:  "IP_API_PROTO_OSPF",
225                 132: "IP_API_PROTO_SCTP",
226                 255: "IP_API_PROTO_RESERVED",
227         }
228         IPProto_value = map[string]uint8{
229                 "IP_API_PROTO_HOPOPT":   0,
230                 "IP_API_PROTO_ICMP":     1,
231                 "IP_API_PROTO_IGMP":     2,
232                 "IP_API_PROTO_TCP":      6,
233                 "IP_API_PROTO_UDP":      17,
234                 "IP_API_PROTO_GRE":      47,
235                 "IP_API_PROTO_ESP":      50,
236                 "IP_API_PROTO_AH":       51,
237                 "IP_API_PROTO_ICMP6":    58,
238                 "IP_API_PROTO_EIGRP":    88,
239                 "IP_API_PROTO_OSPF":     89,
240                 "IP_API_PROTO_SCTP":     132,
241                 "IP_API_PROTO_RESERVED": 255,
242         }
243 )
244
245 func (x IPProto) String() string {
246         s, ok := IPProto_name[uint8(x)]
247         if ok {
248                 return s
249         }
250         return "IPProto(" + strconv.Itoa(int(x)) + ")"
251 }
252
253 // AddressWithPrefix represents VPP binary API alias 'address_with_prefix'.
254 type AddressWithPrefix Prefix
255
256 // IP4Address represents VPP binary API alias 'ip4_address'.
257 type IP4Address [4]uint8
258
259 // IP4AddressWithPrefix represents VPP binary API alias 'ip4_address_with_prefix'.
260 type IP4AddressWithPrefix IP4Prefix
261
262 // IP6Address represents VPP binary API alias 'ip6_address'.
263 type IP6Address [16]uint8
264
265 // IP6AddressWithPrefix represents VPP binary API alias 'ip6_address_with_prefix'.
266 type IP6AddressWithPrefix IP6Prefix
267
268 // Address represents VPP binary API type 'address'.
269 type Address struct {
270         Af AddressFamily `binapi:"address_family,name=af" json:"af,omitempty"`
271         Un AddressUnion  `binapi:"address_union,name=un" json:"un,omitempty"`
272 }
273
274 func (*Address) GetTypeName() string { return "address" }
275
276 func ParseAddress(ip string) (Address, error) {
277         var address Address
278         netIP := net.ParseIP(ip)
279         if netIP == nil {
280                 return address, fmt.Errorf("invalid address: %s", ip)
281         }
282         if ip4 := netIP.To4(); ip4 == nil {
283                 address.Af = ADDRESS_IP6
284                 var ip6addr IP6Address
285                 copy(ip6addr[:], netIP.To16())
286                 address.Un.SetIP6(ip6addr)
287         } else {
288                 address.Af = ADDRESS_IP4
289                 var ip4addr IP4Address
290                 copy(ip4addr[:], netIP.To4())
291                 address.Un.SetIP4(ip4addr)
292         }
293         return address, nil
294 }
295
296 func (a *Address) ToString() string {
297         var ip string
298         if a.Af == ADDRESS_IP6 {
299                 ip6Address := a.Un.GetIP6()
300                 ip = net.IP(ip6Address[:]).To16().String()
301         } else {
302                 ip4Address := a.Un.GetIP4()
303                 ip = net.IP(ip4Address[:]).To4().String()
304         }
305         return ip
306 }
307
308 // IP4Prefix represents VPP binary API type 'ip4_prefix'.
309 type IP4Prefix struct {
310         Address IP4Address `binapi:"ip4_address,name=address" json:"address,omitempty"`
311         Len     uint8      `binapi:"u8,name=len" json:"len,omitempty"`
312 }
313
314 func (*IP4Prefix) GetTypeName() string { return "ip4_prefix" }
315
316 // IP6Prefix represents VPP binary API type 'ip6_prefix'.
317 type IP6Prefix struct {
318         Address IP6Address `binapi:"ip6_address,name=address" json:"address,omitempty"`
319         Len     uint8      `binapi:"u8,name=len" json:"len,omitempty"`
320 }
321
322 func (*IP6Prefix) GetTypeName() string { return "ip6_prefix" }
323
324 // Mprefix represents VPP binary API type 'mprefix'.
325 type Mprefix struct {
326         Af               AddressFamily `binapi:"address_family,name=af" json:"af,omitempty"`
327         GrpAddressLength uint16        `binapi:"u16,name=grp_address_length" json:"grp_address_length,omitempty"`
328         GrpAddress       AddressUnion  `binapi:"address_union,name=grp_address" json:"grp_address,omitempty"`
329         SrcAddress       AddressUnion  `binapi:"address_union,name=src_address" json:"src_address,omitempty"`
330 }
331
332 func (*Mprefix) GetTypeName() string { return "mprefix" }
333
334 // Prefix represents VPP binary API type 'prefix'.
335 type Prefix struct {
336         Address Address `binapi:"address,name=address" json:"address,omitempty"`
337         Len     uint8   `binapi:"u8,name=len" json:"len,omitempty"`
338 }
339
340 func (*Prefix) GetTypeName() string { return "prefix" }
341
342 func ParsePrefix(ip string) (prefix Prefix, err error) {
343         hasPrefix := strings.Contains(ip, "/")
344         if hasPrefix {
345                 netIP, network, err := net.ParseCIDR(ip)
346                 if err != nil {
347                         return Prefix{}, fmt.Errorf("invalid IP %s: %v", ip, err)
348                 }
349                 maskSize, _ := network.Mask.Size()
350                 prefix.Len = byte(maskSize)
351                 prefix.Address, err = ParseAddress(netIP.String())
352                 if err != nil {
353                         return Prefix{}, fmt.Errorf("invalid IP %s: %v", ip, err)
354                 }
355         } else {
356                 netIP := net.ParseIP(ip)
357                 defaultMaskSize, _ := net.CIDRMask(32, 32).Size()
358                 if netIP.To4() == nil {
359                         defaultMaskSize, _ = net.CIDRMask(128, 128).Size()
360                 }
361                 prefix.Len = byte(defaultMaskSize)
362                 prefix.Address, err = ParseAddress(netIP.String())
363                 if err != nil {
364                         return Prefix{}, fmt.Errorf("invalid IP %s: %v", ip, err)
365                 }
366         }
367         return prefix, nil
368 }
369
370 func (p *Prefix) ToString() string {
371         ip := p.Address.ToString()
372         return ip + "/" + strconv.Itoa(int(p.Len))
373 }
374
375 // PrefixMatcher represents VPP binary API type 'prefix_matcher'.
376 type PrefixMatcher struct {
377         Le uint8 `binapi:"u8,name=le" json:"le,omitempty"`
378         Ge uint8 `binapi:"u8,name=ge" json:"ge,omitempty"`
379 }
380
381 func (*PrefixMatcher) GetTypeName() string { return "prefix_matcher" }
382
383 // AddressUnion represents VPP binary API union 'address_union'.
384 type AddressUnion struct {
385         XXX_UnionData [16]byte
386 }
387
388 func (*AddressUnion) GetTypeName() string { return "address_union" }
389
390 func AddressUnionIP4(a IP4Address) (u AddressUnion) {
391         u.SetIP4(a)
392         return
393 }
394 func (u *AddressUnion) SetIP4(a IP4Address) {
395         var b = new(bytes.Buffer)
396         if err := struc.Pack(b, &a); err != nil {
397                 return
398         }
399         copy(u.XXX_UnionData[:], b.Bytes())
400 }
401 func (u *AddressUnion) GetIP4() (a IP4Address) {
402         var b = bytes.NewReader(u.XXX_UnionData[:])
403         struc.Unpack(b, &a)
404         return
405 }
406
407 func AddressUnionIP6(a IP6Address) (u AddressUnion) {
408         u.SetIP6(a)
409         return
410 }
411 func (u *AddressUnion) SetIP6(a IP6Address) {
412         var b = new(bytes.Buffer)
413         if err := struc.Pack(b, &a); err != nil {
414                 return
415         }
416         copy(u.XXX_UnionData[:], b.Bytes())
417 }
418 func (u *AddressUnion) GetIP6() (a IP6Address) {
419         var b = bytes.NewReader(u.XXX_UnionData[:])
420         struc.Unpack(b, &a)
421         return
422 }
423
424 // Reference imports to suppress errors if they are not otherwise used.
425 var _ = api.RegisterMessage
426 var _ = codec.DecodeString
427 var _ = bytes.NewBuffer
428 var _ = context.Background
429 var _ = io.Copy
430 var _ = strconv.Itoa
431 var _ = strings.Contains
432 var _ = struc.Pack
433 var _ = binary.BigEndian
434 var _ = math.Float32bits
435 var _ = net.ParseIP
436 var _ = fmt.Errorf