Search in sources :

Example 86 with DatagramSocket

use of java.net.DatagramSocket in project jdk8u_jdk by JetBrains.

the class MaxRetries method main.

public static void main(String[] args) throws Exception {
    System.setProperty("sun.security.krb5.debug", "true");
    OneKDC kdc = new OneKDC(null).writeJAASConf();
    // An idle UDP socket to prevent PortUnreachableException
    DatagramSocket ds = new DatagramSocket();
    idlePort = ds.getLocalPort();
    cm.addPort(idlePort);
    cm.addPort(kdc.getPort());
    System.setProperty("java.security.krb5.conf", "alternative-krb5.conf");
    Security.setProperty("krb5.kdc.bad.policy", "trylast");
    // We always make the real timeout to be 1 second
    BadKdc.setRatio(0.25f);
    rewriteMaxRetries(4);
    // Explanation: In this case, max_retries=4 and timeout=4s.
    // For AS-REQ without preauth, we will see 4 4s timeout on kdc#1
    // ("a4" repeat 4 times), and one 4s timeout on kdc#2 ("b4"). For
    // AS-REQ with preauth, one 4s timeout on kdc#2 (second "b4").
    // we tolerate 4 real timeout on kdc#2, so make it "(b4){2,6}".
    test1("a4a4a4a4b4b4", "a4a4a4a4(b4){2,6}");
    test1("b4b4", "(b4){2,6}");
    BadKdc.setRatio(1f);
    rewriteMaxRetries(1);
    // Explanation: Since max_retries=1 only, we could fail in 1st or 2nd
    // AS-REQ to kdc#2.
    String actual = test1("a1b1b1", "(a1b1b1|a1b1x|a1b1b1x)");
    if (actual.endsWith("x")) {
        // If 1st attempt fails, all bads are back available
        test1("a1b1b1", "(a1b1b1|a1b1x|a1b1b1x)");
    } else {
        test1("b1b1", "(b1b1|b1x|b1b1x)");
    }
    BadKdc.setRatio(0.2f);
    rewriteMaxRetries(-1);
    test1("a5a5a5b5b5", "a5a5a5(b5){2,4}");
    test1("b5b5", "(b5){2,4}");
    BadKdc.setRatio(0.25f);
    Security.setProperty("krb5.kdc.bad.policy", "tryless:1,1000");
    rewriteMaxRetries(4);
    test1("a4a4a4a4b4a4b4", "a4a4a4a4(b4){1,3}a4(b4){1,3}");
    test1("a4b4a4b4", "a4(b4){1,3}a4(b4){1,3}");
    BadKdc.setRatio(1f);
    rewriteMaxRetries(1);
    actual = test1("a1b1a1b1", "(a1b1|a1b1x|a1b1a1b1|a1b1a1b1x)");
    if (actual.endsWith("x")) {
        test1("a1b1a1b1", "(a1b1|a1b1x|a1b1a1b1|a1b1a1b1x)");
    } else {
        test1("a1b1a1b1", "(a1b1|a1b1x|a1b1a1b1|a1b1a1b1x)");
    }
    BadKdc.setRatio(.2f);
    rewriteMaxRetries(-1);
    test1("a5a5a5b5a5b5", "a5a5a5(b5){1,2}a5(b5){1,2}");
    test1("a5b5a5b5", "a5(b5){1,2}a5(b5){1,2}");
    BadKdc.setRatio(1f);
    rewriteMaxRetries(2);
    if (BadKdc.toReal(2000) > 1000) {
        // Explanation: if timeout is longer than 1s in tryLess,
        // we will see "a1" at 2nd kdc#1 access
        test1("a2a2b2a1b2", "a2a2(b2){1,2}a1(b2){1,2}");
    } else {
        test1("a2a2b2a2b2", "a2a2(b2){1,2}a2(b2){1,2}");
    }
    BadKdc.setRatio(1f);
    // default, no limit
    rewriteUdpPrefLimit(-1, -1);
    test2("UDP");
    // global rules
    rewriteUdpPrefLimit(10, -1);
    test2("TCP");
    // realm rules
    rewriteUdpPrefLimit(10, 10000);
    test2("UDP");
    // realm rules
    rewriteUdpPrefLimit(10000, 10);
    test2("TCP");
    ds.close();
}
Also used : DatagramSocket(java.net.DatagramSocket)

Example 87 with DatagramSocket

use of java.net.DatagramSocket in project kdeconnect-android by KDE.

the class LanLinkProvider method setupUdpListener.

private DatagramSocket setupUdpListener(int udpPort) {
    final DatagramSocket server;
    try {
        server = new DatagramSocket(udpPort);
        server.setReuseAddress(true);
        server.setBroadcast(true);
    } catch (SocketException e) {
        Log.e("LanLinkProvider", "Error creating udp server");
        e.printStackTrace();
        return null;
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            while (listening) {
                final int bufferSize = 1024 * 512;
                byte[] data = new byte[bufferSize];
                DatagramPacket packet = new DatagramPacket(data, bufferSize);
                try {
                    server.receive(packet);
                    udpPacketReceived(packet);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("LanLinkProvider", "UdpReceive exception");
                }
            }
            Log.w("UdpListener", "Stopping UDP listener");
        }
    }).start();
    return server;
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) SocketException(java.net.SocketException) IOException(java.io.IOException)

Example 88 with DatagramSocket

use of java.net.DatagramSocket in project jdk8u_jdk by JetBrains.

the class SocketPermissionTest method sendDatagramPacketTest.

@Test
public void sendDatagramPacketTest() throws Exception {
    byte[] msg = "Hello".getBytes(UTF_8);
    InetAddress group = InetAddress.getByName("229.227.226.221");
    try (DatagramSocket ds = new DatagramSocket(0)) {
        int port = ds.getLocalPort();
        String addr = "localhost:" + port;
        //test for SocketPermission "229.227.226.221", "connect,accept"
        AccessControlContext acc = getAccessControlContext(new SocketPermission(addr, "listen,resolve"), new SocketPermission("229.227.226.221", "connect,accept"));
        // Positive
        AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
            DatagramPacket hi = new DatagramPacket(msg, msg.length, group, port);
            ds.send(hi);
            return null;
        }, acc);
        // Negative
        try {
            AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
                DatagramPacket hi = new DatagramPacket(msg, msg.length, group, port);
                ds.send(hi);
                fail("Expected SecurityException");
                return null;
            }, RESTRICTED_ACC);
        } catch (SecurityException expected) {
        }
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) DatagramSocket(java.net.DatagramSocket) SocketPermission(java.net.SocketPermission) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress) Test(org.testng.annotations.Test)

Example 89 with DatagramSocket

use of java.net.DatagramSocket in project cloudstack by apache.

the class DhcpProtocolParserServer method run.

@Override
public void run() {
    while (_running) {
        try {
            DatagramSocket dhcpSocket = new DatagramSocket(dhcpServerPort, InetAddress.getByAddress(new byte[] { 0, 0, 0, 0 }));
            dhcpSocket.setBroadcast(true);
            while (true) {
                byte[] buf = new byte[bufferSize];
                DatagramPacket dgp = new DatagramPacket(buf, buf.length);
                dhcpSocket.receive(dgp);
            // _executor.execute(new DhcpPacketParser(buf));
            }
        } catch (IOException e) {
            s_logger.debug(e.getMessage());
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException)

Example 90 with DatagramSocket

use of java.net.DatagramSocket in project robovm by robovm.

the class OldAndroidDatagramTest method testDatagramSocketSetSOTimeout.

// Regression test for issue 1018003: DatagramSocket ignored a set timeout.
public void testDatagramSocketSetSOTimeout() throws Exception {
    DatagramSocket sock = null;
    int timeout = 5000;
    long start = System.currentTimeMillis();
    try {
        sock = new DatagramSocket();
        DatagramPacket pack = new DatagramPacket(new byte[100], 100);
        sock.setSoTimeout(timeout);
        sock.receive(pack);
    } catch (SocketTimeoutException e) {
        // expected
        long delay = System.currentTimeMillis() - start;
        if (Math.abs(delay - timeout) > 1000) {
            fail("timeout was not accurate. expected: " + timeout + " actual: " + delay + " miliseconds.");
        }
    } finally {
        if (sock != null) {
            sock.close();
        }
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket)

Aggregations

DatagramSocket (java.net.DatagramSocket)294 DatagramPacket (java.net.DatagramPacket)137 IOException (java.io.IOException)112 SocketException (java.net.SocketException)74 InetAddress (java.net.InetAddress)63 InetSocketAddress (java.net.InetSocketAddress)46 UnknownHostException (java.net.UnknownHostException)33 Test (org.junit.Test)27 SocketTimeoutException (java.net.SocketTimeoutException)24 InterruptedIOException (java.io.InterruptedIOException)18 PortUnreachableException (java.net.PortUnreachableException)18 ServerSocket (java.net.ServerSocket)18 BindException (java.net.BindException)16 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)16 DatagramChannel (java.nio.channels.DatagramChannel)14 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 MulticastSocket (java.net.MulticastSocket)10 SocketAddress (java.net.SocketAddress)9 ByteBuffer (java.nio.ByteBuffer)8 ArrayList (java.util.ArrayList)6