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();
}
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;
}
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) {
}
}
}
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());
}
}
}
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();
}
}
}
Aggregations