Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions ipset_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,11 @@ func buildEntryData(entry *IPSetEntry) (*nl.RtAttr, error) {
data.AddChild(nl.NewRtAttr(nl.IPSET_ATTR_CIDR2, nl.Uint8Attr(entry.CIDR2)))
}

if entry.Port != nil {
if entry.Protocol == nil {
// use tcp protocol as default
val := uint8(unix.IPPROTO_TCP)
entry.Protocol = &val
}
if entry.Protocol != nil {
data.AddChild(nl.NewRtAttr(nl.IPSET_ATTR_PROTO, nl.Uint8Attr(*entry.Protocol)))
}

if entry.Port != nil {
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, *entry.Port)
data.AddChild(nl.NewRtAttr(int(nl.IPSET_ATTR_PORT|nl.NLA_F_NET_BYTEORDER), buf))
Expand Down
24 changes: 21 additions & 3 deletions ipset_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,21 @@ func TestIpsetCreateListAddDelDestroyWithTestCases(t *testing.T) {
Replace: false,
},
},
{
desc: "Type-bitmap:port",
setname: "my-test-ipset-bitmap-port",
typename: "bitmap:port",
options: IpsetCreateOptions{
Replace: true,
Timeout: &timeout,
PortFrom: 0,
PortTo: 1024,
},
entry: &IPSetEntry{
Port: &port,
Replace: false,
},
},
}

for _, tC := range testCases {
Expand Down Expand Up @@ -519,14 +534,17 @@ func TestIpsetCreateListAddDelDestroyWithTestCases(t *testing.T) {
}

if tC.entry.Port != nil {
if *result.Entries[0].Protocol != *tC.entry.Protocol {
t.Fatalf("expected protocol to be '%d', got '%d'", *tC.entry.Protocol, *result.Entries[0].Protocol)
}
if *result.Entries[0].Port != *tC.entry.Port {
t.Fatalf("expected port to be '%d', got '%d'", *tC.entry.Port, *result.Entries[0].Port)
}
}

if tC.entry.Protocol != nil {
if result.Entries[0].Protocol == nil || *result.Entries[0].Protocol != *tC.entry.Protocol {
t.Fatalf("expected protocol to be '%d', got '%v'", *tC.entry.Protocol, result.Entries[0].Protocol)
}
}

if tC.entry.MAC != nil {
if result.Entries[0].MAC.String() != tC.entry.MAC.String() {
t.Fatalf("expected mac to be '%v', got '%v'", tC.entry.MAC, result.Entries[0].MAC)
Expand Down
Loading