From 4121e071fc9ce2d97a41a1bee3e17ad7fcdedc35 Mon Sep 17 00:00:00 2001 From: oldme Date: Thu, 28 Nov 2024 18:03:18 +0800 Subject: [PATCH] feat(net/gipv4): add enhanced the conversion between uint32 and string --- net/gipv4/gipv4.go | 19 +-------- net/gipv4/gipv4_convert.go | 59 ++++++++++++++++++++++++++ net/gipv4/gipv4_z_unit_convert_test.go | 54 +++++++++++++++++++++++ 3 files changed, 114 insertions(+), 18 deletions(-) create mode 100644 net/gipv4/gipv4_convert.go create mode 100644 net/gipv4/gipv4_z_unit_convert_test.go diff --git a/net/gipv4/gipv4.go b/net/gipv4/gipv4.go index ea52f913145..df0c3d6dd6e 100644 --- a/net/gipv4/gipv4.go +++ b/net/gipv4/gipv4.go @@ -6,33 +6,16 @@ // // Package gipv4 provides useful API for IPv4 address handling. + package gipv4 import ( - "encoding/binary" "fmt" - "net" "strconv" "github.com/gogf/gf/v2/text/gregex" ) -// Ip2long converts ip address to an uint32 integer. -func Ip2long(ip string) uint32 { - netIp := net.ParseIP(ip) - if netIp == nil { - return 0 - } - return binary.BigEndian.Uint32(netIp.To4()) -} - -// Long2ip converts an uint32 integer ip address to its string type address. -func Long2ip(long uint32) string { - ipByte := make([]byte, 4) - binary.BigEndian.PutUint32(ipByte, long) - return net.IP(ipByte).String() -} - // Validate checks whether given `ip` a valid IPv4 address. func Validate(ip string) bool { return gregex.IsMatchString(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`, ip) diff --git a/net/gipv4/gipv4_convert.go b/net/gipv4/gipv4_convert.go new file mode 100644 index 00000000000..47d82555c4f --- /dev/null +++ b/net/gipv4/gipv4_convert.go @@ -0,0 +1,59 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. +// + +// Package gipv4 provides useful API for IPv4 address handling. + +package gipv4 + +import ( + "encoding/binary" + "net" +) + +// IpToLongBigEndian converts ip address to an uint32 integer with big endian. +func IpToLongBigEndian(ip string) uint32 { + netIp := net.ParseIP(ip) + if netIp == nil { + return 0 + } + return binary.BigEndian.Uint32(netIp.To4()) +} + +// LongToIpBigEndian converts an uint32 integer ip address to its string type address with big endian. +func LongToIpBigEndian(long uint32) string { + ipByte := make([]byte, 4) + binary.BigEndian.PutUint32(ipByte, long) + return net.IP(ipByte).String() +} + +// IpToLongLittleEndian converts ip address to an uint32 integer with little endian. +func IpToLongLittleEndian(ip string) uint32 { + netIp := net.ParseIP(ip) + if netIp == nil { + return 0 + } + return binary.LittleEndian.Uint32(netIp.To4()) +} + +// LongToIpLittleEndian converts an uint32 integer ip address to its string type address with little endian. +func LongToIpLittleEndian(long uint32) string { + ipByte := make([]byte, 4) + binary.LittleEndian.PutUint32(ipByte, long) + return net.IP(ipByte).String() +} + +// Ip2long converts ip address to an uint32 integer. +// Deprecated: Use IpToLongBigEndian instead. +func Ip2long(ip string) uint32 { + return IpToLongBigEndian(ip) +} + +// Long2ip converts an uint32 integer ip address to its string type address. +// Deprecated: Use LongToIpBigEndian instead. +func Long2ip(long uint32) string { + return LongToIpBigEndian(long) +} diff --git a/net/gipv4/gipv4_z_unit_convert_test.go b/net/gipv4/gipv4_z_unit_convert_test.go new file mode 100644 index 00000000000..19ef9c5f5ec --- /dev/null +++ b/net/gipv4/gipv4_z_unit_convert_test.go @@ -0,0 +1,54 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gipv4_test + +import ( + "testing" + + "github.com/gogf/gf/v2/net/gipv4" + "github.com/gogf/gf/v2/test/gtest" +) + +const ( + ipv4 string = "192.168.1.1" + longBigEndian uint32 = 3232235777 + longLittleEndian uint32 = 16885952 +) + +func TestIpToLongBigEndian(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var u = gipv4.IpToLongBigEndian(ipv4) + t.Assert(u, longBigEndian) + + var u2 = gipv4.Ip2long(ipv4) + t.Assert(u2, longBigEndian) + }) +} + +func TestLongToIpBigEndian(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var s = gipv4.LongToIpBigEndian(longBigEndian) + t.Assert(s, ipv4) + + var s2 = gipv4.Long2ip(longBigEndian) + t.Assert(s2, ipv4) + }) +} + +func TestIpToLongLittleEndian(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var u = gipv4.IpToLongLittleEndian(ipv4) + t.Assert(u, longLittleEndian) + }) +} + +func TestLongToIpLittleEndian(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var s = gipv4.LongToIpLittleEndian(longLittleEndian) + t.Assert(s, ipv4) + }) +}