-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
54 lines (41 loc) · 1.11 KB
/
utils.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
package election
import (
"net"
"encoding/binary"
"strconv"
"strings"
)
func supportedInterfaces() []*net.Interface {
var ifis []*net.Interface
interfaces, err := net.Interfaces()
if err != nil {
return nil
}
for _, ifi := range interfaces {
if ((ifi.Flags & net.FlagUp) != 0) && ((ifi.Flags & net.FlagMulticast) != 0){
ifis = append(ifis, &net.Interface{Index:ifi.Index,MTU:ifi.MTU,Name:ifi.Name,HardwareAddr:ifi.HardwareAddr,Flags:ifi.Flags})
}
}
return ifis;
}
func getLocalInterfaceIpAddress(ifi *net.Interface) (string, uint32) {
addrs, err := ifi.Addrs()
if err != nil {
return "<nil>", 0
}
for _, add := range addrs {
if ipnet, ok := add.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), binary.BigEndian.Uint32(ipnet.IP.To4())
}
}
}
return "<nil>",0
}
func parseMessage(data []string) *mMessage {
num,_ := strconv.ParseUint(data[1],10,32)
num2,_ := strconv.ParseInt(data[2],10,32)
i := strings.Index(data[3],"###")
ip := data[3][:i]
return &mMessage{message:data[0], ipNumber:uint32(num), processId:int(num2), ipAddr:ip}
}