Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort match&options types #42

Merged
merged 1 commit into from
Feb 18, 2024
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
25 changes: 18 additions & 7 deletions iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,35 @@ func NewIPTables(opts ...IPTablesOption) *IPTables {
func (iptables *IPTables) dump() *IPTables {
newiptables := &IPTables{
statement: &Statement{
err: iptables.statement.err,
table: iptables.statement.table,
chain: iptables.statement.chain,
matches: make(map[MatchType]Match),
options: make(map[OptionType]Option),
target: iptables.statement.target,
command: iptables.statement.command,
err: iptables.statement.err,
table: iptables.statement.table,
chain: iptables.statement.chain,
matches: make(map[MatchType]Match),
matchTypeOrder: make([]MatchType, 0),
options: make(map[OptionType]Option),
optionTypeOrder: make([]OptionType, 0),
target: iptables.statement.target,
command: iptables.statement.command,
},
cmdName: iptables.cmdName,
log: iptables.log,
dr: iptables.dr,
drWriter: iptables.drWriter,
}

for _, k := range iptables.statement.matchTypeOrder {
newiptables.statement.matchTypeOrder = append(newiptables.statement.matchTypeOrder, k)
}
for k, v := range iptables.statement.matches {
newiptables.statement.matches[k] = v
}

for _, k := range iptables.statement.optionTypeOrder {
newiptables.statement.optionTypeOrder = append(newiptables.statement.optionTypeOrder, k)
}
for k, v := range iptables.statement.options {
newiptables.statement.options[k] = v
}

return newiptables
}
21 changes: 19 additions & 2 deletions iptables/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ type Statement struct {
chain ChainType
userDefinedChain string
matches map[MatchType]Match
matchTypeOrder []MatchType
options map[OptionType]Option
optionTypeOrder []OptionType
target Target
command Command
dump bool
Expand All @@ -34,10 +36,16 @@ func NewStatement() *Statement {
}

func (statement *Statement) addMatch(match Match) {
if _, ok := statement.matches[match.Type()]; !ok {
statement.matchTypeOrder = append(statement.matchTypeOrder, match.Type())
}
statement.matches[match.Type()] = match
}

func (statement *Statement) addOption(option Option) {
if _, ok := statement.options[option.Type()]; !ok {
statement.optionTypeOrder = append(statement.optionTypeOrder, option.Type())
}
statement.options[option.Type()] = option
}

Expand Down Expand Up @@ -102,7 +110,12 @@ func (statement *Statement) Elems() ([]string, error) {
}

// options
for _, option := range statement.options {
for _, optionType := range statement.optionTypeOrder {
option, ok := statement.options[optionType]
if !ok {
continue
}

args := option.ShortArgs()
if args != nil {
if option.Type() != OptionTypeNumeric ||
Expand All @@ -117,7 +130,11 @@ func (statement *Statement) Elems() ([]string, error) {
}

// matches
for _, match := range statement.matches {
for _, matchType := range statement.matchTypeOrder {
match, ok := statement.matches[matchType]
if !ok {
continue
}
args := match.ShortArgs()
if args != nil {
elems = append(elems, args...)
Expand Down
Loading