public void initialize(UDPPacket packet) { int off = packet.getPacket().getOffset(); int len = packet.getPacket().getLength(); off += UDPPacket.MAC_SIZE + UDPPacket.IV_SIZE; len -= UDPPacket.MAC_SIZE + UDPPacket.IV_SIZE; initialize(packet.getPacket().getData(), off, len); }
/** * BLOCKING if queue between here and PacketHandler is full. * * @return zero (was queue size) */ private final int doReceive(UDPPacket packet) { if (!_keepRunning) return 0; if (_log.shouldLog(Log.INFO)) _log.info("Received: " + packet); RemoteHostId from = packet.getRemoteHost(); if (_transport.isInDropList(from)) { if (_log.shouldLog(Log.INFO)) _log.info("Ignoring packet from the drop-listed peer: " + from); _context.statManager().addRateData("udp.ignorePacketFromDroplist", packet.getLifetime()); packet.release(); return 0; } // drop anything apparently from our IP (any port) if (Arrays.equals(from.getIP(), _transport.getExternalIP()) && !_transport.allowLocal()) { if (_log.shouldLog(Log.WARN)) _log.warn("Dropping (spoofed?) packet from ourselves"); packet.release(); return 0; } /** * ** packet.enqueue(); boolean rejected = false; int queueSize = 0; long headPeriod = 0; * * <p>UDPPacket head = _inboundQueue.peek(); if (head != null) { headPeriod = * head.getLifetime(); if (headPeriod > MAX_QUEUE_PERIOD) { rejected = true; } } if (!rejected) * { ** */ try { _handler.queueReceived(packet); } catch (InterruptedException ie) { packet.release(); _keepRunning = false; } // return queueSize + 1; return 0; /** * ** } * * <p>// rejected packet.release(); _context.statManager().addRateData("udp.droppedInbound", * queueSize, headPeriod); if (_log.shouldLog(Log.WARN)) { queueSize = _inboundQueue.size(); * StringBuilder msg = new StringBuilder(); msg.append("Dropping inbound packet with "); * msg.append(queueSize); msg.append(" queued for "); msg.append(headPeriod); msg.append(" * packet handlers: ").append(_transport.getPacketHandlerStatus()); _log.warn(msg.toString()); } * return queueSize; ** */ }
public void run() { // _socketChanged = false; while (_keepRunning) { // if (_socketChanged) { // Thread.currentThread().setName(_name + "." + _id); // _socketChanged = false; // } UDPPacket packet = UDPPacket.acquire(_context, true); // Android ICS bug // http://code.google.com/p/android/issues/detail?id=24748 if (_isAndroid) packet.getPacket().setLength(UDPPacket.MAX_PACKET_SIZE); // block before we read... // if (_log.shouldLog(Log.DEBUG)) // _log.debug("Before throttling receive"); while (!_context.throttle().acceptNetworkMessage()) try { Thread.sleep(10); } catch (InterruptedException ie) { } try { // if (_log.shouldLog(Log.INFO)) // _log.info("Before blocking socket.receive on " + System.identityHashCode(packet)); // synchronized (Runner.this) { _socket.receive(packet.getPacket()); // } int size = packet.getPacket().getLength(); if (_log.shouldLog(Log.INFO)) _log.info( "After blocking socket.receive: packet is " + size + " bytes on " + System.identityHashCode(packet)); packet.resetBegin(); // and block after we know how much we read but before // we release the packet to the inbound queue if (size >= UDPPacket.MAX_PACKET_SIZE) { // DatagramSocket javadocs: If the message is longer than the packet's length, the // message is truncated. throw new IOException( "packet too large! truncated and dropped from: " + packet.getRemoteHost()); } if (_context.commSystem().isDummy()) { // testing packet.release(); } else if (size > 0) { // FIFOBandwidthLimiter.Request req = _context.bandwidthLimiter().requestInbound(size, // "UDP receiver"); // _context.bandwidthLimiter().requestInbound(req, size, "UDP receiver"); FIFOBandwidthLimiter.Request req = _context.bandwidthLimiter().requestInbound(size, "UDP receiver"); while (req.getPendingRequested() > 0) req.waitForNextAllocation(); receive(packet); // _context.statManager().addRateData("udp.receivePacketSize", size); } else { _context.statManager().addRateData("udp.receiveHolePunch", 1); // nat hole punch packets are 0 bytes if (_log.shouldLog(Log.INFO)) _log.info( "Received a 0 byte udp packet from " + packet.getPacket().getAddress() + ":" + packet.getPacket().getPort()); packet.release(); } } catch (IOException ioe) { // if (_socketChanged) { // if (_log.shouldLog(Log.INFO)) // _log.info("Changing ports..."); // } else { if (_log.shouldLog(Log.WARN)) _log.warn("Error receiving", ioe); // } packet.release(); } } if (_log.shouldLog(Log.DEBUG)) _log.debug("Stop receiving..."); }