-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnet_util.go
61 lines (54 loc) · 1.5 KB
/
gnet_util.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
package main
import (
"fmt"
"net"
"strconv"
"errors"
log "github.com/gopher-net/gnet-ctl/Godeps/_workspace/src/github.com/Sirupsen/logrus"
"strings"
)
// convert AS number from string to uint32
func StrToUin32(s string) uint32 {
n, err := strconv.ParseInt(s, 0, 32)
if err != nil {
log.Errorf("[ %d ] is not a valid 32-bit AS number, please use a valid integer: %s", n, err)
}
n32 := uint32(n)
return n32
}
func isValidIp(s string) bool {
return ValidV4RegEx.MatchString(s)
}
func dnsLookup(s string) (string, error) {
ipAddr, err := net.ResolveIPAddr("ip", s)
return ipAddr.IP.String(), err
}
func getHostRoute(h string) *net.IPNet {
hr := fmt.Sprintf(h + "/32")
_, hroute, err := net.ParseCIDR(hr)
if err != nil {
log.Errorf("cidr parse error: %s", err)
return nil
}
return hroute
}
func GetCidr(ipstr string) (*net.IPNet, error) {
// check for a cidr format
if strings.Contains(ipstr, "/") {
if _, ipnet, err := net.ParseCIDR(ipstr); err == nil {
return ipnet, nil
}
}
// check for a valid IP address. if it is valid build and return a host route.
ok := isValidIp(ipstr)
if ok {
return getHostRoute(ipstr), nil
}
// if the string is not an ip address, try to resolve an ip from hostname
ipstr = strings.TrimSpace(ipstr)
if resolvedIP, err := dnsLookup(ipstr); err == nil {
log.Debug("hostname received, resolved host route:", getHostRoute(resolvedIP))
return getHostRoute(resolvedIP), nil
}
return nil, errors.New("No valid IP address, CIDR or resolvable hostname found")
}