diff --git a/README.md b/README.md index 4edb234..fd17ea8 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,14 @@ Sends a DHCPv6-PD solicit message and displays the response (no request is done) ```` testdhcpv6pd [options] [interface name or index] - available options: - -a string - anonymize ip addresses (format = list nibble indexes to show) (default "12345678") - -l int - prefix length (default 64) - -s dont print debug messages - -v display version +Available options: + -a string + anonymize ip addresses (format = list word indexes to show) (default "12345678") + -p string + ask for a specific prefix and/or length (default "::/64") + -s dont print debug messages + -v display version + ```` Without argument, `testdhcpv6pd` will display the available interfaces @@ -21,8 +22,11 @@ Use `-s` to suppress packet debugging Use `-a format` to anonymize the prefix where format is a list of indexes. Each index, from 1 to 8, is the part number of the address to keep. For instance `-a "18"` only keeps the first and last part of the address. +Use `-p ::/60` to request a /60 prefix or even `-p 2a01:xxx:xxxx:xxxx::/64,` to request a specific prefix + ### notes Not tested on *bsd, plan9 Linux systems require *at least* `cap_net_bind_service` capability to bind to port 546 (see `man 7 capabilities`) or just use `sudo` + Darwin/MacOS: use `sudo` \ No newline at end of file diff --git a/main.go b/main.go index 4c94203..286be23 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "fmt" "log" "net" + "net/netip" "os" "strconv" "time" @@ -55,10 +56,10 @@ func parseInterface(name string) (*net.Interface, error) { } var ( - optPrefixLength = flag.Int("l", 64, "prefix length") - optNoDebug = flag.Bool("s", false, "dont print debug messages") - optVersion = flag.Bool("v", false, "display version") - optAnonymize = flag.String("a", utils.FormatV6Full, "anonymize ip addresses (format = list word indexes to show)") + optNoDebug = flag.Bool("s", false, "dont print debug messages") + optVersion = flag.Bool("v", false, "display version") + optAnonymize = flag.String("a", utils.FormatV6Full, "anonymize ip addresses (format = list word indexes to show)") + optPrefix = flag.String("p", "::/64", "ask for a specific prefix and/or length") ) func main() { @@ -69,9 +70,6 @@ func main() { fmt.Println("version", version) os.Exit(0) } - if *optPrefixLength < 1 || *optPrefixLength > 128 { - log.Fatal("prefix length") - } if len(flag.Args()) != 1 { fmt.Printf("Usage: %s [options] [interface name] or [interface index]\n", os.Args[0]) displayInterfaces() @@ -80,6 +78,10 @@ func main() { os.Exit(0) } + prefix, err := netip.ParsePrefix(*optPrefix) + if err != nil { + log.Fatal(err) + } iface, err := parseInterface(flag.Args()[0]) if err != nil { log.Fatal(err) @@ -116,8 +118,8 @@ func main() { PreferredLifetime: 0, ValidLifetime: 0, Prefix: &net.IPNet{ - Mask: net.CIDRMask(*optPrefixLength, 128), - IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + Mask: net.CIDRMask(prefix.Bits(), 128), + IP: prefix.Addr().AsSlice(), }, Options: dhcpv6.PrefixOptions{Options: dhcpv6.Options{}}, },