Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Aug 9, 2021
1 parent a61b45d commit 5fd73cb
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 100 deletions.
54 changes: 12 additions & 42 deletions netutil/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func ParseIP(s string) (ip net.IP, err error) {
ip = net.ParseIP(s)
if ip == nil {
return nil, &AddrError{
Kind: KindIP,
Kind: AddrKindIP,
Addr: s,
}
}
Expand All @@ -115,14 +115,14 @@ func ParseIP(s string) (ip net.IP, err error) {
func ParseIPv4(s string) (ip net.IP, err error) {
ip, err = ParseIP(s)
if err != nil {
err.(*AddrError).Kind = KindIPv4
err.(*AddrError).Kind = AddrKindIPv4

return nil, err
}

if ip = ip.To4(); ip == nil {
return nil, &AddrError{
Kind: KindIPv4,
Kind: AddrKindIPv4,
Addr: s,
}
}
Expand Down Expand Up @@ -174,17 +174,7 @@ func SplitHost(hostport string) (host string, err error) {
//
// Any error returned will have the underlying type of *AddrError.
func ValidateMAC(mac net.HardwareAddr) (err error) {
defer func() {
if err == nil {
return
}

err = &AddrError{
Err: err,
Kind: KindMAC,
Addr: mac.String(),
}
}()
defer makeAddrError(&err, mac.String(), AddrKindMAC)

switch l := len(mac); l {
case 0:
Expand All @@ -193,7 +183,7 @@ func ValidateMAC(mac net.HardwareAddr) (err error) {
return nil
default:
return &LengthError{
Kind: KindMAC,
Kind: AddrKindMAC,
Allowed: []int{6, 8, 20},
Length: l,
}
Expand All @@ -215,32 +205,22 @@ const MaxDomainNameLen = 253
//
// Any error returned will have the underlying type of *AddrError.
func ValidateDomainNameLabel(label string) (err error) {
defer func() {
if err == nil {
return
}

err = &AddrError{
Err: err,
Kind: KindLabel,
Addr: label,
}
}()
defer makeAddrError(&err, label, AddrKindLabel)

l := len(label)
if l == 0 {
return ErrLabelIsEmpty
} else if l > MaxDomainLabelLen {
return &LengthError{
Kind: KindLabel,
Kind: AddrKindLabel,
Max: MaxDomainLabelLen,
Length: l,
}
}

if r := rune(label[0]); !IsValidHostOuterRune(r) {
return &RuneError{
Kind: KindLabel,
Kind: AddrKindLabel,
Rune: r,
}
} else if l == 1 {
Expand All @@ -250,15 +230,15 @@ func ValidateDomainNameLabel(label string) (err error) {
for _, r := range label[1 : l-1] {
if !IsValidHostInnerRune(r) {
return &RuneError{
Kind: KindLabel,
Kind: AddrKindLabel,
Rune: r,
}
}
}

if r := rune(label[l-1]); !IsValidHostOuterRune(r) {
return &RuneError{
Kind: KindLabel,
Kind: AddrKindLabel,
Rune: r,
}
}
Expand All @@ -273,17 +253,7 @@ func ValidateDomainNameLabel(label string) (err error) {
//
// Any error returned will have the underlying type of *AddrError.
func ValidateDomainName(name string) (err error) {
defer func() {
if err == nil {
return
}

err = &AddrError{
Err: err,
Kind: KindName,
Addr: name,
}
}()
defer makeAddrError(&err, name, AddrKindName)

name, err = idna.ToASCII(name)
if err != nil {
Expand All @@ -295,7 +265,7 @@ func ValidateDomainName(name string) (err error) {
return ErrAddrIsEmpty
} else if l > MaxDomainNameLen {
return &LengthError{
Kind: KindName,
Kind: AddrKindName,
Max: MaxDomainNameLen,
Length: l,
}
Expand Down
52 changes: 35 additions & 17 deletions netutil/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ const (
ErrNotAReversedIP errors.Error = "not a full reversed ip address"
)

// AddrKind is the kind of address or address part used for error reporting.
type AddrKind string

// Kinds of addresses for AddrError.
const (
KindARPA = "arpa domain name"
KindHostPort = "hostport address"
KindIP = "ip address"
KindIPPort = "ipport address"
KindIPv4 = "ipv4 address"
KindLabel = "domain name label"
KindMAC = "mac address"
KindName = "domain name"
AddrKindARPA AddrKind = "arpa domain name"
AddrKindHostPort AddrKind = "hostport address"
AddrKindIP AddrKind = "ip address"
AddrKindIPPort AddrKind = "ipport address"
AddrKindIPv4 AddrKind = "ipv4 address"
AddrKindLabel AddrKind = "domain name label"
AddrKindMAC AddrKind = "mac address"
AddrKindName AddrKind = "domain name"
)

// AddrError is the underlying type of errors returned from validation
// functions when a domain name is invalid.
type AddrError struct {
// Err is the underlying error, if any.
Err error
// Kind is the kind of address. See the KindFoo constants for the list
// of values.
Kind string
// Kind is the kind of address or address part.
Kind AddrKind
// Addr is the text of the invalid address.
Addr string
}
Expand All @@ -59,12 +61,29 @@ func (err *AddrError) Unwrap() (unwrapped error) {
return err.Err
}

// makeAddrError is a deferrable helper for functions that return *AddrError.
// errPtr must be non-nil. Usage example:
//
// defer makeAddrError(&err, addr, AddrKindARPA)
//
func makeAddrError(errPtr *error, addr string, k AddrKind) {
err := *errPtr
if err == nil {
return
}

*errPtr = &AddrError{
Err: err,
Kind: k,
Addr: addr,
}
}

// LengthError is the underlying type of errors returned from validation
// functions when an address or a part of an address has a bad length.
type LengthError struct {
// Kind is the kind of address. See the KindFoo constants for the list
// of values.
Kind string
// Kind is the kind of address or address part.
Kind AddrKind
// Allowed are the allowed lengths for this kind of address. If allowed
// is empty, Max should be non-zero.
Allowed []int
Expand Down Expand Up @@ -92,9 +111,8 @@ func (err *LengthError) Error() (msg string) {
// RuneError is the underlying type of errors returned from validation functions
// when a rune in the address is invalid.
type RuneError struct {
// Kind is the kind of address. See the KindFoo constants for the list
// of values.
Kind string
// Kind is the kind of address or address part.
Kind AddrKind
// Rune is the invalid rune.
Rune rune
}
Expand Down
16 changes: 4 additions & 12 deletions netutil/hostport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,11 @@ type HostPort struct {
// ParseHostPort parses a HostPort from addr. Any error returned will have the
// underlying type of *AddrError.
func ParseHostPort(addr string) (hp *HostPort, err error) {
defer func() {
if err == nil {
return
}
defer makeAddrError(&err, addr, AddrKindHostPort)

err = &AddrError{
Err: err,
Kind: KindHostPort,
Addr: addr,
}
}()

host, port, err := SplitHostPort(addr)
var host string
var port int
host, port, err = SplitHostPort(addr)
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions netutil/hostport_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ func ExampleHostPort_String() {
hp.Port = 0
fmt.Println(hp)

hp.Host = ""
fmt.Println(hp)

// Output:
//
// example.com:12345
// [1234::cdef]:12345
// [1234::cdef]:0
// :0
}

func ExampleHostPort_UnmarshalText() {
Expand Down
28 changes: 13 additions & 15 deletions netutil/ipport.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,17 @@ func IPPortFromAddr(a net.Addr) (ipp *IPPort) {
// ParseIPPort parses an *IPPort from addr. Any error returned will have the
// underlying type of *AddrError.
func ParseIPPort(addr string) (ipp *IPPort, err error) {
defer func() {
if err == nil {
return
}

err = &AddrError{
Err: err,
Kind: KindIPPort,
Addr: addr,
}
}()

host, port, err := SplitHostPort(addr)
defer makeAddrError(&err, addr, AddrKindIPPort)

var host string
var port int
host, port, err = SplitHostPort(addr)
if err != nil {
return nil, err
}

ip, err := ParseIP(host)
var ip net.IP
ip, err = ParseIP(host)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -89,7 +82,12 @@ func (ipp IPPort) MarshalText() (b []byte, err error) {

// String implements the fmt.Stringer interface for *IPPort.
func (ipp IPPort) String() (s string) {
return JoinHostPort(ipp.IP.String(), ipp.Port)
var ipStr string
if ipp.IP != nil {
ipStr = ipp.IP.String()
}

return JoinHostPort(ipStr, ipp.Port)
}

// TCP returns a *net.TCPAddr with a clone of ipp's IP address and its port.
Expand Down
4 changes: 4 additions & 0 deletions netutil/ipport_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ func ExampleIPPort_String() {
ipp.Port = 0
fmt.Println(ipp)

ipp.IP = nil
fmt.Println(ipp)

// Output:
//
// 1.2.3.4:12345
// [1234::cdef]:12345
// [1234::cdef]:0
// :0
}

func ExampleIPPort_TCP() {
Expand Down
18 changes: 4 additions & 14 deletions netutil/reversed.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,12 @@ func IPFromReversedAddr(arpa string) (ip net.IP, err error) {
err = ValidateDomainName(arpa)
if err != nil {
bdErr := err.(*AddrError)
bdErr.Kind = KindARPA
bdErr.Kind = AddrKindARPA

return nil, bdErr
}

defer func() {
if err == nil {
return
}

err = &AddrError{
Err: err,
Kind: KindARPA,
Addr: arpa,
}
}()
defer makeAddrError(&err, arpa, AddrKindARPA)

// TODO(a.garipov): Add stringutil.HasSuffixFold and remove this.
arpa = strings.ToLower(arpa)
Expand All @@ -143,7 +133,7 @@ func IPFromReversedAddr(arpa string) (ip net.IP, err error) {
if strings.HasSuffix(arpa, arpaV6Suffix) {
if l := len(arpa); l != arpaV6MaxLen {
return nil, &LengthError{
Kind: KindARPA,
Kind: AddrKindARPA,
Allowed: []int{arpaV6MaxLen},
Length: l,
}
Expand Down Expand Up @@ -192,7 +182,7 @@ func IPToReversedAddr(ip net.IP) (arpa string, err error) {
}
} else {
return "", &AddrError{
Kind: KindIP,
Kind: AddrKindIP,
Addr: ip.String(),
}
}
Expand Down

0 comments on commit 5fd73cb

Please sign in to comment.