/* $Id$ */ package org.noreply.fancydress.type3.routing; import org.noreply.fancydress.misc.Util; import org.noreply.fancydress.crypto.*; /** * The FWD/HOST and SWAP-FWD/HOST routing as specified in the type III spec. * * FWD/HOST packets are forwarded to another type III node given by * a hostname/port pair. The delivering node (i.e. the node parsing * this routing type) is responsible for resolving the hostname to an * IP address. */ public class RoutingHOST extends RoutingForward { /** * The hostname of the next hop */ private String hostname; /** * Port at which the Type III server is listening */ private int port; /** * Keyid of the Packet Key */ private byte[] keyid; /** * Constructor that creates the routing as either FWD/HOST or SWAP-FWD/HOST * * @param hostname host name of the next hop * @param port TCP port at which the next hop is listening * @param keyid keyid of the identity key * @param boolean if true, have a SWAP-FWD/HOST routing type, FWD/HOST otherwhise */ private RoutingHOST(String hostname, int port, byte[] keyid, boolean asSwap) { super (asSwap ? RoutingType.SWAP_FWD_HOST : RoutingType.FWD_HOST); if (keyid.length != CryptoPrimitives.HASH_LEN) throw new Error("keyid must be HASH_LEN bytes long."); this.hostname = hostname; this.port = port; this.keyid = keyid; } /** * Create a FWD/HOST routing. * * @param hostname host name of the next hop * @param port TCP port at which the next hop is listening * @param keyid keyid of the identity key */ public RoutingHOST(String hostname, int port, byte[] keyid) { this(hostname, port, keyid, false); } /** * Return a routing with the same information but with a SWAP_FWD_HOST routing type. * * @return a SWAP_FWD_HOST routing. */ public RoutingForward asSwap() { RoutingHOST swap = new RoutingHOST(hostname, port, keyid, true); return swap; } /** * Return the total length in octets of the routing information. * * @return total length in octets of the routing information */ public int getRoutingInformationLength() { return 2 + keyid.length + hostname.length(); } /** * Get the routing type of this instance. * * @return routing type */ public byte[] getRoutingInformation() { int length = getRoutingInformationLength(); byte[] result = new byte[length]; int pos = 0; result[pos] = (byte) ( (port >> 8) & 0xff); pos++; result[pos] = (byte) ( port & 0xff); pos++; System.arraycopy(keyid, 0, result, pos, keyid.length); pos += keyid.length; System.arraycopy(Util.toOctets(hostname), 0, result, pos, hostname.length()); pos += hostname.length(); if (pos != length) throw new Error("Did not fill in expected amount of bytes!"); return result; } }