package main
import (
- "bytes"
"fmt"
- "log"
- "net"
-
+ "git.fd.io/govpp.git/codec"
+ "git.fd.io/govpp.git/examples/binapi/interfaces"
"git.fd.io/govpp.git/examples/binapi/ip"
"git.fd.io/govpp.git/examples/binapi/ip_types"
-
- "github.com/lunixbochs/struc"
+ "log"
+ "reflect"
)
+func init() {
+ log.SetFlags(0)
+}
+
func main() {
- encodingExample()
- usageExample()
+ constructExample()
+
+ encodingExampleIP()
+
+ // convert IP from string form into Address type containing union
+ convertIP("10.10.1.1")
+ convertIP("ff80::1")
+
+ // convert IP from string form into Prefix type
+ convertIPPrefix("20.10.1.1/24")
+ convertIPPrefix("21.10.1.1")
+ convertIPPrefix("ff90::1/64")
+ convertIPPrefix("ff91::1")
+
+ // convert MAC address from string into MacAddress
+ convertToMacAddress("00:10:ab:4f:00:01")
}
-func encodingExample() {
- // create union with IPv4 address
- var unionIP4 ip.AddressUnion
- unionIP4.SetIP4(ip.IP4Address{192, 168, 1, 10})
+func constructExample() {
+ var union ip_types.AddressUnion
+
+ // create AddressUnion with AdressUnionXXX constructors
+ union = ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 1, 10})
+ union = ip_types.AddressUnionIP6(ip_types.IP6Address{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02})
- // use it in the Address type
- addr := &ip.Address{
- Af: ip_types.ADDRESS_IP4,
- Un: ip_types.AddressUnionIP4(ip.IP4Address{192, 168, 1, 10}),
+ // set AddressUnion with SetXXX methods
+ union.SetIP4(ip_types.IP4Address{192, 168, 1, 10})
+ union.SetIP6(ip_types.IP6Address{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02})
+}
+
+func encodingExampleIP() {
+ var c = codec.DefaultCodec
+
+ // encode this message
+ var msg = ip.IPPuntRedirect{
+ Punt: ip.PuntRedirect{
+ Nh: ip_types.Address{
+ Af: ip_types.ADDRESS_IP4,
+ Un: ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 1, 10}),
+ },
+ },
+ IsAdd: true,
}
- log.Printf("encoding union IPv4: %v", addr.Un.GetIP4())
+ log.Printf("encoding message: %+v", msg)
- // encode the address with union
- data := encode(addr)
- // decode the address with union
- addr2 := decode(data)
+ b, err := c.EncodeMsg(&msg, 1)
+ if err != nil {
+ log.Fatal(err)
+ }
- log.Printf("decoded union IPv4: %v", addr2.Un.GetIP4())
-}
+ // decode into this message
+ var msg2 ip.IPPuntRedirect
+ if err := c.DecodeMsg(b, &msg2); err != nil {
+ log.Fatal(err)
+ }
+ log.Printf("decoded message: %+v", msg2)
-func encode(addr *ip.Address) []byte {
- log.Printf("encoding address: %#v", addr)
- buf := new(bytes.Buffer)
- if err := struc.Pack(buf, addr); err != nil {
- panic(err)
+ // compare the messages
+ if !reflect.DeepEqual(msg, msg2) {
+ log.Fatal("messages are not equal")
}
- return buf.Bytes()
}
-func decode(data []byte) *ip.Address {
- addr := new(ip.Address)
- buf := bytes.NewReader(data)
- if err := struc.Unpack(buf, addr); err != nil {
- panic(err)
+func convertIP(ip string) {
+ addr, err := ip_types.ParseAddress(ip)
+ if err != nil {
+ log.Printf("error converting IP to Address: %v", err)
+ return
}
- log.Printf("decoded address: %#v", addr)
- return addr
+ fmt.Printf("converted IP %q to: %+v\n", ip, addr)
+
+ ipStr := addr.ToString()
+ fmt.Printf("Address converted back to string IP %+v to: %q\n", addr, ipStr)
}
-func usageExample() {
- var convAddr = func(ip string) {
- addr, err := ipToAddress(ip)
- if err != nil {
- log.Printf("converting ip %q failed: %v", ip, err)
- }
- fmt.Printf("% 0X\n", addr)
+func convertIPPrefix(ip string) {
+ prefix, err := ip_types.ParsePrefix(ip)
+ if err != nil {
+ log.Printf("error converting prefix to IP4Prefix: %v", err)
+ return
}
+ fmt.Printf("converted prefix %q to: %+v\n", ip, prefix)
- convAddr("10.10.10.10")
- convAddr("::1")
- convAddr("")
+ ipStr := prefix.ToString()
+ fmt.Printf("IP4Prefix converted back to string prefix %+v to: %q\n", prefix, ipStr)
}
-func ipToAddress(ipstr string) (addr ip.Address, err error) {
- netIP := net.ParseIP(ipstr)
- if netIP == nil {
- return ip.Address{}, fmt.Errorf("invalid IP: %q", ipstr)
- }
- if ip4 := netIP.To4(); ip4 == nil {
- addr.Af = ip_types.ADDRESS_IP6
- var ip6addr ip.IP6Address
- copy(ip6addr[:], netIP.To16())
- addr.Un.SetIP6(ip6addr)
- } else {
- addr.Af = ip_types.ADDRESS_IP4
- var ip4addr ip.IP4Address
- copy(ip4addr[:], ip4)
- addr.Un.SetIP4(ip4addr)
+func convertToMacAddress(mac string) {
+ parsedMac, err := interfaces.ParseMAC(mac)
+ if err != nil {
+ log.Printf("error converting MAC to MacAddress: %v", err)
+ return
}
- return
-}
+ fmt.Printf("converted prefix %q to: %+v\n", mac, parsedMac)
+
+ macStr := parsedMac.ToString()
+ fmt.Printf("MacAddress converted back to string %+v to: %q\n", parsedMac, macStr)
+}
\ No newline at end of file