-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadapter.go
70 lines (64 loc) · 1.57 KB
/
adapter.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
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"encoding/binary"
"errors"
"fmt"
"net"
"strings"
)
type adapter struct {
name string
ip string
broadcast string
}
func adapters() ([]adapter, error) {
var adapters []adapter
interfaces, err := net.Interfaces()
if err != nil {
return nil, fmt.Errorf("failed to collect network interface: %w", err)
}
for _, ifEntry := range interfaces {
if ifEntry.HardwareAddr == nil || !strings.Contains(ifEntry.Flags.String(), "up") {
continue // ignore odd or down adapters
}
addresses, err := ifEntry.Addrs()
if err != nil || len(addresses) == 0 {
continue // ignore unassigned adapters
}
for _, address := range addresses {
if !strings.Contains(address.String(), ".") {
continue
}
ip, n, err := net.ParseCIDR(address.String())
if err != nil {
continue
}
broadcast := broadcast(n)
adapters = append(adapters, adapter{ifEntry.Name, ip.String(), broadcast})
}
}
if len(adapters) == 0 {
return nil, errors.New("no adapters available")
}
return adapters, nil
}
func broadcast(n *net.IPNet) string {
bc := make(net.IP, len(n.IP.To4()))
binary.BigEndian.PutUint32(bc, binary.BigEndian.Uint32(n.IP.To4())|^binary.BigEndian.Uint32(net.IP(n.Mask).To4()))
return bc.String()
}
func adapterNames(adapters []adapter) []string {
s := make([]string, len(adapters))
for i, a := range adapters {
s[i] = fmt.Sprintf("%s [%s]", a.name, a.ip)
}
return s
}
func findAdapter(adapters []adapter, name string) adapter {
for _, a := range adapters {
if strings.HasPrefix(name, a.name) {
return a
}
}
return adapter{}
}