简体   繁体   English

C ++中的欺骗文件描述符

[英]Spoofing file descriptor in C++

I'm writing an library which will transmit data via multiple routes; 我正在编写一个库,它将通过多条路由传输数据; TCP, UDP, RDMA (Remote Direct Memory Access) and occasionally via a straight function call where client/server are rolled into a single binary. TCP,UDP,RDMA(远程直接内存访问),有时还通过直接的函数调用将客户端/服务器转换为单个二进制文件。

I'll be handling TCP, UDP, RDMA with file descriptors and have been looking at how I could achieve somethign similar with a FunctionCallSocket class which would take the rough form: 我将使用文件描述符处理TCP,UDP,RDMA,并一直在研究如何使用粗略形式的FunctionCallSocket类实现某种类似的功能:

class FunctionCallSocket
{
    public:
        FunctionCallSocket();
        ~FunctionCallSocket();

        void send(char* buf, std::size_t len);
        void recv(char* dest, std::size_t len);

    private:
        char*  m_outboundBuffer;
        char*  m_inboundBuffer;
};

I'd like to do is be able to treat the class like a file descriptor whereby I could pass it along with TCP, UDP, etc. file handles to a select/epoll. 我想做的是能够像对待文件描述符一样对待类,从而可以将其与TCP,UDP等文件句柄一起传递给select / epoll。

From what I understand the file descriptor integer value is generated by the OS from a privately held table which maps files to id's so I'd somehow need to spoof this. 据我了解,文件描述符整数值是由操作系统从一个私有表生成的,该表将文件映射到id,因此我需要以某种方式进行欺骗。

Any thoughts on how I could achieve this? 关于如何实现此目标有任何想法吗?

If you're running on Linux, I suggest looking into eventfd - this does exactly what you're looking for. 如果您在Linux上运行,建议您使用eventfd-这正是您想要的。

http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html

Open the fd in EFD_SEMAPHORE mode and you can use it to keep track of how many queued events you have 在EFD_SEMAPHORE模式下打开fd,您可以使用它来跟踪您有多少个排队事件

(each write() call will increment the counter stored by the kernel with the eventfd, and each read() will decrement the counter) (每个write()调用都会使内核存储的带有事件fd的计数器递增,而每个read()都会使计数器递减)

You could use OS-specific calls to get file descriptors, if you want to. 如果需要,可以使用特定于操作系统的调用来获取文件描述符。 For example, the Unix open function would do just what you want. 例如,Unix open函数可以完成您想要的操作。

Alternatively, you could make your code to read and write data part of a polymorphic class hierarchy, which would let you use file descriptors where appropriate and the streams library (for example) for file I/O. 或者,可以使代码读取和写入多态类层次结构的数据,这将使您可以在适当的地方使用文件描述符,并使用流库(例如)进行文件I / O。 In fact, one idea might be to writw custom stream classes to wrap up the TCP connections, then use ostream and istream as wrappers around the specifics of the connection. 实际上,一种想法可能是编写自定义流类来包装TCP连接,然后使用ostream和istream作为围绕连接细节的包装。

If it isn't a file descriptor, then there is no point in passing it to select(). 如果它不是文件描述符,则没有必要将其传递给select()。 If there is a file descriptor somewhere underneath, then you either need an API to expose it, or you have a low-level API to add file descriptors to the select set and make the class responsible for registering any file descriptors it uses. 如果下面有文件描述符,那么您要么需要一个API来公开它,要么您需要一个低级API来将文件描述符添加到选择集中并使该类负责注册它使用的任何文件描述符。 Note that you are well into asynchronous event handling and callback territory then. 请注意,您现在已经非常了解异步事件处理和回调领域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM