Search in sources :

Example 1 with I2PDatagramDissector

use of net.i2p.client.datagram.I2PDatagramDissector in project i2p.i2p by i2p.

the class KRPC method messageAvailable.

// I2PSessionMuxedListener interface ----------------
/**
 * Instruct the client that the given session has received a message
 *
 * Will be called only if you register via addMuxedSessionListener().
 * Will be called only for the proto(s) and toPort(s) you register for.
 *
 * @param session session to notify
 * @param msgId message number available
 * @param size size of the message - why it's a long and not an int is a mystery
 * @param proto 1-254 or 0 for unspecified
 * @param fromPort 1-65535 or 0 for unspecified
 * @param toPort 1-65535 or 0 for unspecified
 */
public void messageAvailable(I2PSession session, int msgId, long size, int proto, int fromPort, int toPort) {
    // TODO throttle
    try {
        byte[] payload = session.receiveMessage(msgId);
        if (payload == null)
            return;
        _rxPkts.incrementAndGet();
        _rxBytes.addAndGet(payload.length);
        if (toPort == _qPort) {
            // repliable
            I2PDatagramDissector dgDiss = new I2PDatagramDissector();
            dgDiss.loadI2PDatagram(payload);
            payload = dgDiss.getPayload();
            Destination from = dgDiss.getSender();
            // TODO per-dest throttle
            receiveMessage(from, fromPort, payload);
        } else if (toPort == _rPort) {
            // raw
            receiveMessage(null, fromPort, payload);
        } else {
            if (_log.shouldLog(Log.WARN))
                _log.warn("msg on bad port");
        }
    } catch (DataFormatException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("bad msg");
    } catch (I2PInvalidDatagramException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("bad msg");
    } catch (I2PSessionException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("bad msg");
    }
}
Also used : Destination(net.i2p.data.Destination) I2PDatagramDissector(net.i2p.client.datagram.I2PDatagramDissector) DataFormatException(net.i2p.data.DataFormatException) I2PSessionException(net.i2p.client.I2PSessionException) I2PInvalidDatagramException(net.i2p.client.datagram.I2PInvalidDatagramException)

Example 2 with I2PDatagramDissector

use of net.i2p.client.datagram.I2PDatagramDissector in project i2p.i2p-bote by i2p.

the class I2PPacketDispatcher method messageAvailable.

@Override
public void messageAvailable(I2PSession session, int msgId, long size) {
    byte[] msg = new byte[0];
    try {
        msg = session.receiveMessage(msgId);
    } catch (I2PSessionException e) {
        log.error("Can't get new message from I2PSession.", e);
    }
    if (msg == null) {
        log.error("I2PSession returned a null message: msgId=" + msgId + ", size=" + size + ", " + session);
        return;
    }
    I2PDatagramDissector datagramDissector = new I2PDatagramDissector();
    try {
        datagramDissector.loadI2PDatagram(msg);
        // TODO keep this line or remove it?
        datagramDissector.verifySignature();
        byte[] payload = datagramDissector.extractPayload();
        Destination sender = datagramDissector.getSender();
        dispatchPacket(payload, sender);
    } catch (DataFormatException e) {
        log.error("Invalid datagram received.", e);
    } catch (I2PInvalidDatagramException e) {
        log.error("Datagram failed verification.", e);
    } catch (Exception e) {
        log.error("Error processing datagram.", e);
    }
}
Also used : Destination(net.i2p.data.Destination) I2PDatagramDissector(net.i2p.client.datagram.I2PDatagramDissector) DataFormatException(net.i2p.data.DataFormatException) I2PSessionException(net.i2p.client.I2PSessionException) I2PInvalidDatagramException(net.i2p.client.datagram.I2PInvalidDatagramException) DataFormatException(net.i2p.data.DataFormatException) I2PSessionException(net.i2p.client.I2PSessionException) MalformedPacketException(i2p.bote.packet.MalformedPacketException) I2PInvalidDatagramException(net.i2p.client.datagram.I2PInvalidDatagramException)

Example 3 with I2PDatagramDissector

use of net.i2p.client.datagram.I2PDatagramDissector in project i2p.i2p by i2p.

the class I2PSource method run.

public void run() {
    // create dissector
    I2PDatagramDissector diss = new I2PDatagramDissector();
    _running = true;
    while (_running) {
        try {
            // get id
            int id = this.queue.take();
            // receive message
            byte[] msg = this.sess.receiveMessage(id);
            if (!this.raw) {
                // load datagram into it
                diss.loadI2PDatagram(msg);
                // now call sink
                if (this.verify)
                    this.sink.send(diss.getSender(), diss.getPayload());
                else
                    this.sink.send(diss.extractSender(), diss.extractPayload());
            } else {
                // verify is ignored
                this.sink.send(null, msg);
            }
        // System.out.print("r");
        } catch (Exception e) {
            Log log = I2PAppContext.getGlobalContext().logManager().getLog(getClass());
            if (log.shouldWarn())
                log.warn("error sending", e);
            break;
        }
    }
}
Also used : I2PDatagramDissector(net.i2p.client.datagram.I2PDatagramDissector) Log(net.i2p.util.Log)

Aggregations

I2PDatagramDissector (net.i2p.client.datagram.I2PDatagramDissector)3 I2PSessionException (net.i2p.client.I2PSessionException)2 I2PInvalidDatagramException (net.i2p.client.datagram.I2PInvalidDatagramException)2 DataFormatException (net.i2p.data.DataFormatException)2 Destination (net.i2p.data.Destination)2 MalformedPacketException (i2p.bote.packet.MalformedPacketException)1 Log (net.i2p.util.Log)1