1

I'm using C and Winsock2 for my learning project.

I have some questions that I hope some one can confirm.

Let say I have 2 unrelated processes, process A and process B ( without using CreateProcess ). By unrelated I mean it's not parent and child.

1)

Is it possible in Windows to Accept a socket in process A and pass it to process B if they are unrelated?

2)

I guess i have to use WSADuplicateSocket? but that only works for related processes?

I hope someone can explain and confirm the above..

Steve Friedl
  • 3,929
  • 1
  • 23
  • 30
  • When you say "pass that", what is "that"? – stark Dec 15 '21 at 18:31
  • to pass the socket handle :) – sakdkjkj jjsdjds Dec 15 '21 at 18:32
  • Unix systems support [sending sockets and other file descriptors via `AF_UNIX` sockets](https://stackoverflow.com/questions/28003921/sending-file-descriptor-by-linux-socket). Windows [now supports `AF_UNIX` sockets](https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/), but I don't know if that includes sending sockets between processes. – Andrew Henle Dec 15 '21 at 18:33
  • I know about UNIX, sadly you can't pass sockets with AF_UNIX in Windows – sakdkjkj jjsdjds Dec 15 '21 at 18:34
  • Why would would `WSADuplicateSocket` only work for *"related"* processes, and what sort of relationship do you have in mind? – IInspectable Dec 15 '21 at 18:36
  • @IInspectable `WSADuplicateSocket` requires the original socket in order to duplicate it. That socket only exists in that one process (and maybe child processes). The fundamental problem is how to send that socket to another otherwise-unrelated process. – Andrew Henle Dec 15 '21 at 18:38
  • 1
    I'm revisiting a old issue of mine. I remember that WSADuplicateSocket only worked if process A created process B ( like forking).. – sakdkjkj jjsdjds Dec 15 '21 at 18:38
  • @AndrewHenle Yes you get my issue.. – sakdkjkj jjsdjds Dec 15 '21 at 18:39
  • @sakdkjkjjjsdjds [DuplicateHandle()](https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle?redirectedfrom=MSDN) probably goes a long way to solving your problem. From looking at that, if Process A has a handle to Process B, Process A then uses `DuplciateHandle()` to create a new socket handle in Process B. Then Process A has to get the new handle *value* to Process B somehow. – Andrew Henle Dec 15 '21 at 18:46
  • Big complain to Microsoft for not supporting ancillary data in the Windows unix socket implementation... - and a big thanks to you @AndrewHenle for giving me hope. I will try that.. and find way with it – sakdkjkj jjsdjds Dec 15 '21 at 18:51
  • @AndrewHenle it says " [in] bInheritHandle A variable that indicates whether the handle is inheritable. If TRUE, the duplicate handle can be inherited by new processes created by the target process. If FALSE, the new handle cannot be inherited. " look like only will work if process B is created by process A or i'm misunderstanding something - or they mean that option is for process B's childs..? – sakdkjkj jjsdjds Dec 15 '21 at 19:01
  • You are misunderstanding. Parent-child relationships between processes are recorded, but not used for anything interesting. If you `DuplicateHandle` a handle into another process, then the target process can use that handle. `bInheritHandle` limits whether the target process can inherit the handle to another process it creates or not. – IInspectable Dec 15 '21 at 19:57
  • @and Processes need not be related in any way to participate in IPC. You have several options of implementing [IPC](https://learn.microsoft.com/en-us/windows/win32/ipc/interprocess-communications). – IInspectable Dec 15 '21 at 19:58
  • 1
    So you are saying if i run Process A and later Process B - i should be able to use in Process A WSADuplicateSocket and send the socket to Process B with named pipe even if they are not related ? i'm just trying to confirm if DuplicateHandle is needed at all.. – sakdkjkj jjsdjds Dec 15 '21 at 20:25
  • not exist "related" processes at all – RbMm Dec 15 '21 at 21:20
  • @sakdkjkjjjsdjds: There are two ways to share handles and sockets between processes. Inheritance is one (and only works from parent to child). DuplicateHandle/WSADuplicateSocket is a separate way, independent of inheritance and independent of the parent/child relationship that inheritance requires. – Ben Voigt Dec 15 '21 at 21:30
  • @BenVoigt thank you for explaining that step by step! – sakdkjkj jjsdjds Dec 15 '21 at 22:41
  • @sakdkjkjjjsdjds "*So you are saying if i run Process A and later Process B - i should be able to use in Process A WSADuplicateSocket and send the socket to Process B with named pipe even if they are not related ?*" - yes. The `WSADuplicateSocket()` documentation *literally* says as much. – Remy Lebeau Dec 16 '21 at 01:23

1 Answers1

0

Is it possible in Windows to Accept a socket in process A and pass it to process B if they are unrelated?

Yes, via WSADuplicateSocket():

The WSADuplicateSocket function is used to enable socket sharing between processes. A source process calls WSADuplicateSocket to obtain a special WSAPROTOCOL_INFO structure. It uses some interprocess communications (IPC) mechanism to pass the contents of this structure to a target process, which in turn uses it in a call to WSASocket to obtain a descriptor for the duplicated socket. The special WSAPROTOCOL_INFO structure can only be used once by the target process.

I guess i have to use WSADuplicateSocket?

Yes.

but that only works for related processes?

No. It will work fine between any 2 processes, as long as process A knows process B's Process ID, as that is a required parameter of WSADuplicateSocket().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • A quick search for *WSADuplicateSocket* sample([sockdup](https://github.com/microsoft/Windows-classic-samples/tree/27ffb0811ca761741502feaefdb591aebf592193/Samples/Win7Samples/netds/winsock/sockdup)). – YangXiaoPo-MSFT Dec 16 '21 at 03:07