Search in sources :

Example 26 with Destination

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

the class I2PSocketAddressTest method testToString.

@Test
public void testToString() throws Exception {
    Destination dest = I2PClientFactory.createClient().createDestination(new ByteArrayOutputStream());
    I2PSocketAddress addr = new I2PSocketAddress(dest, 1234);
    assertThat(addr.toString(), is(equalTo(dest.calculateHash().toString() + ":1234")));
}
Also used : Destination(net.i2p.data.Destination) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 27 with Destination

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

the class I2PSocketAddressTest method testEquals.

@Test
public void testEquals() {
    I2PSocketAddress addr = I2PSocketAddress.createUnresolved("example.i2p", 1234);
    assertTrue(addr.equals(I2PSocketAddress.createUnresolved("example.i2p", 1234)));
    assertFalse(addr.equals(I2PSocketAddress.createUnresolved("example2.i2p", 1234)));
    assertFalse(addr.equals(I2PSocketAddress.createUnresolved("example.i2p", 1235)));
    assertFalse(addr.equals(I2PSocketAddress.createUnresolved("example.i2p", 1235)));
    Destination dest = new Destination();
    I2PSocketAddress addr2 = new I2PSocketAddress(dest, 1234);
    assertFalse(addr.equals(null));
    assertFalse(addr.equals(dest));
    assertFalse(addr.equals(addr2));
    assertFalse(addr2.equals(addr));
}
Also used : Destination(net.i2p.data.Destination) Test(org.junit.Test)

Example 28 with Destination

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

the class StreamSinkSend method runClient.

/**
 * Actually connect and run the client - this call blocks until completion.
 */
public void runClient() {
    I2PSocketManager mgr = I2PSocketManagerFactory.createManager();
    Destination peer = null;
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(_peerDestFile);
        peer = new Destination();
        peer.readBytes(fis);
    } catch (IOException ioe) {
        _log.error("Error finding the peer destination to contact in " + _peerDestFile, ioe);
        return;
    } catch (DataFormatException dfe) {
        _log.error("Peer destination is not valid in " + _peerDestFile, dfe);
        return;
    } finally {
        if (fis != null)
            try {
                fis.close();
            } catch (IOException ioe) {
            }
    }
    System.out.println("Send " + _sendFile + " to " + peer.calculateHash().toBase64());
    try {
        I2PSocket sock = mgr.connect(peer);
        byte[] buf = new byte[32 * 1024];
        OutputStream out = sock.getOutputStream();
        long beforeSending = System.currentTimeMillis();
        fis = new FileInputStream(_sendFile);
        long size = 0;
        while (true) {
            int read = fis.read(buf);
            if (read < 0)
                break;
            out.write(buf, 0, read);
            size += read;
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("Wrote " + read);
            if (_writeDelay > 0) {
                try {
                    Thread.sleep(_writeDelay);
                } catch (InterruptedException ie) {
                }
            }
        }
        fis.close();
        sock.close();
        long afterSending = System.currentTimeMillis();
        System.out.println("Sent " + (size / 1024) + "KB in " + (afterSending - beforeSending) + "ms");
    } catch (InterruptedIOException iie) {
        _log.error("Timeout connecting to the peer", iie);
        return;
    } catch (NoRouteToHostException nrthe) {
        _log.error("Unable to connect to the peer", nrthe);
        return;
    } catch (ConnectException ce) {
        _log.error("Connection already dropped", ce);
        return;
    } catch (I2PException ie) {
        _log.error("Error connecting to the peer", ie);
        return;
    } catch (IOException ioe) {
        _log.error("IO error sending", ioe);
        return;
    }
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) InterruptedIOException(java.io.InterruptedIOException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) NoRouteToHostException(java.net.NoRouteToHostException) FileInputStream(java.io.FileInputStream) DataFormatException(net.i2p.data.DataFormatException) ConnectException(java.net.ConnectException)

Example 29 with Destination

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

the class StreamSinkServer method runServer.

/**
 * Actually fire up the server - this call blocks forever (or until the server
 * socket closes)
 */
public void runServer() {
    I2PSocketManager mgr = null;
    if (_i2cpHost != null)
        mgr = I2PSocketManagerFactory.createManager(_i2cpHost, _i2cpPort, new Properties());
    else
        mgr = I2PSocketManagerFactory.createManager();
    Destination dest = mgr.getSession().getMyDestination();
    if (_log.shouldLog(Log.INFO))
        _log.info("Listening for connections on: " + dest.calculateHash().toBase64());
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(_destFile);
        dest.writeBytes(fos);
    } catch (IOException ioe) {
        _log.error("Error writing out our destination to " + _destFile, ioe);
        return;
    } catch (DataFormatException dfe) {
        _log.error("Error formatting the destination", dfe);
        return;
    } finally {
        if (fos != null)
            try {
                fos.close();
            } catch (IOException ioe) {
            }
    }
    I2PServerSocket sock = mgr.getServerSocket();
    startup(sock);
}
Also used : Destination(net.i2p.data.Destination) DataFormatException(net.i2p.data.DataFormatException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Properties(java.util.Properties)

Example 30 with Destination

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

the class I2PClientImpl method createDestination.

/**
 * Create the destination with the given payload and write it out along with
 * the PrivateKey and SigningPrivateKey to the destKeyStream
 *
 * If cert is a KeyCertificate, the signing keypair will be of the specified type.
 * The KeyCertificate data must be .............................
 * The padding if any will be randomized. The extra key data if any will be set in the
 * key cert.
 *
 * Caller must close stream.
 *
 * @param destKeyStream location to write out the destination, PrivateKey, and SigningPrivateKey,
 *                      format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
 */
public Destination createDestination(OutputStream destKeyStream, Certificate cert) throws I2PException, IOException {
    Destination d = new Destination();
    Object[] keypair = KeyGenerator.getInstance().generatePKIKeypair();
    PublicKey publicKey = (PublicKey) keypair[0];
    PrivateKey privateKey = (PrivateKey) keypair[1];
    SimpleDataStructure[] signingKeys;
    if (cert.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
        KeyCertificate kcert = cert.toKeyCertificate();
        SigType type = kcert.getSigType();
        try {
            signingKeys = KeyGenerator.getInstance().generateSigningKeys(type);
        } catch (GeneralSecurityException gse) {
            throw new I2PException("keygen fail", gse);
        }
    } else {
        signingKeys = KeyGenerator.getInstance().generateSigningKeys();
    }
    SigningPublicKey signingPubKey = (SigningPublicKey) signingKeys[0];
    SigningPrivateKey signingPrivKey = (SigningPrivateKey) signingKeys[1];
    d.setPublicKey(publicKey);
    d.setSigningPublicKey(signingPubKey);
    if (cert.getCertificateType() == Certificate.CERTIFICATE_TYPE_KEY) {
        // fix up key certificate or padding
        KeyCertificate kcert = cert.toKeyCertificate();
        SigType type = kcert.getSigType();
        int len = type.getPubkeyLen();
        if (len < 128) {
            byte[] pad = new byte[128 - len];
            RandomSource.getInstance().nextBytes(pad);
            d.setPadding(pad);
        } else if (len > 128) {
            System.arraycopy(signingPubKey.getData(), 128, kcert.getPayload(), KeyCertificate.HEADER_LENGTH, len - 128);
        }
    }
    d.setCertificate(cert);
    d.writeBytes(destKeyStream);
    privateKey.writeBytes(destKeyStream);
    signingPrivKey.writeBytes(destKeyStream);
    destKeyStream.flush();
    return d;
}
Also used : I2PException(net.i2p.I2PException) Destination(net.i2p.data.Destination) SigningPublicKey(net.i2p.data.SigningPublicKey) PrivateKey(net.i2p.data.PrivateKey) SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) PublicKey(net.i2p.data.PublicKey) GeneralSecurityException(java.security.GeneralSecurityException) SigType(net.i2p.crypto.SigType) SigningPrivateKey(net.i2p.data.SigningPrivateKey) KeyCertificate(net.i2p.data.KeyCertificate) SimpleDataStructure(net.i2p.data.SimpleDataStructure)

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