Created
November 22, 2023 11:21
-
-
Save alexanderankin/76e09f26e0c78be6f5e635b47f998bd5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package misc.ipdb; | |
import lombok.RequiredArgsConstructor; | |
import misc.ipdb.util.DbFactory; | |
import java.math.BigInteger; | |
import java.util.Arrays; | |
import java.util.HexFormat; | |
import java.util.List; | |
import java.util.StringJoiner; | |
@RequiredArgsConstructor | |
public class IpDbService { | |
private final DbFactory dbFactory; | |
// returns if this ip address is within any of the ranges (or not) | |
public boolean free(IpAddress ipAddress) { | |
throw new UnsupportedOperationException(); | |
} | |
// returns if any addresses in this range are within any of the ranges (or not) | |
public boolean free(IpRange ipRange) { | |
throw new UnsupportedOperationException(); | |
} | |
// find list of ip ranges which contain addresses within this ip range | |
public List<IpRange> foundWithin(IpRange ipRange) { | |
throw new UnsupportedOperationException(); | |
} | |
// find the range containing this ip address (or null if not found) | |
public IpRange rangeOf(IpAddress ipAddress) { | |
throw new UnsupportedOperationException(); | |
} | |
public static void main(String[] args) { | |
System.out.println(Arrays.toString(IpAddress.parseIpV4("1.1.1.1"))); | |
System.out.println(Arrays.toString(new BigInteger("16843009").toByteArray())); | |
System.out.println(Arrays.toString(IpAddress.parseIpV6("2001:0000:130F:0000:0000:09C0:876A:130B"))); | |
System.out.println(new BigInteger(IpAddress.parseIpV6("2001:0000:130F:0000:0000:09C0:876A:130B"))); | |
System.out.println(IpAddress.serializeIpV6(IpAddress.parseIpV6("2001:0000:130F:0000:0000:09C0:876A:130B"))); | |
} | |
public record IpRange(Integer id, String name, IpAddress minInclusive, IpAddress maxExclusive) { | |
} | |
public enum IpVersion {V4, V6} | |
public record IpAddress(String address, byte[] value, IpVersion version) { | |
public static IpAddress v4(String address) { | |
return new IpAddress(address, parseIpV4(address), IpVersion.V4); | |
} | |
public static IpAddress v4(byte[] value) { | |
return new IpAddress(serializeIpV4(value), value, IpVersion.V4); | |
} | |
public static IpAddress v6(String address) { | |
return new IpAddress(address, parseIpV6(address), IpVersion.V6); | |
} | |
public static IpAddress v6(byte[] value) { | |
return new IpAddress(serializeIpV6(value), value, IpVersion.V6); | |
} | |
static byte[] parseIpV4(String address) { | |
var parts = address.split("\\."); | |
if (parts.length != 4) throw new IllegalArgumentException("not an ipv4, needs 4 parts"); | |
var result = new byte[4]; | |
for (int i = 0; i < parts.length; i++) { | |
String part = parts[i]; | |
result[i] = Byte.parseByte(part); | |
} | |
return result; | |
} | |
static byte[] parseIpV6(String address) { | |
String[] groups = address.split(":"); | |
var result = new byte[16]; | |
for (int i = 0; i < groups.length; i++) { | |
String group = groups[i]; | |
byte[] bytes = HexFormat.of().parseHex(group); | |
System.arraycopy(bytes, 0, result, i * 2, 2); | |
} | |
return result; | |
} | |
static String serializeIpV4(byte[] address) { | |
StringJoiner stringJoiner = new StringJoiner("."); | |
for (byte b : address) { | |
stringJoiner.add(String.valueOf(b)); | |
} | |
return stringJoiner.toString(); | |
} | |
static String serializeIpV6(byte[] address) { | |
StringJoiner stringJoiner = new StringJoiner(":"); | |
byte[] tmp = new byte[2]; | |
for (int i = 0; i < address.length; i += 8) { | |
for (int j = 0; j < 8; j+= 2) { | |
System.arraycopy(address, i + j, tmp, 0, 2); | |
stringJoiner.add(HexFormat.of().formatHex(tmp)); | |
} | |
} | |
return stringJoiner.toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment