add command line arguments

This commit is contained in:
Johannes Kimmel 2021-12-16 06:02:42 +01:00
parent 98bc2c21ba
commit 2769e419f6
1 changed files with 22 additions and 12 deletions

34
main.go
View File

@ -1,8 +1,10 @@
package main
import (
"flag"
"fmt"
"log"
"strings"
"git.freifunk-franken.de/jkimmel/abbel/capture"
"git.freifunk-franken.de/jkimmel/abbel/packet"
@ -11,21 +13,29 @@ import (
)
type options struct {
Group string
Port uint16
Ifs []string
UseCapture bool
Group string
Port uint16
Ifs []string
UseMulticast bool
}
func parseOpts() (options, error) {
var opt options
var err error
opt = options{
Group: "ff02:0:0:0:0:0:1:6",
Port: 6696,
Ifs: []string{"babel"},
UseCapture: false,
flag.StringVar(&opt.Group, "group", "ff02:0:0:0:0:0:1:6", "Multicast group to join")
port := flag.Uint("port", 6696, "Port to listen on")
flag.BoolVar(&opt.UseMulticast, "m", false, "Use multicast mode")
ifs := flag.String("i", "any", "Comma-seperated list of interfaces to listen on or \"any\" for all interfaces in capture mode")
flag.Parse()
if *port == 0 || *port >= 0xffff {
return options{}, fmt.Errorf("Invalid port %q", *port)
}
opt.Port = uint16(*port)
for _, iface := range strings.Split(*ifs, ",") {
opt.Ifs = append(opt.Ifs, iface)
}
return opt, err
@ -50,10 +60,10 @@ func run(opt options) error {
var err error
var conn BabelPacketConn
if opt.UseCapture {
conn, err = capture.FromInterface(opt.Ifs[0])
} else {
if opt.UseMulticast {
conn, err = packet.Listen(opt.Group, opt.Port, opt.Ifs...)
} else {
conn, err = capture.FromInterface(opt.Ifs[0])
}
if err != nil {
return err