kos/util/ip/ip.go

29 lines
655 B
Go
Raw Permalink Normal View History

2023-04-23 17:57:36 +08:00
package ip
import (
"encoding/binary"
"net"
)
// isUp Interface is up
func isUp(v net.Flags) bool {
return v&net.FlagUp == net.FlagUp
}
// ToLong Converts a string containing an (IPv4) Internet Protocol dotted address into a long integer
func ToLong(ipAddress string) uint32 {
ip := net.ParseIP(ipAddress)
if ip == nil {
return 0
}
return binary.BigEndian.Uint32(ip.To4())
}
// FromLong Converts a long integer address into a string in (IPv4) Internet standard dotted format
func FromLong(properAddress uint32) string {
ipByte := make([]byte, 4)
binary.BigEndian.PutUint32(ipByte, properAddress)
ip := net.IP(ipByte)
return ip.String()
}