1 /**
2  * telnetcon.h - Class dealing with telnet connections,
3  *               parsing telnet commands.
4  *
5  * Copyright (c) 2011 Kan-Ru Chen <kanru@kanru.info>
6  * Copyright (c) 2005 PCMan <pcman.tw@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef TELNETCON_H
24 #define TELNETCON_H
25 
26 #ifdef __GNUG__
27   #pragma interface "telnetcon.h"
28 #endif
29 
30 #include "pcmanx_utils.h"
31 
32 #include <gtk/gtk.h>
33 
34 #include "termdata.h"
35 #include "site.h"
36 
37 #if !defined(MOZ_PLUGIN)
38 
39 #ifdef USE_NANCY
40 #include "nancy_bot/api.h"
41 #endif
42 
43 #endif /* !defined(MOZ_PLUGIN) */
44 
45 #include <string>
46 #include <vector>
47 #include <list>
48 
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 
52 using namespace std;
53 
54 // Telnet commands
55 #define TC_SE                   (unsigned char)240
56 #define TC_NOP                  (unsigned char)241
57 #define TC_DATA_MARK            (unsigned char)242
58 #define TC_BREAK                (unsigned char)243
59 #define TC_INTERRUPT_PROCESS    (unsigned char)244
60 #define TC_ABORT_OUTPUT         (unsigned char)245
61 #define TC_ARE_YOU_THERE        (unsigned char)246
62 #define TC_ERASE_CHARACTER      (unsigned char)247
63 #define TC_ERASE_LINE           (unsigned char)248
64 #define TC_GO_AHEAD	            (unsigned char)249
65 #define TC_SB                   (unsigned char)250
66 // Option commands
67 #define TC_WILL                 (unsigned char)251
68 #define TC_WONT                 (unsigned char)252
69 #define TC_DO                   (unsigned char)253
70 #define TC_DONT                 (unsigned char)254
71 #define TC_IAC                  (unsigned char)255
72 
73 // Telnet options)
74 #define TO_ECHO                 (unsigned char)1
75 #define TO_SUPRESS_GO_AHEAD     (unsigned char)3
76 #define TO_TERMINAL_TYPE        (unsigned char)24
77 #define	TO_IS                   (unsigned char)0
78 #define	TO_SEND                 (unsigned char)1
79 #define TO_NAWS                 (unsigned char)31
80 
81 /*
82  * A class for Telnet Connections, used to store screen buffer of
83  * every connections and their sockets, etc.
84  */
85 
86 class CDNSRequest;
87 class CTelnetView;
88 class CTelnetCon : public CTermData
89 {
90 public:
91 	virtual int Send( void* buf, int len );
92 	virtual void Bell();	// called from CTermData to process beep.
93 	void OnTimer();
94 	static gboolean OnSocket(GIOChannel *channel, GIOCondition type, CTelnetCon* _this);
GetView()95 	CTelnetView* GetView(){	return (CTelnetView*)m_pView;	}
96 
97 	// A flag used to indicate connecting state;
98 	enum{TS_CONNECTING, TS_CONNECTED, TS_CLOSED} m_State;
99 
100 	void Disconnect();
101 	// class constructor
102 	CTelnetCon(CTermView* pView, CSite& SiteInfo);
103 	// class destructor
104 	~CTelnetCon();
105 	// No description
106 	virtual bool Connect();
107     void Reconnect();
108 	// Connecting duration
109 	unsigned int m_Duration;
110 	// Idle time, during which the user doesn't have any key input
111 	unsigned int m_IdleTime;
112 
113 	CSite m_Site;
114     static list<CDNSRequest*> m_DNSQueue;
115 
116 	virtual void OnClose();
117 	void OnConnect(int code);
118 	bool OnRecv();
119 
120 	// Parse received data, process telnet command, and ANSI escape sequence.
121 	void ParseReceivedData();
122 
123 	// Parse telnet command.
124 	void ParseTelnetCommand();
125 
SendRawString(const char * pdata,int len)126 	void SendRawString(const char* pdata, int len)	{	Send( (void*)pdata, len);	}
127 	void SendUnEscapedString(string str);
128 	void SendString(string str);
129 
IsValid()130 	bool IsValid(){	return m_SockFD >= 0;	}
IsClosed()131 	bool IsClosed(){	return (m_State & TS_CLOSED);	}
132 
IsBellReceived()133 	bool IsBellReceived(){	return (m_BellTimeout != 0);	}
134     void Close();
135 
136 	static void Cleanup();
137     static bool OnBellTimeout( CTelnetCon* _this );
138     void OnNewIncomingMessage(const char* line);
139 
SetSocketTimeout(int timeout)140 	static void SetSocketTimeout(int timeout){	m_SocketTimeout=timeout;	}
DetectDBChar()141     bool DetectDBChar(){	return m_Site.m_DetectDBChar;   }
142     void ConnectAsync();
143 
144     static void Init();
145 #if !defined(MOZ_PLUGIN)
146 #ifdef USE_NANCY
set__UseNancy(bool usenancy)147     void set__UseNancy( bool usenancy )
148     {
149 	    use_nancy = usenancy;
150     }
151 
set__OpenConnectionWithNancySupport(bool nancy_support)152     static void set__OpenConnectionWithNancySupport( bool nancy_support )
153     {
154 	    with_nancy_support = nancy_support;
155     }
156 
get__UseNancy()157     bool get__UseNancy()
158     {
159 	    return use_nancy;
160     }
161 
get__OpenConnectionWithNancySupport()162     static bool get__OpenConnectionWithNancySupport()
163     {
164 	    return with_nancy_support;
165     }
166 #endif
167 #endif /* !defined(MOZ_PLUGIN) */
168 
169 #ifdef USE_MOUSE
GetPageState()170     int GetPageState() {return m_nPageState;}
171     char GetMenuChar(int y);
172 #endif
173 
174 protected:
175 
176 #if !defined(MOZ_PLUGIN)
177 #ifdef USE_NANCY
178     	NancyBot *bot;
179 	static bool with_nancy_support;
180 	bool use_nancy;
181 #endif
182 #endif /* !defined(MOZ_PLUGIN) */
183 
184 	GIOChannel* m_IOChannel;
185 	guint m_IOChannelID;
186 
187 	// Buffer to receive socket incoming data
188 	unsigned char* m_pRecvBuf;
189 	unsigned char* m_pBuf;
190 	unsigned char* m_pLastByte;
191 	enum AUTO_LOGIN_STATE
192 	{
193 		ALS_OFF=0,
194 		ALS_PRELOGIN=1,
195 		ALS_LOGIN=2,
196 		ALS_PASSWD=3,
197 		ALS_END=4
198 	};
199 	unsigned int m_AutoLoginStage;	// 0 means turn off auto-login.
200 
201 	// Client socket
202     int m_SockFD;
203 	int m_Pid;
204 
205 #ifdef USE_MOUSE
206 	// Page state for mouse browsing
207 	/*	enum m_nPageState
208 	{
209 	  NORMAL=-1,
210 	  MENU=0,
211 	  LIST=1,
212 	  READING=2
213 	  };*/
214 	int m_nPageState;
215 #endif
216 
217 protected:
218 	guint m_BellTimeout;
219 	bool m_IsLastLineModified;
220     static GThread* m_DNSThread;
221     string m_PreLoginPrompt;
222     string m_LoginPrompt;
223     string m_PasswdPrompt;
224     static int m_SocketTimeout;
225     struct sockaddr_storage m_SockAddr;
226     string m_Port;
227     void PreConnect(string& address, string& port);
228     void CheckAutoLogin(int row);
229     void SendStringAsync(string str);
230     static void DoDNSLookup( CDNSRequest* data );
231     void OnLineModified(int row);
232     static gboolean OnDNSLookupEnd(CTelnetCon* _this);
233     static gboolean OnConnectCB(GIOChannel *channel, GIOCondition type, CTelnetCon* _this);
234     static void ProcessDNSQueue(gpointer unused);
235 private:
236     static GMutex* m_DNSMutex;
237 private:
238     static bool OnProcessDNSQueueExit(gpointer unused);
239     void SetPageState();
240     bool IsUnicolor(char* line, int start, int end);
241 };
242 
243 class CDNSRequest
244 {
245 public:
CDNSRequest(CTelnetCon * con,string address,string port UNUSED)246 	CDNSRequest(CTelnetCon* con, string address, string port UNUSED)
247 		: m_pCon(con), m_Address(address), m_Running(false)
248 	{
249 	}
250 	CTelnetCon* m_pCon;
251 	string m_Address;
252 	bool m_Running;
253 };
254 
255 
256 
257 #endif // TELNETCON_H
258 
259