Java Code Examples for java.net.DatagramPacket#setLength()

The following examples show how to use java.net.DatagramPacket#setLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatagramSocketAdaptor.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void receive(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                SocketAddress sender = receive(bb);
                p.setSocketAddress(sender);
                p.setLength(bb.position() - p.getOffset());
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example 2
Source File: SnmpAdaptorServer.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 3
Source File: UdpManager.java    From jane with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 分配DatagramPacket对象. 容量固定为PACKET_BUFFER_CAPACITY, 没设置地址
 */
public static DatagramPacket allocPacket()
{
	synchronized (freeList)
	{
		int size = freeSize;
		if (size > 0)
		{
			freeSize = --size;
			DatagramPacket packet = freeList[size];
			freeList[size] = null;
			packet.setLength(PACKET_BUFFER_CAPACITY);
			return packet;
		}
	}
	return new DatagramPacket(new byte[PACKET_BUFFER_CAPACITY], PACKET_BUFFER_CAPACITY);
}
 
Example 4
Source File: SnmpAdaptorServer.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 5
Source File: SnmpAdaptorServer.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 6
Source File: SnmpAdaptorServer.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 7
Source File: SnmpAdaptorServer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 8
Source File: DatagramSocketAdaptor.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void receive(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                SocketAddress sender = receive(bb);
                p.setSocketAddress(sender);
                p.setLength(bb.position() - p.getOffset());
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example 9
Source File: SnmpAdaptorServer.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 10
Source File: SnmpAdaptorServer.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 11
Source File: SnmpAdaptorServer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 12
Source File: DatagramSocketAdaptor.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void receive(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                SocketAddress sender = receive(bb);
                p.setSocketAddress(sender);
                p.setLength(bb.position() - p.getOffset());
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example 13
Source File: SnmpAdaptorServer.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 14
Source File: PacketUtilsTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void testSliceData3() throws Exception {
    DatagramPacket packet = createPacket(10);
    Arrays.fill(packet.getData(), 1, 8, (byte)'a');
    packet.setLength(4);

    byte[] bytes1 = PacketUtils.sliceData(packet, 0);
    Assert.assertArrayEquals(bytes1, Arrays.copyOf(packet.getData(), 4));
}
 
Example 15
Source File: VDatagramSocket.java    From finalspeed with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void receive(DatagramPacket p) throws IOException {
    TunData td;
    try {
        td = packetList.take();
        p.setData(td.data);
        p.setLength(td.data.length);
        p.setAddress(td.tun.remoteAddress);
        p.setPort(CapEnv.toUnsigned(td.tun.remotePort));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
Example 16
Source File: DNSTunnel.java    From java-tcp-tunnel with MIT License 5 votes vote down vote up
public void run() {
  String dateStr = sdf.format(new Date());
  byte[] buffer = new byte[65536];
  DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  while (true) {
    try {
      packet.setData(buffer);
      packet.setLength(buffer.length);
      sourceSocket.receive(packet);
      active = true;
      InetAddress sourceAddress = packet.getAddress();
      int srcPort = packet.getPort();
      InetAddress address = InetAddress.getByName(params.getRemoteHost());
      String strAddr = toStr(packet);
      if (params.isPrint()) {
        System.out.println(dateStr + ": DNS Forwarding " +packet.getLength()+ " bytes " + strAddr + " --> " + address.getHostAddress() + ":" + params.getRemotePort());
      }
      //send client request to server
      packet.setPort(params.getRemotePort());
      packet.setAddress(address);
      Thread tunnel = new DNSForwarder(packet, sourceSocket, sourceAddress, srcPort, params);
      tunnel.start();

    } catch (Throwable e) {
      if (params.isPrint()) {
        String remoteAddr = params.getRemoteHost() + ":" + params.getRemotePort();
        String humanRemoteAddr = Utils.mapAddrToHumanReadable(remoteAddr);
        remoteAddr = remoteAddr + " (" + humanRemoteAddr + ")";
        System.err.println(dateStr + ": Failed to connect to remote host (" + remoteAddr + ")");
        e.printStackTrace();
      }
      connectionBroken();
    }
  }
}
 
Example 17
Source File: DatagramPacketFactoryTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void testBeforeReturn() throws Exception {
    int bufferLength = 10;
    DatagramPacketFactory factory = new DatagramPacketFactory(bufferLength);
    DatagramPacket packet = factory.create();

    packet.setLength(1);
    factory.beforeReturn(packet);
    Assert.assertEquals(bufferLength, packet.getLength());

}
 
Example 18
Source File: PacketUtilsTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void testSliceData1() throws Exception {
    DatagramPacket packet = createPacket(10);
    packet.setLength(5);


    byte[] bytes1 = PacketUtils.sliceData(packet, 0);
    Assert.assertEquals(bytes1.length, 5);
}
 
Example 19
Source File: Socks5DatagramSocket.java    From T0rlib4j with Apache License 2.0 4 votes vote down vote up
/**
 * Receives udp packet. If packet have arrived from the proxy relay server,
 * it is processed and address and port of the packet are set to the address
 * and port of sending host.<BR>
 * If the packet arrived from anywhere else it is not changed.<br>
 * <B> NOTE: </B> DatagramPacket size should be at least 10 bytes bigger
 * than the largest packet you expect (this is for IPV4 addresses). For
 * hostnames and IPV6 it is even more.
 * 
 * @param dp
 *            Datagram in which all relevent information will be copied.
 */
public void receive(DatagramPacket dp) throws IOException {
	super.receive(dp);

	if (server_mode) {
		// Drop all datagrams not from relayIP/relayPort
		final int init_length = dp.getLength();
		final int initTimeout = getSoTimeout();
		final long startTime = System.currentTimeMillis();

		while (!relayIP.equals(dp.getAddress())
				|| (relayPort != dp.getPort())) {

			// Restore datagram size
			dp.setLength(init_length);

			// If there is a non-infinit timeout on this socket
			// Make sure that it happens no matter how often unexpected
			// packets arrive.
			if (initTimeout != 0) {
				final long passed = System.currentTimeMillis() - startTime;
				final int newTimeout = initTimeout - (int) passed;

				if (newTimeout <= 0) {
					throw new InterruptedIOException(
							"In Socks5DatagramSocket->receive()");
				}
				setSoTimeout(newTimeout);
			}

			super.receive(dp);
		}

		// Restore timeout settings
		if (initTimeout != 0) {
			setSoTimeout(initTimeout);
		}

	} else if (!relayIP.equals(dp.getAddress())
			|| (relayPort != dp.getPort())) {
		return; // Recieved direct packet
		// If the datagram is not from the relay server, return it it as is.
	}

	byte[] data;
	data = dp.getData();

	if (encapsulation != null) {
		data = encapsulation.udpEncapsulate(data, false);
	}

	// FIXME: What is this?
	final int offset = 0; // Java 1.1
	// int offset = dp.getOffset(); //Java 1.2

	final ByteArrayInputStream bIn = new ByteArrayInputStream(data, offset,
			dp.getLength());

	final ProxyMessage msg = new Socks5Message(bIn);
	dp.setPort(msg.port);
	dp.setAddress(msg.getInetAddress());

	// what wasn't read by the Message is the data
	final int data_length = bIn.available();
	// Shift data to the left
	System.arraycopy(data, offset + dp.getLength() - data_length, data,
			offset, data_length);

	dp.setLength(data_length);
}
 
Example 20
Source File: DatagramPacketFactory.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Override
public void beforeReturn(DatagramPacket packet) {
    packet.setLength(bufferLength);
}