telnetConsole.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platform/event.h"
  24. #include "network/telnetConsole.h"
  25. #include "game/gameInterface.h"
  26. #include "telnetConsole_ScriptBinding.h"
  27. TelnetConsole *TelConsole = NULL;
  28. void TelnetConsole::create()
  29. {
  30. TelConsole = new TelnetConsole;
  31. }
  32. void TelnetConsole::destroy()
  33. {
  34. delete TelConsole;
  35. TelConsole = NULL;
  36. }
  37. static void telnetCallback(ConsoleLogEntry::Level level, const char *consoleLine)
  38. {
  39. if (TelConsole)
  40. TelConsole->processConsoleLine(consoleLine);
  41. }
  42. TelnetConsole::TelnetConsole()
  43. {
  44. Con::addConsumer(telnetCallback);
  45. mAcceptSocket = InvalidSocket;
  46. mAcceptPort = -1;
  47. mClientList = NULL;
  48. mRemoteEchoEnabled = false;
  49. }
  50. TelnetConsole::~TelnetConsole()
  51. {
  52. Con::removeConsumer(telnetCallback);
  53. if(mAcceptSocket != InvalidSocket)
  54. Net::closeSocket(mAcceptSocket);
  55. TelnetClient *walk = mClientList, *temp;
  56. while(walk)
  57. {
  58. temp = walk->nextClient;
  59. if(walk->socket != InvalidSocket)
  60. Net::closeSocket(walk->socket);
  61. delete walk;
  62. walk = temp;
  63. }
  64. }
  65. void TelnetConsole::setTelnetParameters(S32 port, const char *telnetPassword, const char *listenPassword, bool remoteEcho)
  66. {
  67. if(port == mAcceptPort)
  68. return;
  69. mRemoteEchoEnabled = remoteEcho;
  70. if(mAcceptSocket != InvalidSocket)
  71. {
  72. Net::closeSocket(mAcceptSocket);
  73. mAcceptSocket = InvalidSocket;
  74. }
  75. mAcceptPort = port;
  76. if(mAcceptPort != -1 && mAcceptPort != 0)
  77. {
  78. mAcceptSocket = Net::openSocket();
  79. Net::bind(mAcceptSocket, mAcceptPort);
  80. Net::listen(mAcceptSocket, 4);
  81. Net::setBlocking(mAcceptSocket, false);
  82. }
  83. dStrncpy(mTelnetPassword, telnetPassword, PasswordMaxLength);
  84. dStrncpy(mListenPassword, listenPassword, PasswordMaxLength);
  85. }
  86. void TelnetConsole::processConsoleLine(const char *consoleLine)
  87. {
  88. if (mClientList==NULL) return; // just escape early. don't even do another step...
  89. // ok, spew this line out to all our subscribers...
  90. S32 len = dStrlen(consoleLine)+1;
  91. for(TelnetClient *walk = mClientList; walk; walk = walk->nextClient)
  92. {
  93. if(walk->state == FullAccessConnected || walk->state == ReadOnlyConnected)
  94. {
  95. if ( walk->socket != InvalidSocket )
  96. {
  97. Net::send(walk->socket, (const unsigned char*)consoleLine, len);
  98. Net::send(walk->socket, (const unsigned char*)"\r\n", 2);
  99. }
  100. }
  101. }
  102. }
  103. void TelnetConsole::process()
  104. {
  105. NetAddress address;
  106. if(mAcceptSocket != InvalidSocket)
  107. {
  108. // ok, see if we have any new connections:
  109. NetSocket newConnection;
  110. newConnection = Net::accept(mAcceptSocket, &address);
  111. if(newConnection != InvalidSocket)
  112. {
  113. Con::printf ("Telnet connection from %i.%i.%i.%i",
  114. address.netNum[0], address.netNum[1], address.netNum[2], address.netNum[3]);
  115. TelnetClient *cl = new TelnetClient;
  116. cl->socket = newConnection;
  117. cl->curPos = 0;
  118. cl->state = PasswordTryOne;
  119. Net::setBlocking(newConnection, false);
  120. const char *connectMessage = "Torque Telnet Remote Console\r\n\r\nEnter Password:";
  121. if ( cl->socket != InvalidSocket )
  122. Net::send(cl->socket, (const unsigned char*)connectMessage, dStrlen(connectMessage)+1);
  123. cl->nextClient = mClientList;
  124. mClientList = cl;
  125. }
  126. }
  127. char recvBuf[256];
  128. char reply[1024];
  129. // see if we have any input to process...
  130. for(TelnetClient *client = mClientList; client; client = client->nextClient)
  131. {
  132. S32 numBytes;
  133. Net::Error err = Net::NotASocket;
  134. if ( client->socket != InvalidSocket )
  135. Net::recv(client->socket, (unsigned char*)recvBuf, sizeof(recvBuf), &numBytes);
  136. if((err != Net::NoError && err != Net::WouldBlock) || numBytes == 0)
  137. {
  138. if ( client->socket != InvalidSocket )
  139. {
  140. Net::closeSocket(client->socket);
  141. client->socket = InvalidSocket;
  142. }
  143. continue;
  144. }
  145. S32 replyPos = 0;
  146. for(S32 i = 0; i < numBytes;i++)
  147. {
  148. if(recvBuf[i] == '\r')
  149. continue;
  150. // execute the current command
  151. if(recvBuf[i] == '\n')
  152. {
  153. reply[replyPos++] = '\r';
  154. reply[replyPos++] = '\n';
  155. client->curLine[client->curPos] = 0;
  156. client->curPos = 0;
  157. if(client->state == FullAccessConnected)
  158. {
  159. if ( client->socket != InvalidSocket )
  160. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  161. replyPos = 0;
  162. dStrcpy(mPostEvent.data, client->curLine);
  163. mPostEvent.size = ConsoleEventHeaderSize + dStrlen(client->curLine) + 1;
  164. Game->postEvent(mPostEvent);
  165. // note - send prompt next
  166. const char *prompt = Con::getVariable("Con::Prompt");
  167. if ( client->socket != InvalidSocket )
  168. Net::send(client->socket, (const unsigned char*)prompt, dStrlen(prompt));
  169. }
  170. else if(client->state == ReadOnlyConnected)
  171. {
  172. if ( client->socket != InvalidSocket )
  173. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  174. replyPos = 0;
  175. }
  176. else
  177. {
  178. client->state++;
  179. if(!dStrncmp(client->curLine, mTelnetPassword, PasswordMaxLength))
  180. {
  181. if ( client->socket != InvalidSocket )
  182. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  183. replyPos = 0;
  184. // send prompt
  185. const char *prompt = Con::getVariable("Con::Prompt");
  186. if ( client->socket != InvalidSocket )
  187. Net::send(client->socket, (const unsigned char*)prompt, dStrlen(prompt));
  188. client->state = FullAccessConnected;
  189. }
  190. else if(!dStrncmp(client->curLine, mListenPassword, PasswordMaxLength))
  191. {
  192. if ( client->socket != InvalidSocket )
  193. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  194. replyPos = 0;
  195. // send prompt
  196. const char *listenConnected = "Connected.\r\n";
  197. if ( client->socket != InvalidSocket )
  198. Net::send(client->socket, (const unsigned char*)listenConnected, dStrlen(listenConnected));
  199. client->state = ReadOnlyConnected;
  200. }
  201. else
  202. {
  203. const char *sendStr;
  204. if(client->state == DisconnectThisDude)
  205. sendStr = "Too many tries... cya.";
  206. else
  207. sendStr = "Nope... try agian.\r\nEnter Password:";
  208. if ( client->socket != InvalidSocket )
  209. Net::send(client->socket, (const unsigned char*)sendStr, dStrlen(sendStr));
  210. if(client->state == DisconnectThisDude)
  211. {
  212. if ( client->socket != InvalidSocket )
  213. {
  214. Net::closeSocket(client->socket);
  215. client->socket = InvalidSocket;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. else if(recvBuf[i] == '\b')
  222. {
  223. // pull the old backspace manuever...
  224. if(client->curPos > 0)
  225. {
  226. client->curPos--;
  227. if(client->state == FullAccessConnected)
  228. {
  229. reply[replyPos++] = '\b';
  230. reply[replyPos++] = ' ';
  231. reply[replyPos++] = '\b';
  232. }
  233. }
  234. }
  235. else if(client->curPos < Con::MaxLineLength-1)
  236. {
  237. client->curLine[client->curPos++] = recvBuf[i];
  238. // don't echo password chars...
  239. if(client->state == FullAccessConnected)
  240. reply[replyPos++] = recvBuf[i];
  241. }
  242. }
  243. // Echo the character back to the user, unless the remote echo
  244. // is disabled (by default)
  245. if(replyPos && mRemoteEchoEnabled)
  246. {
  247. if ( client->socket != InvalidSocket )
  248. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  249. }
  250. }
  251. TelnetClient ** walk = &mClientList;
  252. TelnetClient *cl;
  253. while((cl = *walk) != NULL)
  254. {
  255. if(cl->socket == InvalidSocket)
  256. {
  257. *walk = cl->nextClient;
  258. delete cl;
  259. }
  260. else
  261. walk = &cl->nextClient;
  262. }
  263. }