Are there any good tips for implementing the socket technique in a
secure way? I think the naive way would be to just listen for
connections on either the C or Java end, but how do you securely
allow only a specified local instance to connect?
If the server binds the listen socket to the localhost address
127.0.0.1 in addition to the port number, remote processes are
prevented from connecting. Local processes can still connect to the
localhost address.
If you only want a *specific* local process to connect, you need to be
more clever. For example, the server can listen on a "random" port
(specify 0, the system will choose a free one) before starting the
client and passing it the port number on the command line. The client
then connects back to the server which only accepts a single
connection.
This leaves a short (subsecond) window of opportunity for another
process to connect before the client, but this should be sufficient
for most applications. If your processes don't have a parent-child
relationship or you need more security, use an authentication
mechanism when the client connects instead.
There are also some unix-specific mechanisms that might be simpler if
that's your platform. For example, you can use a named pipe instead of
a socket (Java can treat it like any file) and rely on the uid-based
security provided by the filesystem. On the other hand, once you start
using platform specific mechanisms you might as well use JNI.
/gordon