You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Patrick Schlangen <p....@mac.com> on 2009/05/06 18:37:21 UTC

Client IP address

Hi,

I am implementing a thrift-based service in C++ at the moment and encountered a question:

Is there a way to get the client's IP address from inside a service method implementation? I am using a TThreadedServer.

Thanks in advance!

Patrick Schlangen



Re: Client IP address

Posted by Carlos Valiente <su...@gmail.com>.
> I am implementing a thrift-based service in C++ at the moment and encountered a question:
>
> Is there a way to get the client's IP address from inside a service method implementation? I am using a TThreadedServer.

On Java, the trick is to keep a reference to the Thrift transport
object in your service implementation. See http://tinyurl.com/djv4m6,
http://tinyurl.com/c7oj2v and http://tinyurl.com/ddjjkb --- I managed
to achieve that by using a custom protocl factory.

C

AW: Re: AW: Re: Client IP address

Posted by Patrick Schlangen <p....@mac.com>.
 
Am Mittwoch 06 Mai 2009 um 09:35PM schrieb "Krzysztof Godlewski" <kr...@dajerade.pl>:

>This should work, more or less. I omited checks for nulls, locking, etc.
> (...)

Thank you so much, it works!

Patrick

Re: AW: Re: Client IP address

Posted by Krzysztof Godlewski <kr...@dajerade.pl>.
On 2009-05-06, at 21:01, Patrick Schlangen wrote:

>> I implemented this using TServerEventHandler:
>> * there's a static map that holds client addrs: map<unsigned long,
>> string>
>> * client addresses are added to a map in MyHandler::clientBegin(),
>> pthread_self() is the key in address map
>
> That sounds like a great idea. But how do you get the client's IP in  
> clientBegin()?
> I tried to cast the input or output parameter to a TSocket and call  
> getPeerAddress() but it returns an empty string only.

This should work, more or less. I omited checks for nulls, locking, etc.

TBufferedTransport *tbuf = dynamic_cast<TBufferedTransport *>(input- 
 >getTransport().get());
TSocket *sock = dynamic_cast<TSocket *>(tbuf- 
 >getUnderlyingTransport().get());
addrs[pthread_self()] = sock->getPeerAddress();


AW: Re: Client IP address

Posted by Patrick Schlangen <p....@mac.com>.
 
Am Mittwoch 06 Mai 2009 um 08:14PM schrieb "Krzysztof Godlewski" <kr...@dajerade.pl>:
>I implemented this using TServerEventHandler:
>  * there's a static map that holds client addrs: map<unsigned long,  
>string>
>  * client addresses are added to a map in MyHandler::clientBegin(),  
>pthread_self() is the key in address map

That sounds like a great idea. But how do you get the client's IP in clientBegin()?
I tried to cast the input or output parameter to a TSocket and call getPeerAddress() but it returns an empty string only.

Patrick

Re: Client IP address

Posted by Krzysztof Godlewski <kr...@dajerade.pl>.
On 2009-05-06, at 18:37, Patrick Schlangen wrote:

> Hi,
>
> I am implementing a thrift-based service in C++ at the moment and  
> encountered a question:
>
> Is there a way to get the client's IP address from inside a service  
> method implementation? I am using a TThreadedServer.

I implemented this using TServerEventHandler:
  * there's a static map that holds client addrs: map<unsigned long,  
string>
  * client addresses are added to a map in MyHandler::clientBegin(),  
pthread_self() is the key in address map
  * addresses are removed in clientEnd()
  * a static method MyHandler::get() returns addresses[pthread_self()]

I know, it's a very primitive solution, but it works :)


AW: Re: Client IP address

Posted by Patrick Schlangen <p....@mac.com>.
Thank you for your response.
That leads me to new problems as the client is not aware of it's own (public) IP address as it is behind a NAT router. I will think about a way to solve this.

Patrick

Am Mittwoch 06 Mai 2009 um 07:17PM schrieb "Bryan Duxbury" <br...@rapleaf.com>:
>We don't currently have a way to do this, though it has been  
>discussed. The best bet as of right now is to add a parameter to the  
>service method that takes the client address and have the client  
>report it itself.
>
>-Bryan
>

Re: Client IP address

Posted by Bryan Duxbury <br...@rapleaf.com>.
We don't currently have a way to do this, though it has been  
discussed. The best bet as of right now is to add a parameter to the  
service method that takes the client address and have the client  
report it itself.

-Bryan

On May 6, 2009, at 9:37 AM, Patrick Schlangen wrote:

> Hi,
>
> I am implementing a thrift-based service in C++ at the moment and  
> encountered a question:
>
> Is there a way to get the client's IP address from inside a service  
> method implementation? I am using a TThreadedServer.
>
> Thanks in advance!
>
> Patrick Schlangen
>
>