Debian Codegraph @

config/confignet/confignet.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package confignet // import "go.opentelemetry.io/collector/config/confignet"
5
6 import (
7 "context"
8 "fmt"
9 "net"
10 "time"
11 )
12
13 // TransportType represents a type of network transport protocol
14 type TransportType string
15
16 const (
17 TransportTypeTCP TransportType = "tcp"
18 TransportTypeTCP4 TransportType = "tcp4"
19 TransportTypeTCP6 TransportType = "tcp6"
20 TransportTypeUDP TransportType = "udp"
21 TransportTypeUDP4 TransportType = "udp4"
22 TransportTypeUDP6 TransportType = "udp6"
23 TransportTypeIP TransportType = "ip"
24 TransportTypeIP4 TransportType = "ip4"
25 TransportTypeIP6 TransportType = "ip6"
26 TransportTypeUnix TransportType = "unix"
27 TransportTypeUnixgram TransportType = "unixgram"
28 TransportTypeUnixPacket TransportType = "unixpacket"
29 transportTypeEmpty TransportType = ""
30 )
31
32 // UnmarshalText unmarshalls text to a TransportType.
33 // Valid values are "tcp", "tcp4", "tcp6", "udp", "udp4",
34 // "udp6", "ip", "ip4", "ip6", "unix", "unixgram" and "unixpacket"
35 func (tt *TransportType) UnmarshalText(in []byte) error {
36 typ := TransportType(in)
37 switch typ {
38 case TransportTypeTCP,
39 TransportTypeTCP4,
40 TransportTypeTCP6,
41 TransportTypeUDP,
42 TransportTypeUDP4,
43 TransportTypeUDP6,
44 TransportTypeIP,
45 TransportTypeIP4,
46 TransportTypeIP6,
47 TransportTypeUnix,
48 TransportTypeUnixgram,
49 TransportTypeUnixPacket,
50 transportTypeEmpty:
51 *tt = typ
52 return nil
53 default:
54 return fmt.Errorf("unsupported transport type %q", typ)
55 }
56 }
57
58 // DialerConfig contains options for connecting to an address.
59 type DialerConfig struct {
60 // Timeout is the maximum amount of time a dial will wait for
61 // a connect to complete. The default is no timeout.
62 Timeout time.Duration `mapstructure:"timeout,omitempty"`
63 // prevent unkeyed literal initialization
64 _ struct{}
65 }
66
67 // NewDefaultDialerConfig creates a new DialerConfig with any default values set
68 func NewDefaultDialerConfig() DialerConfig {
69 return DialerConfig{}
70 }
71
72 // AddrConfig represents a network endpoint address.
73 type AddrConfig struct {
74 // Endpoint configures the address for this network connection.
75 // For TCP and UDP networks, the address has the form "host:port". The host must be a literal IP address,
76 // or a host name that can be resolved to IP addresses. The port must be a literal port number or a service name.
77 // If the host is a literal IPv6 address it must be enclosed in square brackets, as in "[2001:db8::1]:80" or
78 // "[fe80::1%zone]:80". The zone specifies the scope of the literal IPv6 address as defined in RFC 4007.
79 Endpoint string `mapstructure:"endpoint,omitempty"`
80
81 // Transport to use. Allowed protocols are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only),
82 // "udp6" (IPv6-only), "ip", "ip4" (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and "unixpacket".
83 Transport TransportType `mapstructure:"transport,omitempty"`
84
85 // DialerConfig contains options for connecting to an address.
86 DialerConfig DialerConfig `mapstructure:"dialer,omitempty"`
87 // prevent unkeyed literal initialization
88 _ struct{}
89 }
90
91 // NewDefaultAddrConfig creates a new AddrConfig with any default values set
92 func NewDefaultAddrConfig() AddrConfig {
93 return AddrConfig{
94 DialerConfig: NewDefaultDialerConfig(),
95 }
96 }
97
98 // Dial equivalent with net.Dialer's DialContext for this address.
99 func (na *AddrConfig) Dial(ctx context.Context) (net.Conn, error) {
100 d := net.Dialer{Timeout: na.DialerConfig.Timeout}
101 return d.DialContext(ctx, string(na.Transport), na.Endpoint)
102 }
103
104 // Listen equivalent with net.ListenConfig's Listen for this address.
105 func (na *AddrConfig) Listen(ctx context.Context) (net.Listener, error) {
106 lc := net.ListenConfig{}
107 return lc.Listen(ctx, string(na.Transport), na.Endpoint)
108 }
109
110 func (na *AddrConfig) Validate() error {
111 switch na.Transport {
112 case TransportTypeTCP,
113 TransportTypeTCP4,
114 TransportTypeTCP6,
115 TransportTypeUDP,
116 TransportTypeUDP4,
117 TransportTypeUDP6,
118 TransportTypeIP,
119 TransportTypeIP4,
120 TransportTypeIP6,
121 TransportTypeUnix,
122 TransportTypeUnixgram,
123 TransportTypeUnixPacket:
124 return nil
125 default:
126 return fmt.Errorf("invalid transport type %q", na.Transport)
127 }
128 }
129
130 // TCPAddrConfig represents a TCP endpoint address.
131 type TCPAddrConfig struct {
132 // Endpoint configures the address for this network connection.
133 // The address has the form "host:port". The host must be a literal IP address, or a host name that can be
134 // resolved to IP addresses. The port must be a literal port number or a service name.
135 // If the host is a literal IPv6 address it must be enclosed in square brackets, as in "[2001:db8::1]:80" or
136 // "[fe80::1%zone]:80". The zone specifies the scope of the literal IPv6 address as defined in RFC 4007.
137 Endpoint string `mapstructure:"endpoint,omitempty"`
138
139 // DialerConfig contains options for connecting to an address.
140 DialerConfig DialerConfig `mapstructure:"dialer,omitempty"`
141 // prevent unkeyed literal initialization
142 _ struct{}
143 }
144
145 // NewDefaultTCPAddrConfig creates a new TCPAddrConfig with any default values set
146 func NewDefaultTCPAddrConfig() TCPAddrConfig {
147 return TCPAddrConfig{
148 DialerConfig: NewDefaultDialerConfig(),
149 }
150 }
151
152 // Dial equivalent with net.Dialer's DialContext for this address.
153 func (na *TCPAddrConfig) Dial(ctx context.Context) (net.Conn, error) {
154 d := net.Dialer{Timeout: na.DialerConfig.Timeout}
155 return d.DialContext(ctx, string(TransportTypeTCP), na.Endpoint)
156 }
157
158 // Listen equivalent with net.ListenConfig's Listen for this address.
159 func (na *TCPAddrConfig) Listen(ctx context.Context) (net.Listener, error) {
160 lc := net.ListenConfig{}
161 return lc.Listen(ctx, string(TransportTypeTCP), na.Endpoint)
162 }