Example usage for java.lang SecurityManager checkListen

List of usage examples for java.lang SecurityManager checkListen

Introduction

In this page you can find the example usage for java.lang SecurityManager checkListen.

Prototype

public void checkListen(int port) 

Source Link

Document

Throws a SecurityException if the calling thread is not allowed to wait for a connection request on the specified local port number.

Usage

From source file:Main.java

public static void main(String[] args) {
    System.setProperty("java.security.policy", "file:/C:/java.policy");

    SecurityManager sm = new Main();

    System.setSecurityManager(sm);

    // perform the check
    sm.checkListen(8080);

    System.out.println("Allowed!");
}

From source file:Tcpbw100.java

public boolean test_sfw(Protocol ctl) throws IOException {
    Message msg = new Message();
    if ((tests & TEST_SFW) == TEST_SFW) {
        showStatus(messages.getString("sfwTest"));
        results.append(messages.getString("checkingFirewalls") + "  ");
        statistics.append(messages.getString("checkingFirewalls") + "  ");
        emailText = messages.getString("checkingFirewalls") + "  ";
        pub_status = "checkingFirewalls";

        if (ctl.recv_msg(msg) != 0) {
            errmsg = messages.getString("protocolError") + Integer.parseInt(new String(msg.body), 16)
                    + " instead\n";
            return true;
        }//ww w  . j  a  va 2s .c  o m
        if (msg.type != TEST_PREPARE) {
            errmsg = messages.getString("sfwWrongMessage") + "\n";
            if (msg.type == MSG_ERROR) {
                errmsg += "ERROR MSG: " + Integer.parseInt(new String(msg.body), 16) + "\n";
            }
            return true;
        }

        String message = new String(msg.body);

        int srvPort, testTime;
        try {
            int k = message.indexOf(" ");
            srvPort = Integer.parseInt(message.substring(0, k));
            testTime = Integer.parseInt(message.substring(k + 1));
        } catch (Exception e) {
            errmsg = messages.getString("sfwWrongMessage") + "\n";
            return true;
        }

        System.out.println("SFW: port=" + srvPort);
        System.out.println("SFW: testTime=" + testTime);

        ServerSocket srvSocket;
        try {
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                System.out.println("Asking security manager for listen permissions...");
                security.checkListen(0);
            }
            srvSocket = new ServerSocket(0);
        } catch (Exception e) {
            e.printStackTrace();
            errmsg = messages.getString("sfwSocketFail") + "\n";
            return true;
        }

        System.out.println("SFW: oport=" + srvSocket.getLocalPort());
        ctl.send_msg(TEST_MSG, Integer.toString(srvSocket.getLocalPort()).getBytes());

        if (ctl.recv_msg(msg) != 0) {
            errmsg = messages.getString("protocolError") + Integer.parseInt(new String(msg.body), 16)
                    + " instead\n";
            return true;
        }
        if (msg.type != TEST_START) {
            errmsg = messages.getString("sfwWrongMessage");
            if (msg.type == MSG_ERROR) {
                errmsg += "ERROR MSG: " + Integer.parseInt(new String(msg.body), 16) + "\n";
            }
            return true;
        }

        OsfwWorker osfwTest = new OsfwWorker(srvSocket, testTime);
        new Thread(osfwTest).start();

        Socket sfwSocket = new Socket();
        try {
            sfwSocket.connect(new InetSocketAddress(host, srvPort), testTime * 1000);

            Protocol sfwCtl = new Protocol(sfwSocket);
            sfwCtl.send_msg(TEST_MSG, new String("Simple firewall test").getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (ctl.recv_msg(msg) != 0) {
            errmsg = messages.getString("protocolError") + Integer.parseInt(new String(msg.body), 16)
                    + " instead\n";
            return true;
        }
        if (msg.type != TEST_MSG) {
            errmsg = messages.getString("sfwWrongMessage") + "\n";
            if (msg.type == MSG_ERROR) {
                errmsg += "ERROR MSG: " + Integer.parseInt(new String(msg.body), 16) + "\n";
            }
            return true;
        }
        c2sResult = Integer.parseInt(new String(msg.body));

        osfwTest.finalize();

        if (ctl.recv_msg(msg) != 0) {
            errmsg = messages.getString("protocolError") + Integer.parseInt(new String(msg.body), 16)
                    + " instead\n";
            return true;
        }
        if (msg.type != TEST_FINALIZE) {
            errmsg = messages.getString("sfwWrongMessage") + "\n";
            if (msg.type == MSG_ERROR) {
                errmsg += "ERROR MSG: " + Integer.parseInt(new String(msg.body), 16) + "\n";
            }
            return true;
        }
        results.append(messages.getString("done") + "\n");
        statistics.append(messages.getString("done") + "\n");
        emailText += messages.getString("done") + "\n%0A";
    }
    return false;
}