-
Notifications
You must be signed in to change notification settings - Fork 48
Add ability to get record associated with lookup #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb3c876
Fix issues identified by linter
oschwald 4cfcbea
Make search tree method slightly less verbose
oschwald 5930bfc
Replace sketchy resource handling
oschwald d0bcfba
Update submodule to most recent
oschwald 0f17d19
Add ability to get network for the lookup
oschwald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package com.maxmind.db; | ||
|
|
||
| import java.net.InetAddress; | ||
| import java.net.UnknownHostException; | ||
|
|
||
| /** | ||
| * <code>Network</code> represents an IP network. | ||
| */ | ||
| public final class Network { | ||
| private final InetAddress ipAddress; | ||
| private final int prefixLength; | ||
| private InetAddress networkAddress = null; | ||
|
|
||
| /** | ||
| * Construct a <code>Network</code> | ||
| * | ||
| * @param ipAddress An IP address in the network. This does not have to be | ||
| * the first address in the network. | ||
| * @param prefixLength The prefix length for the network. | ||
| */ | ||
| public Network(InetAddress ipAddress, int prefixLength) { | ||
| this.ipAddress = ipAddress; | ||
| this.prefixLength = prefixLength; | ||
| } | ||
|
|
||
| /** | ||
| * @return The first address in the network. | ||
| */ | ||
| public InetAddress getNetworkAddress() { | ||
| if (networkAddress != null) { | ||
| return networkAddress; | ||
| } | ||
| byte[] ipBytes = ipAddress.getAddress(); | ||
| byte[] networkBytes = new byte[ipBytes.length]; | ||
| int curPrefix = prefixLength; | ||
| for (int i = 0; i < ipBytes.length && curPrefix > 0; i++) { | ||
| byte b = ipBytes[i]; | ||
| if (curPrefix < 8) { | ||
| int shiftN = 8 - curPrefix; | ||
| b = (byte) ((b >> shiftN) << shiftN); | ||
| } | ||
| networkBytes[i] = b; | ||
| curPrefix -= 8; | ||
| } | ||
|
|
||
| try { | ||
| networkAddress = InetAddress.getByAddress(networkBytes); | ||
| } catch (UnknownHostException e) { | ||
| throw new RuntimeException("Illegal network address byte length of " + networkBytes.length); | ||
| } | ||
| return networkAddress; | ||
| } | ||
|
|
||
| /** | ||
| * @return The prefix length is the number of leading 1 bits in the subnet | ||
| * mask. Sometimes also known as netmask length. | ||
| */ | ||
| public int getPrefixLength() { | ||
| return prefixLength; | ||
| } | ||
|
|
||
| /*** | ||
| * @return A string representation of the network in CIDR notation, e.g., | ||
| * <code>1.2.3.0/24</code> or <code>2001::/8</code>. | ||
| */ | ||
| public String toString() { | ||
| return getNetworkAddress().getHostAddress() + "/" + prefixLength; | ||
| } | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.maxmind.db; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
|
|
||
| import java.net.InetAddress; | ||
|
|
||
| /** | ||
| * Record represents the data and metadata associated with a database lookup. | ||
| */ | ||
| public final class Record { | ||
| private final JsonNode data; | ||
| private final Network network; | ||
|
|
||
| /** | ||
| * Create a new record. | ||
| * | ||
| * @param data the data for the record in the database. | ||
| * @param ipAddress the IP address used in the lookup. | ||
| * @param prefixLength the network prefix length associated with the record in the database. | ||
| */ | ||
| public Record( JsonNode data, InetAddress ipAddress, int prefixLength) { | ||
| this.data = data; | ||
| this.network = new Network(ipAddress, prefixLength); | ||
| } | ||
|
|
||
| /** | ||
| * @return the data for the record in the database. The record will be | ||
| * <code>null</code> if there was no data for the address in the database. | ||
| */ | ||
| public JsonNode getData() { | ||
| return data; | ||
| } | ||
|
|
||
| /** | ||
| * @return the network associated with the record in the database. This is | ||
| * the largest network where all of the IPs in the network have the same | ||
| * data. | ||
| */ | ||
| public Network getNetwork() { | ||
| return network; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,3 @@ | ||
| /** | ||
| * | ||
| */ | ||
| /** | ||
| * @author greg | ||
| * | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool!