| 1 | /* |
| 2 | Copyright 2023 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package knftables |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "strconv" |
| 22 | "strings" |
| 23 | ) |
| 24 | |
| 25 | // PtrTo can be used to fill in optional field values in objects |
| 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 | // ParsePriority tries to convert the string form of a chain priority into a number |
| 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 | // Concat is a helper (primarily) for constructing Rule objects. It takes a series of |
| 77 | // arguments and concatenates them together into a single string with spaces between the |
| 78 | // arguments. Strings are output as-is, string arrays are output element by element, |
| 79 | // numbers are output as with `fmt.Sprintf("%d")`, and all other types are output as with |
| 80 | // `fmt.Sprintf("%s")`. To help with set/map lookup syntax, an argument of "@" will not |
| 81 | // be followed by a space, so you can do, eg, `Concat("ip saddr", "@", setName)`. |
| 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 | } |