From 2d28d16c9239ed3c7af118266bb28a3a6a2b0edc Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Tue, 16 Sep 2014 17:33:44 -0700 Subject: [PATCH] network: add support for CIDR addresses Debian routes OnMetal is changing their template from: `route add -net 1.2.3.0 netmask 255.255.255.0 gw 10.1.2.1 || true` to: `route add -net 1.2.3.0/24 gw 10.1.2.1 || true` --- network/stanza.go | 6 +++- network/stanza_test.go | 70 +++++++++++++++++++++++++++++------------- 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/network/stanza.go b/network/stanza.go index 3c7334e..3799f89 100644 --- a/network/stanza.go +++ b/network/stanza.go @@ -236,7 +236,11 @@ func parseInterfaceStanza(attributes []string, options []string) (*stanzaInterfa for i, field := range fields[:len(fields)-1] { switch field { case "-net": - route.destination.IP = net.ParseIP(fields[i+1]) + if _, dst, err := net.ParseCIDR(fields[i+1]); err == nil { + route.destination = *dst + } else { + route.destination.IP = net.ParseIP(fields[i+1]) + } case "netmask": route.destination.Mask = net.IPMask(net.ParseIP(fields[i+1]).To4()) case "gw": diff --git a/network/stanza_test.go b/network/stanza_test.go index b54e95c..cc1e7f3 100644 --- a/network/stanza_test.go +++ b/network/stanza_test.go @@ -285,31 +285,57 @@ func TestBadParseInterfaceStanzasStaticPostUp(t *testing.T) { } func TestParseInterfaceStanzaStaticPostUp(t *testing.T) { - options := []string{ - "address 192.168.1.100", - "netmask 255.255.255.0", - "post-up route add gw 192.168.1.1 -net 192.168.1.0 netmask 255.255.255.0", - } - expect := []route{ + for _, tt := range []struct { + options []string + expect []route + }{ { - destination: net.IPNet{ - IP: net.IPv4(192, 168, 1, 0), - Mask: net.IPv4Mask(255, 255, 255, 0), + options: []string{ + "address 192.168.1.100", + "netmask 255.255.255.0", + "post-up route add gw 192.168.1.1 -net 192.168.1.0 netmask 255.255.255.0", + }, + expect: []route{ + { + destination: net.IPNet{ + IP: net.IPv4(192, 168, 1, 0), + Mask: net.IPv4Mask(255, 255, 255, 0), + }, + gateway: net.IPv4(192, 168, 1, 1), + }, }, - gateway: net.IPv4(192, 168, 1, 1), }, - } - - iface, err := parseInterfaceStanza([]string{"eth", "inet", "static"}, options) - if err != nil { - t.FailNow() - } - static, ok := iface.configMethod.(configMethodStatic) - if !ok { - t.FailNow() - } - if !reflect.DeepEqual(static.routes, expect) { - t.FailNow() + { + options: []string{ + "address 192.168.1.100", + "netmask 255.255.255.0", + "post-up route add gw 192.168.1.1 -net 192.168.1.0/24 || true", + }, + expect: []route{ + { + destination: func() net.IPNet { + if _, net, err := net.ParseCIDR("192.168.1.0/24"); err == nil { + return *net + } else { + panic(err) + } + }(), + gateway: net.IPv4(192, 168, 1, 1), + }, + }, + }, + } { + iface, err := parseInterfaceStanza([]string{"eth", "inet", "static"}, tt.options) + if err != nil { + t.Fatalf("bad error (%+v): want nil, got %s\n", tt, err) + } + static, ok := iface.configMethod.(configMethodStatic) + if !ok { + t.Fatalf("bad config method (%+v): want configMethodStatic, got %T\n", tt, iface.configMethod) + } + if !reflect.DeepEqual(static.routes, tt.expect) { + t.Fatalf("bad routes (%+v): want %#v, got %#v\n", tt, tt.expect, static.routes) + } } }