File tree Expand file tree Collapse file tree
main/java/org/apache/rocketmq/common/utils
test/java/org/apache/rocketmq/common/utils
store/src/main/java/org/apache/rocketmq/store Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -179,6 +179,18 @@ public static String normalizeHostAddress(final InetAddress localHost) {
179179 }
180180 }
181181
182+ public static String denormalizeHostAddress (final String bracketedAddress ) {
183+ if (bracketedAddress == null ) {
184+ return null ;
185+ }
186+
187+ if (bracketedAddress .startsWith ("[" ) && bracketedAddress .endsWith ("]" )) {
188+ return bracketedAddress .substring (1 , bracketedAddress .length () - 1 );
189+ } else {
190+ return bracketedAddress ;
191+ }
192+ }
193+
182194 public static SocketAddress string2SocketAddress (final String addr ) {
183195 int split = addr .lastIndexOf (":" );
184196 String host = addr .substring (0 , split );
@@ -211,4 +223,18 @@ private static boolean isBridge(NetworkInterface networkInterface) {
211223 }
212224 return false ;
213225 }
226+
227+ // valid various ipv6 format like:
228+ // with scope 2001:0db8:85a3:0000:0000:8a2e:0370:7334%eth0
229+ // with bracketed [2001:0db8:85a3:0000:0000:8a2e:0370:7334]
230+ public static boolean validCommonInet6Address (String ipOrCidr ) {
231+ String ipWithoutBracketed = denormalizeHostAddress (ipOrCidr );
232+ if (ipWithoutBracketed != null && ipWithoutBracketed .length () != 0 ) {
233+ InetAddressValidator validator = InetAddressValidator .getInstance ();
234+ if (validator .isValidInet6Address (ipWithoutBracketed .split ("%" )[0 ])) {
235+ return true ;
236+ }
237+ }
238+ return false ;
239+ }
214240}
Original file line number Diff line number Diff line change 1818
1919import org .junit .Test ;
2020
21+ import static org .apache .rocketmq .common .utils .NetworkUtil .validCommonInet6Address ;
22+
2123public class IPAddressUtilsTest {
2224
2325 @ Test
@@ -72,4 +74,19 @@ public void isValidIPOrCidr() {
7274 assert IPAddressUtils .isValidIPOrCidr (ipv4Cidr );
7375 assert IPAddressUtils .isValidIPOrCidr (ipv6Cidr );
7476 }
77+
78+ @ Test
79+ public void isValidIPv6Common () {
80+ String ipv6WithoutScope = "2001:0db8:85a3:0000:0000:8a2e:0370:7334" ;
81+ assert validCommonInet6Address (ipv6WithoutScope );
82+ String ipv6WithScope = "2001:0db8:85a3:0000:0000:8a2e:0370:7334%eth0" ;
83+ assert validCommonInet6Address (ipv6WithScope );
84+ String ipv6WithBracketedAndScope = "[2001:0db8:85a3:0000:0000:8a2e:0370:7334%eth0]" ;
85+ assert validCommonInet6Address (ipv6WithBracketedAndScope );
86+ String ipv4 = "192.168.1.0" ;
87+ assert !validCommonInet6Address (ipv4 );
88+ String ipv4Cidr = "192.168.1.0/24" ;
89+ assert !validCommonInet6Address (ipv4Cidr );
90+ }
91+
7592}
Original file line number Diff line number Diff line change 5757import java .util .concurrent .atomic .AtomicLong ;
5858import java .util .function .Supplier ;
5959import org .apache .commons .lang3 .StringUtils ;
60- import org .apache .commons .validator .routines .InetAddressValidator ;
6160import org .apache .rocketmq .common .AbstractBrokerRunnable ;
6261import org .apache .rocketmq .common .BoundaryType ;
6362import org .apache .rocketmq .common .BrokerConfig ;
8079import org .apache .rocketmq .common .sysflag .MessageSysFlag ;
8180import org .apache .rocketmq .common .topic .TopicValidator ;
8281import org .apache .rocketmq .common .utils .CleanupPolicyUtils ;
82+ import org .apache .rocketmq .common .utils .NetworkUtil ;
8383import org .apache .rocketmq .common .utils .QueueTypeUtils ;
8484import org .apache .rocketmq .common .utils .ServiceProvider ;
8585import org .apache .rocketmq .common .utils .ThreadUtils ;
@@ -1174,8 +1174,7 @@ public long getEarliestMessageTime() {
11741174 minPhyOffset += DLedgerEntry .BODY_OFFSET ;
11751175 }
11761176 int size = MessageDecoder .MESSAGE_STORE_TIMESTAMP_POSITION + 8 ;
1177- InetAddressValidator validator = InetAddressValidator .getInstance ();
1178- if (validator .isValidInet6Address (this .brokerConfig .getBrokerIP1 ())) {
1177+ if (NetworkUtil .validCommonInet6Address (this .brokerConfig .getBrokerIP1 ())) {
11791178 size = MessageDecoder .MESSAGE_STORE_TIMESTAMP_POSITION + 20 ;
11801179 }
11811180 return this .getCommitLog ().pickupStoreTimestamp (minPhyOffset , size );
You can’t perform that action at this time.
0 commit comments