forked from irai/packet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
59 lines (51 loc) · 1.08 KB
/
input.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 (
"bufio"
"fmt"
"net"
"net/netip"
"os"
"strings"
)
func readInput() []string {
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = strings.ToLower(text[:len(text)-1])
// handle windows line feed
if len(text) > 1 && text[len(text)-1] == '\r' {
text = strings.ToLower(text[:len(text)-1])
}
tokens := strings.Split(text, " ")
return tokens
}
func getString(tokens []string, pos int) string {
if len(tokens) < pos+1 {
fmt.Println("missing value", tokens)
return ""
}
return tokens[pos]
}
func getIP(tokens []string, pos int) netip.Addr {
if len(tokens) < pos+1 {
fmt.Println("missing ip", tokens)
return netip.Addr{}
}
ip, err := netip.ParseAddr(tokens[pos])
if err != nil {
fmt.Println("invalid ip=", tokens[pos], err)
return netip.Addr{}
}
return ip
}
func getMAC(tokens []string, pos int) net.HardwareAddr {
if len(tokens) < pos+1 {
fmt.Println("missing mac", tokens)
return nil
}
mac, err := net.ParseMAC(tokens[pos])
if err != nil {
fmt.Println("invalid mac=", tokens[pos])
return nil
}
return mac
}