forked from NubeDev/bacnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaildation.go
46 lines (40 loc) · 1.03 KB
/
vaildation.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
package validation
import (
"fmt"
"net"
"regexp"
"strings"
)
func ValidCIDR(ip string, cidr int) (ok bool) {
ip = fmt.Sprintf("%s/%d", ip, cidr)
_, _, err := net.ParseCIDR(ip)
if err != nil {
return
}
ok = true
return
}
func ValidPort(port int) bool {
t := fmt.Sprintf("%d", port)
re, _ := regexp.Compile(`^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$`)
if re.MatchString(t) {
return true
}
return false
}
// ValidIP return true if string ip contains a valid representation of an IPv4 or IPv6 address
func ValidIP(ip string) bool {
ipaddr := net.ParseIP(NormaliseIPAddr(ip))
return ipaddr != nil
}
// NormaliseIPAddr return ip address without /32 (IPv4 or /128 (IPv6)
func NormaliseIPAddr(ip string) string {
if strings.HasSuffix(ip, "/32") && strings.Contains(ip, ".") { // single host (IPv4)
ip = strings.TrimSuffix(ip, "/32")
} else {
if strings.HasSuffix(ip, "/128") { // single host (IPv6)
ip = strings.TrimSuffix(ip, "/128")
}
}
return ip
}