views:

268

answers:

2

Hi,

I am running a small java server through xinetd superserver. I'd like to get the originating client IP but I can't because streams are between xinetd and java (stin/stdout).

Does somebody know how to get the client IP, without looking to xinetd logfile ? (seems a bad solution to me)

Thanks !

+1  A: 

Given the situation you have described, trawling the xinetd logfile is your only option.

If your Java application is talking to the client via its standard input and standard output, there is no way for the application to access the underlying socket. Indeed, I don't think you could do this in any language.

EDIT : actually, you probably could do this in C and C++ because they expose the file descriptors (fds) and have library APIs for doing socket operations using fds.

But it won't work in pure Java. Even if you could drill down to the fd inside the Stream objects associated with System.in or System.out, I don't think that the Java class libraries provide an API for turning the fd into a Socket object. To do socket operations on the fd's 0 and 1 you would need to resort to JNI and native code.

And as the commenter points out, if the real client is behind a proxy, the client IP that you get from the socket will be the IP of the proxy.

Stephen C
... and the underlying socket may only have the source ip:port information of the nearest proxy, not the actual client.
EJP
There's still the way through getpeername (which may or may not require JNI).
ShiDoiSi
It requires JNI because getpeername is not present in the Java socket APIs.
Stephen C
A: 

I think you can call getpeername on TCP sockets (but not UDP), see Stevens chapter 4.10.

ShiDoiSi
That's a C / C++ solution, not a Java solution.
Stephen C