开发者

recv() from an application buffer

I have a 3rd party library that reads RTMP packets from a network socket, using recv() and taking the socket descriptor as an argument. I want it to read these packets from the application buff开发者_如何学Goer instead (allocated in heap).

Is it possible?


You may be able to use socketpair, which creates a pair of connected (unix domain) sockets. If you are on Linux, You can stuff the socket using vmsplice, otherwise send() as usual.


I don't know a direct way to do that, but you can use pipe in order to send yourself the data to the 3rd party stack. It will provide you two file descriptors, one on which you will write your data and the other one has to be provided to the stack that will be able to read your data.


Use a pipe?

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>

    main()
    {
            int     fd[2];

            pipe(fd);

    }

fd[0] is the one you pass to 3rd party, fd[1] you write the packets into.


Unless your 3rd party library already supports reading from buffers instead of the network it's not going to be easy:

  • If you have access to the source-code 3rd-party library and are allowed to modify it, the proper solution would probably be to modify its source code to allow reading from buffers.

  • You may be able to replay the network packets through a local proxy server and have the 3rd-party library use that. However this is not exactly a solution for production applications - only something to do for testing or under duress.

  • Depending on your use of the network, you may be able to override the recv() library function with a preloaded library. Of course, this depends on the 3rd-party library using the C library stub for recv() and not the system call directly.

  • If you are in the mood for more questionable techniques, and you are allowed by the license of te 3rd-party library (unlikely for commercial products), you could modify the shared object of the library to replace recv() with another function of your own that has the same interface but receives data from a heap buffer instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜