0

Is there a way to map a descriptor created by socket() to a memory buffer?

The reason why I am looking for this is because I want to make an existing application to read from the memory buffer I created instead of its associated TCP buffer. I shouldn't modify the application, so I want to map a fd returned by the application to a buffer I created.

I found a similar question: Can descriptors for sockets be converted to File Pointers?

But I don't know if fdopen() can be used for my purpose because fdopen() takes only two arguments (fd and mode) and I don't know how to re-associate the fd to a memory I create with malloc().

Community
  • 1
  • 1
  • How does your application know when there's new data in that `malloc(3)`-ed buffer? There might be a solution, but much depends on your existing interface. – Nikolai Fetissov Jun 17 '13 at 16:03
  • That is actually a good question. I just assumed that select() of the application (in which I can't make a modification) monitors whether my malloc()ed buffer is ready to be read. But, my assumption may be a big mistake. – Cristóbal D. Jun 17 '13 at 16:18
  • Yeah, figure out how what notifications/callbacks are there. Having that will give you a better idea how to hook into existing app. – Nikolai Fetissov Jun 17 '13 at 16:29

1 Answers1

0

Is there a way to map a descriptor created by socket() to a memory buffer?

No. It doesn't make sense. A mapped file makes sense because of the virtual memory system. A mapped socket doesn't.

I want to map a fd returned by the application to a buffer I created.

You will have to write code to read from the socket into your buffer.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I think your answer may not be entirely correct. If Linux, as I should assume, allocates memory to assemble an incoming/outgoing stream of data on a TCP connection, there is in principle nothing wrong with some imaginary system call that maps said memory region as a page in a process page table, thus allowing the process a "zero-copy" socket data read/write operation. This in effect would be akin to a memory-mapped socket receive/transmit buffer. – Armen Michaeli Jan 22 '23 at 15:21