-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathnet.go
More file actions
69 lines (60 loc) · 1.47 KB
/
net.go
File metadata and controls
69 lines (60 loc) · 1.47 KB
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
package net
import (
"errors"
"net"
"strconv"
"strings"
"time"
)
// InterfaceIPAddress is used to know the interface and ip address in the sytem
type InterfaceIPAddress struct {
Iface string
IP string
}
// WaitForPort wait for successful network connection
func WaitForPort(proto string, ip string, port int, timeout time.Duration) error {
for {
con, err := net.DialTimeout(proto, ip+":"+strconv.Itoa(port), timeout)
if err == nil {
con.Close()
break
}
}
return nil
}
// RandomPort return a random not used TCP port
func RandomPort(proto string) (int, error) {
switch proto {
case "tcp":
l, _ := net.Listen(proto, "127.0.0.1:0")
defer l.Close()
port := l.Addr()
lPort, _ := strconv.Atoi(strings.Split(port.String(), ":")[1])
return lPort, nil
case "udp":
addr, _ := net.ResolveUDPAddr(proto, "127.0.0.1:0")
l, _ := net.ListenUDP(proto, addr)
defer l.Close()
return addr.Port, nil
default:
return -1, errors.New("invalid protocol")
}
}
// GetNetworkInterfaces return the list of
// network interfaces and IP address
func GetNetworkInterfaces() []InterfaceIPAddress {
result := []InterfaceIPAddress{}
interfaces, _ := net.Interfaces()
for _, inter := range interfaces {
if addrs, err := inter.Addrs(); err == nil {
for _, addr := range addrs {
result = append(result, InterfaceIPAddress{inter.Name, addr.String()})
}
}
}
return result
}
// ParseIP parses s as an IP address
func ParseIP(s string) net.IP {
return net.ParseIP(s)
}