Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "interconnect" two sockets in Linux?

There are two connected sockets. How can I interconnect them?

  1. Data appeared on the one socket should be written to the other.
  2. EOF/FIN should propogate well. If one is half-closed, the other should also be half-closed.
int client = get_connected_client_socket();
int proxy = get_connected_proxy_socket();
negotiate_with_proxy(proxy);
iterconnect(client, proxy); 
// Now forgot about both client and proxy. 
// System should handle IO/shutdown/close. 
// Ideally even without any support of the user-space process.

Can Linux do it? Can it be done by tricking connection tracking to change tracking status of existing connection?

@related Determine how much can I write into a filehandle; copying data from one FH to the other

like image 514
Vi. Avatar asked Apr 20 '10 17:04

Vi.


People also ask

Can two processes share a socket?

You can share a socket between two (or more) processes in Linux and even Windows. Under Linux (Or POSIX type OS), using fork() will cause the forked child to have copies of all the parent's file descriptors.

Can you open multiple sockets?

@premktiw: Yes, multiple client sockets can be bound to the same local IP/port pair at the same time, if they are connected to different server IP/Port pairs so the tuples of local+remote pairs are unique. And yes, it is possible for a client to have more than 64K concurrent connections total.

How sockets work in Linux?

Sockets are a way to enable inter-process communication between programs running on a server, or between programs running on separate servers. Communication between servers relies on network sockets, which use the Internet Protocol (IP) to encapsulate and handle sending and receiving data.

What are sockets in programming?

Sockets allow you to exchange information between processes on the same machine or across a network, distribute work to the most efficient machine, and they easily allow access to centralized data. Socket application program interfaces (APIs) are the network standard for TCP/IP.


1 Answers

Are you aware of splice(). Based on your two questions I think this is where you are headed. Last I checked you can't do this in one splice call because both of file descriptors can't be sockets. But you should be able to do it in 2 calls (sockin->pipe->sockout). Also take a look at tee(). These may not be exactly what you want but from what I can figure they are in the ballpark.

like image 85
Newton Falls Avatar answered Oct 13 '22 09:10

Newton Falls