github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/extension/gohanscript/lib/iputil.go (about)

     1  package lib
     2  
     3  import (
     4  	"math/big"
     5  	"net"
     6  	"strings"
     7  )
     8  
     9  //IPToInt converts ip string to int
    10  func IPToInt(ip string) int {
    11  	i := big.NewInt(0)
    12  	i.SetBytes(net.ParseIP(ip).To4())
    13  	return int(i.Int64())
    14  }
    15  
    16  //IntToIP converts int to ip string
    17  func IntToIP(value int) string {
    18  	i := big.NewInt(0)
    19  	i.SetInt64(int64(value))
    20  	ip := net.IP(i.Bytes())
    21  	return ip.String()
    22  }
    23  
    24  //IPAdd adds int for ip
    25  func IPAdd(ip string, value int) string {
    26  	i := big.NewInt(0)
    27  	if strings.Contains(ip, ".") {
    28  		i.SetBytes(net.ParseIP(ip).To4())
    29  	} else {
    30  		i.SetBytes(net.ParseIP(ip).To16())
    31  	}
    32  	j := big.NewInt(int64(value))
    33  	i.Add(i, j)
    34  	ipObj := net.IP(i.Bytes())
    35  	return ipObj.String()
    36  }
    37  
    38  //ParseCidr parse cidr for start ip and number of ips
    39  func ParseCidr(cidr string) (string, int, int) {
    40  	_, n, err := net.ParseCIDR(cidr)
    41  	if err != nil {
    42  		return "", -1, -1
    43  	}
    44  	ones, bits := n.Mask.Size()
    45  	return n.IP.String(), ones, bits
    46  }
    47  
    48  //FloatToInt converts float to int
    49  func FloatToInt(value float64) int {
    50  	return int(value)
    51  }