public I2PSinkAnywhere(I2PSession sess, boolean raw) { this.sess = sess; this.raw = raw; // create maker if (raw) { this.maker = null; } else { this.maker = new I2PDatagramMaker(); this.maker.setI2PDatagramMaker(this.sess); } }
/** * Get the sender of an I2P repliable datagram (previously loaded with the * loadI2PDatagram() method), verifying the datagram signature. * * @return The Destination of the I2P repliable datagram sender * * @throws I2PInvalidDatagramException if the signature verification fails */ public Destination getSender() throws I2PInvalidDatagramException { this.verifySignature(); return this.extractSender(); }
/** * Get the payload carried by an I2P repliable datagram (previously loaded * with the loadI2PDatagram() method), verifying the datagram signature. * * @return A byte array containing the datagram payload * * @throws I2PInvalidDatagramException if the signature verification fails */ public byte[] getPayload() throws I2PInvalidDatagramException { this.verifySignature(); return this.extractPayload(); }
public void testDatagram() throws Exception{ ByteArrayOutputStream out = new ByteArrayOutputStream(); I2PClient client = I2PClientFactory.createClient(); Destination d = client.createDestination(out); I2PSession session = client.createSession(new ByteArrayInputStream(out.toByteArray()), null); I2PDatagramMaker dm = new I2PDatagramMaker(session); byte[] dg = dm.makeI2PDatagram(DataHelper.getASCII("What's the deal with 42?")); I2PDatagramDissector dd = new I2PDatagramDissector(); dd.loadI2PDatagram(dg); byte[] x = dd.getPayload(); assertTrue(DataHelper.eq(x, DataHelper.getASCII("What's the deal with 42?"))); x = dd.extractPayload(); assertTrue(DataHelper.eq(x, DataHelper.getASCII("What's the deal with 42?"))); assertEquals(d, dd.getSender()); assertEquals(d, dd.extractSender()); }
public void run() { I2PDatagramDissector diss = new I2PDatagramDissector(); _running = true; while (_running) { diss.loadI2PDatagram(msg); this.sink.send(diss.getSender(), diss.getPayload()); else this.sink.send(diss.extractSender(), diss.extractPayload()); } else {
I2PDatagramDissector dd = new I2PDatagramDissector(); dd.loadI2PDatagram(data); dd.getPayload(); }catch(I2PInvalidDatagramException i2pide){ error = true; assertTrue(error); dd.getSender(); }catch(I2PInvalidDatagramException i2pide){ error = true; assertTrue(error); dd.getHash(); }catch(I2PInvalidDatagramException i2pide){ error = true; assertTrue(error);
if (toPort == _qPort) { I2PDatagramDissector dgDiss = new I2PDatagramDissector(); dgDiss.loadI2PDatagram(payload); payload = dgDiss.getPayload(); Destination from = dgDiss.getSender();
protected void messageReceived(byte[] msg, int proto, int fromPort, int toPort) { byte[] payload; Destination sender; try { synchronized (dgramDissector) { dgramDissector.loadI2PDatagram(msg); sender = dgramDissector.getSender(); payload = dgramDissector.extractPayload(); } } catch (DataFormatException e) { if (_log.shouldLog(Log.DEBUG)) { _log.debug("Dropping ill-formatted I2P repliable datagram", e); } return; } catch (I2PInvalidDatagramException e) { if (_log.shouldLog(Log.DEBUG)) { _log.debug("Dropping ill-signed I2P repliable datagram", e); } return; } try { recv.receiveDatagramBytes(sender, payload, proto, fromPort, toPort); } catch (IOException e) { _log.error("Error forwarding message to receiver", e); close(); } }
I2PDatagramMaker dgMaker = new I2PDatagramMaker(_session); payload = dgMaker.makeI2PDatagram(payload); if (payload == null) { if (_log.shouldLog(Log.WARN))
/** * @param src ignored * @throws RuntimeException if session is closed */ public synchronized void send(Destination src, byte[] data) { //System.out.print("w"); // create payload byte[] payload; if(!this.raw) { synchronized(this.maker) { payload = this.maker.makeI2PDatagram(data); } } else payload = data; // send message try { this.sess.sendMessage(this.dest, payload, (this.raw ? I2PSession.PROTO_DATAGRAM_RAW : I2PSession.PROTO_DATAGRAM), I2PSession.PORT_UNSPECIFIED, I2PSession.PORT_UNSPECIFIED); } catch (I2PSessionException ise) { throw new RuntimeException("failed to send data", ise); } }
/** * Create a new SAM DATAGRAM session on an existing I2P session. * * @param props unused for now * @since 0.9.25 */ protected SAMDatagramSession(I2PSession sess, Properties props, int listenPort, SAMDatagramReceiver recv) throws IOException, DataFormatException, I2PSessionException { super(sess, I2PSession.PROTO_DATAGRAM, listenPort); this.recv = recv; dgramMaker = new I2PDatagramMaker(getI2PSession()); }
/** * Extract the hash of the payload of an I2P repliable datagram (previously * loaded with the loadI2PDatagram() method), verifying the datagram * signature. * * As of 0.9.14, for signature types other than DSA_SHA1, this returns null. * * @return The hash of the payload of the I2P repliable datagram * @throws I2PInvalidDatagramException if the signature verification fails */ public Hash getHash() throws I2PInvalidDatagramException { // make sure it has a valid signature this.verifySignature(); return extractHash(); }
/** * Construct a new I2PDatagramMaker that will be able to create I2P * repliable datagrams going to be sent through the specified I2PSession. * * @param session I2PSession used to send I2PDatagrams through */ public I2PDatagramMaker(I2PSession session) { this.setI2PDatagramMaker(session); }
/** * Verify the signature of this datagram (previously loaded with the * loadI2PDatagram() method) * @throws I2PInvalidDatagramException if the signature is invalid */ public void verifySignature() throws I2PInvalidDatagramException { // first check if it already got validated if(this.valid) return; if (rxSign == null || rxSign.getData() == null || rxDest == null) throw new I2PInvalidDatagramException("Datagram not yet read"); // now validate SigningPublicKey spk = rxDest.getSigningPublicKey(); SigType type = spk.getType(); if (type == null) throw new I2PInvalidDatagramException("unsupported sig type"); if (type == SigType.DSA_SHA1) { if (!this.dsaEng.verifySignature(rxSign, rxHash, spk)) throw new I2PInvalidDatagramException("Incorrect I2P repliable datagram signature"); } else { if (!this.dsaEng.verifySignature(rxSign, rxPayload, 0, rxPayloadLen, spk)) throw new I2PInvalidDatagramException("Incorrect I2P repliable datagram signature"); } // set validated this.valid = true; } }
/** * @param to - where it's going * @throws RuntimeException if session is closed */ public synchronized void send(Destination to, byte[] data) { // create payload byte[] payload; if(!this.raw) { synchronized(this.maker) { payload = this.maker.makeI2PDatagram(data); } } else payload = data; // send message try { this.sess.sendMessage(to, payload, (this.raw ? I2PSession.PROTO_DATAGRAM_RAW : I2PSession.PROTO_DATAGRAM), I2PSession.PORT_UNSPECIFIED, I2PSession.PORT_UNSPECIFIED); } catch (I2PSessionException ise) { throw new RuntimeException("failed to send data", ise); } }
public I2PSink(I2PSession sess, Destination dest, boolean raw) { this.sess = sess; this.dest = dest; this.raw = raw; // create maker if (raw) { this.maker = null; } else { this.maker = new I2PDatagramMaker(); this.maker.setI2PDatagramMaker(this.sess); } }
/** * Create a new SAM DATAGRAM session. * * @param dest Base64-encoded destination (private key) * @param props Properties to setup the I2P session * @param recv Object that will receive incoming data * @throws IOException * @throws DataFormatException * @throws I2PSessionException */ protected SAMDatagramSession(String dest, Properties props, SAMDatagramReceiver recv) throws IOException, DataFormatException, I2PSessionException { super(dest, props); this.recv = recv; dgramMaker = new I2PDatagramMaker(getI2PSession()); }
/** * Send bytes through a SAM DATAGRAM session. * * @since 0.9.25 */ public boolean sendBytes(String dest, byte[] data, int proto, int fromPort, int toPort, boolean sendLeaseSet, int sendTags, int tagThreshold, int expiration) throws DataFormatException, I2PSessionException { if (data.length > DGRAM_SIZE_MAX) throw new DataFormatException("Datagram size exceeded (" + data.length + ")"); byte[] dgram ; synchronized (dgramMaker) { dgram = dgramMaker.makeI2PDatagram(data); } return sendBytesThroughMessageSession(dest, dgram, I2PSession.PROTO_DATAGRAM, fromPort, toPort, sendLeaseSet, sendTags,tagThreshold, expiration); }
/** * Create a new SAM DATAGRAM session. * * Caller MUST call start(). * * @param destStream Input stream containing the destination keys * @param props Properties to setup the I2P session * @param recv Object that will receive incoming data * @throws IOException * @throws DataFormatException * @throws I2PSessionException */ public SAMDatagramSession(InputStream destStream, Properties props, SAMDatagramReceiver recv) throws IOException, DataFormatException, I2PSessionException { super(destStream, props); this.recv = recv; dgramMaker = new I2PDatagramMaker(getI2PSession()); }
/** * Send bytes through a SAM DATAGRAM session. * * @param dest Destination * @param data Bytes to be sent * @param proto ignored, will always use PROTO_DATAGRAM (17) * * @return True if the data was sent, false otherwise * @throws DataFormatException on unknown / bad dest * @throws I2PSessionException on serious error, probably session closed */ public boolean sendBytes(String dest, byte[] data, int proto, int fromPort, int toPort) throws DataFormatException, I2PSessionException { if (data.length > DGRAM_SIZE_MAX) throw new DataFormatException("Datagram size exceeded (" + data.length + ")"); byte[] dgram ; synchronized (dgramMaker) { dgram = dgramMaker.makeI2PDatagram(data); } return sendBytesThroughMessageSession(dest, dgram, I2PSession.PROTO_DATAGRAM, fromPort, toPort); }