Search in sources :

Example 11 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class SAMMessageSession method sendBytesThroughMessageSession.

/**
 * Actually send bytes through the SAM message-based session I2PSession,
 * using per-message extended options.
 * For efficiency, use the method without all the extra options if they are all defaults.
 *
 * @param dest Destination
 * @param data Bytes to be sent
 * @param proto I2CP protocol
 * @param fromPort I2CP from port
 * @param toPort I2CP to port
 * @param sendLeaseSet true is the usual setting and the I2CP default
 * @param sendTags 0 to leave as default
 * @param tagThreshold 0 to leave as default
 * @param expiration SECONDS from now, NOT absolute time, 0 to leave as default
 *
 * @return True if the data was sent, false otherwise
 * @throws DataFormatException on unknown / bad dest
 * @throws I2PSessionException on serious error, probably session closed
 * @since 0.9.24
 */
protected boolean sendBytesThroughMessageSession(String dest, byte[] data, int proto, int fromPort, int toPort, boolean sendLeaseSet, int sendTags, int tagThreshold, int expiration) throws DataFormatException, I2PSessionException {
    Destination d = SAMUtils.getDest(dest);
    if (_log.shouldLog(Log.DEBUG)) {
        _log.debug("Sending " + data.length + " bytes to " + dest);
    }
    SendMessageOptions opts = new SendMessageOptions();
    if (!sendLeaseSet)
        opts.setSendLeaseSet(false);
    if (sendTags > 0)
        opts.setTagsToSend(sendTags);
    if (tagThreshold > 0)
        opts.setTagThreshold(tagThreshold);
    if (expiration > 0)
        opts.setDate(I2PAppContext.getGlobalContext().clock().now() + (expiration * 1000));
    return session.sendMessage(d, data, 0, data.length, proto, fromPort, toPort, opts);
}
Also used : Destination(net.i2p.data.Destination) SendMessageOptions(net.i2p.client.SendMessageOptions)

Example 12 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class SAMStreamSession method connect.

/**
 * Connect the SAM STREAM session to the specified Destination
 *
 * @param id Unique id for the connection
 * @param dest Base64-encoded Destination to connect to
 * @param props Options to be used for connection
 *
 * @return true if successful
 * @throws DataFormatException if the destination is not valid
 * @throws SAMInvalidDirectionException if trying to connect through a
 *                                      receive-only session
 * @throws ConnectException if the destination refuses connections
 * @throws NoRouteToHostException if the destination can't be reached
 * @throws InterruptedIOException if the connection timeouts
 * @throws I2PException if there's another I2P-related error
 * @throws IOException
 */
public boolean connect(int id, String dest, Properties props) throws I2PException, ConnectException, NoRouteToHostException, DataFormatException, InterruptedIOException, SAMInvalidDirectionException, IOException {
    if (!canCreate) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("Trying to create an outgoing connection using a receive-only session");
        throw new SAMInvalidDirectionException("Trying to create connections through a receive-only session");
    }
    if (checkSocketHandlerId(id)) {
        if (_log.shouldLog(Log.DEBUG))
            _log.debug("The specified id (" + id + ") is already in use");
        return false;
    }
    Destination d = SAMUtils.getDest(dest);
    I2PSocketOptions opts = socketMgr.buildOptions(props);
    if (props.getProperty(I2PSocketOptions.PROP_CONNECT_TIMEOUT) == null)
        opts.setConnectTimeout(60 * 1000);
    if (_log.shouldLog(Log.DEBUG))
        _log.debug("Connecting new I2PSocket...");
    // blocking connection (SAMv1)
    I2PSocket i2ps = socketMgr.connect(d, opts);
    createSocketHandler(i2ps, id);
    recv.notifyStreamOutgoingConnection(id, "OK", null);
    return true;
}
Also used : Destination(net.i2p.data.Destination) I2PSocket(net.i2p.client.streaming.I2PSocket) I2PSocketOptions(net.i2p.client.streaming.I2PSocketOptions)

Example 13 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class SAMUtils method lookupHost.

/**
 * Resolved the specified hostname.
 *
 * @param name Hostname to be resolved
 *
 * @return the Destination for the specified hostname, or null if not found
 */
private static Destination lookupHost(String name) {
    NamingService ns = I2PAppContext.getGlobalContext().namingService();
    Destination dest = ns.lookup(name);
    return dest;
}
Also used : Destination(net.i2p.data.Destination) NamingService(net.i2p.client.naming.NamingService)

Example 14 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class SAMUtils method genRandomKey.

/**
 * Generate a random destination key.
 * Caller must close streams. Fails silently.
 *
 * @param priv Stream used to write the destination and private keys
 * @param pub Stream used to write the destination (may be null)
 * @param sigType what signature type
 * @since 0.9.14
 */
public static void genRandomKey(OutputStream priv, OutputStream pub, SigType sigType) {
    // _log.debug("Generating random keys...");
    try {
        I2PClient c = I2PClientFactory.createClient();
        Destination d = c.createDestination(priv, sigType);
        priv.flush();
        if (pub != null) {
            d.writeBytes(pub);
            pub.flush();
        }
    } catch (I2PException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) I2PClient(net.i2p.client.I2PClient) IOException(java.io.IOException)

Example 15 with Destination

use of net.i2p.data.Destination in project i2p.i2p by i2p.

the class SAMUtils method getDest.

/**
 * Resolve the destination from a key or a hostname
 *
 * @param s Hostname or key to be resolved
 *
 * @return the Destination for the specified hostname, non-null
 * @throws DataFormatException on bad Base 64 or name not found
 */
public static Destination getDest(String s) throws DataFormatException {
    // NamingService caches b64 so just use it for everything
    // TODO: Add a static local cache here so SAM doesn't flush the
    // NamingService cache
    Destination d = lookupHost(s);
    if (d == null) {
        String msg;
        if (s.length() >= 516)
            msg = "Bad Base64 dest: ";
        else if (s.length() == 60 && s.endsWith(".b32.i2p"))
            msg = "Lease set not found: ";
        else
            msg = "Host name not found: ";
        throw new DataFormatException(msg + s);
    }
    return d;
}
Also used : Destination(net.i2p.data.Destination) DataFormatException(net.i2p.data.DataFormatException)

Aggregations

Destination (net.i2p.data.Destination)149 IOException (java.io.IOException)46 DataFormatException (net.i2p.data.DataFormatException)33 Properties (java.util.Properties)29 I2PException (net.i2p.I2PException)26 Hash (net.i2p.data.Hash)18 ArrayList (java.util.ArrayList)13 File (java.io.File)12 I2PSessionException (net.i2p.client.I2PSessionException)12 SigType (net.i2p.crypto.SigType)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 I2PSession (net.i2p.client.I2PSession)10 I2PSocket (net.i2p.client.streaming.I2PSocket)10 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 I2PClient (net.i2p.client.I2PClient)7 I2PSocketOptions (net.i2p.client.streaming.I2PSocketOptions)7 Test (org.junit.Test)6