Skip to content

Commit ed12608

Browse files
committed
Fix IPv6 address parsing in Ratis consensus Utils
The address parsing methods in Utils.java used split(":") which cannot correctly handle IPv6 addresses like [::1]:10710. This caused: - NumberFormatException when parsing empty string as port - ConfigNode/DataNode startup failure when configured with IPv6 addresses Changes: - hostAddress(): Wrap IPv6 addresses in brackets [ip]:port format - fromRaftPeerAddressToTEndPoint(): Parse IPv6 format correctly - fromRaftPeerProtoToTEndPoint(): Parse IPv6 format correctly
1 parent 554ac26 commit ed12608

3 files changed

Lines changed: 59 additions & 10 deletions

File tree

  • iotdb-client
  • iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/utils

iotdb-client/client-py/iotdb/Session.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,22 @@ def init_from_node_urls(
150150
session.__hosts = []
151151
session.__ports = []
152152
for node_url in node_urls:
153-
split = node_url.split(":")
154-
session.__hosts.append(split[0])
155-
session.__ports.append(int(split[1]))
153+
# Handle IPv6 address format [ipv6]:port
154+
if node_url.startswith("["):
155+
bracket_end = node_url.find("]")
156+
if bracket_end > 0:
157+
host = node_url[1:bracket_end]
158+
# Port comes after "]:"
159+
port_str = node_url[bracket_end + 2:]
160+
session.__hosts.append(host)
161+
session.__ports.append(int(port_str))
162+
else:
163+
raise RuntimeError(f"Invalid IPv6 address format: {node_url}")
164+
else:
165+
# IPv4 format: host:port
166+
split = node_url.split(":")
167+
session.__hosts.append(split[0])
168+
session.__ports.append(int(split[1]))
156169
session.__host = session.__hosts[0]
157170
session.__port = session.__ports[0]
158171
session.__default_endpoint = TEndPoint(session.__host, session.__port)

iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public class Utils {
4242
static final String RPC_COMPRESS = "rpc_compress";
4343

4444
/**
45-
* Parse JDBC connection URL The only supported format of the URL is:
46-
* jdbc:iotdb://localhost:6667/.
45+
* Parse JDBC connection URL The only supported format of the URL is: jdbc:iotdb://localhost:6667/
46+
* or jdbc:iotdb://[::1]:6667/ for IPv6.
4747
*/
4848
static IoTDBConnectionParams parseUrl(String url, Properties info) throws IoTDBURLException {
4949
IoTDBConnectionParams params = new IoTDBConnectionParams(url);
@@ -57,10 +57,24 @@ static IoTDBConnectionParams parseUrl(String url, Properties info) throws IoTDBU
5757
String suffixURL = null;
5858
if (url.startsWith(Config.IOTDB_URL_PREFIX)) {
5959
String subURL = url.substring(Config.IOTDB_URL_PREFIX.length());
60-
int i = subURL.lastIndexOf(COLON);
61-
host = subURL.substring(0, i);
60+
int i;
61+
// Handle IPv6 address format [ipv6]:port
62+
if (subURL.startsWith("[")) {
63+
int bracketEnd = subURL.indexOf(']');
64+
if (bracketEnd > 0) {
65+
host = subURL.substring(1, bracketEnd);
66+
// Find port after "]:"
67+
i = bracketEnd + 2;
68+
} else {
69+
throw new IoTDBURLException("Invalid IPv6 address format in URL: " + url);
70+
}
71+
} else {
72+
// IPv4 format: use lastIndexOf to find port separator
73+
i = subURL.lastIndexOf(COLON);
74+
host = subURL.substring(0, i);
75+
i++;
76+
}
6277
params.setHost(host);
63-
i++;
6478
// parse port
6579
int port = 0;
6680
for (; i < subURL.length() && Character.isDigit(subURL.charAt(i)); i++) {

iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/utils/Utils.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ public class Utils {
8585
private Utils() {}
8686

8787
public static String hostAddress(TEndPoint endpoint) {
88-
return String.format("%s:%d", endpoint.getIp(), endpoint.getPort());
88+
String ip = endpoint.getIp();
89+
// IPv6 addresses need to be wrapped in brackets
90+
if (ip.contains(":")) {
91+
return String.format("[%s]:%d", ip, endpoint.getPort());
92+
}
93+
return String.format("%s:%d", ip, endpoint.getPort());
8994
}
9095

9196
public static String fromTEndPointToString(TEndPoint endpoint) {
@@ -106,6 +111,14 @@ public static RaftPeerId fromNodeIdToRaftPeerId(int nodeId) {
106111
}
107112

108113
public static TEndPoint fromRaftPeerAddressToTEndPoint(String address) {
114+
// Handle IPv6 address format [ipv6]:port
115+
if (address.startsWith("[")) {
116+
int bracketEnd = address.indexOf(']');
117+
String ip = address.substring(1, bracketEnd);
118+
int port = Integer.parseInt(address.substring(bracketEnd + 2));
119+
return new TEndPoint(ip, port);
120+
}
121+
// IPv4 format: ip:port
109122
String[] items = address.split(":");
110123
return new TEndPoint(items[0], Integer.parseInt(items[1]));
111124
}
@@ -115,7 +128,16 @@ public static int fromRaftPeerIdToNodeId(RaftPeerId id) {
115128
}
116129

117130
public static TEndPoint fromRaftPeerProtoToTEndPoint(RaftPeerProto proto) {
118-
String[] items = proto.getAddress().split(":");
131+
String address = proto.getAddress();
132+
// Handle IPv6 address format [ipv6]:port
133+
if (address.startsWith("[")) {
134+
int bracketEnd = address.indexOf(']');
135+
String ip = address.substring(1, bracketEnd);
136+
int port = Integer.parseInt(address.substring(bracketEnd + 2));
137+
return new TEndPoint(ip, port);
138+
}
139+
// IPv4 format: ip:port
140+
String[] items = address.split(":");
119141
return new TEndPoint(items[0], Integer.parseInt(items[1]));
120142
}
121143

0 commit comments

Comments
 (0)