Example #1
0
 public UDPReceiver(
     RouterContext ctx, UDPTransport transport, DatagramSocket socket, String name) {
   _context = ctx;
   _log = ctx.logManager().getLog(UDPReceiver.class);
   _name = name;
   _socket = socket;
   _transport = transport;
   _handler = transport.getPacketHandler();
   if (_handler == null) throw new IllegalStateException();
   _runner = new Runner();
   // _context.statManager().createRateStat("udp.receivePacketSize", "How large packets received
   // are", "udp", UDPTransport.RATES);
   // _context.statManager().createRateStat("udp.receiveRemaining", "How many packets are left
   // sitting on the receiver's queue", "udp", UDPTransport.RATES);
   // _context.statManager().createRateStat("udp.droppedInbound", "How many packet are queued up
   // but not yet received when we drop", "udp", UDPTransport.RATES);
   _context
       .statManager()
       .createRateStat(
           "udp.receiveHolePunch",
           "How often we receive a NAT hole punch",
           "udp",
           UDPTransport.RATES);
   _context
       .statManager()
       .createRateStat(
           "udp.ignorePacketFromDroplist",
           "Packet lifetime for those dropped on the drop list",
           "udp",
           UDPTransport.RATES);
 }
Example #2
0
  /**
   * 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; **
     */
  }