private void sendReset(Packet packet) { boolean ok = packet.verifySignature(_context, packet.getOptionalFrom(), null); if (!ok) { if (_log.shouldLog(Log.WARN)) _log.warn("Received a spoofed SYN packet: they said they were " + packet.getOptionalFrom()); return; } PacketLocal reply = new PacketLocal(_context, packet.getOptionalFrom()); reply.setFlag(Packet.FLAG_RESET); reply.setFlag(Packet.FLAG_SIGNATURE_INCLUDED); reply.setAckThrough(packet.getSequenceNum()); reply.setSendStreamId(packet.getReceiveStreamId()); reply.setReceiveStreamId(0); reply.setOptionalFrom(_manager.getSession().getMyDestination()); if (_log.shouldLog(Log.DEBUG)) _log.debug("Sending RST: " + reply + " because of " + packet); // this just sends the packet - no retries or whatnot _manager.getPacketQueue().enqueue(reply); }
private void receiveUnknownCon(Packet packet, long sendId, boolean queueIfNoConn) { if (packet.isFlagSet(Packet.FLAG_ECHO)) { if (packet.getSendStreamId() > 0) { if (_manager.answerPings()) receivePing(packet); else if (_log.shouldLog(Log.WARN)) _log.warn("Dropping Echo packet on unknown con: " + packet); } else if (packet.getReceiveStreamId() > 0) { receivePong(packet); } else { if (_log.shouldLog(Log.WARN)) _log.warn("Echo packet received with no stream IDs: " + packet); } packet.releasePayload(); } else { if (_log.shouldLog(Log.WARN) && !packet.isFlagSet(Packet.FLAG_SYNCHRONIZE)) _log.warn("Packet received on an unknown stream (and not an ECHO or SYN): " + packet); if (sendId <= 0) { Connection con = _manager.getConnectionByOutboundId(packet.getReceiveStreamId()); if (con != null) { if ((con.getHighestAckedThrough() <= 5) && (packet.getSequenceNum() <= 5)) { if (_log.shouldLog(Log.WARN)) _log.warn( "Received additional packet w/o SendStreamID after the syn on " + con + ": " + packet); receiveKnownCon(con, packet); return; } else { if (_log.shouldLog(Log.WARN)) _log.warn( "hrmph, received while ack of syn was in flight on " + con + ": " + packet + " acked: " + con.getAckedPackets()); // allow unlimited packets without a SendStreamID for now receiveKnownCon(con, packet); return; } } } if (packet.isFlagSet(Packet.FLAG_SYNCHRONIZE)) { // logTCPDump() will be called in ConnectionManager.receiveConnection(), // which is called by ConnectionHandler.receiveNewSyn(), // after we have a new conn, which makes the logging better. _manager.getConnectionHandler().receiveNewSyn(packet); } else if (queueIfNoConn) { // don't call logTCPDump() here, wait for it to find a conn // We can get here on the 2nd+ packet if the 1st (SYN) packet // is still on the _synQueue in the ConnectionHandler, and // ConnectionManager.receiveConnection() hasn't run yet to put // the StreamID on the getConnectionByOutboundId list. // Then the 2nd packet gets discarded and has to be retransmitted. // // We fix this by putting this packet on the syn queue too! // Then ConnectionHandler.accept() will check the connection list // and call receivePacket() above instead of receiveConnection(). if (_log.shouldLog(Log.WARN)) { _log.warn("Packet belongs to no other cons, putting on the syn queue: " + packet); } if (_log.shouldLog(Log.DEBUG)) { StringBuilder buf = new StringBuilder(128); for (Connection con : _manager.listConnections()) { buf.append(con.toString()).append(" "); } _log.debug( "connections: " + buf.toString() + " sendId: " + (sendId > 0 ? Packet.toId(sendId) : " unknown")); } // packet.releasePayload(); _manager.getConnectionHandler().receiveNewSyn(packet); } else { // log it here, just before we kill it - dest will be unknown if (I2PSocketManagerFull.pcapWriter != null && _context.getBooleanProperty(I2PSocketManagerFull.PROP_PCAP)) packet.logTCPDump(null); // don't queue again (infinite loop!) sendReset(packet); packet.releasePayload(); } } }
private void receiveKnownCon(Connection con, Packet packet) { // is this ok here or does it need to be below each packetHandler().receivePacket() ? if (I2PSocketManagerFull.pcapWriter != null && _context.getBooleanProperty(I2PSocketManagerFull.PROP_PCAP)) packet.logTCPDump(con); if (packet.isFlagSet(Packet.FLAG_ECHO)) { if (packet.getSendStreamId() > 0) { if (con.getOptions().getAnswerPings()) receivePing(packet); else if (_log.shouldLog(Log.WARN)) _log.warn("Dropping Echo packet on existing con: " + packet); } else if (packet.getReceiveStreamId() > 0) { receivePong(packet); } else { if (_log.shouldLog(Log.WARN)) _log.warn("Echo packet received with no stream IDs: " + packet); } packet.releasePayload(); return; } // the packet is pointed at a stream ID we're receiving on if (isValidMatch(con.getSendStreamId(), packet.getReceiveStreamId())) { // the packet's receive stream ID also matches what we expect // if (_log.shouldLog(Log.DEBUG)) // _log.debug("receive valid: " + packet); try { con.getPacketHandler().receivePacket(packet, con); } catch (I2PException ie) { if (_log.shouldLog(Log.WARN)) _log.warn("Received forged packet for " + con, ie); } } else { if (packet.isFlagSet(Packet.FLAG_RESET)) { // refused if (_log.shouldLog(Log.DEBUG)) _log.debug("receive reset: " + packet); try { con.getPacketHandler().receivePacket(packet, con); } catch (I2PException ie) { if (_log.shouldLog(Log.WARN)) _log.warn("Received forged reset for " + con, ie); } } else { if ((con.getSendStreamId() <= 0) || (con.getSendStreamId() == packet.getReceiveStreamId()) || (packet.getSequenceNum() <= ConnectionOptions.MIN_WINDOW_SIZE)) { // its in flight from the first batch long oldId = con.getSendStreamId(); if (packet.isFlagSet(Packet.FLAG_SYNCHRONIZE)) { if (oldId <= 0) { // con fully established, w00t con.setSendStreamId(packet.getReceiveStreamId()); } else if (oldId == packet.getReceiveStreamId()) { // ok, as expected... } else { if (_log.shouldLog(Log.WARN)) _log.warn("Received a syn with the wrong IDs, con=" + con + " packet=" + packet); sendReset(packet); packet.releasePayload(); return; } } try { con.getPacketHandler().receivePacket(packet, con); } catch (I2PException ie) { if (_log.shouldLog(Log.ERROR)) _log.error("Received forged packet for " + con + "/" + oldId + ": " + packet, ie); con.setSendStreamId(oldId); } } else if (packet.isFlagSet(Packet.FLAG_SYNCHRONIZE)) { if (_log.shouldLog(Log.WARN)) _log.warn("Receive a syn packet with the wrong IDs, sending reset: " + packet); sendReset(packet); packet.releasePayload(); } else { if (!con.getResetSent()) { // someone is sending us a packet on the wrong stream // It isn't a SYN so it isn't likely to have a FROM to send a reset back to if (_log.shouldLog(Log.ERROR)) { StringBuilder buf = new StringBuilder(512); buf.append("Received a packet on the wrong stream: "); buf.append(packet); buf.append("\nthis connection:\n"); buf.append(con); buf.append("\nall connections:"); for (Connection cur : _manager.listConnections()) { buf.append('\n').append(cur); } _log.error(buf.toString(), new Exception("Wrong stream")); } } packet.releasePayload(); } } } }