- Direct Known Subclasses:
DummyNamingService, SingleFileNamingService
Field Summary
Modifier and TypeFieldDescriptionprotected final I2PAppContextThe app context.protected final Set<NamingServiceListener> The listeners.protected final LogThe logger.protected final Set<NamingServiceUpdater> The updaters.private static final StringService tried when the configured one fails.private static final StringNaming service used when the property is unset.private static final StringThe previous default, still accepted.static final StringWhat classname should be used as the naming service impl?Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedNamingService(I2PAppContext context) The naming service should only be constructed and accessed through the application context.Method Summary
Modifier and TypeMethodDescriptionbooleanaddDestination(String hostname, Destination d) Add a Destination to an existing hostname's entry in the addressbook.booleanaddDestination(String hostname, Destination d, Properties options) Add a Destination to an existing hostname's entry in the addressbook.booleanOnly for chaining-capable NamingServices.booleanaddNamingService(NamingService ns, boolean head) Only for chaining-capable NamingServices.static final NamingServicecreateInstance(I2PAppContext context) WARNING - for use byI2PAppContextonly - others must useI2PAppContext.namingService().voidExport in a hosts.txt format.voidexport(Writer out, Properties options) Export in a hosts.txt format.getBase64Entries(Properties options) This may be more or less efficient thangetEntries(), depending on the implementation.Warning - unimplemented in any subclass.Warning - This obviously brings the whole database into memory, so use is discouraged.getEntries(Properties options) Warning - This will bring the whole database into memory if options is null, empty, or unsupported, use with caution.getName()The name of this naming service.getNames()All known hostnames.getNames(Properties options) All known hostnames matching the specified options.For daisy chaining (MetaNamingService).This implementation returns null.static booleanIs this a b32 hostname? Does NOT validate the base32 part except for length.static booleanisBlindedHost(String hostname) Is this a blinded ("b33") hostname? Does NOT validate the base32 part except for length.static booleanIs this an i2p hostname?Look up a hostname.abstract Destinationlookup(String hostname, Properties lookupOptions, Properties storedOptions) Same as lookup(hostname) but with in and out options Note that whether this (and lookup(hostname)) resolve Base 32 addresses in the form {52 chars}.b32.i2p is NamingService-specific.Same aslookupBase32(String, int)but with the SHA256 Hash precalculated This implementation returns null.For NamingServices that support multiple Destinations for a single hostname, return all of them.lookupAll(String hostname, Properties lookupOptions, List<Properties> storedOptions) For NamingServices that support multiple Destinations and Properties for a single hostname, return all of them.lookupBase32(String hostname, int timeout) Lookup a Base 32 address.protected DestinationlookupBase64(String hostname) If the hostname is a valid Base64 encoded destination, return the decoded Destination.booleanput(String hostname, Destination d) Add a hostname and Destination to the addressbook.booleanput(String hostname, Destination d, Properties options) Add a hostname and Destination to the addressbook.booleanputAll(Map<String, Destination> entries, Properties options) Put all the entries, each with the given options.booleanputIfAbsent(String hostname, Destination d) Add a hostname and Destination to the addressbook.booleanputIfAbsent(String hostname, Destination d, Properties options) Add a hostname and Destination to the addressbook.voidRegister a listener for naming service changes.voidRegister an updater for the naming service.booleanDelete the entry.booleanremove(String hostname, Properties options) Delete the entry.booleanremove(String hostname, Destination d) Remove a hostname's entry only if it contains the Destination d.booleanremove(String hostname, Destination d, Properties options) Remove a hostname's entry only if it contains the Destination d.booleanOnly for chaining-capable NamingServices.voidrequestUpdate(Properties options) Ask any registered updaters to update now.reverseLookup(Destination dest) Reverse lookup a destination.reverseLookup(Destination d, Properties options) Same as reverseLookup(dest) but with options This implementation returns null.Reverse lookup a hash.reverseLookupAll(Destination dest) Reverse lookup a destination This implementation returns reverseLookupAll(dest, null).reverseLookupAll(Destination d, Properties options) Same as reverseLookupAll(dest) but with options This implementation returns the result from reverseLookup, or null.Reverse lookup a hash.booleanWarning - unimplemented in any subclass.voidshutdown()Parent will call when removed.intsize()The number of entries in this naming service.intsize(Properties options) This implementation returns -1.voidstart()Parent will call when added.toString()Returns a string representation of this naming service.voidUnregister a listener for naming service changes.voidUnregister an updater for the naming service.booleanupdate(String hostname, Destination d, Properties options) Fails if entry did not previously exist.
Field Details
_log
The logger._context
The app context._listeners
The listeners._updaters
The updaters.PROP_IMPL
What classname should be used as the naming service impl?- See Also:
DEFAULT_IMPL
Naming service used when the property is unset.- See Also:
OLD_DEFAULT_IMPL
The previous default, still accepted.- See Also:
BACKUP_IMPL
Service tried when the configured one fails.- See Also:
Constructor Details
NamingService
The naming service should only be constructed and accessed through the application context. This constructor should only be used by the appropriate application context itself.- Parameters:
context- the application context
Method Details
lookup
Look up a hostname.- Parameters:
hostname- the host name to resolve, which may be a hostname, an IPv4 address, or a Base 32 address- Returns:
- the Destination for this hostname, or
nullif name is unknown.
reverseLookup
Reverse lookup a destination. This implementation returns reverseLookup(dest, null).- Parameters:
dest- non-null destination to look up- Returns:
- a hostname for this Destination, or
nullif none is known. It is safe for subclasses to always returnnullif no reverse lookup is possible.
reverseLookup
Reverse lookup a hash. This implementation returns null. Subclasses implementing reverse lookups should override.- Parameters:
h- non-null hash to look up- Returns:
- a hostname for this hash, or
nullif none is known. It is safe for subclasses to always returnnullif no reverse lookup is possible.
lookupBase64
If the hostname is a valid Base64 encoded destination, return the decoded Destination. Useful as a "fallback" in custom naming implementations. This is misnamed as it isn't a "lookup" at all, but a simple conversion from a Base64 string to a Destination.- Parameters:
hostname- 516+ character Base 64- Returns:
- Destination or null on error
toString
getName
getConfiguration
Warning - unimplemented in any subclass. Returns null always.- Returns:
- NamingService-specific options or null
setConfiguration
Warning - unimplemented in any subclass. Returns true always.- Parameters:
p- the properties to set- Returns:
- always true; the properties are ignored
getNamingServices
For daisy chaining (MetaNamingService). This implementation returns null. Subclasses implementing chaining should override.- Returns:
- chained naming services or null
getParent
This implementation returns null. Subclasses implementing chaining should override.- Returns:
- parent naming service or null if this is the root
addNamingService
Only for chaining-capable NamingServices. Add to end of the list.- Parameters:
ns- the naming service to add- Returns:
- as
addNamingService(NamingService, boolean)with head false
addNamingService
Only for chaining-capable NamingServices. This implementation returns false. Subclasses implementing chaining should override.- Parameters:
ns- the naming service to addhead- true to prepend to the chain, false to append- Returns:
- true if ns was chained in; this base implementation always returns false
removeNamingService
Only for chaining-capable NamingServices. This implementation returns false. Subclasses implementing chaining should override.- Parameters:
ns- the naming service to remove- Returns:
- true if ns was chained out; this base implementation always returns false
size
public int size()The number of entries in this naming service.- Returns:
- number of entries or -1 if unknown
size
This implementation returns -1. Most subclasses should override.- Parameters:
options- NamingService-specific, can be null- Returns:
- number of entries (matching the options if non-null) or -1 if unknown
getEntries
Warning - This obviously brings the whole database into memory, so use is discouraged.- Returns:
- every stored hostname mapped to its Destination or empty Map if none; Returned Map is not necessarily sorted, implementation dependent
getEntries
Warning - This will bring the whole database into memory if options is null, empty, or unsupported, use with caution.- Parameters:
options- NamingService-specific, can be null- Returns:
- all mappings (matching the options if non-null) or empty Map if none; Returned Map is not necessarily sorted, implementation dependent
getBase64Entries
This may be more or less efficient thangetEntries(), depending on the implementation. Warning - This will bring the whole database into memory if options is null, empty, or unsupported, use with caution.This implementation calls getEntries(options) and returns a SortedMap. Subclasses should override if they store base64 natively.
- Parameters:
options- NamingService-specific, can be null- Returns:
- all mappings (matching the options if non-null) or empty Map if none; Returned Map is not necessarily sorted, implementation dependent
export
Export in a hosts.txt format. Output is not necessarily sorted, implementation dependent. Output may or may not contain comment lines, implementation dependent. Caller must close writer.This implementation calls
getBase64Entries(Properties). Subclasses should override if they store in a hosts.txt format natively.- Parameters:
out- the writer to export to- Throws:
IOException- if an I/O error occurs
export
Export in a hosts.txt format. Output is not necessarily sorted, implementation dependent. Output may or may not contain comment lines, implementation dependent. Caller must close writer.This implementation calls getBase64Entries(options). Subclasses should override if they store in a hosts.txt format natively.
- Parameters:
out- the writer to export tooptions- NamingService-specific, can be null- Throws:
IOException- if an I/O error occurs
getNames
getNames
All known hostnames matching the specified options.- Parameters:
options- NamingService-specific, can be null- Returns:
- all known hostnames (matching the options if non-null) or empty Set if none; Returned Set is not necessarily sorted, implementation dependent
put
Add a hostname and Destination to the addressbook. Overwrites old entry if it exists. See alsoputIfAbsent(String, Destination)and update().- Parameters:
hostname- the host name to map to the destination, overwriting any existing entryd- the destination for the hostname- Returns:
- true if the entry was stored
put
Add a hostname and Destination to the addressbook. Overwrites old entry if it exists. See also putIfAbsent() andupdate(String, Destination, Properties).- Parameters:
hostname- the host name to map to the destination, overwriting any existing entryd- the destination for the hostnameoptions- NamingService-specific, can be null- Returns:
- true if the entry was stored; this base implementation always returns false
putIfAbsent
Add a hostname and Destination to the addressbook. Fails if entry previously exists. See also put() and update().- Parameters:
hostname- the host name to map to the destination, which must not already be presentd- the destination for the hostname- Returns:
- true if no entry existed and the new one was stored
putIfAbsent
Add a hostname and Destination to the addressbook. Fails if entry previously exists. See also put() and update().- Parameters:
hostname- the host name to map to the destination, which must not already be presentd- the destination for the hostnameoptions- NamingService-specific, can be null- Returns:
- true if no entry existed and the new one was stored; this base implementation always returns false
putAll
Put all the entries, each with the given options. This implementation calls put() for each entry. Subclasses may override if a more efficient implementation is available.- Parameters:
entries- host name to Destination mappings to add, one put() per entryoptions- NamingService-specific, can be null- Returns:
- total success, or false if any put failed
update
Fails if entry did not previously exist. Warning - unimplemented in any subclass. This implementation returns false.- Parameters:
hostname- the host name whose existing entry is to be updatedd- may be null if only options are changingoptions- NamingService-specific, can be null- Returns:
- true if an existing entry was updated; this base implementation always returns false
remove
Delete the entry.- Parameters:
hostname- the host name whose entry is to be deleted- Returns:
- true if removed successfully, false on error or if it did not exist
remove
Delete the entry.- Parameters:
hostname- the host name whose entry is to be deletedoptions- NamingService-specific, can be null- Returns:
- true if removed successfully, false on error or if it did not exist
requestUpdate
Ask any registered updaters to update now.- Parameters:
options- NamingService- or updater-specific, may be null
registerListener
Register a listener for naming service changes.- Parameters:
nsl- the listener to register
unregisterListener
Unregister a listener for naming service changes.- Parameters:
nsl- the listener to unregister
registerUpdater
Register an updater for the naming service.- Parameters:
nsu- the updater to register
unregisterUpdater
Unregister an updater for the naming service.- Parameters:
nsu- the updater to unregister
lookup
public abstract Destination lookup(String hostname, Properties lookupOptions, Properties storedOptions) Same as lookup(hostname) but with in and out options Note that whether this (and lookup(hostname)) resolve Base 32 addresses in the form {52 chars}.b32.i2p is NamingService-specific.- Parameters:
hostname- the host name to resolve, which may be a hostname, an IPv4 address, or a Base 32 addresslookupOptions- input parameter, NamingService-specific, can be nullstoredOptions- output parameter, NamingService-specific, any stored properties will be added if non-null- Returns:
- dest or null
reverseLookup
Same as reverseLookup(dest) but with options This implementation returns null. Subclasses implementing reverse lookups should override.- Parameters:
d- non-null destination to look upoptions- NamingService-specific, can be null- Returns:
- hostname or null
lookupBase32
Lookup a Base 32 address. This may require the router to fetch the LeaseSet, which may take quite a while. This implementation returns null. See also lookup(Hash, int).- Parameters:
hostname- must be {52 chars}.b32.i2ptimeout- in seconds; <= 0 means use router default- Returns:
- dest or null
lookup
Same aslookupBase32(String, int)but with the SHA256 Hash precalculated This implementation returns null.- Parameters:
hash- the hash to look uptimeout- in seconds; <= 0 means use router default- Returns:
- dest or null
start
public void start()Parent will call when added. If this is the root naming service, the core will start it. Should not be called by others.shutdown
public void shutdown()Parent will call when removed. If this is the root naming service, the core will stop it. Should not be called by others.lookupAll
For NamingServices that support multiple Destinations for a single hostname, return all of them.It is recommended that the returned list is in order of priority, highest-first, but this is NamingService-specific.
Not recommended for resolving Base 32 addresses; whether this does resolve Base 32 addresses in the form {52 chars}.b32.i2p is NamingService-specific.
- Parameters:
hostname- the host name to resolve, for which every known destination is returned- Returns:
- non-empty List of Destinations, or null if nothing found
lookupAll
public List<Destination> lookupAll(String hostname, Properties lookupOptions, List<Properties> storedOptions) For NamingServices that support multiple Destinations and Properties for a single hostname, return all of them.It is recommended that the returned list is in order of priority, highest-first, but this is NamingService-specific.
If storedOptions is non-null, it must be a List that supports null entries. If the returned value (the List of Destinations) is non-null, the same number of Properties objects will be added to storedOptions. If no properties were found for a given Destination, the corresponding entry in the storedOptions list will be null.
Not recommended for resolving Base 32 addresses; whether this does resolve Base 32 addresses in the form {52 chars}.b32.i2p is NamingService-specific.
This implementation simply calls
lookup(String). Subclasses implementing multiple destinations per hostname should override.- Parameters:
hostname- the host name to resolve, for which every known destination is returnedlookupOptions- input parameter, NamingService-specific, may be nullstoredOptions- output parameter, NamingService-specific, any stored properties will be added if non-null- Returns:
- non-empty List of Destinations, or null if nothing found
addDestination
Add a Destination to an existing hostname's entry in the addressbook.- Parameters:
hostname- the host name whose existing entry is to be extendedd- the destination to add- Returns:
- true if d was added to the hostname's entry
addDestination
Add a Destination to an existing hostname's entry in the addressbook. This implementation simply calls putIfAbsent(). Subclasses implementing multiple destinations per hostname should override.- Parameters:
hostname- the host name whose existing entry is to be extendedd- the destination to addoptions- NamingService-specific, may be null- Returns:
- as putIfAbsent(), so d is added only when the hostname has no entry yet
remove
Remove a hostname's entry only if it contains the Destination d. If the NamingService supports multiple Destinations per hostname, and this is the only Destination, removes the entire entry. If additional Destinations remain, it only removes the specified Destination from the entry.- Parameters:
hostname- the host name whose entry holds the destination to removed- the destination that must be present- Returns:
- true if entry containing d was successfully removed.
remove
Remove a hostname's entry only if it contains the Destination d. If the NamingService supports multiple Destinations per hostname, and this is the only Destination, removes the entire entry. If additional Destinations remain, it only removes the specified Destination from the entry.This implementation simply calls lookup() and
remove(String). Subclasses implementing multiple destinations per hostname, or with more efficient implementations, should override. Fails if entry previously exists.- Parameters:
hostname- the host name whose entry holds the destination to removed- the destination that must be presentoptions- NamingService-specific, may be null- Returns:
- true if entry containing d was successfully removed.
reverseLookupAll
Reverse lookup a hash. This implementation returns the result from reverseLookup, or null. Subclasses implementing reverse lookups should override.- Parameters:
h- non-null- Returns:
- a non-empty list of hostnames for this hash, or
nullif none is known. It is safe for subclasses to always returnnullif no reverse lookup is possible.
reverseLookupAll
Reverse lookup a destination This implementation returns reverseLookupAll(dest, null).- Parameters:
dest- non-null destination to look up- Returns:
- a non-empty list of hostnames for this Destination, or
nullif none is known. It is safe for subclasses to always returnnullif no reverse lookup is possible.
reverseLookupAll
Same as reverseLookupAll(dest) but with options This implementation returns the result from reverseLookup, or null. Subclasses implementing reverse lookups should override.- Parameters:
d- non-nulloptions- NamingService-specific, can be null- Returns:
- a non-empty list of hostnames for this Destination, or
null
isI2PHost
Is this an i2p hostname?ref: https://www.rfc-editor.org/rfc/rfc9476.html
- Parameters:
hostname- non-null- Returns:
- true if hostname (case insensitive) ends with .i2p or .i2p.alt
isB32Host
Is this a b32 hostname? Does NOT validate the base32 part except for length.ref: https://www.rfc-editor.org/rfc/rfc9476.html
- Parameters:
hostname- non-null- Returns:
- true if hostname (case insensitive) ends with .b32.i2p or .b32.i2p.alt and is long enough. May or may not be blinded.
isBlindedHost
Is this a blinded ("b33") hostname? Does NOT validate the base32 part except for length. SeeBlinding.decode(I2PAppContext, String)for full validation.ref: https://www.rfc-editor.org/rfc/rfc9476.html
- Parameters:
hostname- non-null- Returns:
- true if hostname (case insensitive) ends with .b32.i2p or .b32.i2p.alt and is long enough.
createInstance
WARNING - for use byI2PAppContextonly - others must useI2PAppContext.namingService().Create a naming service instance, choosing the implementation from the "
i2p.naming.impl" system property (defaulting to the router or client implementation according to the context type). This is a factory, not a cache: every call constructs and returns a new instance, and no deduplication happens here.I2PAppContext.namingService()owns the single instance per context, so call this at most once per context.- Parameters:
context- the application context- Returns:
- a new NamingService instance
- Throws:
RuntimeException- if the naming service cannot be loaded