1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
//go:build linux
// +build linux
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"sort"
"github.com/vishvananda/netlink"
)
type command struct {
Function func([]string)
Description string
ArgCount int
}
var (
commands = map[string]command{
"protocol": {cmdProtocol, "prints the protocol version", 0},
"create": {cmdCreate, "creates a new ipset", 2},
"destroy": {cmdDestroy, "creates a new ipset", 1},
"list": {cmdList, "list specific ipset", 1},
"listall": {cmdListAll, "list all ipsets", 0},
"add": {cmdAddDel(netlink.IpsetAdd), "add entry", 2},
"del": {cmdAddDel(netlink.IpsetDel), "delete entry", 2},
"test": {cmdTest, "test whether an entry is in a set or not", 2},
}
timeoutVal *uint32
timeout = flag.Int("timeout", -1, "timeout, negative means omit the argument")
comment = flag.String("comment", "", "comment")
withComments = flag.Bool("with-comments", false, "create set with comment support")
withCounters = flag.Bool("with-counters", false, "create set with counters support")
withSkbinfo = flag.Bool("with-skbinfo", false, "create set with skbinfo support")
replace = flag.Bool("replace", false, "replace existing set/entry")
)
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
printUsage()
os.Exit(1)
}
if *timeout >= 0 {
v := uint32(*timeout)
timeoutVal = &v
}
log.SetFlags(log.Lshortfile)
cmdName := args[0]
args = args[1:]
cmd, exist := commands[cmdName]
if !exist {
fmt.Printf("Unknown command '%s'\n\n", cmdName)
printUsage()
os.Exit(1)
}
if cmd.ArgCount != len(args) {
fmt.Printf("Invalid number of arguments. expected=%d given=%d\n", cmd.ArgCount, len(args))
os.Exit(1)
}
cmd.Function(args)
}
func printUsage() {
fmt.Printf("Usage: %s COMMAND [args] [-flags]\n\n", os.Args[0])
names := make([]string, 0, len(commands))
for name := range commands {
names = append(names, name)
}
sort.Strings(names)
fmt.Println("Available commands:")
for _, name := range names {
fmt.Printf(" %-15v %s\n", name, commands[name].Description)
}
fmt.Println("\nAvailable flags:")
flag.PrintDefaults()
}
func cmdProtocol(_ []string) {
protocol, minProto, err := netlink.IpsetProtocol()
check(err)
log.Println("Protocol:", protocol, "min:", minProto)
}
func cmdCreate(args []string) {
err := netlink.IpsetCreate(args[0], args[1], netlink.IpsetCreateOptions{
Replace: *replace,
Timeout: timeoutVal,
Comments: *withComments,
Counters: *withCounters,
Skbinfo: *withSkbinfo,
})
check(err)
}
func cmdDestroy(args []string) {
check(netlink.IpsetDestroy(args[0]))
}
func cmdList(args []string) {
result, err := netlink.IpsetList(args[0])
check(err)
log.Printf("%+v", result)
}
func cmdListAll(args []string) {
result, err := netlink.IpsetListAll()
check(err)
for _, ipset := range result {
log.Printf("%+v", ipset)
}
}
func cmdAddDel(f func(string, *netlink.IPSetEntry) error) func([]string) {
return func(args []string) {
setName := args[0]
element := args[1]
mac, _ := net.ParseMAC(element)
entry := netlink.IPSetEntry{
Timeout: timeoutVal,
MAC: mac,
Comment: *comment,
Replace: *replace,
}
check(f(setName, &entry))
}
}
func cmdTest(args []string) {
setName := args[0]
element := args[1]
ip := net.ParseIP(element)
entry := &netlink.IPSetEntry{
Timeout: timeoutVal,
IP: ip,
Comment: *comment,
Replace: *replace,
}
exist, err := netlink.IpsetTest(setName, entry)
check(err)
log.Printf("existence: %t\n", exist)
}
// panic on error
func check(err error) {
if err != nil {
panic(err)
}
}
|