ReactOS  0.4.12-dev-14-gd0c8636
tnclass.cpp
Go to the documentation of this file.
1 //Telnet Win32 : an ANSI telnet client.
3 //Copyright (C) 1998 Paul Brannan
4 //Copyright (C) 1998 I.Ioannou
5 //Copyright (C) 1997 Brad Johnson
6 //
7 //This program is free software; you can redistribute it and/or
8 //modify it under the terms of the GNU General Public License
9 //as published by the Free Software Foundation; either version 2
10 //of the License, or (at your option) any later version.
11 //
12 //This program is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with this program; if not, write to the Free Software
19 //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 //I.Ioannou
22 //roryt@hol.gr
23 //
25 
27 //
28 // Module: tnclass.cpp
29 //
30 // Contents: telnet object definition
31 //
32 // Product: telnet
33 //
34 // Revisions: August 30, 1998 Paul Brannan <pbranna@clemson.edu>
35 // July 12, 1998 Paul Brannan
36 // June 15, 1998 Paul Brannan
37 // May 14, 1998 Paul Brannan
38 // 5.April.1997 jbj@nounname.com
39 // 14.Sept.1996 jbj@nounname.com
40 // Version 2.0
41 //
43 
44 #include "precomp.h"
45 
46 // Mingw32 needs these (Paul Brannan 9/4/98)
47 #ifndef ICON_SMALL
48 #define ICON_SMALL 0
49 #endif
50 #ifndef ICON_BIG
51 #define ICON_BIG 1
52 #endif
53 
54 // Ioannou Dec. 8, 1998
55 #ifdef __BORLANDC__
56 #ifndef WM_SETICON
57 #define WM_SETICON STM_SETICON
58 #endif
59 #endif
60 
61 // DoInit() - performs initialization that is common to both the
62 // constructors (Paul Brannan 6/15/98)
65  bConnected = 0;
66  bNetPaused = 1;
67  bNetFinished = 1;
68  bNetFinish = 0;
69  hThread = 0; // Sam Robertson 12/7/98
70  hProcess = 0;
71 
72  WSADATA WsaData;
73 
74  // Set the title
75  telSetConsoleTitle("No Connection");
76 
77  // Change the icon
80 
81  if (WSAStartup(MAKEWORD(1, 1), &WsaData)) {
82  DWORD dwLastError = GetLastError();
83  printm(0, FALSE, MSG_ERROR, "WSAStartup()");
84  printm(0, TRUE, dwLastError);
85  bWinsockUp = 0;
86  return;
87  }
88  bWinsockUp = 1;
89 
90  // Get keyfile (Paul Brannan 5/12/98)
91  const char *keyfile = ini.get_keyfile();
92 
93  // This should be changed later to use the Tnerror routines
94  // This has been done (Paul Brannan 6/5/98)
95  if(LoadKeyMap( keyfile, ini.get_default_config()) != 1)
96  // printf("Error loading keymap.\n");
98 }
99 
107 Scroller(Mouse, ini.get_scroll_size()),
109  DoInit();
110 }
111 
112 Telnet::Telnet(const char * szHost1, const char *strPort1):
119 Scroller(Mouse, ini.get_scroll_size()),
121  DoInit();
122  Open( szHost1, strPort1);
123 }
124 
126  if (bWinsockUp){
127  if(bConnected) Close();
128  WSACleanup();
129  }
130 
131  // Paul Brannan 8/10/98
132  if(iconChange) {
134  }
135 
136 }
137 
138 // changed from char * to const char * (Paul Brannan 5/12/98)
139 int Telnet::LoadKeyMap(const char * file, const char * name){
140  // printf("Loading %s from %s.\n", name ,file);
141  printm(0, FALSE, MSG_KEYMAP, name, file);
142  return MapLoader.Load(file,name);
143 }
144 
145 void Telnet::DisplayKeyMap(){ // display available keymaps
146  MapLoader.Display();
147 };
148 
149 int Telnet::SwitchKeyMap(int to) { // switch to selected keymap
150  int ret = KeyTrans.SwitchTo(to);
151  switch(ret) {
152  case -1: printm(0, FALSE, MSG_KEYNOKEYMAPS); break;
153  case 0: printm(0, FALSE, MSG_KEYBADMAP); break;
154  case 1: printm(0, FALSE, MSG_KEYMAPSWITCHED); break;
155  }
156  return ret;
157 };
158 
159 
160 int Telnet::Open(const char *szHost1, const char *strPort1){
161  if (bWinsockUp && !bConnected){
162  telSetConsoleTitle(szHost1);
163 
164  strncpy (szHost,szHost1, 127);
165  strncpy(strPort, strPort1, sizeof(strPort));
166 
167  // Determine whether to pipe to an executable or use our own sockets
168  // (Paul Brannan March 18, 1999)
169  const char *netpipe;
170  if(*(netpipe=ini.get_netpipe())) {
172  HANDLE hInWrite, hOutRead, hErrRead;
173  if(!CreateHiddenConsoleProcess(netpipe, &pi, &hInWrite,
174  &hOutRead, &hErrRead)) {
175  printm(0, FALSE, MSG_ERRPIPE);
176  return TNNOCON;
177  }
178  Network.SetPipe(hOutRead, hInWrite);
179  hProcess = pi.hProcess;
180  } else {
181  Socket = Connect();
182  if (Socket == INVALID_SOCKET) {
183  printm(0, FALSE, GetLastError());
184  return TNNOCON;
185  }
188  }
189 
190  bNetFinish = 0;
191  bConnected = 1;
198  DWORD idThread;
199 
200  // Disable Ctrl-break (PB 5/14/98);
201  // Fixed (Thomas Briggs 8/17/98)
204 
205  hThread = CreateThread(0, 0,
207  (LPVOID)&ThreadParams, 0, &idThread);
208  // This helps the display thread a little (Paul Brannan 8/3/98)
210  return Resume();
211  } else if(bWinsockUp && bConnected) {
213  }
214 
215  return TNNOCON; // cannot do winsock stuff or already connected
216 }
217 
218 // There seems to be a bug with MSVC's optimization. This turns them off
219 // for these two functions.
220 // (Paul Brannan 5/14/98)
221 #ifdef _MSC_VER
222 #pragma optimize("", off)
223 #endif
224 
225 
227  Console.sync();
228  switch(Network.get_net_type()) {
229  case TN_NETSOCKET:
232  break;
233  case TN_NETPIPE:
234  if(hProcess != 0) {
237  hProcess = 0;
238  }
239  break;
240  }
241 
242  // Enable Ctrl-break (PB 5/14/98);
243  // Ioannou : this must be FALSE
245 
246  if (hThread) CloseHandle(hThread); // Paul Brannan 8/11/98
247  hThread = NULL; // Daniel Straub 11/12/98
248 
250  bNetFinish = 1;
251  while (!bNetFinished)
252  Sleep (0); // give up our time slice- this lets our connection thread
253  // finish itself, so we don't hang -crn@ozemail.com.au
254  telSetConsoleTitle("No Connection");
255  bConnected = 0;
256  return 1;
257 }
258 
260  int i;
261  if (bConnected) {
262  Console.sync();
263  for(;;){
267  if (i) bConnected = 1;
268  else bConnected = 0;
271  while (!bNetPaused)
272  Sleep (0); // give up our time slice- this lets our connection thread
273  // unpause itself, so we don't hang -crn@ozemail.com.au
274  switch (i){
275  case TNNOCON:
276  Close();
277  return TNDONE;
278  case TNPROMPT:
279  return TNPROMPT;
280  case TNSCROLLBACK:
282  break;
283  case TNSPAWN:
284  NewProcess();
285  }
286  }
287  }
288  return TNNOCON;
289 }
290 
291 // Turn optimization back on (Paul Brannan 5/12/98)
292 #ifdef _MSC_VER
293 #pragma optimize("", on)
294 #endif
295 
296 // The scrollback functions have been moved to TScroll.cpp
297 // (Paul Brannan 6/15/98)
299 {
300  SOCKET Socket1 = socket(AF_INET, SOCK_STREAM, 0);
301  SOCKADDR_IN SockAddr;
302  SockAddr.sin_family = AF_INET;
303  SockAddr.sin_addr.s_addr = inet_addr(szHost);
304 
305  // determine the port correctly -crn@ozemail.com.au 15/12/98
306  SERVENT *sp;
307  sp = getservbyname (strPort, "tcp");
308  if (sp == NULL) {
309  if (isdigit (*(strPort)))
310  SockAddr.sin_port = htons(atoi(strPort));
311  else {
313  return INVALID_SOCKET;
314  }
315  } else
316  SockAddr.sin_port = sp->s_port;
318 
319  // Were we given host name?
320  if (SockAddr.sin_addr.s_addr == INADDR_NONE) {
321 
322  // Resolve host name to IP address.
324  hostent* pHostEnt = gethostbyname(szHost);
325  if (!pHostEnt)
326  return INVALID_SOCKET;
327  printit("\n");
328 
329  SockAddr.sin_addr.s_addr = *(DWORD*)pHostEnt->h_addr;
330  }
331 
332  // Print a message telling the user the IP we are connecting to
333  // (Paul Brannan 5/14/98)
334  char ss_b1[4], ss_b2[4], ss_b3[4], ss_b4[4], ss_b5[12];
335  itoa(SockAddr.sin_addr.S_un.S_un_b.s_b1, ss_b1, 10);
336  itoa(SockAddr.sin_addr.S_un.S_un_b.s_b2, ss_b2, 10);
337  itoa(SockAddr.sin_addr.S_un.S_un_b.s_b3, ss_b3, 10);
338  itoa(SockAddr.sin_addr.S_un.S_un_b.s_b4, ss_b4, 10);
339  itoa(ntohs(SockAddr.sin_port), ss_b5, 10);
340  printm(0, FALSE, MSG_TRYING, ss_b1, ss_b2, ss_b3, ss_b4, ss_b5);
341 
342  if (connect(Socket1, (sockaddr*)&SockAddr, sizeof(SockAddr)))
343  return INVALID_SOCKET;
344 
345  char esc[2];
346  esc [0] = ini.get_escape_key();
347  esc [1] = 0;
348  printm(0, FALSE, MSG_CONNECTED, szHost, esc);
349 
350  return Socket1;
351 }
352 
353 void Telnet::telSetConsoleTitle(const char * szHost1)
354 {
355  char szTitle[128] = "Telnet - ";
356  strcat(szTitle, szHost1);
357  if(ini.get_set_title()) SetConsoleTitle(szTitle);
358 }
359 
361  char cmd_line[MAX_PATH*2];
363 
364  strcpy(cmd_line, ini.get_startdir());
365  strcat(cmd_line, ini.get_exename()); // Thomas Briggs 12/7/98
366 
367  if(!SpawnProcess(cmd_line, &pi)) printm(0, FALSE, MSG_NOSPAWN);
368 }
369 
371  SOCKADDR_IN SockAddr;
372  int size = sizeof(SOCKADDR_IN);
373  memset(&SockAddr, 0, sizeof(SockAddr));
374  SockAddr.sin_family = AF_INET;
375 
376  getsockname(Network.GetSocket(), (sockaddr*)&SockAddr, &size);
377  char ss_b1[4], ss_b2[4], ss_b3[4], ss_b4[4];
378  itoa(SockAddr.sin_addr.S_un.S_un_b.s_b1, ss_b1, 10);
379  itoa(SockAddr.sin_addr.S_un.S_un_b.s_b2, ss_b2, 10);
380  itoa(SockAddr.sin_addr.S_un.S_un_b.s_b3, ss_b3, 10);
381  itoa(SockAddr.sin_addr.S_un.S_un_b.s_b4, ss_b4, 10);
382 
383  char addr[40];
384  strcpy(addr, ss_b1);
385  strcat(addr, ".");
386  strcat(addr, ss_b2);
387  strcat(addr, ".");
388  strcat(addr, ss_b3);
389  strcat(addr, ".");
390  strcat(addr, ss_b4);
391  strcat(addr, ":0.0");
392 
393  Network.SetLocalAddress(addr);
394 }
395 
int LoadKeyMap(const char *file, const char *name)
Definition: tnclass.cpp:139
#define CreateEvent
Definition: winbase.h:3562
TCharmap Charmap
Definition: tnclass.h:50
void telSetConsoleTitle(const char *szHost)
Definition: tnclass.cpp:353
TelThreadParams ThreadParams
Definition: tnclass.h:55
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
#define MSG_KEYMAP
Definition: resource.h:16
const char * get_exename() const
Definition: tnconfig.h:33
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
u_short sin_port
Definition: winsock.h:511
int SwitchKeyMap(int)
Definition: tnclass.cpp:149
#define inet_addr(cp)
Definition: inet.h:98
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
LPARAM oldBIcon
Definition: tnclass.h:62
HANDLE hUnPause
Definition: tparams.h:7
void SetPipe(HANDLE pIn, HANDLE pOut)
Definition: tnetwork.cpp:46
#define MAKEWORD(a, b)
Definition: typedefs.h:247
#define MSG_ERRPIPE
Definition: resource.h:26
#define MSG_ALREADYCONNECTED
Definition: resource.h:28
void sync()
Definition: tconsole.cpp:109
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
#define INADDR_NONE
Definition: tcp.c:42
#define MSG_KEYNOKEYMAPS
Definition: resource.h:47
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:679
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
TNetwork Network
Definition: tnclass.h:49
HWND hConsoleWindow
Definition: tnclass.h:61
const char * get_netpipe() const
Definition: tnconfig.h:98
void ResetIcon(HWND hConsoleWindow, LPARAM oldBIcon, LPARAM oldSIcon)
Definition: tnmisc.cpp:179
DWORD WINAPI telProcessNetwork(LPVOID lpParameter)
Definition: ttelhndl.cpp:479
INT WSAAPI connect(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: sockctrl.c:23
#define MSG_NOSERVICE
Definition: resource.h:24
#define ntohs(x)
Definition: module.h:208
NetParams p
Definition: tparams.h:19
volatile int * bNetPaused
Definition: tparams.h:8
int Open(const char *szHost, const char *strPort="23")
Definition: tnclass.cpp:160
#define MSG_TRYING
Definition: resource.h:13
volatile int bWinsockUp
Definition: tnclass.h:42
#define MSG_RESOLVING
Definition: resource.h:23
#define THREAD_PRIORITY_ABOVE_NORMAL
Definition: winbase.h:272
GLenum GLclampf GLint i
Definition: glfuncs.h:14
TMouse Mouse
Definition: tnclass.h:57
bool get_control_break_as_c() const
Definition: tnconfig.h:61
volatile int bConnected
Definition: tnclass.h:41
#define closesocket
Definition: main.c:39
void SetLocalAddress(char *buf)
Definition: tnetwork.cpp:88
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine, BOOL Add)
Definition: console.c:2111
TConsole Console
Definition: tnclass.h:53
HANDLE hThread
Definition: tnclass.h:65
bool iconChange
Definition: tnclass.h:63
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:112
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
smooth NULL
Definition: ftsmooth.c:416
volatile int bNetPaused
Definition: tnclass.h:43
char strPort[32]
Definition: tnclass.h:39
Definition: tncon.h:15
void DoInit()
Definition: tnclass.cpp:63
#define isdigit(c)
Definition: acclib.h:68
BOOL WINAPI SetThreadPriority(IN HANDLE hThread, IN int nPriority)
Definition: thread.c:662
BOOL WINAPI ControlEventHandler(DWORD event)
Definition: tncon.cpp:357
#define MSG_ERRKEYMAP
Definition: resource.h:17
#define MSG_KEYMAPSWITCHED
Definition: resource.h:50
TConfig ini
Definition: tnconfig.cpp:45
void SetSocket(SOCKET s)
Definition: tnetwork.cpp:40
_CRTIMP char *__cdecl itoa(_In_ int _Val, _Pre_notnull_ _Post_z_ char *_DstBuf, _In_ int _Radix)
PHOSTENT WSAAPI gethostbyname(IN const char FAR *name)
Definition: getxbyxx.c:221
PSERVENT WSAAPI getservbyname(IN const char FAR *name, IN const char FAR *proto)
Definition: getxbyxx.c:500
GLsizeiptr size
Definition: glext.h:5919
#define MSG_CONNECTED
Definition: resource.h:14
#define INVALID_SOCKET
Definition: winsock.h:332
LPARAM oldSIcon
Definition: tnclass.h:62
void NewProcess()
Definition: tnclass.cpp:360
#define MSG_KEYBADMAP
Definition: resource.h:49
#define MAX_PATH
Definition: compat.h:26
BOOL CreateHiddenConsoleProcess(LPCTSTR szChildName, PROCESS_INFORMATION *ppi, LPHANDLE phInWrite, LPHANDLE phOutRead, LPHANDLE phErrRead)
Definition: tnmisc.cpp:5
unsigned long DWORD
Definition: ntddk_ex.h:95
HANDLE hProcess
Definition: tnclass.h:66
int printit(const char *it)
Definition: tnerror.cpp:58
TScroller Scroller
Definition: tnclass.h:58
TCHAR szTitle[MAX_LOADSTRING]
Definition: magnifier.c:36
~Telnet()
Definition: tnclass.cpp:125
volatile int bNetFinish
Definition: tnclass.h:45
static DWORD pi
Definition: protocol.c:150
NetworkType get_net_type()
Definition: tnetwork.h:35
int ret
SOCKET Socket
Definition: tnclass.h:38
const char * get_keyfile() const
Definition: tnconfig.h:34
GLenum const GLvoid * addr
Definition: glext.h:9621
GLdouble s
Definition: gl.h:2039
void Display()
Definition: tmapldr.cpp:739
SOCKET Connect()
Definition: tnclass.cpp:298
int printm(LPTSTR szModule, BOOL fSystem, DWORD dwMessageId,...)
Definition: tnerror.cpp:84
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
int Resume()
Definition: tnclass.cpp:259
void DisplayKeyMap()
Definition: tnclass.cpp:145
char szHost[127]
Definition: tnclass.h:40
struct in_addr sin_addr
Definition: winsock.h:512
bool get_disable_break() const
Definition: tnconfig.h:48
Definition: tncon.h:18
int Close()
Definition: tnclass.cpp:226
const char * get_default_config() const
Definition: tnconfig.h:39
#define SetConsoleTitle
Definition: wincon.h:771
BOOL WINAPI TerminateProcess(IN HANDLE hProcess, IN UINT uExitCode)
Definition: proc.c:1562
TMapLoader MapLoader
Definition: tnclass.h:52
short s_port
Definition: winsock.h:165
struct sockaddr_in SOCKADDR_IN
Definition: winsock.h:487
Definition: tncon.h:19
void ScrollBack()
Definition: tscroll.cpp:163
HANDLE hExit
Definition: tparams.h:6
const char * get_startdir() const
Definition: tnconfig.h:32
#define MSG_NOSPAWN
Definition: resource.h:22
volatile int bNetFinished
Definition: tnclass.h:44
BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent(IN HANDLE hEvent)
Definition: synch.c:660
HANDLE hPause
Definition: tparams.h:7
char get_escape_key() const
Definition: tnconfig.h:82
bool get_set_title() const
Definition: tnconfig.h:55
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
int telProcessConsole(NetParams *pParams, KeyTranslator &KeyTrans, TConsole &Console, TNetwork &Network, TMouse &Mouse, Tnclip &Clipboard, HANDLE hThread)
Definition: tncon.cpp:199
Definition: name.c:36
void SetLocalAddress(SOCKET s)
Definition: tnclass.cpp:370
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define AF_INET
Definition: tcpip.h:117
volatile int * bNetFinished
Definition: tparams.h:9
volatile int * bNetFinish
Definition: tparams.h:10
static const WCHAR sp[]
Definition: suminfo.c:288
UINT_PTR SOCKET
Definition: winsock.h:47
BOOL SpawnProcess(char *cmd_line, PROCESS_INFORMATION *pi)
Definition: tnmisc.cpp:87
Telnet()
Definition: tnclass.cpp:100
TTelnetHandler TelHandler
Definition: tnclass.h:54
int Load(const char *filename, const char *szKeysetName)
Definition: tmapldr.cpp:464
#define SOCK_STREAM
Definition: tcpip.h:118
#define htons(x)
Definition: module.h:213
SOCKET GetSocket()
Definition: tnetwork.h:29
#define memset(x, y, z)
Definition: compat.h:39
Definition: tncon.h:16
HWND TelnetGetConsoleWindow()
Definition: tnmisc.cpp:115
bool SetIcon(HWND hConsoleWindow, HANDLE hIcon, LPARAM *pOldBIcon, LPARAM *pOldSIcon, const char *icondir)
Definition: tnmisc.cpp:136
TANSIParser Parser
Definition: tnclass.h:59
short sin_family
Definition: winsock.h:510
INT WSAAPI getsockname(IN SOCKET s, OUT LPSOCKADDR name, IN OUT INT FAR *namelen)
Definition: sockctrl.c:213
Tnclip Clipboard
Definition: tnclass.h:56
#define MSG_ERROR
Definition: resource.h:10
int SwitchTo(int)
Definition: keytrans.cpp:82
Definition: fci.c:126
KeyTranslator KeyTrans
Definition: tnclass.h:51