From 2a00e2c6e9ae4771f66b66dd01bde7432ef22519 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Wed, 14 Oct 2015 08:43:04 +0200 Subject: [PATCH] Fix for IPv4 address --- common/net/address.go | 12 ++++++++++++ common/net/address_test.go | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/common/net/address.go b/common/net/address.go index 14b1bc16..5a70f18a 100644 --- a/common/net/address.go +++ b/common/net/address.go @@ -22,6 +22,15 @@ type Address interface { String() string // String representation of this Address } +func allZeros(data []byte) bool { + for _, v := range data { + if v != 0 { + return false + } + } + return true +} + // IPAddress creates an Address with given IP and port. func IPAddress(ip []byte, port uint16) Address { switch len(ip) { @@ -31,6 +40,9 @@ func IPAddress(ip []byte, port uint16) Address { ip: [4]byte{ip[0], ip[1], ip[2], ip[3]}, } case net.IPv6len: + if allZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff { + return IPAddress(ip[12:16], port) + } return IPv6Address{ PortAddress: PortAddress{port: port}, ip: [16]byte{ diff --git a/common/net/address_test.go b/common/net/address_test.go index 29b62fe2..bb26a150 100644 --- a/common/net/address_test.go +++ b/common/net/address_test.go @@ -1,6 +1,7 @@ package net import ( + "net" "testing" "github.com/v2ray/v2ray-core/testing/unit" @@ -55,3 +56,13 @@ func TestDomainAddress(t *testing.T) { assert.Uint16(addr.Port()).Equals(port) assert.String(addr.String()).Equals("v2ray.com:443") } + +func TestNetIPv4Address(t *testing.T) { + assert := unit.Assert(t) + + ip := net.IPv4(1, 2, 3, 4) + port := uint16(80) + addr := IPAddress(ip, port) + assert.Bool(addr.IsIPv4()).IsTrue() + assert.String(addr.String()).Equals("1.2.3.4:80") +}