Networking Forums

Networking Forums > Computer Networking > Windows Networking > Stdin/stdout tunneling through sockets to child process under Win9X

Reply
Thread Tools Display Modes

Stdin/stdout tunneling through sockets to child process under Win9X

 
 
Bedrich Svoboda
Guest
Posts: n/a

 
      12-04-2004, 04:20 PM
I need to use telnet to control console based applications (such as
cmd.exe/command.com) on different machines. Simplest way how to do it is to
listen on some port of the host. When accept() returns (telnet is
connecting), create the desired process with standard handles redirected to
the socket returned by the accept(). I have successfully tested the
following code under WinXP and Win2K.

Unfortunately, under Win9X, the child process is created with no errors, but
the redirection doesn't happen. Even the handle numbers are different (thus
properly passed to the child) than the standard values (tested by calling
GetStdHandle(STD_INPUT_HANDLE) and GetStdHandle(STD_OUTPUT_HANDLE)). When I
try to WriteFile() to GetStdHandle(STD_OUTPUT_HANDLE) within the child
application, I got GetLastError()=ERROR_CALL_NOT_IMPLEMENTED - "This
function is not supported on this system". Nor calling of WSAStartup() in
the child before any read/write helps.

Does anybody know how to make it working under Win9X? I really wouldn't like
to make special thread per each child application for forwarding data
from/to anonymous pipe connecting the thread and the child application
to/from the socket.

The code does not contain any return value tests nor the shutdown mechanism
to be as simple as possible.

void main(void)
{
WSADATA wd;
WSAStartup(MAKEWORD(2,0), &wd);
SOCKET listenSocket=WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
0); //NOT use WSA_FLAG_OVERLAPPED in order to open non-overlapped sockets
struct sockaddr_in service;
service.sin_family=AF_INET;
service.sin_addr.s_addr=htonl(INADDR_ANY);
service.sin_port=htons(12345);
bind(listenSocket, (const struct sockaddr *)&service, sizeof(service));
listen(listenSocket, SOMAXCONN);
for(;
{
SOCKET acceptSocket=accept(listenSocket, NULL, NULL);
printf("server: Client connected.\n");

//create handle for child's process stdout
HANDLE currentProcess;
currentProcess=GetCurrentProcess();
HANDLE output;
DuplicateHandle(currentProcess, (HANDLE)acceptSocket, currentProcess,
&output, 0, TRUE, DUPLICATE_SAME_ACCESS);

//create handle for child's process stdin
HANDLE input;
DuplicateHandle(currentProcess, (HANDLE)acceptSocket, currentProcess,
&input, 0, TRUE, DUPLICATE_SAME_ACCESS);

//execute the executable
PROCESS_INFORMATION pi;
{
STARTUPINFO s;
ZeroMemory(&s, sizeof(s));
s.cb=sizeof(s);
s.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLE S;
s.wShowWindow=SW_SHOWMINNOACTIVE;
s.hStdInput=input;
s.hStdOutput=output;
s.hStdError=GetStdHandle(STD_ERROR_HANDLE); //keep stderr in slave's
console (???)
CreateProcess(getenv("COMSPEC"), getenv("COMSPEC"), NULL, NULL, TRUE,
CREATE_NEW_CONSOLE|IDLE_PRIORITY_CLASS, NULL, NULL, &s, &pi);
}
CloseHandle(pi.hThread);
closesocket(acceptSocket);
WaitForInputIdle(pi.hProcess, INFINITE);
CloseHandle(input);
CloseHandle(output);
CloseHandle(pi.hProcess);
}
closesocket(listenSocket);
}


 
Reply With Quote
 
 
 
 
Eugene Gershnik
Guest
Posts: n/a

 
      12-05-2004, 12:43 AM
http://msdn.microsoft.com/library/de...and_output.asp

--
Eugene


 
Reply With Quote
 
Bedrich Svoboda
Guest
Posts: n/a

 
      12-05-2004, 08:44 AM
I have examined that example before. As you can see, my code is quite
similar, but it uses sockets instead of anonymous pipes (I need to use
telnet from different machines). That Microsoft code works well under both
Win9X and Win2K+; my code with sockets works under Win2K+ only.

The problem is that I haven't found any sample or description, on how
sockets should behave when passed to child processes. Note that there is
many sources describing handle passing to child processes OR using sockets
as Windows handles, but none describing it together with information about
Winsock/Windows kernel versions.

"Eugene Gershnik" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> http://msdn.microsoft.com/library/de...and_output.asp



 
Reply With Quote
 
JJ
Guest
Posts: n/a

 
      12-06-2004, 12:17 AM
I don't think this can be done with Windows 9x. You need a helper process
that sits between your process and the other process, passing the data back
and forth to the child via pipes.

See this article (note the last bullet):

http://support.microsoft.com/kb/q150523/

The article says this:

It is also common practice on Windows NT to set the standard handles
(standard input, output, or error) of the child process to the socket
handle. In such cases, the child process usually does not know that its
standard handles are actually sockets.

Windows 9x differs from Windows NT/Windows 2000 in the following manner:

- Socket handles are not inheritable when created. To ensure that a child
process can obtain and use a socket handle created in the parent, the handle
must be explicitly duplicated using the Win32 API DuplicateHandle. Set the
bInheritHandle parameter of the API to TRUE.

- Socket handles cannot be set to the standard handles of the child process.
A programmer may use other mechanisms to pass the socket handle to the
client, such as passing the handle values as command line arguments so that
the child process can simply look at its argument vector.




"Bedrich Svoboda" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I have examined that example before. As you can see, my code is quite
>similar, but it uses sockets instead of anonymous pipes (I need to use
>telnet from different machines). That Microsoft code works well under both
>Win9X and Win2K+; my code with sockets works under Win2K+ only.
>
> The problem is that I haven't found any sample or description, on how
> sockets should behave when passed to child processes. Note that there is
> many sources describing handle passing to child processes OR using sockets
> as Windows handles, but none describing it together with information about
> Winsock/Windows kernel versions.
>
> "Eugene Gershnik" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> http://msdn.microsoft.com/library/de...and_output.asp

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stdin/stdout tunneling through sockets to child process under Win9X Bedrich Svoboda Windows Networking 3 12-06-2004 12:17 AM
epoll, and exceeding maximum sockets hangs process? Billy Joe Linux Networking 0 09-15-2004 06:13 PM
Win9x Crobie Windows Networking 0 06-02-2004 08:23 PM
win9x and xp missing a few pc's Haggis Windows Networking 4 01-16-2004 09:05 PM
win9x and XP Haggis Windows Networking 0 01-16-2004 04:14 PM



1 2 3 4 5 6 7 8 9 10 11