-
Notifications
You must be signed in to change notification settings - Fork 9
/
address.go
42 lines (32 loc) · 1015 Bytes
/
address.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
package bifrost
import (
"errors"
"regexp"
"strings"
)
var ErrIPv4Format = errors.New("invalid IPv4 format")
//Address to connect other peer
type Address struct {
IP string
}
func validIP4(ipAddress string) bool {
ipAddress = strings.Trim(ipAddress, " ")
// just ip address xxx.xxx.xxx.xxx
//re, _ := regexp.Compile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`)
// ip with port number xxx.xxx.xxx.xxx:xxxx(x) -> port number can be 0~99999 (real port numbers are in 0~65535 -> unsigned short 2bytes)
re, _ := regexp.Compile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]){1}([:][0-9][0-9][0-9][0-9][0-9]?)$`)
if re.MatchString(ipAddress) {
return true
}
return false
}
//format should be xxx.xxx.xxx.xxx:xxxx
func ToAddress(ipv4 string) (Address, error) {
valid := validIP4(ipv4)
if !valid {
return Address{}, ErrIPv4Format
}
return Address{
IP: ipv4,
}, nil
}