Skip to content

Commit

Permalink
added Address#isLocal and Address#isRemote
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsLaivy committed Jul 30, 2024
1 parent 4183fb2 commit bf95e00
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/main/java/codes/laivy/address/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ static boolean validate(@NotNull String string) {
*/
@NotNull String toString(@NotNull Port port);

/**
* Checks if this address belongs to the localhost.
*
* @return {@code true} if this address is a localhost address; {@code false} otherwise.
*/
boolean isLocal();

/**
* Checks if this address belongs to the remote network.
*
* @return {@code true} if this address is a remote address; {@code false} otherwise.
*/
default boolean isRemote() {
return !isLocal();
}

// Cloneable

@NotNull Address clone();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/codes/laivy/address/domain/Domain.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private Domain(@NotNull Subdomain @NotNull [] subdomains, @NotNull SLD sld, @Nul
*
* @return {@code true} if this domain is "localhost", {@code false} otherwise
*/
public boolean isLocalhost() {
public boolean isLocal() {
return getTLD() == null && getSLD().equalsIgnoreCase("localhost");
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/codes/laivy/address/ip/IPv4Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public IPv4Address(int @NotNull [] octets) {
*
* @return {@code true} if this IPv4 address is a localhost address; {@code false} otherwise.
*/
public boolean isLocalhost() {
public boolean isLocal() {
return octets[0] == 127;
}

Expand Down Expand Up @@ -315,7 +315,7 @@ public boolean isMulticast() {
* @return {@code true} if this IPv4 address is publicly routable; {@code false} otherwise.
*/
public boolean isPubliclyRoutable() {
return !isPrivate() && !isLocalhost() && !isMulticast();
return !isPrivate() && !isLocal() && !isMulticast();
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/codes/laivy/address/ip/IPv6Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,36 @@ public boolean isWithinRange(@NotNull IPv6Address start, @NotNull IPv6Address en
return new IPv6Address(broadcastGroups);
}

/**
* Determines if the current IPv6 address belongs to a local network.
* Specifically, it checks if the address is a link-local address in the
* IPv6 range `fe80::/10`.
* Link-local addresses are typically used for
* communication within a single network segment or link and are not
* routable beyond that link.
* <p>
* The method performs this check by examining the first 10 bits of the
* address's first group of 16 bits (the first element in the `groups` array).
* According to the IPv6 standard, link-local addresses have the first 10 bits
* set to the binary value `1111 1110 10`.
* In hexadecimal, this corresponds to
* `0xFE80` when masked with `0xFFC0`.
* <p>
* This method overrides a method with the same signature in a parent class
* or interface.
*
* @return {@code true} if the IPv6 address is a link-local address, {@code false} otherwise.
*/
@Override
public boolean isLocal() {
short firstBlock = groups[0];

int maskedValue = firstBlock & 0xFFC0;
int expectedValue = 0xFE80;

return maskedValue == expectedValue;
}

// Implementations

@Override
Expand Down

0 comments on commit bf95e00

Please sign in to comment.