[omniORB] Re: Getting client IP address in server + example interceptors

Brecht Vermeulen brecht.vermeulen@rug.ac.be
Thu, 14 Feb 2002 13:07:49 +0100


Hi all,

sorry for replying to my own mail, but the problem is solved and I
thought other persons would be interested in the solution (and maybe
point out some odd things in my code :-) ). This is also an example of
the omni Interceptors.

I used basically the eg2 (although an adapted version) example, and I
will provide here the clues to the changes, if you would like full
source code of my example, just contact me.

add:

// For interceptors
#include <omniORB4/omniInterceptors.h>

// Thread specific data
pthread_key_t key;

// Specifically for use of info in giop_s in
serverReceiveRequest_T::info_T
// to compile, add -I...omni/src/lib/omniORB/include/
// This poses the question: wouldn't it be interesting to add these to
omni/include/omniORB4, just as
// omniInterceptors.h ??, now you need the source code to compile this
(or you have to copy these header files)
#include <giopStrand.h>
#include <giopStream.h>
#include <GIOP_S.h>



CORBA::Boolean
testfunc(omni::omniInterceptors::serverReceiveRequest_T::info_T& info) {
	char *clientaddress;
	cout << "Thread ID : " << pthread_self() << endl;
	cout << "Operation name : " << info.giop_s.operation_name() << endl;
	cout << "Peer (client) address : " << ( (omni::giopStrand&)info.giop_s
).connection->peeraddress() << endl;
	cout << "My (server) address : " << ( (omni::giopStrand&)info.giop_s
).connection->myaddress() << endl;
	clientaddress = (char *)pthread_getspecific(key);
	delete[] clientaddress;
	clientaddress = new char [strlen( ( (omni::giopStrand&)info.giop_s
).connection->peeraddress() ) +1];
	strcpy(clientaddress, ( (omni::giopStrand&)info.giop_s
).connection->peeraddress() );
	pthread_setspecific(key, (void *)clientaddress);
	return true;
}

void delete_tsd(void *data) {
	cout << "Deleting thread specific data: " << (char *)data << endl;
	delete[] (char *)data;
}

and in the main function, I added it before the poamanageer->activate
and orb->run

if (pthread_key_create(&key, &delete_tsd) ) {
	cerr << "cannot create key for thread specific data" << endl;
	exit(1);
}
omniORB::getInterceptors()->serverReceiveRequest.add(&testfunc);



then, in the implementation of your IDL operations:

  cout << "echoString: client address : " << (char
*)pthread_getspecific(key) << endl;
 
and that's it.

The output is:
Thread ID : 5124
Operation name : echoString
Peer (client) address : giop:tcp:192.168.2.1:34125
My (server) address : giop:tcp:192.168.2.1:34122
echoString: client address : giop:tcp:192.168.2.1:34125

best regards,
Brecht

Brecht Vermeulen wrote:
> 
> Hi,
> 
> we're in the process of creating a CORBA Naming Server which supports
> load balancing and is based on the omniNames Naming Server.
> 
> For one of the possible algorithms for load balancing we would like to
> return an object reference based on the client IP address.
> 
> Now is of course my question if it's possible to get the IP address of
> the client in the server and if so, what is the best way ?
> 
> There was a post of Sai-Lai in March 2001, and I would like to know if
> this is still the way to go with omniORB4, or if interceptors or
> something else could be more easy/interesting for this ?
> 
> http://www.uk.research.att.com/omniORB/archives/2001-03/0197.html
> 
> >>>>> Duncan Grisby writes:
> 
> > On Friday 23 March, SaE wrote:
> >> how to find out client IP on server side ?
> 
> > You can't. The CORBA standard doesn't specify a way to do it, and
> > omniORB doesn't have a proprietary extension for it.
> 
> If you just want to decide, based on the client IP address, whether to
> accept or decline a connection request from the client, you can use the
> gatekeeper interface inside omniORB. See the user guide for details.
> 
> On the otherhand, if your design requires you to determine the client IP
> address inside the implementation of an operation, you can use the
> gatekeeper interface to extract the client IP address from the socket
> (using getpeername()), put the info into a per-thread storage and
> extract
> the value in your implementation. You either use the pthread "key" or
> the
> equivalent in Win32 thread API to access the per-thread storage. With
> Win32, you must clean up the storage after use or there will be a memory
> leak.
> 
> Regards,
> 
> Sai-Lai
> 
> thanks for any hint,
> best regards,
> Brecht Vermeulen
> 
> Department of Information Technology
> Ghent University