-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.go
88 lines (83 loc) · 1.74 KB
/
validate.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
package pubip
import (
"encoding/hex"
"errors"
"fmt"
"strconv"
"strings"
)
var (
errNotV6Address = errors.New("not a IPv6 address")
errNotV4Address = errors.New("not a IPv4 address")
)
// IsValid returns false if the ip address format is faulty.
func IsValid(ip string) bool {
ipLen := len(ip)
if ipLen == 0 {
return false
}
if ipLen <= 15 { // max IPv4 len is 15 bytes: 255.255.255.255
if IsIPv4(ip) {
return true
}
}
return IsIPv6(ip)
}
// IsIPv6 is true if the passed string is a valid IPv6 address.
func IsIPv6(ip string) bool {
// is empty or exceeds max length
if l := len(ip); l == 0 || l > 45 {
return false
}
a := strings.Split(ip, ":")
// check if last block is IPv4 notation
if strings.Contains(ip, ".") {
// check IPv4 block
if !IsIPv4(a[len(a)-1]) {
return false
}
// remove IPv4 block from slice
a = a[:len(a)-1]
}
// check block count
if l := len(a); l < 1 || l > 8 {
return false
}
var hasContent bool
for _, block := range a {
blockLen := len(block)
if !hasContent && blockLen > 0 {
hasContent = true
}
// skip block containing only one zero – it is a valid value
if block == "0" {
continue
}
// maybe add leading zeros to block, eg db8 to 0db8
if blockLen != 4 {
block = fmt.Sprintf("%#04s", block)
}
if b, err := hex.DecodeString(block); err != nil || len(b) == 0 {
return false
}
}
return hasContent
}
// IsIPv4 is true if the passed string is a valid IPv4 address.
func IsIPv4(ip string) bool {
// is empty
if len(ip) == 0 {
return false
}
a := strings.Split(ip, ".")
// check block count
if len(a) != 4 {
return false
}
for _, block := range a {
if i, err := strconv.Atoi(block); err != nil || i < 0 || i > 255 {
return false
}
}
return true
}