forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
44 lines (37 loc) · 1.1 KB
/
tcp.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
package transport
import (
"fmt"
"net"
"strings"
"time"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/testing"
)
func NetDialer(timeout time.Duration) Dialer {
return TestNetDialer(testing.NullDriver, timeout)
}
func TestNetDialer(d testing.Driver, timeout time.Duration) Dialer {
return DialerFunc(func(network, address string) (net.Conn, error) {
switch network {
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
default:
d.Fatal("network type", fmt.Errorf("unsupported network type %v", network))
return nil, fmt.Errorf("unsupported network type %v", network)
}
host, port, err := net.SplitHostPort(address)
d.Fatal("parse host", err)
if err != nil {
return nil, err
}
addresses, err := net.LookupHost(host)
d.Fatal("dns lookup", err)
d.Info("addresses", strings.Join(addresses, ", "))
if err != nil {
logp.Warn(`DNS lookup failure "%s": %v`, host, err)
return nil, err
}
// dial via host IP by randomized iteration of known IPs
dialer := &net.Dialer{Timeout: timeout}
return DialWith(dialer, network, host, addresses, port)
})
}