Skip to content

Commit 49b80df

Browse files
author
Shlomi Noach
committed
Parsing ipv6 addresses
1 parent 1e067e5 commit 49b80df

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

go/logic/migrator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ func (this *Migrator) initiateInspector() (err error) {
739739
log.Infof("Master found to be %+v", *this.migrationContext.ApplierConnectionConfig.ImpliedKey)
740740
} else {
741741
// Forced master host.
742-
key, err := mysql.ParseRawInstanceKeyLoose(this.migrationContext.AssumeMasterHostname)
742+
key, err := mysql.ParseInstanceKey(this.migrationContext.AssumeMasterHostname)
743743
if err != nil {
744744
return err
745745
}

go/mysql/instance_key.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package mysql
77

88
import (
99
"fmt"
10+
"regexp"
1011
"strconv"
1112
"strings"
1213
)
@@ -15,6 +16,12 @@ const (
1516
DefaultInstancePort = 3306
1617
)
1718

19+
var (
20+
ipv4HostPortRegexp = regexp.MustCompile("^([^:]+):([0-9]+)$")
21+
ipv4HostRegexp = regexp.MustCompile("^([^:]+)$")
22+
ipv6HostPortRegexp = regexp.MustCompile("^\\[(.+)\\]:([0-9]+)$") // e.g. [2001:db8:1f70::999:de8:7648:6e8]:3308
23+
)
24+
1825
// InstanceKey is an instance indicator, identified by hostname and port
1926
type InstanceKey struct {
2027
Hostname string
@@ -25,25 +32,33 @@ const detachHint = "//"
2532

2633
// ParseInstanceKey will parse an InstanceKey from a string representation such as 127.0.0.1:3306
2734
func NewRawInstanceKey(hostPort string) (*InstanceKey, error) {
28-
tokens := strings.SplitN(hostPort, ":", 2)
29-
if len(tokens) != 2 {
30-
return nil, fmt.Errorf("Cannot parse InstanceKey from %s. Expected format is host:port", hostPort)
35+
hostname := ""
36+
port := ""
37+
if submatch := ipv4HostPortRegexp.FindStringSubmatch(hostPort); len(submatch) > 0 {
38+
hostname = submatch[1]
39+
port = submatch[2]
40+
} else if submatch := ipv4HostRegexp.FindStringSubmatch(hostPort); len(submatch) > 0 {
41+
hostname = submatch[1]
42+
} else if submatch := ipv6HostPortRegexp.FindStringSubmatch(hostPort); len(submatch) > 0 {
43+
hostname = submatch[1]
44+
port = submatch[2]
45+
} else {
46+
return nil, fmt.Errorf("Cannot parse address: %s", hostPort)
3147
}
32-
instanceKey := &InstanceKey{Hostname: tokens[0]}
33-
var err error
34-
if instanceKey.Port, err = strconv.Atoi(tokens[1]); err != nil {
35-
return instanceKey, fmt.Errorf("Invalid port: %s", tokens[1])
48+
instanceKey := &InstanceKey{Hostname: hostname, Port: DefaultInstancePort}
49+
if port != "" {
50+
var err error
51+
if instanceKey.Port, err = strconv.Atoi(port); err != nil {
52+
return instanceKey, fmt.Errorf("Invalid port: %s", port)
53+
}
3654
}
3755

3856
return instanceKey, nil
3957
}
4058

41-
// ParseRawInstanceKeyLoose will parse an InstanceKey from a string representation such as 127.0.0.1:3306.
59+
// ParseInstanceKey will parse an InstanceKey from a string representation such as 127.0.0.1:3306.
4260
// The port part is optional; there will be no name resolve
43-
func ParseRawInstanceKeyLoose(hostPort string) (*InstanceKey, error) {
44-
if !strings.Contains(hostPort, ":") {
45-
return &InstanceKey{Hostname: hostPort, Port: DefaultInstancePort}, nil
46-
}
61+
func ParseInstanceKey(hostPort string) (*InstanceKey, error) {
4762
return NewRawInstanceKey(hostPort)
4863
}
4964

go/mysql/instance_key_map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (this *InstanceKeyMap) ReadCommaDelimitedList(list string) error {
9292
}
9393
tokens := strings.Split(list, ",")
9494
for _, token := range tokens {
95-
key, err := ParseRawInstanceKeyLoose(token)
95+
key, err := ParseInstanceKey(token)
9696
if err != nil {
9797
return err
9898
}

0 commit comments

Comments
 (0)