Browse code

netutils: minor cleanups

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2020/11/01 01:57:38
Showing 2 changed files
... ...
@@ -72,8 +72,7 @@ func GetIfaceAddr(name string) (net.Addr, []net.Addr, error) {
72 72
 	if err != nil {
73 73
 		return nil, nil, err
74 74
 	}
75
-	var addrs4 []net.Addr
76
-	var addrs6 []net.Addr
75
+	var addrs4, addrs6 []net.Addr
77 76
 	for _, addr := range addrs {
78 77
 		ip := (addr.(*net.IPNet)).IP
79 78
 		if ip4 := ip.To4(); ip4 != nil {
... ...
@@ -84,7 +83,7 @@ func GetIfaceAddr(name string) (net.Addr, []net.Addr, error) {
84 84
 	}
85 85
 	switch {
86 86
 	case len(addrs4) == 0:
87
-		return nil, nil, fmt.Errorf("Interface %v has no IPv4 addresses", name)
87
+		return nil, nil, fmt.Errorf("interface %v has no IPv4 addresses", name)
88 88
 	case len(addrs4) > 1:
89 89
 		fmt.Printf("Interface %v has more than 1 IPv4 address. Defaulting to using %v\n",
90 90
 			name, (addrs4[0].(*net.IPNet)).IP)
... ...
@@ -173,9 +172,9 @@ func ParseAlias(val string) (string, string, error) {
173 173
 	if val == "" {
174 174
 		return "", "", errors.New("empty string specified for alias")
175 175
 	}
176
-	arr := strings.Split(val, ":")
176
+	arr := strings.SplitN(val, ":", 3)
177 177
 	if len(arr) > 2 {
178
-		return "", "", fmt.Errorf("bad format for alias: %s", val)
178
+		return "", "", errors.New("bad format for alias: " + val)
179 179
 	}
180 180
 	if len(arr) == 1 {
181 181
 		return val, val, nil
... ...
@@ -69,10 +69,7 @@ func GenerateIfaceName(nlh *netlink.Handle, prefix string, len int) (string, err
69 69
 // list the first IPv4 address which does not conflict with other
70 70
 // interfaces on the system.
71 71
 func ElectInterfaceAddresses(name string) ([]*net.IPNet, []*net.IPNet, error) {
72
-	var (
73
-		v4Nets []*net.IPNet
74
-		v6Nets []*net.IPNet
75
-	)
72
+	var v4Nets, v6Nets []*net.IPNet
76 73
 
77 74
 	defer osl.InitOSContext()()
78 75