Skip to content

Commit bf95e00

Browse files
added Address#isLocal and Address#isRemote
1 parent 4183fb2 commit bf95e00

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

src/main/java/codes/laivy/address/Address.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ static boolean validate(@NotNull String string) {
7979
*/
8080
@NotNull String toString(@NotNull Port port);
8181

82+
/**
83+
* Checks if this address belongs to the localhost.
84+
*
85+
* @return {@code true} if this address is a localhost address; {@code false} otherwise.
86+
*/
87+
boolean isLocal();
88+
89+
/**
90+
* Checks if this address belongs to the remote network.
91+
*
92+
* @return {@code true} if this address is a remote address; {@code false} otherwise.
93+
*/
94+
default boolean isRemote() {
95+
return !isLocal();
96+
}
97+
8298
// Cloneable
8399

84100
@NotNull Address clone();

src/main/java/codes/laivy/address/domain/Domain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private Domain(@NotNull Subdomain @NotNull [] subdomains, @NotNull SLD sld, @Nul
200200
*
201201
* @return {@code true} if this domain is "localhost", {@code false} otherwise
202202
*/
203-
public boolean isLocalhost() {
203+
public boolean isLocal() {
204204
return getTLD() == null && getSLD().equalsIgnoreCase("localhost");
205205
}
206206

src/main/java/codes/laivy/address/ip/IPv4Address.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public IPv4Address(int @NotNull [] octets) {
233233
*
234234
* @return {@code true} if this IPv4 address is a localhost address; {@code false} otherwise.
235235
*/
236-
public boolean isLocalhost() {
236+
public boolean isLocal() {
237237
return octets[0] == 127;
238238
}
239239

@@ -315,7 +315,7 @@ public boolean isMulticast() {
315315
* @return {@code true} if this IPv4 address is publicly routable; {@code false} otherwise.
316316
*/
317317
public boolean isPubliclyRoutable() {
318-
return !isPrivate() && !isLocalhost() && !isMulticast();
318+
return !isPrivate() && !isLocal() && !isMulticast();
319319
}
320320

321321
/**

src/main/java/codes/laivy/address/ip/IPv6Address.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,36 @@ public boolean isWithinRange(@NotNull IPv6Address start, @NotNull IPv6Address en
381381
return new IPv6Address(broadcastGroups);
382382
}
383383

384+
/**
385+
* Determines if the current IPv6 address belongs to a local network.
386+
* Specifically, it checks if the address is a link-local address in the
387+
* IPv6 range `fe80::/10`.
388+
* Link-local addresses are typically used for
389+
* communication within a single network segment or link and are not
390+
* routable beyond that link.
391+
* <p>
392+
* The method performs this check by examining the first 10 bits of the
393+
* address's first group of 16 bits (the first element in the `groups` array).
394+
* According to the IPv6 standard, link-local addresses have the first 10 bits
395+
* set to the binary value `1111 1110 10`.
396+
* In hexadecimal, this corresponds to
397+
* `0xFE80` when masked with `0xFFC0`.
398+
* <p>
399+
* This method overrides a method with the same signature in a parent class
400+
* or interface.
401+
*
402+
* @return {@code true} if the IPv6 address is a link-local address, {@code false} otherwise.
403+
*/
404+
@Override
405+
public boolean isLocal() {
406+
short firstBlock = groups[0];
407+
408+
int maskedValue = firstBlock & 0xFFC0;
409+
int expectedValue = 0xFE80;
410+
411+
return maskedValue == expectedValue;
412+
}
413+
384414
// Implementations
385415

386416
@Override

0 commit comments

Comments
 (0)