-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddr.go
240 lines (203 loc) · 5.66 KB
/
addr.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright 2017 Farsight Security, Inc.
*
* 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 config
import (
"encoding/json"
"fmt"
"net"
"strings"
)
// Addr is a generic network address with JSON and YAML Marshaler and
// Unmarshaler methods.
type Addr struct{ net.Addr }
type addr struct {
Net string
Addr string
}
func (a *addr) Network() string {
return a.Net
}
func (a *addr) String() string {
return a.Addr
}
type errAddrFormatInvalid string
func (e errAddrFormatInvalid) Error() string {
return fmt.Sprintf("Invalid address format '%s':"+
" should be net:addr", string(e))
}
// Set satisfies flag.Value for use with command line flags.
func (a *Addr) Set(s string) error {
l := strings.SplitN(s, ":", 2)
if len(l) < 2 {
return errAddrFormatInvalid(s)
}
a.Addr = &addr{l[0], l[1]}
return nil
}
// UnmarshalJSON satisfies the json.Unmarshaler interface
func (a *Addr) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return a.Set(s)
}
// UnmarshalYAML satisfies the yaml.Unmarshaler interface
func (a *Addr) UnmarshalYAML(u func(interface{}) error) error {
var s string
if err := u(&s); err != nil {
return err
}
return a.Set(s)
}
// MarshalJSON satisfies the json.Marshaler interface
func (a Addr) MarshalJSON() ([]byte, error) {
return json.Marshal(fmt.Sprintf("%s:%s", a.Network(), a.String()))
}
// MarshalYAML satisfies the yaml.Marshaler interface
func (a Addr) MarshalYAML() (interface{}, error) {
return fmt.Sprintf("%s:%s", a.Network(), a.String()), nil
}
type errInvalidUDPNetwork string
func (e errInvalidUDPNetwork) Error() string {
return fmt.Sprintf("Invalid UDP network '%s'", string(e))
}
type errInvalidTCPNetwork string
func (e errInvalidTCPNetwork) Error() string {
return fmt.Sprintf("Invalid TCP network '%s'", string(e))
}
type errInvalidUnixNetwork string
func (e errInvalidUnixNetwork) Error() string {
return fmt.Sprintf("Invalid Unix network '%s'", string(e))
}
// UDPAddr is an address restricted to be in the network "udp",
// "udp4", or "udp6". Other networks are considered an error.
type UDPAddr struct{ *net.UDPAddr }
// Set satisfies flag.Value for use in command line arguments
func (u *UDPAddr) Set(s string) (err error) {
var a Addr
if err = a.Set(s); err != nil {
return
}
n := a.Network()
if n != "udp" && n != "udp4" && n != "udp6" {
return errInvalidUDPNetwork(n)
}
u.UDPAddr, err = net.ResolveUDPAddr(n, a.String())
return
}
// UnmarshalJSON satisfies the json.Unmarshaler interface
func (u *UDPAddr) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return u.Set(s)
}
// UnmarshalYAML satisfies the yaml.Unmarshaler interface
func (u *UDPAddr) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
return u.Set(s)
}
// MarshalJSON satisfies the json.Marshaler interface
func (u UDPAddr) MarshalJSON() ([]byte, error) {
a := Addr{u.UDPAddr}
return a.MarshalJSON()
}
// MarshalYAML satisfies the yaml.Marshaler interface
func (u UDPAddr) MarshalYAML() (interface{}, error) {
a := Addr{u.UDPAddr}
return a.MarshalYAML()
}
// TCPAddr is an address restricted to be in the "tcp", "tcp4", or "tcp6"
// networks.
type TCPAddr struct{ *net.TCPAddr }
// Set satisfies flag.Value for use in command line arguments
func (t *TCPAddr) Set(s string) (err error) {
var a Addr
if err = a.Set(s); err != nil {
return
}
n := a.Network()
if n != "tcp" && n != "tcp4" && n != "tcp6" {
return errInvalidTCPNetwork(n)
}
t.TCPAddr, err = net.ResolveTCPAddr(n, a.String())
return
}
// UnmarshalJSON satisfies the json.Unmarshaler interface
func (t *TCPAddr) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return t.Set(s)
}
// UnmarshalYAML satisfies the yaml.Unmarshaler interface
func (t *TCPAddr) UnmarshalYAML(u func(interface{}) error) error {
var s string
if err := u(&s); err != nil {
return err
}
return t.Set(s)
}
// MarshalJSON satisfies the json.Marshaler interface
func (t TCPAddr) MarshalJSON() ([]byte, error) {
a := Addr{t.TCPAddr}
return a.MarshalJSON()
}
// MarshalYAML satisfies the yaml.Marshaler interface
func (t TCPAddr) MarshalYAML() (interface{}, error) {
a := Addr{t.TCPAddr}
return a.MarshalYAML()
}
// UnixAddr is a unix-domain socket address in the "unix", "unixpacket",
// or "unixgram" network
type UnixAddr struct{ *net.UnixAddr }
// Set satisfies flag.Value for use in command line arguments
func (u *UnixAddr) Set(s string) (err error) {
var a Addr
if err = a.Set(s); err != nil {
return
}
n := a.Network()
if n != "unix" && n != "unixpacket" && n != "unixgram" {
return errInvalidUnixNetwork(n)
}
u.UnixAddr, err = net.ResolveUnixAddr(n, a.String())
return
}
// UnmarshalJSON satisfies the json.Unmarshaler interface
func (u *UnixAddr) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return u.Set(s)
}
// UnmarshalYAML satisfies the yaml.Unmarshaler interface
func (u *UnixAddr) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
return u.Set(s)
}
// MarshalJSON satisfies the json.Marshaler interface
func (u UnixAddr) MarshalJSON() ([]byte, error) {
a := Addr{u.UnixAddr}
return a.MarshalJSON()
}
// MarshalYAML satisfies the yaml.Marshaler interface
func (u UnixAddr) MarshalYAML() (interface{}, error) {
a := Addr{u.UnixAddr}
return a.MarshalYAML()
}