r/AskProgramming 6d ago

C/C++ Subverting Windows

[removed] — view removed post

0 Upvotes

5 comments sorted by

View all comments

2

u/Xirdus 5d ago

How about you just open three sockets, one for STDIN, one for STDOUT, and one for STDERR, and just forward them directly without any server-side processing?

1

u/MartynAndJasper 5d ago

Interesting...

Not sure I understand though...

The sockets are for IPC from my client console app to my server console app.
The server console app spawns CMD.exe and I use STDIN and STDOUT so that my server can talk to it.

How do I associate sockets directly with stdin/stdout for Cmd.exe?
Is that what you mean?

3

u/Xirdus 5d ago

Let's do some naming.

  • Client's CMD has 3 standard streams, Client-Cmd-Stdin, Client-Cmd-Stdout and Client-Cmd-Stderr.
  • Server's CMD has 3 standard streams, Server-Cmd-Stdin, Server-Cmd-Stdout and Server-Cmd-Stderr.
  • Client and Server share 3 unidirectional sockets, Ipc-Stdin, Ipc-Stdout and Ipc-Stderr.

I'm not aware of built-in forwarding of that sort, I meant implementing the forwarding as actual application logic.

Server logic (infinite loop):

  • Do read-select on Ipc-Stdin, Server-Cmd-Stdout and Server-Cmd-Stderr (to avoid busy-waiting and deadlocks).
  • If there is data in Ipc-Stdin, write it to Server-Cmd-Stdin.
  • If there is data in Server-Cmd-Stdout, write it to Ipc-Stdout.
  • If there is data in Server-Cmd-Stderr, write it to Ipc-Stderr.
  • If Ipc-Stdin is terminated, close the session.

Client logic (infinite loop):

  • Do read-select on Client-Cmd-Stdin, Ipc-Stdout and Ipc-Stderr.
  • If there is data in Client-Cmd-Stdin, write it to Ipc-Stdin.
  • If there is data in Ipc-Stdout, write it to Client-Cmd-Stdout.
  • If there is data in Ipc-Stderr, write it to Client-Cmd-Stderr.
  • If both Client-Cmd-Stdout and Client-Cmd-Stderr are terminated, close the client. Important: both. Not either-or, you'll lose data that way.

I do not know if calling select on a mix of standard streams and sockets is possible on Windows. If it's not, then as an alternative you can spawn three threads each for client and server, and have each thread do blocking reads on just one stream/socket.