Skip to content

Commit

Permalink
support add timeout 0 entry (#5)
Browse files Browse the repository at this point in the history
support add excl flag
  • Loading branch information
cuisj committed May 23, 2022
1 parent 3d8a324 commit 08468a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 13 additions & 7 deletions internal/netlink/netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type NetLink struct {
type Options struct {
IPv6 bool
Timeout uint32
Excl bool
}

// Option func parameter
Expand Down Expand Up @@ -171,19 +172,24 @@ func (nl *NetLink) HandleAddr(cmd int, setName string, ip netip.Addr, cidr netip
return errors.New("setName too long")
}

req := NewNetlinkRequest(cmd|(NFNL_SUBSYS_IPSET<<8), syscall.NLM_F_REQUEST)
option := Options{}
for _, opt := range opts {
opt(&option)
}

flags := syscall.NLM_F_REQUEST
if option.Excl {
flags |= syscall.NLM_F_EXCL
}

req := NewNetlinkRequest(cmd|(NFNL_SUBSYS_IPSET<<8), flags)
req.AddData(NewNfGenMsg(syscall.AF_INET, 0, 0))
req.AddData(NewRtAttr(IPSET_ATTR_PROTOCOL, Uint8Attr(IPSET_PROTOCOL)))
req.AddData(NewRtAttr(IPSET_ATTR_SETNAME, ZeroTerminated(setName)))

attrData := NewRtAttr(IPSET_ATTR_DATA|NLA_F_NESTED, nil)

option := Options{}
for _, opt := range opts {
opt(&option)
}

if option.Timeout != 0 {
if option.Timeout >= 0 {
attrData.AddChild(&Uint32Attribute{Type: IPSET_ATTR_TIMEOUT | NLA_F_NET_BYTEORDER, Value: option.Timeout})
}

Expand Down
2 changes: 2 additions & 0 deletions ipset_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func OptIPv6() Option { return func(opts *netlink.Options) { opts.IPv6 = true }
// OptTimeout sets `timeout xx` parameter to operations.
func OptTimeout(timeout uint32) Option { return func(opts *netlink.Options) { opts.Timeout = timeout } }

func OptExcl() Option { return func(opts *netlink.Options) { opts.Excl = true } }

// Init prepares a netlink socket of ipset.
func Init() (err error) {
nl, err = netlink.New()
Expand Down

0 comments on commit 08468a7

Please sign in to comment.