//----------------------------------------------------------------------------- // V12 Engine // // Copyright (c) 2001 GarageGames.Com // Portions Copyright (c) 2001 by Sierra Online, Inc. //----------------------------------------------------------------------------- #include "Platform/platform.h" #include "Platform/event.h" #include "console/telnetConsole.h" #include "Platform/gameInterface.h" TelnetConsole *TelConsole = NULL; void TelnetConsole::create() { TelConsole = new TelnetConsole; } void TelnetConsole::destroy() { delete TelConsole; TelConsole = NULL; } static void cTelnetSetParams(SimObject *, S32, const char **argv) { TelConsole->setTelnetParameters(dAtoi(argv[1]), argv[2], argv[3]); } static void telnetCallback(ConsoleLogEntry::Level level, const char *consoleLine) { TelConsole->processConsoleLine(consoleLine); } TelnetConsole::TelnetConsole() { Con::addConsumer(telnetCallback); Con::addCommand("telnetSetParameters", cTelnetSetParams, "telnetSetParameters(port,consolePass,listenPass)", 4, 4); mAcceptSocket = InvalidSocket; mAcceptPort = -1; mClientList = NULL; } TelnetConsole::~TelnetConsole() { Con::removeConsumer(telnetCallback); if(mAcceptSocket != InvalidSocket) Net::closeSocket(mAcceptSocket); TelnetClient *walk = mClientList, *temp; while(walk) { temp = walk->nextClient; if(walk->socket != InvalidSocket) Net::closeSocket(walk->socket); delete walk; walk = temp; } } void TelnetConsole::setTelnetParameters(S32 port, const char *telnetPassword, const char *listenPassword) { if(port == mAcceptPort) return; if(mAcceptSocket != InvalidSocket) { Net::closeSocket(mAcceptSocket); mAcceptSocket = InvalidSocket; } mAcceptPort = port; if(mAcceptPort != -1 && mAcceptPort != 0) { mAcceptSocket = Net::openSocket(); Net::bind(mAcceptSocket, mAcceptPort); Net::listen(mAcceptSocket, 4); Net::setBlocking(mAcceptSocket, false); } dStrncpy(mTelnetPassword, telnetPassword, PasswordMaxLength); dStrncpy(mListenPassword, listenPassword, PasswordMaxLength); } void TelnetConsole::processConsoleLine(const char *consoleLine) { // ok, spew this line out to all our subscribers... S32 len = dStrlen(consoleLine)+1; for(TelnetClient *walk = mClientList; walk; walk = walk->nextClient) { if(walk->state == FullAccessConnected || walk->state == ReadOnlyConnected) { Net::send(walk->socket, (const unsigned char*)consoleLine, len); Net::send(walk->socket, (const unsigned char*)"\r\n", 2); } } } void TelnetConsole::process() { NetAddress address; if(mAcceptSocket != InvalidSocket) { // ok, see if we have any new connections: NetSocket newConnection; newConnection = Net::accept(mAcceptSocket, &address); if(newConnection != InvalidSocket) { Con::printf ("Telnet connection from %i.%i.%i.%i", address.netNum[0], address.netNum[1], address.netNum[2], address.netNum[3]); TelnetClient *cl = new TelnetClient; cl->socket = newConnection; cl->curPos = 0; cl->state = PasswordTryOne; Net::setBlocking(newConnection, false); char *connectMessage = "Tribes 2 Telnet\r\n\r\nEnter Password:"; Net::send(cl->socket, (const unsigned char*)connectMessage, dStrlen(connectMessage)+1); cl->nextClient = mClientList; mClientList = cl; } } char recvBuf[256]; char reply[1024]; // see if we have any input to process... for(TelnetClient *client = mClientList; client; client = client->nextClient) { S32 numBytes; Net::Error err = Net::recv(client->socket, (unsigned char*)recvBuf, sizeof(recvBuf), &numBytes); if((err != Net::NoError && err != Net::WouldBlock) || numBytes == 0) { Net::closeSocket(client->socket); client->socket = InvalidSocket; continue; } S32 replyPos = 0; for(S32 i = 0; i < numBytes;i++) { if(recvBuf[i] == '\r') continue; // execute the current command if(recvBuf[i] == '\n') { reply[replyPos++] = '\r'; reply[replyPos++] = '\n'; client->curLine[client->curPos] = 0; client->curPos = 0; if(client->state == FullAccessConnected) { Net::send(client->socket, (const unsigned char*)reply, replyPos); replyPos = 0; dStrcpy(mPostEvent.data, client->curLine); mPostEvent.size = ConsoleEventHeaderSize + dStrlen(client->curLine) + 1; Game->postEvent(mPostEvent); // note - send prompt next const char *prompt = Con::getVariable("Con::Prompt"); Net::send(client->socket, (const unsigned char*)prompt, dStrlen(prompt)); } else if(client->state == ReadOnlyConnected) { Net::send(client->socket, (const unsigned char*)reply, replyPos); replyPos = 0; } else { client->state++; if(!dStrncmp(client->curLine, mTelnetPassword, PasswordMaxLength)) { Net::send(client->socket, (const unsigned char*)reply, replyPos); replyPos = 0; // send prompt const char *prompt = Con::getVariable("Con::Prompt"); Net::send(client->socket, (const unsigned char*)prompt, dStrlen(prompt)); client->state = FullAccessConnected; } else if(!dStrncmp(client->curLine, mListenPassword, PasswordMaxLength)) { Net::send(client->socket, (const unsigned char*)reply, replyPos); replyPos = 0; // send prompt const char *listenConnected = "Connected.\r\n"; Net::send(client->socket, (const unsigned char*)listenConnected, dStrlen(listenConnected)); client->state = ReadOnlyConnected; } else { const char *sendStr; if(client->state == DisconnectThisDude) sendStr = "Too many tries... cya."; else sendStr = "Nope... try agian.\r\nEnter Password:"; Net::send(client->socket, (const unsigned char*)sendStr, dStrlen(sendStr)); if(client->state == DisconnectThisDude) { Net::closeSocket(client->socket); client->socket = InvalidSocket; } } } } else if(recvBuf[i] == '\b') { // pull the old backspace manuever... if(client->curPos > 0) { client->curPos--; if(client->state == FullAccessConnected) { reply[replyPos++] = '\b'; reply[replyPos++] = ' '; reply[replyPos++] = '\b'; } } } else if(client->curPos < Con::MaxLineLength-1) { client->curLine[client->curPos++] = recvBuf[i]; // don't echo password chars... if(client->state == FullAccessConnected) reply[replyPos++] = recvBuf[i]; } } if(replyPos) Net::send(client->socket, (const unsigned char*)reply, replyPos); } TelnetClient ** walk = &mClientList; TelnetClient *cl; while((cl = *walk) != NULL) { if(cl->socket == InvalidSocket) { *walk = cl->nextClient; delete cl; } else walk = &cl->nextClient; } }