forked from I2P_Developers/i2p.i2p
NamingService, BFNS: Add API methods to lookup all reverse entries
Addressbook: Use new API methods to simplify delete-all code i2ptunnel: Output full authentication line HostTxtEntry: More tweaks for i2ptunnel
This commit is contained in:
@ -620,10 +620,10 @@ public class BlockfileNamingService extends DummyNamingService {
|
||||
* Returns null without exception on error (logs only).
|
||||
* Returns without logging if no reverse skiplist (version 1).
|
||||
*
|
||||
* @return the first one found if more than one
|
||||
* @since 0.8.9
|
||||
* @return all found if more than one
|
||||
* @since 0.9.26 from getReverseEntry() 0.8.9
|
||||
*/
|
||||
private String getReverseEntry(Hash hash) {
|
||||
private List<String> getReverseEntries(Hash hash) {
|
||||
try {
|
||||
SkipList<Integer, Properties> rev = _bf.getIndex(REVERSE_SKIPLIST, _hashIndexSerializer, _infoSerializer);
|
||||
if (rev == null)
|
||||
@ -633,13 +633,21 @@ public class BlockfileNamingService extends DummyNamingService {
|
||||
Properties props = rev.get(idx);
|
||||
if (props == null)
|
||||
return null;
|
||||
for (Object okey : props.keySet()) {
|
||||
String key = (String) okey;
|
||||
List<String> rv = new ArrayList<String>(props.size());
|
||||
for (String key : props.stringPropertyNames()) {
|
||||
// now do the forward lookup to verify (using the cache)
|
||||
Destination d = lookup(key);
|
||||
if (d != null && d.calculateHash().equals(hash))
|
||||
return key;
|
||||
List<Destination> ld = lookupAll(key);
|
||||
if (ld != null) {
|
||||
for (Destination d : ld) {
|
||||
if (d.calculateHash().equals(hash)) {
|
||||
rv.add(key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!rv.isEmpty())
|
||||
return rv;
|
||||
} catch (IOException ioe) {
|
||||
_log.error("DB get reverse error", ioe);
|
||||
} catch (RuntimeException e) {
|
||||
@ -1385,10 +1393,33 @@ public class BlockfileNamingService extends DummyNamingService {
|
||||
*/
|
||||
@Override
|
||||
public String reverseLookup(Hash h) {
|
||||
List<String> ls;
|
||||
synchronized(_bf) {
|
||||
if (_isClosed)
|
||||
return null;
|
||||
return getReverseEntry(h);
|
||||
ls = getReverseEntries(h);
|
||||
}
|
||||
return (ls != null) ? ls.get(0) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param options ignored
|
||||
* @since 0.9.26
|
||||
*/
|
||||
@Override
|
||||
public List<String> reverseLookupAll(Destination d, Properties options) {
|
||||
return reverseLookupAll(d.calculateHash());
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 0.9.26
|
||||
*/
|
||||
@Override
|
||||
public List<String> reverseLookupAll(Hash h) {
|
||||
synchronized(_bf) {
|
||||
if (_isClosed)
|
||||
return null;
|
||||
return getReverseEntries(h);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -134,13 +134,21 @@ public class HostTxtEntry {
|
||||
* Includes newline.
|
||||
*/
|
||||
public void write(BufferedWriter out) throws IOException {
|
||||
write((Writer) out);
|
||||
out.newLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write as a standard line name=dest[#!k1=v1#k2=v2...]
|
||||
* Does not include newline.
|
||||
*/
|
||||
public void write(Writer out) throws IOException {
|
||||
if (name != null && dest != null) {
|
||||
out.write(name);
|
||||
out.write(KV_SEPARATOR);
|
||||
out.write(dest);
|
||||
}
|
||||
writeProps(out);
|
||||
out.newLine();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -691,6 +691,51 @@ public abstract class NamingService {
|
||||
return remove(hostname, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse lookup a hash.
|
||||
* This implementation returns the result from reverseLookup, or null.
|
||||
* Subclasses implementing reverse lookups should override.
|
||||
*
|
||||
* @param h non-null
|
||||
* @return a non-empty list of host names for this hash, or <code>null</code>
|
||||
* if none is known. It is safe for subclasses to always return
|
||||
* <code>null</code> if no reverse lookup is possible.
|
||||
* @since 0.9.26
|
||||
*/
|
||||
public List<String> reverseLookupAll(Hash h) {
|
||||
String s = reverseLookup(h);
|
||||
return (s != null) ? Collections.singletonList(s) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse lookup a destination
|
||||
* This implementation returns reverseLookupAll(dest, null).
|
||||
*
|
||||
* @param dest non-null
|
||||
* @return a non-empty list of host names for this Destination, or <code>null</code>
|
||||
* if none is known. It is safe for subclasses to always return
|
||||
* <code>null</code> if no reverse lookup is possible.
|
||||
* @since 0.9.26
|
||||
*/
|
||||
public List<String> reverseLookupAll(Destination dest) {
|
||||
return reverseLookupAll(dest, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as reverseLookupAll(dest) but with options
|
||||
* This implementation returns the result from reverseLookup, or null.
|
||||
* Subclasses implementing reverse lookups should override.
|
||||
*
|
||||
* @param d non-null
|
||||
* @param options NamingService-specific, can be null
|
||||
* @return a non-empty list of host names for this Destination, or <code>null</code>
|
||||
* @since 0.9.26
|
||||
*/
|
||||
public List<String> reverseLookupAll(Destination d, Properties options) {
|
||||
String s = reverseLookup(d, options);
|
||||
return (s != null) ? Collections.singletonList(s) : null;
|
||||
}
|
||||
|
||||
//// End new API for multiple Destinations
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user