TelnetTransport.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "NativeFeatureIncludes.h"
  11. #if _RAKNET_SUPPORT_TelnetTransport==1 && _RAKNET_SUPPORT_TCPInterface==1
  12. #include "TelnetTransport.h"
  13. #include "TCPInterface.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include "LinuxStrings.h"
  18. // #define _PRINTF_DEBUG
  19. #define ECHO_INPUT
  20. #ifdef _MSC_VER
  21. #pragma warning( push )
  22. #endif
  23. using namespace RakNet;
  24. STATIC_FACTORY_DEFINITIONS(TelnetTransport,TelnetTransport);
  25. TelnetTransport::TelnetTransport()
  26. {
  27. tcpInterface=0;
  28. sendSuffix=0;
  29. sendPrefix=0;
  30. }
  31. TelnetTransport::~TelnetTransport()
  32. {
  33. Stop();
  34. if (sendSuffix)
  35. rakFree_Ex(sendSuffix, _FILE_AND_LINE_ );
  36. if (sendPrefix)
  37. rakFree_Ex(sendPrefix, _FILE_AND_LINE_ );
  38. }
  39. bool TelnetTransport::Start(unsigned short port, bool serverMode)
  40. {
  41. (void) serverMode;
  42. AutoAllocate();
  43. RakAssert(serverMode);
  44. return tcpInterface->Start(port, 64);
  45. }
  46. void TelnetTransport::Stop(void)
  47. {
  48. if (tcpInterface==0) return;
  49. tcpInterface->Stop();
  50. unsigned i;
  51. for (i=0; i < remoteClients.Size(); i++)
  52. RakNet::OP_DELETE(remoteClients[i], _FILE_AND_LINE_);
  53. remoteClients.Clear(false, _FILE_AND_LINE_);
  54. RakNet::OP_DELETE(tcpInterface, _FILE_AND_LINE_);
  55. tcpInterface=0;
  56. }
  57. void TelnetTransport::Send( SystemAddress systemAddress, const char *data,... )
  58. {
  59. if (tcpInterface==0) return;
  60. if (data==0 || data[0]==0)
  61. return;
  62. char text[REMOTE_MAX_TEXT_INPUT];
  63. size_t prefixLength;
  64. if (sendPrefix)
  65. {
  66. strcpy(text, sendPrefix);
  67. prefixLength = strlen(sendPrefix);
  68. }
  69. else
  70. {
  71. text[0]=0;
  72. prefixLength=0;
  73. }
  74. va_list ap;
  75. va_start(ap, data);
  76. _vsnprintf(text+prefixLength, REMOTE_MAX_TEXT_INPUT-prefixLength, data, ap);
  77. va_end(ap);
  78. text[REMOTE_MAX_TEXT_INPUT-1]=0;
  79. if (sendSuffix)
  80. {
  81. size_t length = strlen(text);
  82. size_t availableChars = REMOTE_MAX_TEXT_INPUT-length-1;
  83. strncat(text, sendSuffix, availableChars);
  84. }
  85. tcpInterface->Send(text, (unsigned int) strlen(text), systemAddress, false);
  86. }
  87. void TelnetTransport::CloseConnection( SystemAddress systemAddress )
  88. {
  89. tcpInterface->CloseConnection(systemAddress);
  90. }
  91. Packet* TelnetTransport::Receive( void )
  92. {
  93. if (tcpInterface==0) return 0;
  94. Packet *p = tcpInterface->Receive();
  95. if (p==0)
  96. return 0;
  97. /*
  98. if (p->data[0]==255)
  99. {
  100. unsigned i;
  101. for (i=0; i < p->length; i++)
  102. {
  103. RAKNET_DEBUG_PRINTF("%i ", p->data[i]);
  104. }
  105. RAKNET_DEBUG_PRINTF("\n");
  106. tcpInterface->DeallocatePacket(p);
  107. return 0;
  108. }
  109. */
  110. // Get this guy's cursor buffer. This is real bullcrap that I have to do this.
  111. unsigned i;
  112. TelnetClient *remoteClient=0;
  113. for (i=0; i < remoteClients.Size(); i++)
  114. {
  115. if (remoteClients[i]->systemAddress==p->systemAddress)
  116. remoteClient=remoteClients[i];
  117. }
  118. //RakAssert(remoteClient);
  119. if (remoteClient==0)
  120. {
  121. tcpInterface->DeallocatePacket(p);
  122. return 0;
  123. }
  124. if (p->length==3 && p->data[0]==27 && p->data[1]==91 && p->data[2]==65)
  125. {
  126. if (remoteClient->lastSentTextInput[0])
  127. {
  128. // Up arrow, return last string
  129. for (int i=0; remoteClient->textInput[i]; i++)
  130. remoteClient->textInput[i]=8;
  131. strcat(remoteClient->textInput, remoteClient->lastSentTextInput);
  132. tcpInterface->Send((const char *)remoteClient->textInput, (unsigned int) strlen(remoteClient->textInput), p->systemAddress, false);
  133. strcpy(remoteClient->textInput,remoteClient->lastSentTextInput);
  134. remoteClient->cursorPosition=(unsigned int) strlen(remoteClient->textInput);
  135. }
  136. return 0;
  137. }
  138. // 127 is delete - ignore that
  139. // 9 is tab
  140. // 27 is escape
  141. if (p->data[0]>=127 || p->data[0]==9 || p->data[0]==27)
  142. {
  143. tcpInterface->DeallocatePacket(p);
  144. return 0;
  145. }
  146. // Hack - I don't know what the hell this is about but cursor keys send 3 characters at a time. I can block these
  147. //Up=27,91,65
  148. //Down=27,91,66
  149. //Right=27,91,67
  150. //Left=27,91,68
  151. if (p->length==3 && p->data[0]==27 && p->data[1]==91 && p->data[2]>=65 && p->data[2]<=68)
  152. {
  153. tcpInterface->DeallocatePacket(p);
  154. return 0;
  155. }
  156. // Echo
  157. #ifdef ECHO_INPUT
  158. tcpInterface->Send((const char *)p->data, p->length, p->systemAddress, false);
  159. #endif
  160. bool gotLine;
  161. // Process each character in turn
  162. for (i=0; i < p->length; i++)
  163. {
  164. #ifdef ECHO_INPUT
  165. if (p->data[i]==8)
  166. {
  167. char spaceThenBack[2];
  168. spaceThenBack[0]=' ';
  169. spaceThenBack[1]=8;
  170. tcpInterface->Send((const char *)spaceThenBack, 2, p->systemAddress, false);
  171. }
  172. #endif
  173. gotLine=ReassembleLine(remoteClient, p->data[i]);
  174. if (gotLine && remoteClient->textInput[0])
  175. {
  176. Packet *reassembledLine = (Packet*) rakMalloc_Ex(sizeof(Packet), _FILE_AND_LINE_);
  177. reassembledLine->length=(unsigned int) strlen(remoteClient->textInput);
  178. memcpy(remoteClient->lastSentTextInput, remoteClient->textInput, reassembledLine->length+1);
  179. RakAssert(reassembledLine->length < REMOTE_MAX_TEXT_INPUT);
  180. reassembledLine->data= (unsigned char*) rakMalloc_Ex( reassembledLine->length+1, _FILE_AND_LINE_ );
  181. memcpy(reassembledLine->data, remoteClient->textInput, reassembledLine->length);
  182. #ifdef _PRINTF_DEBUG
  183. memset(remoteClient->textInput, 0, REMOTE_MAX_TEXT_INPUT);
  184. #endif
  185. reassembledLine->data[reassembledLine->length]=0;
  186. reassembledLine->systemAddress=p->systemAddress;
  187. tcpInterface->DeallocatePacket(p);
  188. return reassembledLine;
  189. }
  190. }
  191. tcpInterface->DeallocatePacket(p);
  192. return 0;
  193. }
  194. void TelnetTransport::DeallocatePacket( Packet *packet )
  195. {
  196. if (tcpInterface==0) return;
  197. rakFree_Ex(packet->data, _FILE_AND_LINE_ );
  198. rakFree_Ex(packet, _FILE_AND_LINE_ );
  199. }
  200. SystemAddress TelnetTransport::HasNewIncomingConnection(void)
  201. {
  202. unsigned i;
  203. SystemAddress newConnection;
  204. newConnection = tcpInterface->HasNewIncomingConnection();
  205. // 03/16/06 Can't force the stupid windows telnet to use line mode or local echo so now I have to track all the remote players and their
  206. // input buffer
  207. if (newConnection != UNASSIGNED_SYSTEM_ADDRESS)
  208. {
  209. unsigned char command[10];
  210. // http://www.pcmicro.com/netfoss/RFC857.html
  211. // IAC WON'T ECHO
  212. command[0]=255; // IAC
  213. //command[1]=253; // WON'T
  214. command[1]=251; // WILL
  215. command[2]=1; // ECHO
  216. tcpInterface->Send((const char*)command, 3, newConnection, false);
  217. /*
  218. // Tell the other side to use line mode
  219. // http://www.faqs.org/rfcs/rfc1184.html
  220. // IAC DO LINEMODE
  221. // command[0]=255; // IAC
  222. // command[1]=252; // DO
  223. // command[2]=34; // LINEMODE
  224. // tcpInterface->Send((const char*)command, 3, newConnection);
  225. */
  226. TelnetClient *remoteClient=0;
  227. for (i=0; i < remoteClients.Size(); i++)
  228. {
  229. if (remoteClients[i]->systemAddress==newConnection)
  230. {
  231. remoteClient=remoteClients[i];
  232. remoteClient->cursorPosition=0;
  233. }
  234. }
  235. if (remoteClient==0)
  236. {
  237. remoteClient=new TelnetClient;
  238. remoteClient->lastSentTextInput[0]=0;
  239. remoteClient->cursorPosition=0;
  240. remoteClient->systemAddress=newConnection;
  241. #ifdef _PRINTF_DEBUG
  242. memset(remoteClient->textInput, 0, REMOTE_MAX_TEXT_INPUT);
  243. #endif
  244. }
  245. remoteClients.Insert(remoteClient, _FILE_AND_LINE_);
  246. }
  247. return newConnection;
  248. }
  249. SystemAddress TelnetTransport::HasLostConnection(void)
  250. {
  251. SystemAddress systemAddress;
  252. unsigned i;
  253. systemAddress=tcpInterface->HasLostConnection();
  254. if (systemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
  255. {
  256. for (i=0; i < remoteClients.Size(); i++)
  257. {
  258. if (remoteClients[i]->systemAddress==systemAddress)
  259. {
  260. RakNet::OP_DELETE(remoteClients[i], _FILE_AND_LINE_);
  261. remoteClients[i]=remoteClients[remoteClients.Size()-1];
  262. remoteClients.RemoveFromEnd();
  263. }
  264. }
  265. }
  266. return systemAddress;
  267. }
  268. CommandParserInterface* TelnetTransport::GetCommandParser(void)
  269. {
  270. return 0;
  271. }
  272. void TelnetTransport::SetSendSuffix(const char *suffix)
  273. {
  274. if (sendSuffix)
  275. {
  276. rakFree_Ex(sendSuffix, _FILE_AND_LINE_ );
  277. sendSuffix=0;
  278. }
  279. if (suffix)
  280. {
  281. sendSuffix = (char*) rakMalloc_Ex(strlen(suffix)+1, _FILE_AND_LINE_);
  282. strcpy(sendSuffix, suffix);
  283. }
  284. }
  285. void TelnetTransport::SetSendPrefix(const char *prefix)
  286. {
  287. if (sendPrefix)
  288. {
  289. rakFree_Ex(sendPrefix, _FILE_AND_LINE_ );
  290. sendPrefix=0;
  291. }
  292. if (prefix)
  293. {
  294. sendPrefix = (char*) rakMalloc_Ex(strlen(prefix)+1, _FILE_AND_LINE_);
  295. strcpy(sendPrefix, prefix);
  296. }
  297. }
  298. void TelnetTransport::AutoAllocate(void)
  299. {
  300. if (tcpInterface==0)
  301. tcpInterface=new TCPInterface;
  302. }
  303. bool TelnetTransport::ReassembleLine(TelnetTransport::TelnetClient* remoteClient, unsigned char c)
  304. {
  305. if (c=='\n')
  306. {
  307. remoteClient->textInput[remoteClient->cursorPosition]=0;
  308. remoteClient->cursorPosition=0;
  309. #ifdef _PRINTF_DEBUG
  310. RAKNET_DEBUG_PRINTF("[Done] %s\n", remoteClient->textInput);
  311. #endif
  312. return true;
  313. }
  314. else if (c==8) // backspace
  315. {
  316. if (remoteClient->cursorPosition>0)
  317. {
  318. remoteClient->textInput[--remoteClient->cursorPosition]=0;
  319. #ifdef _PRINTF_DEBUG
  320. RAKNET_DEBUG_PRINTF("[Back] %s\n", remoteClient->textInput);
  321. #endif
  322. }
  323. }
  324. else if (c>=32 && c <127)
  325. {
  326. if (remoteClient->cursorPosition < REMOTE_MAX_TEXT_INPUT)
  327. {
  328. remoteClient->textInput[remoteClient->cursorPosition++]=c;
  329. #ifdef _PRINTF_DEBUG
  330. RAKNET_DEBUG_PRINTF("[Norm] %s\n", remoteClient->textInput);
  331. #endif
  332. }
  333. }
  334. return false;
  335. }
  336. #ifdef _MSC_VER
  337. #pragma warning( pop )
  338. #endif
  339. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号