AddIP prototype change and fix for go 1.6

Removed extra pointer not needed in AddIP
Updated tests

Removed the use of io.SeekCurrent and defined it as a const

Signed-off-by: Flavio Crisciani <flavio.crisciani@docker.com>
This commit is contained in:
Flavio Crisciani 2017-04-06 10:52:59 -07:00 committed by Vish Ishaya
parent 8bf4a3aacd
commit 1e86b2bee5
2 changed files with 23 additions and 30 deletions

View File

@ -5,7 +5,6 @@ import (
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"syscall"
@ -24,6 +23,11 @@ const (
ConntrackExpectTable = 2
)
const (
// backward compatibility with golang 1.6 which does not have io.SeekCurrent
seekCurrent = 1
)
// InetFamily Family type
type InetFamily uint8
@ -167,13 +171,13 @@ func parseIpTuple(reader *bytes.Reader, tpl *ipTuple) {
}
}
// Skip the next 4 bytes nl.NLA_F_NESTED|nl.CTA_TUPLE_PROTO
reader.Seek(4, io.SeekCurrent)
reader.Seek(4, seekCurrent)
_, t, _, v := parseNfAttrTLV(reader)
if t == nl.CTA_PROTO_NUM {
tpl.Protocol = uint8(v[0])
}
// Skip some padding 3 bytes
reader.Seek(3, io.SeekCurrent)
reader.Seek(3, seekCurrent)
for i := 0; i < 2; i++ {
_, t, _ := parseNfAttrTL(reader)
switch t {
@ -183,7 +187,7 @@ func parseIpTuple(reader *bytes.Reader, tpl *ipTuple) {
parseBERaw16(reader, &tpl.DstPort)
}
// Skip some padding 2 byte
reader.Seek(2, io.SeekCurrent)
reader.Seek(2, seekCurrent)
}
}
@ -218,7 +222,7 @@ func parseRawData(data []byte) *ConntrackFlow {
binary.Read(reader, nl.NativeEndian(), &s.FamilyType)
// skip rest of the Netfilter header
reader.Seek(3, io.SeekCurrent)
reader.Seek(3, seekCurrent)
// The message structure is the following:
// <len, NLA_F_NESTED|CTA_TUPLE_ORIG> 4 bytes
// <len, NLA_F_NESTED|CTA_TUPLE_IP> 4 bytes
@ -240,7 +244,7 @@ func parseRawData(data []byte) *ConntrackFlow {
break
} else {
// Header not recognized skip it
reader.Seek(int64(l), io.SeekCurrent)
reader.Seek(int64(l), seekCurrent)
}
}
}
@ -291,14 +295,14 @@ type ConntrackFilter struct {
}
// AddIP adds an IP to the conntrack filter
func (f *ConntrackFilter) AddIP(tp ConntrackFilterType, ip *net.IP) error {
func (f *ConntrackFilter) AddIP(tp ConntrackFilterType, ip net.IP) error {
if f.ipFilter == nil {
f.ipFilter = make(map[ConntrackFilterType]net.IP)
}
if _, ok := f.ipFilter[tp]; ok {
return errors.New("Filter attribute already present")
}
f.ipFilter[tp] = *ip
f.ipFilter[tp] = ip
return nil
}

View File

@ -231,8 +231,7 @@ func TestConntrackTableDelete(t *testing.T) {
// Create a filter to erase groupB flows
filter := &ConntrackFilter{}
ip := net.ParseIP("127.0.0.20")
filter.AddIP(ConntrackOrigDstIP, &ip)
filter.AddIP(ConntrackOrigDstIP, net.ParseIP("127.0.0.20"))
// Flush entries of groupB
var deleted uint
@ -328,12 +327,10 @@ func TestConntrackFilter(t *testing.T) {
// SrcIP filter
filterV4 := &ConntrackFilter{}
ip := net.ParseIP("10.0.0.1")
filterV4.AddIP(ConntrackOrigSrcIP, &ip)
filterV4.AddIP(ConntrackOrigSrcIP, net.ParseIP("10.0.0.1"))
filterV6 := &ConntrackFilter{}
ip = net.ParseIP("eeee:eeee:eeee:eeee:eeee:eeee:eeee:eeee")
filterV6.AddIP(ConntrackOrigSrcIP, &ip)
filterV6.AddIP(ConntrackOrigSrcIP, net.ParseIP("eeee:eeee:eeee:eeee:eeee:eeee:eeee:eeee"))
v4Match, v6Match = applyFilter(flowList, filterV4, filterV6)
if v4Match != 1 || v6Match != 1 {
@ -342,12 +339,10 @@ func TestConntrackFilter(t *testing.T) {
// DstIp filter
filterV4 = &ConntrackFilter{}
ip = net.ParseIP("20.0.0.1")
filterV4.AddIP(ConntrackOrigDstIP, &ip)
filterV4.AddIP(ConntrackOrigDstIP, net.ParseIP("20.0.0.1"))
filterV6 = &ConntrackFilter{}
ip = net.ParseIP("dddd:dddd:dddd:dddd:dddd:dddd:dddd:dddd")
filterV6.AddIP(ConntrackOrigDstIP, &ip)
filterV6.AddIP(ConntrackOrigDstIP, net.ParseIP("dddd:dddd:dddd:dddd:dddd:dddd:dddd:dddd"))
v4Match, v6Match = applyFilter(flowList, filterV4, filterV6)
if v4Match != 1 || v6Match != 1 {
@ -356,12 +351,10 @@ func TestConntrackFilter(t *testing.T) {
// SrcIP for NAT
filterV4 = &ConntrackFilter{}
ip = net.ParseIP("20.0.0.1")
filterV4.AddIP(ConntrackNatSrcIP, &ip)
filterV4.AddIP(ConntrackNatSrcIP, net.ParseIP("20.0.0.1"))
filterV6 = &ConntrackFilter{}
ip = net.ParseIP("dddd:dddd:dddd:dddd:dddd:dddd:dddd:dddd")
filterV6.AddIP(ConntrackNatSrcIP, &ip)
filterV6.AddIP(ConntrackNatSrcIP, net.ParseIP("dddd:dddd:dddd:dddd:dddd:dddd:dddd:dddd"))
v4Match, v6Match = applyFilter(flowList, filterV4, filterV6)
if v4Match != 1 || v6Match != 1 {
@ -370,12 +363,10 @@ func TestConntrackFilter(t *testing.T) {
// DstIP for NAT
filterV4 = &ConntrackFilter{}
ip = net.ParseIP("192.168.1.1")
filterV4.AddIP(ConntrackNatDstIP, &ip)
filterV4.AddIP(ConntrackNatDstIP, net.ParseIP("192.168.1.1"))
filterV6 = &ConntrackFilter{}
ip = net.ParseIP("dddd:dddd:dddd:dddd:dddd:dddd:dddd:dddd")
filterV6.AddIP(ConntrackNatDstIP, &ip)
filterV6.AddIP(ConntrackNatDstIP, net.ParseIP("dddd:dddd:dddd:dddd:dddd:dddd:dddd:dddd"))
v4Match, v6Match = applyFilter(flowList, filterV4, filterV6)
if v4Match != 2 || v6Match != 0 {
@ -384,12 +375,10 @@ func TestConntrackFilter(t *testing.T) {
// AnyIp for Nat
filterV4 = &ConntrackFilter{}
ip = net.ParseIP("192.168.1.1")
filterV4.AddIP(ConntrackNatAnyIP, &ip)
filterV4.AddIP(ConntrackNatAnyIP, net.ParseIP("192.168.1.1"))
filterV6 = &ConntrackFilter{}
ip = net.ParseIP("eeee:eeee:eeee:eeee:eeee:eeee:eeee:eeee")
filterV6.AddIP(ConntrackNatAnyIP, &ip)
filterV6.AddIP(ConntrackNatAnyIP, net.ParseIP("eeee:eeee:eeee:eeee:eeee:eeee:eeee:eeee"))
v4Match, v6Match = applyFilter(flowList, filterV4, filterV6)
if v4Match != 2 || v6Match != 1 {