Java Security Manager in Multi-threaded application

I am writing a multi-threaded application listening (TCP and UDP) to several ports. To help implement certain security features (eg. refusing connections from certain ip address), I'm using the java.lang.SecurityManager (by subclassing it). I am having a few problems and queries and am hoping someone here can help me.

1. As all the threads are calling the checkAccept(host, port) method. Is there a way to know which thread is currently accessing the security manager? For example if host A were to make 2 connections to the application, one to thread 1 (say listening to port 5001) and the other to to thread 2 (say listening to port 5002). I intend to refuse the connection to thread 2 but there is not way of differentiating the 2 connections since they are from the same host and there isnt any way of knowing their port number.

2. Are calls to the Security Manager thread safe?

I have been having this problem for a long time, would appreciate if someone can help point me to the right direction. cheers.

[1040 byte] By [jinfunna] at [2007-10-3 4:38:41]
# 1

> 1. As all the threads are calling the

> checkAccept(host, port) method. Is there a way to

> know which thread is currently accessing the security

> manager?

Just use Thread.currentThread(). As specified in the Javadoc for e.g. SecurityManager.checkAccept().

> 2. Are calls to the Security Manager thread safe?

No.

ejpa at 2007-7-14 22:42:33 > top of Java-index,Core,Core APIs...
# 2
1. You can use Thread.currentThread() and compare the returning Thread instance with your thread instance.2. It depends on how you implement your secority checks in the security manager. So if what you write is thread safe it will be thread safe.
LRMKa at 2007-7-14 22:42:33 > top of Java-index,Core,Core APIs...
# 3
> 1. You can use Thread.currentThread() and compare> the returning Thread instance with your thread> instance.On a multi processor machine, how will the jvm decide which instance of thread to return?
jinfunna at 2007-7-14 22:42:33 > top of Java-index,Core,Core APIs...
# 4
The current thread. The one that is executing the code that is currently executing your SecurityManager.checkAccess() method. That one.
ejpa at 2007-7-14 22:42:33 > top of Java-index,Core,Core APIs...
# 5
Thank you for your help.
jinfunna at 2007-7-14 22:42:33 > top of Java-index,Core,Core APIs...
# 6
The checkConnect() method in my security manager is getting a connection with port = -1. Isnt that an invalid port number? When will such a connection be made?
jinfunna at 2007-7-14 22:42:33 > top of Java-index,Core,Core APIs...
# 7
See the Javadoc for SecurityManager.checkConnect().
ejpa at 2007-7-14 22:42:33 > top of Java-index,Core,Core APIs...
# 8
I get the feeling you should be rolling your own IP protection scheme instead of trying to extend the SecurityManager. You are asking for such simple functionality, it seems like the SecurityManager is way to low-level for this sort of thing.
Jim_Manicoa at 2007-7-14 22:42:33 > top of Java-index,Core,Core APIs...
# 9
Well Jim the post is six weeks old but yes I would have thought a security policy file would probably do the job.
ejpa at 2007-7-14 22:42:33 > top of Java-index,Core,Core APIs...
# 10

> but there is not way of

> differentiating the 2 connections since they are from

> the same host and there isnt any way of knowing their

> port number.

Wrong. Java provides many details on Socket connections, include the local and remote source port and destination port and host name and IP address.

watertownjordana at 2007-7-14 22:42:33 > top of Java-index,Core,Core APIs...