-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (49 loc) · 932 Bytes
/
main.go
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
package main
import (
"errors"
"fmt"
"net/netip"
"os"
)
func printNetmask(prefix netip.Prefix) {
masked := prefix.Masked()
for ip := masked.Addr(); masked.Contains(ip); ip = ip.Next() {
fmt.Println(ip)
}
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s <netmask> [address]\n", os.Args[0])
}
func run() error {
narg := len(os.Args)
if narg < 2 {
usage()
return errors.New("error: not enough arguments")
}
prefix, err := netip.ParsePrefix(os.Args[1])
if err != nil {
return err
}
switch narg {
case 2:
printNetmask(prefix)
case 3:
ip, err := netip.ParseAddr(os.Args[2])
if err != nil {
return err
}
if !prefix.Contains(ip) {
return fmt.Errorf("%s does not contain %s", prefix.Masked(), ip)
}
default:
usage()
return errors.New("error: too many arguments")
}
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}