Skip to content

Commit 4eca197

Browse files
committed
Return errors
1 parent 3c527fe commit 4eca197

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

plugin/errors.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package plugin
2+
3+
import "errors"
4+
5+
var (
6+
ErrInvalidAddressPortPair = errors.New("invalid address:port pair")
7+
)

plugin/utils.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,52 +152,52 @@ func validateIP(ip net.IP) bool {
152152
}
153153

154154
// validateAddressPort validates an address:port string.
155-
func validateAddressPort(addressPort string) bool {
155+
func validateAddressPort(addressPort string) (bool, error) {
156156
// Split the address and port.
157157
data := strings.Split(strings.TrimSpace(addressPort), ":")
158158
if len(data) != AddressPortPairLength {
159-
return false
159+
return false, ErrInvalidAddressPortPair
160160
}
161161

162162
// Validate the port.
163163
port, err := strconv.ParseUint(data[1], 10, 16)
164164
if err != nil {
165-
return false
165+
return false, err
166166
}
167167

168168
// Resolve the IP address, if it is a host.
169169
ipAddress, err := net.ResolveIPAddr("ip", data[0])
170170
if err != nil {
171-
return false
171+
return false, err
172172
}
173173

174174
// Validate the IP address and port.
175175
if (validateIP(net.ParseIP(data[0])) || validateIP(ipAddress.IP)) && (port > 0 && port <= 65535) {
176-
return true
176+
return true, nil
177177
}
178178

179-
return false
179+
return false, nil
180180
}
181181

182182
// validateHostPort validates a host:port string.
183183
// TODO: Add support for IPv6.
184-
func validateHostPort(hostPort string) bool {
184+
func validateHostPort(hostPort string) (bool, error) {
185185
data := strings.Split(hostPort, ":")
186186
if len(data) != AddressPortPairLength {
187-
return false
187+
return false, ErrInvalidAddressPortPair
188188
}
189189

190190
port, err := strconv.ParseUint(data[1], 10, 16)
191191
if err != nil {
192-
return false
192+
return false, err
193193
}
194194

195195
// FIXME: There is not much to validate on the host side.
196196
if data[0] != "" && port > 0 && port <= 65535 {
197-
return true
197+
return true, nil
198198
}
199199

200-
return false
200+
return false, nil
201201
}
202202

203203
// isBusy checks if a client address exists in cache by matching the address

0 commit comments

Comments
 (0)