mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Fix parseIP error when parseIP before get AddressFamily
Signed-off-by: Tom Zhao <zlwangel@gmail.com>
This commit is contained in:
parent
65ade31ba1
commit
7ab62b791f
2 changed files with 145 additions and 123 deletions
|
@ -133,24 +133,41 @@ func TestService(t *testing.T) {
|
||||||
|
|
||||||
for _, protocol := range protocols {
|
for _, protocol := range protocols {
|
||||||
for _, schedMethod := range schedMethods {
|
for _, schedMethod := range schedMethods {
|
||||||
|
testDatas := []struct {
|
||||||
s := Service{
|
AddressFamily uint16
|
||||||
|
IP string
|
||||||
|
Netmask uint32
|
||||||
|
}{
|
||||||
|
{
|
||||||
AddressFamily: nl.FAMILY_V4,
|
AddressFamily: nl.FAMILY_V4,
|
||||||
|
IP: "1.2.3.4",
|
||||||
|
Netmask: 0xFFFFFFFF,
|
||||||
|
}, {
|
||||||
|
AddressFamily: nl.FAMILY_V6,
|
||||||
|
IP: "2001:db8:3c4d:15::1a00",
|
||||||
|
Netmask: 128,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, td := range testDatas {
|
||||||
|
s := Service{
|
||||||
|
AddressFamily: td.AddressFamily,
|
||||||
SchedName: schedMethod,
|
SchedName: schedMethod,
|
||||||
}
|
}
|
||||||
|
|
||||||
switch protocol {
|
switch protocol {
|
||||||
case "FWM":
|
case "FWM":
|
||||||
s.FWMark = 1234
|
s.FWMark = 1234
|
||||||
|
s.Netmask = td.Netmask
|
||||||
case "TCP":
|
case "TCP":
|
||||||
s.Protocol = unix.IPPROTO_TCP
|
s.Protocol = unix.IPPROTO_TCP
|
||||||
s.Port = 80
|
s.Port = 80
|
||||||
s.Address = net.ParseIP("1.2.3.4")
|
s.Address = net.ParseIP(td.IP)
|
||||||
s.Netmask = 0xFFFFFFFF
|
s.Netmask = td.Netmask
|
||||||
case "UDP":
|
case "UDP":
|
||||||
s.Protocol = unix.IPPROTO_UDP
|
s.Protocol = unix.IPPROTO_UDP
|
||||||
s.Port = 53
|
s.Port = 53
|
||||||
s.Address = net.ParseIP("2.3.4.5")
|
s.Address = net.ParseIP(td.IP)
|
||||||
|
s.Netmask = td.Netmask
|
||||||
}
|
}
|
||||||
|
|
||||||
err := i.NewService(&s)
|
err := i.NewService(&s)
|
||||||
|
@ -178,6 +195,7 @@ func TestService(t *testing.T) {
|
||||||
checkService(t, i, &s, false)
|
checkService(t, i, &s, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
svcs := []Service{
|
svcs := []Service{
|
||||||
{
|
{
|
||||||
|
@ -251,24 +269,44 @@ func TestDestination(t *testing.T) {
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
|
||||||
for _, protocol := range protocols {
|
for _, protocol := range protocols {
|
||||||
|
testDatas := []struct {
|
||||||
s := Service{
|
AddressFamily uint16
|
||||||
|
IP string
|
||||||
|
Netmask uint32
|
||||||
|
Destinations []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
AddressFamily: nl.FAMILY_V4,
|
AddressFamily: nl.FAMILY_V4,
|
||||||
|
IP: "1.2.3.4",
|
||||||
|
Netmask: 0xFFFFFFFF,
|
||||||
|
Destinations: []string{"10.1.1.2", "10.1.1.3", "10.1.1.4"},
|
||||||
|
}, {
|
||||||
|
AddressFamily: nl.FAMILY_V6,
|
||||||
|
IP: "2001:db8:3c4d:15::1a00",
|
||||||
|
Netmask: 128,
|
||||||
|
Destinations: []string{"2001:db8:3c4d:15::1a2b", "2001:db8:3c4d:15::1a2c", "2001:db8:3c4d:15::1a2d"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, td := range testDatas {
|
||||||
|
s := Service{
|
||||||
|
AddressFamily: td.AddressFamily,
|
||||||
SchedName: RoundRobin,
|
SchedName: RoundRobin,
|
||||||
}
|
}
|
||||||
|
|
||||||
switch protocol {
|
switch protocol {
|
||||||
case "FWM":
|
case "FWM":
|
||||||
s.FWMark = 1234
|
s.FWMark = 1234
|
||||||
|
s.Netmask = td.Netmask
|
||||||
case "TCP":
|
case "TCP":
|
||||||
s.Protocol = unix.IPPROTO_TCP
|
s.Protocol = unix.IPPROTO_TCP
|
||||||
s.Port = 80
|
s.Port = 80
|
||||||
s.Address = net.ParseIP("1.2.3.4")
|
s.Address = net.ParseIP(td.IP)
|
||||||
s.Netmask = 0xFFFFFFFF
|
s.Netmask = td.Netmask
|
||||||
case "UDP":
|
case "UDP":
|
||||||
s.Protocol = unix.IPPROTO_UDP
|
s.Protocol = unix.IPPROTO_UDP
|
||||||
s.Port = 53
|
s.Port = 53
|
||||||
s.Address = net.ParseIP("2.3.4.5")
|
s.Address = net.ParseIP(td.IP)
|
||||||
|
s.Netmask = td.Netmask
|
||||||
}
|
}
|
||||||
|
|
||||||
err := i.NewService(&s)
|
err := i.NewService(&s)
|
||||||
|
@ -277,68 +315,38 @@ func TestDestination(t *testing.T) {
|
||||||
|
|
||||||
s.SchedName = ""
|
s.SchedName = ""
|
||||||
for _, fwdMethod := range fwdMethods {
|
for _, fwdMethod := range fwdMethods {
|
||||||
d1 := Destination{
|
destinations := make([]Destination, 0)
|
||||||
AddressFamily: nl.FAMILY_V4,
|
for _, ip := range td.Destinations {
|
||||||
Address: net.ParseIP("10.1.1.2"),
|
d := Destination{
|
||||||
|
AddressFamily: td.AddressFamily,
|
||||||
|
Address: net.ParseIP(ip),
|
||||||
Port: 5000,
|
Port: 5000,
|
||||||
Weight: 1,
|
Weight: 1,
|
||||||
ConnectionFlags: fwdMethod,
|
ConnectionFlags: fwdMethod,
|
||||||
}
|
}
|
||||||
|
destinations = append(destinations, d)
|
||||||
err := i.NewDestination(&s, &d1)
|
err := i.NewDestination(&s, &d)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
checkDestination(t, i, &s, &d1, true)
|
checkDestination(t, i, &s, &d, true)
|
||||||
d2 := Destination{
|
|
||||||
AddressFamily: nl.FAMILY_V4,
|
|
||||||
Address: net.ParseIP("10.1.1.3"),
|
|
||||||
Port: 5000,
|
|
||||||
Weight: 1,
|
|
||||||
ConnectionFlags: fwdMethod,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = i.NewDestination(&s, &d2)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
checkDestination(t, i, &s, &d2, true)
|
|
||||||
|
|
||||||
d3 := Destination{
|
|
||||||
AddressFamily: nl.FAMILY_V4,
|
|
||||||
Address: net.ParseIP("10.1.1.4"),
|
|
||||||
Port: 5000,
|
|
||||||
Weight: 1,
|
|
||||||
ConnectionFlags: fwdMethod,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = i.NewDestination(&s, &d3)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
checkDestination(t, i, &s, &d3, true)
|
|
||||||
|
|
||||||
for _, updateFwdMethod := range fwdMethods {
|
for _, updateFwdMethod := range fwdMethods {
|
||||||
if updateFwdMethod == fwdMethod {
|
if updateFwdMethod == fwdMethod {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
d1.ConnectionFlags = updateFwdMethod
|
for _, d := range destinations {
|
||||||
err = i.UpdateDestination(&s, &d1)
|
d.ConnectionFlags = updateFwdMethod
|
||||||
|
err = i.UpdateDestination(&s, &d)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
checkDestination(t, i, &s, &d1, true)
|
checkDestination(t, i, &s, &d, true)
|
||||||
|
|
||||||
d2.ConnectionFlags = updateFwdMethod
|
|
||||||
err = i.UpdateDestination(&s, &d2)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
checkDestination(t, i, &s, &d2, true)
|
|
||||||
|
|
||||||
d3.ConnectionFlags = updateFwdMethod
|
|
||||||
err = i.UpdateDestination(&s, &d3)
|
|
||||||
assert.NilError(t, err)
|
|
||||||
checkDestination(t, i, &s, &d3, true)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
err = i.DelDestination(&s, &d1)
|
for _, d := range destinations {
|
||||||
|
err = i.DelDestination(&s, &d)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
err = i.DelDestination(&s, &d2)
|
checkDestination(t, i, &s, &d, false)
|
||||||
assert.NilError(t, err)
|
}
|
||||||
err = i.DelDestination(&s, &d3)
|
}
|
||||||
assert.NilError(t, err)
|
|
||||||
checkDestination(t, i, &s, &d3, false)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -315,6 +315,7 @@ func assembleStats(msg []byte) (SvcStats, error) {
|
||||||
func assembleService(attrs []syscall.NetlinkRouteAttr) (*Service, error) {
|
func assembleService(attrs []syscall.NetlinkRouteAttr) (*Service, error) {
|
||||||
|
|
||||||
var s Service
|
var s Service
|
||||||
|
var addressBytes []byte
|
||||||
|
|
||||||
for _, attr := range attrs {
|
for _, attr := range attrs {
|
||||||
|
|
||||||
|
@ -327,11 +328,7 @@ func assembleService(attrs []syscall.NetlinkRouteAttr) (*Service, error) {
|
||||||
case ipvsSvcAttrProtocol:
|
case ipvsSvcAttrProtocol:
|
||||||
s.Protocol = native.Uint16(attr.Value)
|
s.Protocol = native.Uint16(attr.Value)
|
||||||
case ipvsSvcAttrAddress:
|
case ipvsSvcAttrAddress:
|
||||||
ip, err := parseIP(attr.Value, s.AddressFamily)
|
addressBytes = attr.Value
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
s.Address = ip
|
|
||||||
case ipvsSvcAttrPort:
|
case ipvsSvcAttrPort:
|
||||||
s.Port = binary.BigEndian.Uint16(attr.Value)
|
s.Port = binary.BigEndian.Uint16(attr.Value)
|
||||||
case ipvsSvcAttrFWMark:
|
case ipvsSvcAttrFWMark:
|
||||||
|
@ -353,6 +350,16 @@ func assembleService(attrs []syscall.NetlinkRouteAttr) (*Service, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parse Address after parse AddressFamily incase of parseIP error
|
||||||
|
if addressBytes != nil {
|
||||||
|
ip, err := parseIP(addressBytes, s.AddressFamily)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.Address = ip
|
||||||
|
}
|
||||||
|
|
||||||
return &s, nil
|
return &s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -416,6 +423,7 @@ func (i *Handle) doCmdWithoutAttr(cmd uint8) ([][]byte, error) {
|
||||||
func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error) {
|
func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error) {
|
||||||
|
|
||||||
var d Destination
|
var d Destination
|
||||||
|
var addressBytes []byte
|
||||||
|
|
||||||
for _, attr := range attrs {
|
for _, attr := range attrs {
|
||||||
|
|
||||||
|
@ -426,11 +434,7 @@ func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error)
|
||||||
case ipvsDestAttrAddressFamily:
|
case ipvsDestAttrAddressFamily:
|
||||||
d.AddressFamily = native.Uint16(attr.Value)
|
d.AddressFamily = native.Uint16(attr.Value)
|
||||||
case ipvsDestAttrAddress:
|
case ipvsDestAttrAddress:
|
||||||
ip, err := parseIP(attr.Value, d.AddressFamily)
|
addressBytes = attr.Value
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
d.Address = ip
|
|
||||||
case ipvsDestAttrPort:
|
case ipvsDestAttrPort:
|
||||||
d.Port = binary.BigEndian.Uint16(attr.Value)
|
d.Port = binary.BigEndian.Uint16(attr.Value)
|
||||||
case ipvsDestAttrForwardingMethod:
|
case ipvsDestAttrForwardingMethod:
|
||||||
|
@ -453,6 +457,16 @@ func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error)
|
||||||
d.Stats = DstStats(stats)
|
d.Stats = DstStats(stats)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parse Address after parse AddressFamily incase of parseIP error
|
||||||
|
if addressBytes != nil {
|
||||||
|
ip, err := parseIP(addressBytes, d.AddressFamily)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
d.Address = ip
|
||||||
|
}
|
||||||
|
|
||||||
return &d, nil
|
return &d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue