:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / apps / utils / net / telnet / telnet.cpp
1 /* $Id$
2  *
3  * FILE       : telnet.cpp
4  * AUTHOR     : unknown (sources found on www.telnet.org)
5  * PROJECT    : ReactOS Operating System
6  * DESCRIPTION: telnet client for the W32 subsystem
7  * DATE       : 2001-01-21
8  * REVISIONS
9  *      2001-02-21 ea   Modified to compile under 0.0.16 src tree
10  */
11 #include <winsock.h>
12 #include <windows.h>
13
14 #include "telnet.h"
15 #include "console.h"
16
17
18 //
19 // sock_loop is the thread dedicatd to reading socket input.
20 // It waits for data from the socket, and then gives it one byte at a time
21 // to the telnet vm to process.
22 //
23
24 DWORD sock_loop(SOCKET server)
25 {
26   char buf[256];
27   unsigned long read;
28   char* scan;
29
30   while( (read = recv(server,buf,sizeof(buf),0)) && read != SOCKET_ERROR )
31   {
32     scan = buf;
33     while(read--)
34       vm(server,*scan++);
35   }
36   int x = WSAGetLastError();
37   return 0;
38 }
39
40 DWORD input_loop(SOCKET server)
41 {
42   char buf[256];
43   DWORD read;
44
45   do
46   {
47     WaitForSingleObject(StandardInput, INFINITE);
48     ReadFile(StandardInput, buf, sizeof buf, & read, NULL);
49   }
50   while(SOCKET_ERROR != send(server, buf, read, 0));
51
52   return 0;
53
54 }
55
56 void telnet(SOCKET server)
57 {
58   DWORD dwThreadIdsock;
59   DWORD dwThreadIdinput;
60   HANDLE threads[2];
61
62
63   threads[0] = CreateThread( 
64     NULL,                        /* no security attributes        */ 
65     0,                           /* use default stack size        */ 
66     (LPTHREAD_START_ROUTINE) sock_loop,  /* thread function       */ 
67     (LPVOID)server,              /* argument to thread function   */ 
68     0,                           /* use default creation flags    */ 
69     &dwThreadIdsock);            /* returns the thread identifier */ 
70
71   //wait for the other thread to complete any setup negotiation...
72   //Sleep(500); //- this is not the problem - its just bloody stuffing up!
73
74   threads[1] = CreateThread( 
75     NULL,                        /* no security attributes        */ 
76     0,                           /* use default stack size        */ 
77     (LPTHREAD_START_ROUTINE) input_loop, /* thread function       */ 
78     (LPVOID)server,              /* argument to thread function   */ 
79     0,                           /* use default creation flags    */ 
80     &dwThreadIdinput);           /* returns the thread identifier */ 
81
82
83   WaitForMultipleObjects(2,threads,FALSE,INFINITE);
84 }
85
86
87 //
88 // connect to the hostname,port
89 // 
90 void telnet(
91   char const* pszHostName,
92   const short nPort)
93 {
94   unsigned long ip;
95   if((*pszHostName <= '9') && (*pszHostName >= '0'))
96   {
97      if((ip = inet_addr(pszHostName)) == INADDR_NONE)
98        err("invalid host IP address given");
99   }
100   else
101   {
102     hostent* ent = gethostbyname(pszHostName);
103     if(!ent)
104       err(sockmsg(WSAGetLastError()));
105     ip = *(unsigned long*)(ent->h_addr);
106   }
107
108   sockaddr_in name;
109   name.sin_family = AF_INET;
110   name.sin_port = htons(nPort);
111   name.sin_addr = *(in_addr*)&ip;
112
113   console_title_connecting (pszHostName, nPort);
114   
115   SOCKET server;
116
117   if((server = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
118     err(sockmsg(WSAGetLastError()));
119
120   if(SOCKET_ERROR == connect(server,(sockaddr*)&name,sizeof(sockaddr)))
121     err(sockmsg(WSAGetLastError()));
122
123   console_title_connected (pszHostName, nPort);
124   
125   telnet(server);
126
127   closesocket(server);
128 }
129
130 /* EOF */