Skip to content

Commit 684c397

Browse files
committed
parse vxlanid as hex too
1 parent b58270d commit 684c397

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

docker/network/driver.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package network
22

33
import (
44
"fmt"
5-
"strconv"
65

76
log "github.com/Sirupsen/logrus"
87
gphnet "github.com/docker/go-plugins-helpers/network"
98

109
"github.com/TrilliumIT/vxrouter"
1110
"github.com/TrilliumIT/vxrouter/docker/core"
11+
"github.com/TrilliumIT/vxrouter/vxlan"
1212
)
1313

1414
const (
@@ -75,18 +75,8 @@ func (d *Driver) CreateNetwork(r *gphnet.CreateNetworkRequest) error {
7575
return err
7676
}
7777

78-
vid, err := strconv.Atoi(vxlID.(string))
79-
if err != nil {
80-
d.log.WithError(err).WithField("vxlanid", vxlID.(string)).Errorf("failed to parse vxlanid")
81-
return err
82-
}
83-
if vid < 0 || vid > 16777215 {
84-
err = fmt.Errorf("vxlanid is out of range")
85-
d.log.WithField("vxlanid", vid).WithError(err).Error()
86-
return err
87-
}
88-
89-
return nil
78+
_, err := vxlan.ParseVxlanID(vxlID.(string))
79+
return err
9080
}
9181

9282
// AllocateNetwork is never called

vxlan/vxlan.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,19 @@ func parseIP(s string) (net.IP, error) {
3333
return r, err
3434
}
3535

36-
func parseInt(v string) (int, error) {
37-
i, err := strconv.ParseInt(v, 0, 32)
38-
return int(i), err
36+
// ParseVxlanID converts a string to a int to validate a vxlan id
37+
func ParseVxlanID(v string) (int, error) {
38+
vid, err := strconv.Atoi(v)
39+
if err != nil {
40+
var v64 int64
41+
v64, err = strconv.ParseInt(v, 0, 0)
42+
vid = int(v64)
43+
}
44+
45+
if vid < 0 || vid > 16777215 {
46+
err = fmt.Errorf("vxlanid is out of range")
47+
}
48+
return vid, err
3949
}
4050

4151
func linkIndexByName(name string) (int, error) {
@@ -78,7 +88,7 @@ func NewVxlan(vxlanName string, opts map[string]string) (*Vxlan, error) {
7888
case "vxlantxqlen":
7989
nl.LinkAttrs.TxQLen, err = strconv.Atoi(v)
8090
case "vxlanid":
81-
nl.VxlanId, err = parseInt(v)
91+
nl.VxlanId, err = ParseVxlanID(v)
8292
case "vtepdev":
8393
nl.VtepDevIndex, err = linkIndexByName(v)
8494
case "srcaddr":

0 commit comments

Comments
 (0)