| 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 knftablesscip-go gomod sigs.k8s.io/knftables . `sigs.k8s.io/knftables`/ |
| 18 | |
| 19 | import ( |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.20 fmt/" |
| 21 | "strconvscip-go gomod github.com/golang/go/src go1.20 strconv/" |
| 22 | "stringsscip-go gomod github.com/golang/go/src go1.20 strings/" |
| 23 | ) |
| 24 | |
| 25 | // PtrTo can be used to fill in optional field values in objects |
| 26 | func PtrToscip-go gomod sigs.k8s.io/knftables . `sigs.k8s.io/knftables`/PtrTo().[T any](val T) *T { |
| 27 | return &val |
| 28 | } |
| 29 | |
| 30 | var numericPrioritiesscip-go gomod sigs.k8s.io/knftables . `sigs.k8s.io/knftables`/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 bridgeNumericPrioritiesscip-go gomod sigs.k8s.io/knftables . `sigs.k8s.io/knftables`/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 ParsePriorityscip-go gomod sigs.k8s.io/knftables . `sigs.k8s.io/knftables`/ParsePriority().(family Familyscip-go gomod sigs.k8s.io/knftables . `sigs.k8s.io/knftables`/Family#, priority string) (int, error) { |
| 48 | val, err := strconvscip-go gomod github.com/golang/go/src go1.20 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.20 strconv/Atoi().(priority) |
| 49 | if err == nil { |
| 50 | return val, nil |
| 51 | } |
| 52 | |
| 53 | modVal := 0 |
| 54 | if i := stringsscip-go gomod github.com/golang/go/src go1.20 strings/.IndexAnyscip-go gomod github.com/golang/go/src go1.20 strings/IndexAny().(priority, "+-"); i != -1 { |
| 55 | mod := priority[i:] |
| 56 | modVal, err = strconvscip-go gomod github.com/golang/go/src go1.20 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.20 strconv/Atoi().(mod) |
| 57 | if err != nil { |
| 58 | return 0, fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.20 fmt/Errorf().("could not parse modifier %q: %w", mod, err) |
| 59 | } |
| 60 | priority = priority[:i] |
| 61 | } |
| 62 | |
| 63 | var found bool |
| 64 | if family == BridgeFamilyscip-go gomod sigs.k8s.io/knftables . `sigs.k8s.io/knftables`/BridgeFamily. { |
| 65 | val, found = bridgeNumericPrioritiesscip-go gomod sigs.k8s.io/knftables . `sigs.k8s.io/knftables`/bridgeNumericPriorities.[priority] |
| 66 | } else { |
| 67 | val, found = numericPrioritiesscip-go gomod sigs.k8s.io/knftables . `sigs.k8s.io/knftables`/numericPriorities.[priority] |
| 68 | } |
| 69 | if !found { |
| 70 | return 0, fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.20 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 Concatscip-go gomod sigs.k8s.io/knftables . `sigs.k8s.io/knftables`/Concat().(args ...interface{}) string { |
| 83 | b := &stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Builderscip-go gomod github.com/golang/go/src go1.20 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.WriteBytescip-go gomod github.com/golang/go/src go1.20 strings/Builder#WriteByte().(' ') |
| 90 | } |
| 91 | b.WriteStringscip-go gomod github.com/golang/go/src go1.20 strings/Builder#WriteString().(x) |
| 92 | wroteAt = (x == "@") |
| 93 | case []string: |
| 94 | for _, s := range x { |
| 95 | if needSpace { |
| 96 | b.WriteBytescip-go gomod github.com/golang/go/src go1.20 strings/Builder#WriteByte().(' ') |
| 97 | } |
| 98 | b.WriteStringscip-go gomod github.com/golang/go/src go1.20 strings/Builder#WriteString().(s) |
| 99 | wroteAt = (s == "@") |
| 100 | needSpace = b.Lenscip-go gomod github.com/golang/go/src go1.20 strings/Builder#Len().() > 0 && !wroteAt |
| 101 | } |
| 102 | case int, uint, int16, uint16, int32, uint32, int64, uint64: |
| 103 | if needSpace { |
| 104 | b.WriteBytescip-go gomod github.com/golang/go/src go1.20 strings/Builder#WriteByte().(' ') |
| 105 | } |
| 106 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Fprintf().(b, "%d", x) |
| 107 | default: |
| 108 | if needSpace { |
| 109 | b.WriteBytescip-go gomod github.com/golang/go/src go1.20 strings/Builder#WriteByte().(' ') |
| 110 | } |
| 111 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Fprintf().(b, "%s", x) |
| 112 | } |
| 113 | |
| 114 | needSpace = b.Lenscip-go gomod github.com/golang/go/src go1.20 strings/Builder#Len().() > 0 && !wroteAt |
| 115 | } |
| 116 | return b.Stringscip-go gomod github.com/golang/go/src go1.20 strings/Builder#String().() |
| 117 | } |