-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip.go
173 lines (129 loc) · 2.69 KB
/
ip.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
package golidators
import (
"strings"
utils "github.com/eredotpkfr/golidators/internal/utilities"
)
const (
// IP CIDR separator
cidrSep = "/"
// 0xFF -> 255
ipv4MaxPart = 0xFF
ipv4Length = 4
// 0xFFFF -> 65535
ipv6MaxPart = 0xFFFF
ipv6Length = 16
)
// Ipv4 function for validating IPv4
func Ipv4(ipv4Addr string) bool {
for index := 0; index < ipv4Length; index++ {
if len(ipv4Addr) == 0 {
return false
}
if index > 0 {
if ipv4Addr[0] != '.' {
return false
}
ipv4Addr = ipv4Addr[1:]
}
number, consumed, ok := utils.Dtoi(ipv4Addr)
if !ok || number > ipv4MaxPart {
return false
}
if consumed > 1 && ipv4Addr[0] == '0' {
return false
}
ipv4Addr = ipv4Addr[consumed:]
}
if len(ipv4Addr) != 0 {
return false
}
return true
}
// Ipv4Cidr function for validating IPv4CIDR
func Ipv4Cidr(ipv4AddrCidr string) bool {
splitted := strings.Split(ipv4AddrCidr, cidrSep)
if len(splitted) != 2 {
return false
}
prefix, suffix := splitted[0], splitted[1]
number, consumed, ok := utils.Dtoi(suffix)
if !ok || !Ipv4(prefix) || consumed > 2 {
return false
}
return 0 <= number && number <= 32
}
// Ipv6 function for validating IPv6
func Ipv6(ipv6Addr string) bool {
ellipsis := -1
if len(ipv6Addr) >= 2 && ipv6Addr[0] == ':' && ipv6Addr[1] == ':' {
ellipsis = 0
ipv6Addr = ipv6Addr[2:]
if len(ipv6Addr) == 0 {
return true
}
}
index := 0
for index < ipv6Length {
number, consumed, ok := utils.Xtoi(ipv6Addr)
if !ok || number > ipv6MaxPart {
return false
}
if consumed < len(ipv6Addr) && ipv6Addr[consumed] == '.' {
if ellipsis < 0 && index != ipv6Length-ipv4Length {
return false
}
if index+ipv4Length > ipv6Length {
return false
}
if !Ipv4(ipv6Addr) {
return false
}
ipv6Addr = ""
index += ipv4Length
break
}
index += 2
ipv6Addr = ipv6Addr[consumed:]
if len(ipv6Addr) == 0 {
break
}
if ipv6Addr[0] != ':' || len(ipv6Addr) == 1 {
return false
}
ipv6Addr = ipv6Addr[1:]
if ipv6Addr[0] == ':' {
if ellipsis >= 0 {
return false
}
ellipsis = index
ipv6Addr = ipv6Addr[1:]
if len(ipv6Addr) == 0 {
break
}
}
}
if len(ipv6Addr) != 0 {
return false
}
if index < ipv6Length {
if ellipsis < 0 {
return false
}
} else if ellipsis >= 0 {
return false
}
return true
}
// Ipv6Cidr function for validating IPv6CIDR
func Ipv6Cidr(ipv6AddrCidr string) bool {
splitted := strings.Split(ipv6AddrCidr, cidrSep)
if len(splitted) != 2 {
return false
}
prefix, suffix := splitted[0], splitted[1]
number, consumed, ok := utils.Dtoi(suffix)
if !ok || !Ipv6(prefix) || consumed > 3 {
return false
}
return 0 <= number && number <= 128
}