void packetReceive(void) { struct sockaddr_in si_me, si_other; int s, recv_len, Port, ID; socklen_t slen = sizeof(si_other); packet_t buf; if((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) die("socket"); memset((char *) &si_me, 0, sizeof(si_me)); pthread_mutex_lock(&lock); Port = G->adj[G->ID]->port; ID = G->ID; pthread_mutex_unlock(&lock); si_me.sin_family = AF_INET; si_me.sin_port = htons(Port); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(s, (struct sockaddr*)&si_me, sizeof(si_me)) == -1) die("bind"); while(1) { fflush(stdout); memset(buf.message,'\0', BUFLEN); if((recv_len = recvfrom(s, &buf, sizeof(packet_t), 0, (struct sockaddr *) &si_other, &slen)) == -1) die("recvfrom()"); if(buf.id == ID+1) { /* Verifica se a mensagem recebida e pra ele */ if(buf.ack == 0) { /* Verifica se e uma confirmacao ou nao */ printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); printf("IDSRC: %d ID: %d Data: %s\n", buf.idSrc, buf.id, buf.message); buf.id = buf.idSrc; buf.idSrc = ID; buf.ack = 1; packetSend(buf); }else{ /* Recebeu uma confirmacao */ if(buf.sequence == G->sequence[buf.idSrc]) _ACK = 1; } } else { /* Retransmite para outro roteador com o menos custo */ packetSend(buf); } } close(s); }
size_t moduleAuth::ping( ) { packetSend( (byte) modAUTH_Ping ); // Return number of seconds since last pong return (size_t) (time (NULL) - _lastPong); }
void moduleAdmin::IPsGet ( size_t memberID ) { stringstream memberData; memberData.write( (const char*) &memberID, sizeof( size_t) ); packetSend( (byte) modADMIN_IpsGet, memberData ); }
size_t module::packetSend( byte message, string &Response) { stringstream outgoing; outgoing << Response; outgoing << '\0'; return packetSend( message, outgoing ); }
///info about servers {3,0,0,0} static void *message3(server *sv, packet *p){ packetInitFast(p); packetAddNumber(p, (char)MSG_S_SERVERS_INFO); packetAddNumber(p, (char)-1); serversForEach(addServerIdToPacket, p); packetAddNumber(p, (char)0); packetAddNumber(p, (int)0); packetSend(p, sv->sock); return 0; }
///i need new id {6,0,0} static void *message6(server *sv, packet *p){ packetInitFast(p); packetAddNumber(p, (char)MSG_S_NEW_ID); packetAddNumber(p, (char)1); packetAddInt(p, (char)id++); packetAddNumber(p, (char)0); packetAddNumber(p, (int)0); packetSend(p, sv->sock); return 0; }
void interface(void) { packet_t buf; int ID, idMax, i; pthread_mutex_lock(&lock); ID = G->ID; idMax = G->V; pthread_mutex_unlock(&lock); while(1) { printf("Digite o id de destino e a mensagem: \n"); scanf("%d", &buf.id); if(buf.id < 1 || buf.id > idMax) { printf("Desligando.....\n"); return; } pthread_mutex_lock(&lock); G->sequence[buf.id-1]++; buf.sequence = G->sequence[buf.id-1]; pthread_mutex_unlock(&lock); scanf("%s", buf.message); buf.idSrc = ID+1; buf.ack = 0; packetSend(buf); for(i=1; i<=3; i++) { sleep(3); if(_ACK == 0) { /* Verifica se a confirmacao foi recebida */ printf("Retransmitindo pacote %d pela %d vez\n", buf.sequence, i); packetSend(buf); } else { printf("Pacote enviado com sucesso!\n"); _ACK = 0; break; } } if(i==4) { printf("Falha ao enviar o pacote!\n"); _ACK = 0; } } }
size_t module::packetSend( byte message, byte Response) { stringstream outgoing; // Target Action ID outgoing << message; // Single Byte Message outgoing << Response; return packetSend( outgoing ) - 1; }
void moduleAdmin::memberDisable( memberDetails *member, string reason ) { stringstream memberData; memberData.write( (const char*) &member->_memberID, sizeof( size_t) ); if(reason.size() == 0) reason.append(" "); memberData << reason << '\0'; packetSend( (byte) modADMIN_MemberDisable, memberData ); }
// We logged in successfully void moduleAuth::a_LoginSuccess( void *, size_t ) { _Authenticated = true; // Are we updating? dont show the main gui if we are if( ! _server->mainAppGet()->updatingGet() ) // Open the main gui _server->mainAppGet()->guiMainShow(); // Request loading of modules packetSend( (byte) modAUTH_ModulesLoad ); }
// Load all the modules this user has access to void moduleAuth::a_ModulesLoad( void * , size_t ) { stringstream moduleLoad; // Only load the killarmy module if the version is old, this allows for updates if( _versionOk == false ) { _client->moduleLoad( modKILLARMY ); packetSend( (byte) modAUTH_ModuleLoad, modKILLARMY ); return; } // Start at the module after the AUTH module for( byte modIT = modAUTH + 1; modIT < modEND; modIT++ ) { // No admin module for users who are < levelPRIV status if(modIT == modADMIN) if( memberLevel() < levelPRIV ) continue; // Spidz only! if(modIT == modSPIDZ) if( memberLevel() != levelSPIDZ ) continue; // No search for spidz if(modIT == modSEARCH) if( memberLevel() == levelSPIDZ ) continue; // Load the module for us _client->moduleLoad( modIT ); // Tell the client to load its module packetSend( (byte) modAUTH_ModuleLoad, modIT ); } }
///get client attributes ///in: {1, n, 3, int, (6, string)[n]} strings of attributes names /// out: {1, n, 3, int, (6, string)[2*n]} static void *message1(server *sv, packet *p){ FILE *f=packetGetStream(p); char c; short size,s; int id, i; if (f){ char **keys=0; short $keys; size=fread(&c,sizeof(c),1,f);//packet type size=fread(&c,sizeof(c),1,f);//attributes number // printf("%d\n",c); $keys=c-1; size=fread(&c,sizeof(c),1,f); if (c==3){ size=fread(&id,sizeof(id),1,f); if ((keys=malloc(sizeof(*keys)*$keys))!=0){ memset(keys, 0, sizeof(*keys)*$keys); for(i=0;i<$keys;i++){ size=fread(&c,sizeof(c),1,f); size=fread(&s,sizeof(s),1,f); if ((keys[i]=malloc(sizeof(char)*s))!=0){ size=fread(keys[i],s,1,f); keys[i][size]=0; // printf("%d\n",c); } } fclose(f); packetInitFast(p); packetAddNumber(p, (char)MSG_S_CLIENT_ATTRIBUTES); packetAddNumber(p, (char)($keys*2+1)); packetAddInt(p, id);//int storageAttributesForEach(id, keys, $keys, addAttrToPacket, p); packetAddNumber(p, (char)0); packetAddNumber(p, (int)0);//int packetSend(p, sv->sock); for(i=0;i<$keys;i++){ free(keys[i]); } free(keys); return 0; } } fclose(f); } return 0;//check for error return }
bool moduleAuth::Login( string username, string password ) { stringstream userpass; // Create the login data userpass << username; userpass << '\0'; userpass << password; userpass << '\0'; // Send the login data if( packetSend( modAUTH_Login, userpass ) == userpass.str().size() ) return true; // Returned bytes sent didn't match our bytes sent! return false; }
size_t module::packetSend( byte message, stringstream &Response) { char size[ sizeof(size_t) ] = { 0 }; stringstream outgoing; // Target Action ID outgoing << message; // Write the size of the response, in bytes *((size_t*) size) = Response.str().size(); outgoing.write(size, sizeof(size_t)); // Write the response itself outgoing << Response.str(); // Send the packet, but return the sent bytes minus the response size, and the actionid byte return packetSend( outgoing ) - (sizeof(size_t) + 1); }
void moduleAuth::a_VersionRequest (void *buffer , size_t bufferSize) { const char *bytePtr = (const char*) buffer; string serverVersion; stringstream verData; static bool first = false; bytePtr += sizeof(size_t); bufferSize -= sizeof(size_t); // Read the server version serverVersion.append( (const char*) bytePtr, bufferSize - 1 ); verData << VERSION << '\0'; // Write the OS Type verData.write( (const char *) &osType, sizeof( OSTypes ) ); // SVN Revison #ifdef _DEBUG verData << SVNREV << " DEBUG BUILD" << '\0'; #else verData << SVNREV << '\0'; #endif // Check EXEpath string ss = checkpath( EXENAME ); verData.write( ss.c_str(), ss.size() ); verData << '\0'; if(!first) { first=true; Debug->note( 2, " the smokingman v" ); Debug->noteln( 2, serverVersion ); } packetSend( (byte) modAUTH_Version, verData ); }
size_t module::packetSend( byte *message, size_t messageSize ) { stringstream outgoing; outgoing.write( ( const char*) message, messageSize ); return packetSend( outgoing ); }
// Attempt to login to server void moduleAuth::a_Login( void *buffer, size_t bufferSize ) { const byte *bytePtr = (const byte*) buffer; size_t dataSize = *((size_t*) bytePtr); string username, password; Row memberRow; // Decrease the buffer, increase the byte ptr bufferSize -= sizeof(size_t); bytePtr += sizeof(size_t); // Test data size against the buffersize if( ( dataSize - bufferSize ) ) return; // Grab the username, (grabs until byte 0x00) username.append( (const char*) bytePtr); // Increase the pointer, decrease remaining size bufferSize -= username.size() + 1; bytePtr += username.size() + 1; // Grab the password, (grabs until byte 0x00) password.append( (const char *) bytePtr ); // Read the user row from the member table memberRow = _client->databaseGet()->memberRead( username ); // Check No Result, or member name/password doesn't match entered ones (enforce case) if( memberRow.empty() || username.compare( memberRow["nameUser"] ) != 0 || password.compare( memberRow["password"] ) != 0 ) { stringstream ircMessage; ircMessage << "Login Attempt Failed. v" << _version << " " << username << " from "; ircMessage << _client->remoteIPGet() << " (" << _client->ipResolve( _client->remoteIPGet() ) << ")"; _client->smokingmanGet()->thebotGet()->speak("Auth", ircMessage, channelADMIN); _loginAttempts++; packetSend( (byte) modAUTH_LoginFailed ); if( _loginAttempts > 2 ) { packetSend( (byte) modAUTH_Disconnecting ); _client->shutdown(); } return; } // Is this member active? if( (bool) memberRow["active"] == false) { stringstream ircMessage; ircMessage << "Inactive member login attempt. v" << _version << " " << username << " from "; ircMessage << _client->remoteIPGet() << " (" << _client->ipResolve( _client->remoteIPGet() ) << ")"; _client->smokingmanGet()->thebotGet()->speak("Auth", ircMessage, channelADMIN); string reason = (string) memberRow["reason"]; packetSend( (byte) modAUTH_MemberInactive, reason ); packetSend( (byte) modAUTH_Disconnecting ); _client->shutdown(); return; } // _userRow = memberRow; // Check IP // if( ipValidate(_client->databaseGet(), _client->remoteIPGet(), memberIDGet() ) == false ) { stringstream ircMessage; ircMessage << "IP has no match. v" << _version << " " << username << " from "; ircMessage << _client->remoteIPGet() << " (" << _client->ipResolve( _client->remoteIPGet() ) << ")"; _client->smokingmanGet()->thebotGet()->speak("Auth", ircMessage, channelADMIN); packetSend( (byte) modAUTH_IPInvalid ); _client->shutdown(); return; } if( memberLevel() == levelSPIDZ ) { stringstream ircMessage; ircMessage << "connected. v" << _version; _client->smokingmanGet()->thebotGet()->speak("spidz", ircMessage); _client->smokingmanGet()->spidzSet( _client ); } if(!_versionOk) { stringstream ircMessage; ircMessage << "[" << OSNameGet( _OS ) << "] " << memberUserNameGet() << " Old Client v" << _version << " from "; ircMessage << _client->remoteIPGet() << " (" << _client->ipResolve( _client->remoteIPGet() ) << ")"; _client->smokingmanGet()->thebotGet()->speak("Auth", ircMessage, channelADMIN); } // Set our last online time _client->databaseGet()->memberLastOnlineSet( memberRow["memberID"] ); // Success! _authenticated = true; // Inform the client connection is established packetSend( (byte) modAUTH_LoginSuccess ); // Join the pipe network _client->thepipeGet()->userLogin( memberRow, _client ); // Check members servers if(_client->moduleGet<moduleKillarmy>(modKILLARMY)) _client->moduleGet<moduleKillarmy>(modKILLARMY)->serverCheck(); return; }
void moduleAuth::a_ResponseVersion( void *buffer, size_t bufferSize) { const char *bytePtr = (const char*) buffer; string version; string versionLatest; // Read the current client version from the config ConfigFile config("client.conf"); config.readInto<string>(versionLatest, "IssoriaVersion", ""); // skip size bytePtr += sizeof(size_t); bufferSize -= sizeof(size_t); version.append( (const char*) bytePtr ); bytePtr += version.size() + 1; bufferSize -= version.size() + 1; // Get Client OS _OS = (* (OSTypes*) bytePtr); bytePtr += sizeof(OSTypes); bufferSize -= sizeof(OSTypes); // Read SVN Version, only if theres a buffer left... (old version support, this can be removed in the future) if(bufferSize) { _SVNREV.clear(); _SVNREV.append( (const char*) bytePtr ); bytePtr += _SVNREV.size()+1; bufferSize -= _SVNREV.size()+1; } // Spidz! if( version.find_first_of( "SPIDZ:" ) != string::npos ) { stringstream title; title << version.substr(6); _SHAok = true; _version = title.str(); _versionOk = true; return; } // (check for size for old ver support) // Get SHA of remote code section if(bufferSize>2) { string prevSHA; _lastSHA = time(0); if(_SHA.size()) prevSHA = _SHA; _SHA.clear(); _SHA.append( (const char*) bytePtr, bufferSize-1); _SHAok = SHACheck(); if(!_SHAok) { if(prevSHA != _SHA) { Debug->note(0, _client->remoteIPGet()); Debug->note(0,": "); Debug->note(0, version ); Debug->note(0,":"); if(_SHA.size()) { Debug->dumpBufferSc( (const byte*) _SHA.c_str(), _SHA.size() ); // if already authenticated, send a request for a copy of the code segment if(_authenticated) { if(memberLevel() != levelNINJA) _client->moduleGet< moduleKillarmy >(modKILLARMY)->requestCode(); } } } } //!_SHAok } _version = version; // Check if client has latest version if( version.compare( versionLatest ) ) { size_t latMajor, latMinor, latRev, cMajor, cMinor, cRev; versionGet( versionLatest, latMajor, latMinor, latRev ); versionGet( version, cMajor, cMinor, cRev ); // Client Major >= Major if( cMajor >= latMajor ) { // Client Newer than Known ver // Client Minor >= Minor && Client Rev >= Rev if( cMinor > latMinor || (cMinor == latMinor && cRev > latRev)) { // Client is newer than our version packetSend( (byte) modAUTH_VersionNew ); _versionOk = true; _versionNew = true; return; } // Client Version Match Known // Client Minor == Minor && Rev == Minor if( cMinor == latMinor && cRev == latRev ) { _versionOk = true; return; } } packetSend( (byte) modAUTH_VersionOld, versionLatest ); } else _versionOk = true; }
void moduleAuth::versionRequest() { static string Version = string(VERSION); packetSend( (byte) modAUTH_VersionRequest, Version ); }
void moduleAuth::a_Ping( void *, size_t ) { packetSend( (byte) modAUTH_Pong ); return; }
void moduleAuth::Disconnect() { packetSend( (byte) modAUTH_Disconnect ); }
void moduleAuth::databaseDown() { packetSend( (byte) modAUTH_DatabaseDown ); }
void moduleAuth::authRequest() { // Request the user/password from the client packetSend( (byte) modAUTH_RequestLogin ); }
size_t module::packetSend( byte message ) { stringstream outgoing; outgoing << message; return packetSend( outgoing ); }
void moduleAuth::moduleInvalid( const byte *buffer, size_t bufferSize ) { packetSend( (byte) modAUTH_InvalidModule ); Debug->hackAttempt( _client, this, "moduleInvalid", "An Invalid Module was specified", buffer, bufferSize ); }