-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.go
78 lines (61 loc) · 1.95 KB
/
host.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
71
72
73
74
75
76
77
78
// SPDX-License-Identifier: MPL-2.0
/*
* Copyright (C) 2024 The Noisy Sockets Authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package network
import (
"context"
stdnet "net"
"os"
"github.com/noisysockets/pinger"
"github.com/noisysockets/resolver"
)
// Ensure that HostNetwork implements Network interface.
var _ Network = (*HostNetwork)(nil)
type HostNetwork struct {
pinger *pinger.Pinger
}
// Host returns a Network implementation that uses the standard library's network operations.
func Host() *HostNetwork {
return &HostNetwork{
pinger: pinger.New(),
}
}
func (net *HostNetwork) Close() error {
return nil
}
func (net *HostNetwork) Hostname() (string, error) {
return os.Hostname()
}
func (net *HostNetwork) Domain() (string, error) {
return resolver.Domain()
}
func (net *HostNetwork) InterfaceAddrs() ([]stdnet.Addr, error) {
return stdnet.InterfaceAddrs()
}
func (net *HostNetwork) LookupHost(host string) ([]string, error) {
return stdnet.LookupHost(host)
}
func (net *HostNetwork) LookupHostContext(ctx context.Context, host string) ([]string, error) {
return stdnet.DefaultResolver.LookupHost(ctx, host)
}
func (net *HostNetwork) Dial(network, address string) (stdnet.Conn, error) {
return stdnet.Dial(network, address)
}
func (net *HostNetwork) DialContext(ctx context.Context, network, address string) (stdnet.Conn, error) {
var d stdnet.Dialer
return d.DialContext(ctx, network, address)
}
func (net *HostNetwork) Listen(network, address string) (stdnet.Listener, error) {
return stdnet.Listen(network, address)
}
func (net *HostNetwork) ListenPacket(network, address string) (stdnet.PacketConn, error) {
return stdnet.ListenPacket(network, address)
}
func (net *HostNetwork) Ping(ctx context.Context, network, host string) error {
return net.pinger.Ping(ctx, network, host)
}