/** * Send a block of encoded bytes to a peer. This is called by send, and by * IncomingPacketFilter.processOutgoing(..). * * @param blockToSend The data block to send. * @param destination The peer to send it to. */ public void sendPacket(byte[] blockToSend, Peer destination, boolean allowLocalAddresses) throws LocalAddressException { assert (blockToSend != null); if (!_active) { Logger.error(this, "Trying to send packet but no longer active"); // It is essential that for recording accurate AddressTracker data that we don't send any more // packets after shutdown. return; } // there should be no DNS needed here, but go ahead if we can, but complain doing it if (destination.getAddress(false, allowLocalAddresses) == null) { Logger.error( this, "Tried sending to destination without pre-looked up IP address(needs a real Peer.getHostname()): null:" + destination.getPort(), new Exception("error")); if (destination.getAddress(true, allowLocalAddresses) == null) { Logger.error( this, "Tried sending to bad destination address: null:" + destination.getPort(), new Exception("error")); return; } } if (_dropProbability > 0) { if (dropRandom.nextInt() % _dropProbability == 0) { Logger.normal(this, "DROPPED: " + _sock.getLocalPort() + " -> " + destination.getPort()); return; } } InetAddress address = destination.getAddress(false, allowLocalAddresses); assert (address != null); int port = destination.getPort(); DatagramPacket packet = new DatagramPacket(blockToSend, blockToSend.length); packet.setAddress(address); packet.setPort(port); try { _sock.send(packet); tracker.sentPacketTo(destination); collector.addInfo(address + ":" + port, 0, blockToSend.length + UDP_HEADERS_LENGTH); if (logMINOR) Logger.minor( this, "Sent packet length " + blockToSend.length + " to " + address + ':' + port); } catch (IOException e) { if (packet.getAddress() instanceof Inet6Address) { Logger.normal( this, "Error while sending packet to IPv6 address: " + destination + ": " + e, e); } else { Logger.error(this, "Error while sending packet to " + destination + ": " + e, e); } } }