mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +08:00
ipset: optimize code
This commit is contained in:
parent
7fda4f8710
commit
9dbc0acc88
@ -161,26 +161,13 @@ func CreateSet(fd int, lsa syscall.SockaddrNetlink, setName string) {
|
|||||||
req := NewNetlinkRequest(IPSET_CMD_CREATE|(NFNL_SUBSYS_IPSET<<8), syscall.NLM_F_REQUEST)
|
req := NewNetlinkRequest(IPSET_CMD_CREATE|(NFNL_SUBSYS_IPSET<<8), syscall.NLM_F_REQUEST)
|
||||||
|
|
||||||
// TODO: support AF_INET6
|
// TODO: support AF_INET6
|
||||||
nfgenMsg := NewNfGenMsg(syscall.AF_INET, 0, 0)
|
req.AddData(NewNfGenMsg(syscall.AF_INET, 0, 0))
|
||||||
req.AddData(nfgenMsg)
|
req.AddData(NewRtAttr(IPSET_ATTR_PROTOCOL, Uint8Attr(IPSET_PROTOCOL)))
|
||||||
|
req.AddData(NewRtAttr(IPSET_ATTR_SETNAME, ZeroTerminated(setName)))
|
||||||
attrProto := NewRtAttr(IPSET_ATTR_PROTOCOL, Uint8Attr(IPSET_PROTOCOL))
|
req.AddData(NewRtAttr(IPSET_ATTR_TYPENAME, ZeroTerminated("hash:net")))
|
||||||
req.AddData(attrProto)
|
req.AddData(NewRtAttr(IPSET_ATTR_REVISION, Uint8Attr(1)))
|
||||||
|
req.AddData(NewRtAttr(IPSET_ATTR_FAMILY, Uint8Attr(2)))
|
||||||
attrSiteName := NewRtAttr(IPSET_ATTR_SETNAME, ZeroTerminated(setName))
|
req.AddData(NewRtAttr(IPSET_ATTR_DATA|NLA_F_NESTED, nil))
|
||||||
req.AddData(attrSiteName)
|
|
||||||
|
|
||||||
attrSiteType := NewRtAttr(IPSET_ATTR_TYPENAME, ZeroTerminated("hash:net"))
|
|
||||||
req.AddData(attrSiteType)
|
|
||||||
|
|
||||||
attrRev := NewRtAttr(IPSET_ATTR_REVISION, Uint8Attr(1))
|
|
||||||
req.AddData(attrRev)
|
|
||||||
|
|
||||||
attrFamily := NewRtAttr(IPSET_ATTR_FAMILY, Uint8Attr(2))
|
|
||||||
req.AddData(attrFamily)
|
|
||||||
|
|
||||||
attrData := NewRtAttr(IPSET_ATTR_DATA|NLA_F_NESTED, nil)
|
|
||||||
req.AddData(attrData)
|
|
||||||
|
|
||||||
err := syscall.Sendto(fd, req.Serialize(), 0, &lsa)
|
err := syscall.Sendto(fd, req.Serialize(), 0, &lsa)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -236,14 +223,9 @@ func AddToSet(fd int, lsa syscall.SockaddrNetlink, setName, entry string) {
|
|||||||
req := NewNetlinkRequest(IPSET_CMD_ADD|(NFNL_SUBSYS_IPSET<<8), syscall.NLM_F_REQUEST)
|
req := NewNetlinkRequest(IPSET_CMD_ADD|(NFNL_SUBSYS_IPSET<<8), syscall.NLM_F_REQUEST)
|
||||||
|
|
||||||
// TODO: support AF_INET6
|
// TODO: support AF_INET6
|
||||||
nfgenMsg := NewNfGenMsg(syscall.AF_INET, 0, 0)
|
req.AddData(NewNfGenMsg(syscall.AF_INET, 0, 0))
|
||||||
req.AddData(nfgenMsg)
|
req.AddData(NewRtAttr(IPSET_ATTR_PROTOCOL, Uint8Attr(IPSET_PROTOCOL)))
|
||||||
|
req.AddData(NewRtAttr(IPSET_ATTR_SETNAME, ZeroTerminated(setName)))
|
||||||
attrProto := NewRtAttr(IPSET_ATTR_PROTOCOL, Uint8Attr(IPSET_PROTOCOL))
|
|
||||||
req.AddData(attrProto)
|
|
||||||
|
|
||||||
attrSiteName := NewRtAttr(IPSET_ATTR_SETNAME, ZeroTerminated(setName))
|
|
||||||
req.AddData(attrSiteName)
|
|
||||||
|
|
||||||
attrNested := NewRtAttr(IPSET_ATTR_DATA|NLA_F_NESTED, nil)
|
attrNested := NewRtAttr(IPSET_ATTR_DATA|NLA_F_NESTED, nil)
|
||||||
attrIP := NewRtAttrChild(attrNested, IPSET_ATTR_IP|NLA_F_NESTED, nil)
|
attrIP := NewRtAttrChild(attrNested, IPSET_ATTR_IP|NLA_F_NESTED, nil)
|
||||||
@ -266,7 +248,7 @@ func AddToSet(fd int, lsa syscall.SockaddrNetlink, setName, entry string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get native endianness for the system
|
// NativeEndian get native endianness for the system
|
||||||
func NativeEndian() binary.ByteOrder {
|
func NativeEndian() binary.ByteOrder {
|
||||||
if nativeEndian == nil {
|
if nativeEndian == nil {
|
||||||
var x uint32 = 0x01020304
|
var x uint32 = 0x01020304
|
||||||
@ -289,6 +271,7 @@ type NetlinkRequestData interface {
|
|||||||
Serialize() []byte
|
Serialize() []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NfGenMsg .
|
||||||
type NfGenMsg struct {
|
type NfGenMsg struct {
|
||||||
nfgenFamily uint8
|
nfgenFamily uint8
|
||||||
version uint8
|
version uint8
|
||||||
@ -304,10 +287,12 @@ func NewNfGenMsg(nfgenFamily, version, resID int) *NfGenMsg {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Len .
|
||||||
func (m *NfGenMsg) Len() int {
|
func (m *NfGenMsg) Len() int {
|
||||||
return rtaAlignOf(4)
|
return rtaAlignOf(4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serialize .
|
||||||
func (m *NfGenMsg) Serialize() []byte {
|
func (m *NfGenMsg) Serialize() []byte {
|
||||||
native := NativeEndian()
|
native := NativeEndian()
|
||||||
|
|
||||||
@ -344,6 +329,7 @@ func NewRtAttrChild(parent *RtAttr, attrType int, data []byte) *RtAttr {
|
|||||||
return attr
|
return attr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Len .
|
||||||
func (a *RtAttr) Len() int {
|
func (a *RtAttr) Len() int {
|
||||||
if len(a.children) == 0 {
|
if len(a.children) == 0 {
|
||||||
return (syscall.SizeofRtAttr + len(a.Data))
|
return (syscall.SizeofRtAttr + len(a.Data))
|
||||||
@ -385,13 +371,14 @@ func (a *RtAttr) Serialize() []byte {
|
|||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NetlinkRequest .
|
||||||
type NetlinkRequest struct {
|
type NetlinkRequest struct {
|
||||||
syscall.NlMsghdr
|
syscall.NlMsghdr
|
||||||
Data []NetlinkRequestData
|
Data []NetlinkRequestData
|
||||||
RawData []byte
|
RawData []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new netlink request from proto and flags
|
// NewNetlinkRequest create a new netlink request from proto and flags
|
||||||
// Note the Len value will be inaccurate once data is added until
|
// Note the Len value will be inaccurate once data is added until
|
||||||
// the message is serialized
|
// the message is serialized
|
||||||
func NewNetlinkRequest(proto, flags int) *NetlinkRequest {
|
func NewNetlinkRequest(proto, flags int) *NetlinkRequest {
|
||||||
|
Loading…
Reference in New Issue
Block a user