Skip to content

Commit

Permalink
Merge pull request #42 from eleztian/main
Browse files Browse the repository at this point in the history
sort match&options types
  • Loading branch information
singchia authored Feb 18, 2024
2 parents aef48ed + 8de9fb5 commit af1ef04
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
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

0 comments on commit af1ef04

Please sign in to comment.