This is an automated email from the ASF dual-hosted git repository.

wenfeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new e69e907  [ISSUE #695] Feat: support IPV6 (#641)
e69e907 is described below

commit e69e9074e6d49ea4060ae43e46502cb80cd65b25
Author: 徐旭 <[email protected]>
AuthorDate: Thu Jul 1 20:45:22 2021 +0800

    [ISSUE #695] Feat: support IPV6 (#641)
    
    * support ipv6
---
 primitive/base.go      | 11 +++++++----
 primitive/base_test.go | 12 ++++++++++++
 primitive/message.go   | 35 ++++++++++++++++++++++++++---------
 primitive/result.go    |  6 ++++--
 4 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/primitive/base.go b/primitive/base.go
index 35b6268..fca34a2 100644
--- a/primitive/base.go
+++ b/primitive/base.go
@@ -23,7 +23,8 @@ import (
 )
 
 var (
-       ipRegex, _ = 
regexp.Compile(`^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))`)
+       ipv4Regex, _ = 
regexp.Compile(`^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))`)
+       ipv6Regex, _ = 
regexp.Compile(`(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0
 [...]
 )
 
 type NamesrvAddr []string
@@ -70,12 +71,14 @@ func verifyIP(ip string) error {
        if strings.Contains(ip, ";") {
                return ErrMultiIP
        }
-       ips := ipRegex.FindAllString(ip, -1)
-       if len(ips) == 0 {
+       ipV4s := ipv4Regex.FindAllString(ip, -1)
+       ipV6s := ipv6Regex.FindAllString(ip, -1)
+
+       if len(ipV4s) == 0 && len(ipV6s) == 0 {
                return ErrIllegalIP
        }
 
-       if len(ips) > 1 {
+       if len(ipV4s) > 1 || len(ipV6s) > 1 {
                return ErrMultiIP
        }
        return nil
diff --git a/primitive/base_test.go b/primitive/base_test.go
index 03a978d..db947c4 100644
--- a/primitive/base_test.go
+++ b/primitive/base_test.go
@@ -43,6 +43,18 @@ func TestVerifyIP(t *testing.T) {
        IPs = "127.0.0.1:9876;12.24.123.243:10911"
        err = verifyIP(IPs)
        assert.Equal(t, "multiple IP addr does not support", err.Error())
+
+       IPs = "bdbd:bdbd:ff:1:1:2:3:4:8888"
+       err = verifyIP(IPs)
+       assert.Nil(t, err)
+
+       IPs = "[bdbd:bdbd:ff:1:1:2:3:4]:8888"
+       err = verifyIP(IPs)
+       assert.Nil(t, err)
+
+       IPs = "[bdbd:bdbd:ff:1:1:2:3:4]:8888;[bdbd:bdbd:ff:1:1:2:3:4]:8889"
+       err = verifyIP(IPs)
+       assert.Equal(t, "multiple IP addr does not support", err.Error())
 }
 
 func TestBase(t *testing.T) {
diff --git a/primitive/message.go b/primitive/message.go
index b330dc1..b8e8f83 100644
--- a/primitive/message.go
+++ b/primitive/message.go
@@ -320,22 +320,39 @@ func DecodeMessage(data []byte) []*MessageExt {
                binary.Read(buf, binary.BigEndian, &msg.BornTimestamp)
                count += 8
 
+               var (
+                       port      int32
+                       hostBytes []byte
+               )
                // 10. born host
-               hostBytes := buf.Next(4)
-               var port int32
-               binary.Read(buf, binary.BigEndian, &port)
-               msg.BornHost = fmt.Sprintf("%s:%d", 
utils.GetAddressByBytes(hostBytes), port)
-               count += 8
+               if msg.SysFlag&FlagBornHostV6 == FlagBornHostV6 {
+                       hostBytes = buf.Next(16)
+                       binary.Read(buf, binary.BigEndian, &port)
+                       msg.BornHost = fmt.Sprintf("%s:%d", 
utils.GetAddressByBytes(hostBytes), port)
+                       count += 20
+               } else {
+                       hostBytes = buf.Next(4)
+                       binary.Read(buf, binary.BigEndian, &port)
+                       msg.BornHost = fmt.Sprintf("%s:%d", 
utils.GetAddressByBytes(hostBytes), port)
+                       count += 8
+               }
 
                // 11. store timestamp
                binary.Read(buf, binary.BigEndian, &msg.StoreTimestamp)
                count += 8
 
                // 12. store host
-               hostBytes = buf.Next(4)
-               binary.Read(buf, binary.BigEndian, &port)
-               msg.StoreHost = fmt.Sprintf("%s:%d", 
utils.GetAddressByBytes(hostBytes), port)
-               count += 8
+               if msg.SysFlag&FlagStoreHostV6 == FlagStoreHostV6 {
+                       hostBytes = buf.Next(16)
+                       binary.Read(buf, binary.BigEndian, &port)
+                       msg.StoreHost = fmt.Sprintf("%s:%d", 
utils.GetAddressByBytes(hostBytes), port)
+                       count += 20
+               } else {
+                       hostBytes = buf.Next(4)
+                       binary.Read(buf, binary.BigEndian, &port)
+                       msg.StoreHost = fmt.Sprintf("%s:%d", 
utils.GetAddressByBytes(hostBytes), port)
+                       count += 8
+               }
 
                // 13. reconsume times
                binary.Read(buf, binary.BigEndian, &msg.ReconsumeTimes)
diff --git a/primitive/result.go b/primitive/result.go
index b3d6479..20d393a 100644
--- a/primitive/result.go
+++ b/primitive/result.go
@@ -31,8 +31,10 @@ const (
        SendSlaveNotAvailable
        SendUnknownError
 
-       FlagCompressed = 0x1
-       MsgIdLength    = 8 + 8
+       FlagCompressed  = 0x1
+       FlagBornHostV6  = 0x1 << 4
+       FlagStoreHostV6 = 0x1 << 5
+       MsgIdLength     = 8 + 8
 
        propertySeparator  = '\002'
        nameValueSeparator = '\001'

Reply via email to