| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package knftables |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"strconv" |
| 22 |
"strings" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
func PtrTo[T any](val T) *T { |
| 27 |
return &val |
| 28 |
} |
| 29 |
|
| 30 |
var numericPriorities = map[string]int{ |
| 31 |
"raw": -300, |
| 32 |
"mangle": -150, |
| 33 |
"dstnat": -100, |
| 34 |
"filter": 0, |
| 35 |
"security": 50, |
| 36 |
"srcnat": 100, |
| 37 |
} |
| 38 |
|
| 39 |
var bridgeNumericPriorities = map[string]int{ |
| 40 |
"dstnat": -300, |
| 41 |
"filter": -200, |
| 42 |
"out": 100, |
| 43 |
"srcnat": 300, |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func ParsePriority(family Family, priority string) (int, error) { |
| 48 |
val, err := strconv.Atoi(priority) |
| 49 |
if err == nil { |
| 50 |
return val, nil |
| 51 |
} |
| 52 |
|
| 53 |
modVal := 0 |
| 54 |
if i := strings.IndexAny(priority, "+-"); i != -1 { |
| 55 |
mod := priority[i:] |
| 56 |
modVal, err = strconv.Atoi(mod) |
| 57 |
if err != nil { |
| 58 |
return 0, fmt.Errorf("could not parse modifier %q: %w", mod, err) |
| 59 |
} |
| 60 |
priority = priority[:i] |
| 61 |
} |
| 62 |
|
| 63 |
var found bool |
| 64 |
if family == BridgeFamily { |
| 65 |
val, found = bridgeNumericPriorities[priority] |
| 66 |
} else { |
| 67 |
val, found = numericPriorities[priority] |
| 68 |
} |
| 69 |
if !found { |
| 70 |
return 0, fmt.Errorf("unknown priority %q", priority) |
| 71 |
} |
| 72 |
|
| 73 |
return val + modVal, nil |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
func Concat(args ...interface{}) string { |
| 83 |
b := &strings.Builder{} |
| 84 |
var needSpace, wroteAt bool |
| 85 |
for _, arg := range args { |
| 86 |
switch x := arg.(type) { |
| 87 |
case string: |
| 88 |
if needSpace { |
| 89 |
b.WriteByte(' ') |
| 90 |
} |
| 91 |
b.WriteString(x) |
| 92 |
wroteAt = (x == "@") |
| 93 |
case []string: |
| 94 |
for _, s := range x { |
| 95 |
if needSpace { |
| 96 |
b.WriteByte(' ') |
| 97 |
} |
| 98 |
b.WriteString(s) |
| 99 |
wroteAt = (s == "@") |
| 100 |
needSpace = b.Len() > 0 && !wroteAt |
| 101 |
} |
| 102 |
case int, uint, int16, uint16, int32, uint32, int64, uint64: |
| 103 |
if needSpace { |
| 104 |
b.WriteByte(' ') |
| 105 |
} |
| 106 |
fmt.Fprintf(b, "%d", x) |
| 107 |
default: |
| 108 |
if needSpace { |
| 109 |
b.WriteByte(' ') |
| 110 |
} |
| 111 |
fmt.Fprintf(b, "%s", x) |
| 112 |
} |
| 113 |
|
| 114 |
needSpace = b.Len() > 0 && !wroteAt |
| 115 |
} |
| 116 |
return b.String() |
| 117 |
} |