如何获取C++节俭的客户端 IP 地址

How to get client IP Address with C++ in thrift

本文关键字:客户端 IP 地址 C++ 何获取 获取      更新时间:2023-10-16

我目前正在C++中实现基于节俭的(0.4.0)服务,并遇到了一个问题:

有没有办法从服务方法实现内部获取客户端的 IP 地址?我正在使用 TNonblocking服务器。

提前感谢!

在 TNonblockingServer 中,当 TProcessor::p rocess() 被调用时,TProtocol.transport 是一个 TMemoryBuffer,因此获取客户端 IP 地址是不可能的。

但是我们可以扩展类 TServerEventHandler,方法TServerEventHandler::p rocessContext() 在客户端即将调用处理器时调用。

static boost::thread_specific_ptr<std::string> thrift_client_ip; // thread specific
class MyServerEventHandler : public TServerEventHandler
{
    virtual void processContext(void* serverContext, boost::shared_ptr<TTransport> transport)
    {
        TSocket *sock = static_cast<TSocket *>(transport.get());
        if (sock)
        {
            //thrift_client_ip.reset(new string(sock->getPeerAddress())); // 0.9.2, reused TNonblockingServer::TConnection return dirty address, see https://issues.apache.org/jira/browse/THRIFT-3270
            sock->getCachedAddress(); // use this api instead
        }
    }
};
// create nonblocking server
TNonblockingServer server(processor, protocolFactory, port, threadManager);
boost::shared_ptr<MyServerEventHandler> eventHandler(new MyServerEventHandler());
server.setServerEventHandler(eventHandler);

Ticket THRIFT-1053描述了对Java的类似请求。解决方案基本上是允许访问内部(端点)传输并从中检索数据。如果没有真正测试,为C++构建类似的解决方案应该很容易。由于您使用的是 Thrift 0.4.0,因此我强烈建议您先查看当前的主干 (0.9.3)。TBufferedTransportTFramedTransportTShortReadTransport已经实施

boost::shared_ptr<TTransport> getUnderlyingTransport();

因此,上面提到的补丁可能根本不需要。

调用 TProcessor 派生类时process()获取这两个传输。如果覆盖该方法,您应该能够管理对感兴趣的数据的访问:

/**
 * A processor is a generic object that acts upon two streams of data, one
 * an input and the other an output. The definition of this object is loose,
 * though the typical case is for some sort of server that either generates
 * responses to an input stream or forwards data from one pipe onto another.
 *
 */
class TProcessor {
public:
  // more code
  virtual bool process(boost::shared_ptr<protocol::TProtocol> in,
                       boost::shared_ptr<protocol::TProtocol> out,
                       void* connectionContext) = 0;
  // more code
#ifndef NONBLOCK_SERVER_EVENT_HANDLER_H
#define NONBLOCK_SERVER_EVENT_HANDLER_H
#include <thrift/transport/TSocket.h>
#include <thrift/server/TServer.h>
namespace apache{
namespace thrift{
namespace server{
class ServerEventHandler:public TServerEventHandler{
        void* createContext(boost::shared_ptr<TProtocol> input, boost::shared_ptr<TProtocol> output){
    (void)input;
    (void)output;
    return (void*)(new char[32]);//TODO
  }
  virtual void deleteContext(void* serverContext, 
                                boost::shared_ptr<TProtocol>input,
                                boost::shared_ptr<TProtocol>output) {
                delete [](char*)serverContext;
  }
  virtual void processContext(void *serverContext, boost::shared_ptr<TTransport> transport){
    TSocket *tsocket = static_cast<TSocket*>(transport.get());
    if(socket){
                        struct sockaddr* addrPtr;
                        socklen_t addrLen;
      addrPtr = tsocket->getCachedAddress(&addrLen);
                  if (addrPtr){
                                        getnameinfo((sockaddr*)addrPtr,addrLen,(char*)serverContext,32,NULL,0,0) ;
                        }
    }
  }
};
}
}
}
#endif
boost::shared_ptr<ServerEventHandler> serverEventHandler(new ServerEventHandler()
server.setServerEventHandler(serverEventHandler);