Skip to content

Commit fb269dd

Browse files
committed
Add iteration mechanism
1 parent 004265d commit fb269dd

File tree

4 files changed

+658
-4
lines changed

4 files changed

+658
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.maxmind.db;
2+
3+
import java.net.InetAddress;
4+
5+
public class BadVersionException extends Exception {
6+
public BadVersionException(InetAddress ip) {
7+
super("you attempted to use an IPv6 network in an IPv4-only database: " + ip.toString());
8+
}
9+
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package com.maxmind.db;
2+
3+
import java.io.IOException;
4+
import java.net.Inet4Address;
5+
import java.net.InetAddress;
6+
import java.nio.ByteBuffer;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.Iterator;
10+
11+
public class Networks<T> implements Iterator<DatabaseRecord<T>> {
12+
private final Reader reader;
13+
private ArrayList<NetworkNode> nodes;
14+
private NetworkNode lastNode;
15+
private boolean skipAliasedNetworks;
16+
private Exception err;
17+
private ByteBuffer buffer; /* Stores the buffer for Next() calls */
18+
private Class<T> typeParameterClass;
19+
20+
/**
21+
* Constructs a Networks instance.
22+
* @param reader The reader object.
23+
* @param skipAliasedNetworks The boolean to skip aliased networks.
24+
* @throws ClosedDatabaseException Exception for a closed database.
25+
*/
26+
Networks(Reader reader, boolean skipAliasedNetworks)
27+
throws ClosedDatabaseException {
28+
this(reader, skipAliasedNetworks, new NetworkNode[]{});
29+
}
30+
31+
/**
32+
* Constructs a Networks instance.
33+
* @param reader The reader object.
34+
* @param skipAliasedNetworks The boolean to skip aliased networks.
35+
* @param nodes The initial nodes array to start Networks iterator with.
36+
* @throws ClosedDatabaseException Exception for a closed database.
37+
*/
38+
Networks(Reader reader, boolean skipAliasedNetworks, NetworkNode[] nodes)
39+
throws ClosedDatabaseException {
40+
this.reader = reader;
41+
this.skipAliasedNetworks = skipAliasedNetworks;
42+
this.nodes = new ArrayList<NetworkNode>(Arrays.asList(nodes));
43+
this.buffer = reader.getBufferHolder().get();
44+
}
45+
46+
/**
47+
* Constructs a Networks instance with skipAliasedNetworks set to false by default.
48+
* @param reader The reader object.
49+
*/
50+
Networks(Reader reader) throws ClosedDatabaseException {
51+
this(reader, false);
52+
}
53+
54+
/**
55+
* Returns if Networks had any errors.
56+
* @return Exception The exception to the Networks iteration.
57+
*/
58+
public Exception getErr() {
59+
return this.err;
60+
}
61+
62+
/**
63+
* Sets the Class for the data type in DataRecord.
64+
* @param cls The class object. ( For example, Map.class )
65+
*/
66+
public void setDataClass(Class<T> cls) {
67+
this.typeParameterClass = cls;
68+
}
69+
70+
/**
71+
* Returns the next NetworksItem. You need to set the class using
72+
* prepareForClass before calling next.
73+
* For example,
74+
* networks.prepareForClass(Map.Class);
75+
* Map test = networks.next();
76+
*/
77+
@Override
78+
public DatabaseRecord<T> next() {
79+
if (this.err != null) {
80+
return null;
81+
}
82+
83+
try {
84+
T data = this.reader.resolveDataPointer(
85+
this.buffer, this.lastNode.pointer, this.typeParameterClass);
86+
87+
byte[] ip = this.lastNode.ip;
88+
int prefixLength = this.lastNode.prefix;
89+
90+
// We do this because uses of SkipAliasedNetworks expect the IPv4 networks
91+
// to be returned as IPv4 networks. If we are not skipping aliased
92+
// networks, then the user will get IPv4 networks from the ::FFFF:0:0/96
93+
// network.
94+
if (this.skipAliasedNetworks && isInIpv4Subtree(ip)) {
95+
ip = Arrays.copyOfRange(ip, 12, ip.length);
96+
prefixLength -= 96;
97+
}
98+
99+
// If the ip is in ipv6 form, drop the prefix manually
100+
// as InetAddress converts it to ipv4.
101+
InetAddress ipAddr = InetAddress.getByAddress(ip);
102+
if (ipAddr instanceof Inet4Address && ip.length > 4
103+
&& ip[10] == -1 && ip[11] == -1 && prefixLength > 32) {
104+
prefixLength -= 96;
105+
}
106+
107+
return new DatabaseRecord<T>(data, InetAddress.getByAddress(ip), prefixLength);
108+
} catch (IOException e) {
109+
this.err = e;
110+
return null;
111+
}
112+
}
113+
114+
public boolean isInIpv4Subtree(byte[] ip) {
115+
if (ip.length != 16) {
116+
return false;
117+
}
118+
for (int i = 0; i < 12; i++) {
119+
if (ip[i] != 0) {
120+
return false;
121+
}
122+
}
123+
return true;
124+
}
125+
126+
/*
127+
* Next prepares the next network for reading with the Network method. It
128+
* returns true if there is another network to be processed and false if there
129+
* are no more networks or if there is an error.
130+
*/
131+
@Override
132+
public boolean hasNext() {
133+
if (this.err != null) {
134+
return false;
135+
}
136+
while (!this.nodes.isEmpty()) {
137+
// Pop the last one.
138+
NetworkNode node = this.nodes.remove(this.nodes.size() - 1);
139+
140+
// Next until we don't have data.
141+
while (node.pointer != this.reader.getMetadata().getNodeCount()) {
142+
// This skips IPv4 aliases without hardcoding the networks that the writer
143+
// currently aliases.
144+
if (this.skipAliasedNetworks && this.reader.getIpv4Start() != 0
145+
&& node.pointer == this.reader.getIpv4Start()
146+
&& !isInIpv4Subtree(node.ip)) {
147+
break;
148+
}
149+
150+
if (node.pointer > this.reader.getMetadata().getNodeCount()) {
151+
this.lastNode = node;
152+
return true;
153+
}
154+
155+
byte[] ipRight = Arrays.copyOf(node.ip, node.ip.length);
156+
if (ipRight.length <= (node.prefix >> 3)) {
157+
this.err = new InvalidDatabaseException("Invalid search tree");
158+
return false;
159+
}
160+
161+
ipRight[node.prefix >> 3] |= 1 << (7 - (node.prefix % 8));
162+
163+
try {
164+
int rightPointer = this.reader.readNode(this.buffer, node.pointer, 1);
165+
node.prefix++;
166+
167+
this.nodes.add(new NetworkNode(ipRight, node.prefix, rightPointer));
168+
node.pointer = this.reader.readNode(this.buffer, node.pointer, 0);
169+
} catch (InvalidDatabaseException e) {
170+
this.err = e;
171+
return false;
172+
}
173+
}
174+
}
175+
return false;
176+
}
177+
178+
protected static class NetworkNode {
179+
public byte[] ip;
180+
public int prefix;
181+
public int pointer;
182+
183+
/**
184+
* Constructs a network node for internal use.
185+
* @param ip The ip address of the node.
186+
* @param prefix The prefix of the node.
187+
* @param pointer The node number
188+
*/
189+
public NetworkNode(byte[] ip, int prefix, int pointer) {
190+
this.ip = ip;
191+
this.prefix = prefix;
192+
this.pointer = pointer;
193+
}
194+
}
195+
196+
}

0 commit comments

Comments
 (0)