Add Protocol() method to iptables

Enables simpler printing of which IP family the iptables interface is
managing.
This commit is contained in:
Tim Hockin
2020-04-09 16:26:29 -07:00
parent b874f7c626
commit ef934a2c5e
8 changed files with 59 additions and 70 deletions

View File

@@ -55,8 +55,10 @@ type Interface interface {
EnsureRule(position RulePosition, table Table, chain Chain, args ...string) (bool, error)
// DeleteRule checks if the specified rule is present and, if so, deletes it.
DeleteRule(table Table, chain Chain, args ...string) error
// IsIpv6 returns true if this is managing ipv6 tables
// IsIpv6 returns true if this is managing ipv6 tables.
IsIpv6() bool
// Protocol returns the IP family this instance is managing,
Protocol() Protocol
// SaveInto calls `iptables-save` for table and stores result in a given buffer.
SaveInto(table Table, buffer *bytes.Buffer) error
// Restore runs `iptables-restore` passing data through []byte.
@@ -87,13 +89,13 @@ type Interface interface {
}
// Protocol defines the ip protocol either ipv4 or ipv6
type Protocol byte
type Protocol string
const (
// ProtocolIpv4 represents ipv4 protocol in iptables
ProtocolIpv4 Protocol = iota + 1
ProtocolIpv4 Protocol = "IPv4"
// ProtocolIpv6 represents ipv6 protocol in iptables
ProtocolIpv6
ProtocolIpv6 Protocol = "IPv6"
)
// Table represents different iptable like filter,nat, mangle and raw
@@ -323,6 +325,10 @@ func (runner *runner) IsIpv6() bool {
return runner.protocol == ProtocolIpv6
}
func (runner *runner) Protocol() Protocol {
return runner.protocol
}
// SaveInto is part of Interface.
func (runner *runner) SaveInto(table Table, buffer *bytes.Buffer) error {
runner.mu.Lock()