Biswa96/wslbridge2

Discussion: Merge the networking logic of WSL1 and WSL2 in wslbridge2

Closed this issue · 2 comments

tl;dr: What are the odds of rand() (in Cygwin) returning same value twice in one session?

  • How the network channels are initiated now in wslbridge2?

    • In case of WSL1 (AF_INET), 1. frontend listens with three sockets 2. frontend sends three unassigned port number with command line to backend 3. backend connects 4. frontend accepts. Done.

    • In case of WSL2 (AF_HYPERV), 1. frontend listens with one socket 2. frontend sends one port number with command line to backend 3. backend connects 4. backend creates three sockets with a random unassigned port number and listens 5. frontend receives that random port and creates three sockets with that random port number and connects 6. backend accepts. Done.

  • What will it be after merging?

    • The networking will be same in wslbridge2 frontend and backend as in WSL1 now.
  • Why has not it done in first place?

    • Due to difference in Hyper-V and TCP/IP network sockets. In AF_INET, if bind() is used with port zero the socket is automatically bound with random free port. But it is possible in Linux's AF_VSOCK, not in AF_HYPERV in MS Windows. The win_vsock_listen() in frontend mimics that behavior using rand(). The one and only failure point is that rand() returning same integer twice resulting same port for in and out.

So the main question, is it possible for rand() (in Cygwin) returning same integer twice in one session of wslbridge2?

  • i dont know the depth and breadth of the issue,
  • but how about you couple the random generating function with time - the precision available is pretty high i think 1 μs for the unix time clock
  • ... like as some seed input, or maybe with some arithmetic operation or direct concatenating somehow??
  • that will be pretty effective i guess since the context here seems to be only limited to getting unique number on same device with rand() called multiple times (at hopefully some Δt ≠ 0)

This had been implemented long ago...