Changeset 595 for library/trunk

Show
Ignore:
Timestamp:
07/29/06 22:58:33 (2 years ago)
Author:
common
Message:

library

  • need to sync local changes with svn before i can svn copy the changes, else you get the message 'svn: Invalid change ordering: new node revision ID without delete'
    • ignore this commit
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • library/trunk/library-core/include/Library.hpp

    r485 r595  
    116116        class GeoLocationManager; 
    117117        class SQLManager; 
     118        class SampleManager; 
    118119 
    119120 
     
    143144#endif  
    144145                virtual SQLManager                      *getSQLMgr(); 
     146                virtual SampleManager           *getSampleMgr(); 
    145147 
    146148                virtual bool                            doLoop(); 
     
    161163                Utilities           *m_Utilities; 
    162164                SQLManager                      *m_SQLManager; 
    163                  
     165                SampleManager          *m_SampleManager; 
    164166                 
    165167                 
  • library/trunk/library-core/src/Library.cpp

    r485 r595  
    6363#include "GeoLocationManager.hpp" 
    6464#include "SQLManager.hpp" 
    65  
     65#include "SampleManager.hpp" 
    6666 
    6767#ifdef STDTAGS 
     
    393393                        m_DNSManager        = new DNSManager(this); 
    394394                        m_SQLManager            = new SQLManager(this); 
     395                        m_SampleManager         = new SampleManager(this); 
    395396                } 
    396397        } 
     
    535536            m_SocketManager->doList(); 
    536537                } 
     538 
    537539 
    538540 
     
    779781} 
    780782 
     783SampleManager *Library::getSampleMgr() 
     784{ 
     785        return m_SampleManager; 
     786} 
    781787 
    782788 
  • library/trunk/library-core/src/Makefile.am

    r474 r595  
    2727library_SOURCES += Utilities.cpp 
    2828library_SOURCES += SQLManager.cpp SQLHandler.cpp SQLQuery.cpp SQLResult.cpp SQLCallback.cpp 
     29library_SOURCES += SampleManager.cpp SampleManager.hpp 
    2930 
    3031 
  • library/trunk/modules/service-gotek2/Makefile.am

    r513 r595  
    1212 
    1313servicegotek2_la_SOURCES = service-gotek2.cpp service-gotek2.hpp 
     14servicegotek2_la_SOURCES += GotekDialogue.cpp GotekDialogue.hpp 
    1415servicegotek2_la_SOURCES += GotekPacket.cpp GotekPacket.hpp 
    1516servicegotek2_la_SOURCES += GotekException.cpp GotekException.hpp 
  • library/trunk/modules/service-gotek2/service-gotek2.cpp

    r514 r595  
    3232 
    3333#include "service-gotek2.hpp" 
     34#include "GotekDialogue.hpp" 
    3435 
    3536#include "GotekPacket.hpp" 
     
    4344 
    4445#include "LogManager.hpp" 
    45  
    46 #include "Buffer.hpp" 
    47 #include "Buffer.cpp" 
    48  
    49 #include "Message.hpp" 
    50 #include "Message.cpp" 
    5146 
    5247#include "Config.hpp" 
     
    8277 
    8378 
     79ServiceGotek2 *g_ServiceGotek2; 
    8480 
    8581char *library::mapEnum(GotekPacketType t) 
    8682{ 
    87         static uint8_t  GotekPacketTypeEnumMapping; 
    88         GotekPacketTypeEnumMapping = (uint8_t) t; 
    89         return (char *)&GotekPacketTypeEnumMapping; 
     83        static uint8_t  GotekPacketTypeEnumMapping[5]; 
     84        memset(GotekPacketTypeEnumMapping,0,5); 
     85 
     86        GotekPacketTypeEnumMapping[0] = (uint8_t) t; 
     87        return (char *)GotekPacketTypeEnumMapping; 
    9088} 
    9189 
     
    119117 
    120118        g_Library = library; 
     119        g_ServiceGotek2 = this; 
    121120} 
    122121 
     
    137136bool ServiceGotek2::Init() 
    138137{ 
    139         m_Library->getSocketMgr()->bindTCPSocket(0,34109,0,300,this); 
     138        m_Library->getSocketMgr()->bindTCPSocket(0,34109,0,10,this); 
    140139        return true; 
    141140} 
     
    161160 
    162161 
    163  
    164  
    165  
    166  
    167  
    168 /** 
    169  * Dialogue::Dialogue(Socket *) 
    170  * construktor for the ServiceGotek2Dialogue, creates a new ServiceGotek2Dialogue 
    171  *  
    172  * replies some crap to the socket 
    173  *  
    174  * @param socket the Socket the Dialogue has to use 
    175  */ 
    176 ServiceGotek2Dialogue::ServiceGotek2Dialogue(Socket *socket) 
    177 
    178         m_Socket = socket; 
    179     m_DialogueName = "ServiceGotek2Dialogue"; 
    180         m_DialogueDescription = "talking to gotek clients"; 
    181  
    182         m_ConsumeLevel = CL_ASSIGN; 
    183  
    184         m_ClientType    = GCT_LOGIN; 
    185  
    186  
    187         GotekPacket *gp = GotekPacket::createWritePacket(SCMD_WELCOME); 
    188 // protocol version 
    189         gp->writeUInt8(2);       
    190         gp->writeUInt8(0); 
    191  
    192 // session key 
    193         m_SessionKey = (((unsigned long long) time(0)) << 48) | (((unsigned long long) (rand() & 0xFF)) << 32); 
    194         m_SessionKey |= (unsigned long long) rand(); 
    195         gp->writeRaw((void *)&m_SessionKey,8); 
    196  
    197 // server version 
    198         gp->writeString(string("library going gotek 2.0")); 
    199  
    200 // server description 
    201         gp->writeString(string("no description yet")); 
    202  
    203         m_Socket->doWrite((char *)gp->buffer().c_str(),gp->size()+5); 
    204  
    205         delete gp; 
    206  
    207 
    208  
    209 ServiceGotek2Dialogue::~ServiceGotek2Dialogue() 
    210 
    211         while (m_QueryQueue.size() > 0) 
     162void ServiceGotek2::newSample(string &hash) 
     163
     164        logPF(); 
     165        list <ServiceGotek2Dialogue *>::iterator it; 
     166 
     167        for (it = m_Clients.begin(); it != m_Clients.end(); it++) 
    212168        { 
    213                 m_QueryQueue.front()->cancelCallback(); 
    214                 m_QueryQueue.pop_front(); 
    215         } 
    216 
    217  
    218 /** 
    219  * Dialogue::incomingData(Message *) 
    220  *  
    221  * a small and ugly shell where we can use 
    222  * "download protocol://localction:port/path/to/file 
    223  * to trigger a download 
    224  *  
    225  * @param msg the Message the Socker received. 
    226  *  
    227  *  
    228  * @return CL_ASSIGN 
    229  */ 
    230 ConsumeLevel ServiceGotek2Dialogue::incomingData(Message *msg) 
    231 
    232         logPF(); 
    233         m_Buffer.append(msg->getMsg(),msg->getSize()); 
    234     return process(); 
    235 
    236  
    237  
    238  
    239  
    240 ConsumeLevel ServiceGotek2Dialogue::process() 
    241 
    242         logPF(); 
    243         ConsumeLevel cl = CL_ASSIGN; 
    244         bool needmoredata=false; 
    245         while ( needmoredata == false && cl == CL_ASSIGN ) 
    246         { 
    247                 switch ( m_ClientType ) 
     169                if ( (*it)->m_Notifications.size() == 1 && (*it)->m_Notifications.front() == "sample" ) 
    248170                { 
    249                 case GCT_LOGIN: 
    250                         cl= processLOGIN(&needmoredata); 
    251                         break; 
    252  
    253                 case GCT_CONTROL: 
    254                         cl=processCTRL(&needmoredata); 
    255                         break; 
    256  
    257                 case GCT_DATA: 
    258                         cl=processDATA(&needmoredata); 
    259                         break; 
     171                        logInfo("sending \"sample\" notification to %x \n",*it); 
     172 
     173                        Socket *sock = (*it)->getSocket(); 
     174                        GotekPacket *gp = GotekPacket::createWritePacket(SCMD_NOTIFY); 
     175// hash 
     176                        gp->writeRaw(hash.data(),hash.size()); 
     177// one magnet 
     178                        gp->writeUInt32(1); 
     179 
     180// 'magnet name' 
     181                        gp->writeString(string("sample")); 
     182 
     183// magnet payload, the hash again 
     184                        gp->writeUInt16(64); 
     185                        gp->writeRaw(hash.data(),hash.size()); 
     186 
     187                        sock->doWrite((char *)gp->buffer().c_str(),gp->size()+5); 
     188 
     189                        delete gp; 
    260190                } 
    261191        } 
    262         logSpam("return %i\n",cl); 
    263         return cl; 
    264 
    265  
    266 ConsumeLevel ServiceGotek2Dialogue::processLOGIN(bool *needmoredata) 
    267 
    268         logPF(); 
    269 // verify the packet is complete 
    270  
    271         if (m_Buffer.size() < 5) 
     192 
     193
     194void ServiceGotek2::newMagnet(string &hash) 
     195
     196        logPF(); 
     197
     198 
     199bool ServiceGotek2::sqlSuccess(SQLResult *result) 
     200
     201        logPF(); 
     202        return true; 
     203
     204 
     205bool ServiceGotek2::sqlFailure(SQLResult *result) 
     206
     207        logPF(); 
     208        return true; 
     209
     210 
     211void ServiceGotek2::addClient(ServiceGotek2Dialogue *client) 
     212
     213        logPF(); 
     214        m_Clients.push_back(client); 
     215
     216 
     217void ServiceGotek2::delClient(ServiceGotek2Dialogue *client) 
     218
     219        logPF(); 
     220        list <ServiceGotek2Dialogue *>::iterator it; 
     221 
     222        for (it = m_Clients.begin(); it != m_Clients.end(); it++) 
    272223        { 
    273                 *needmoredata = true; 
    274                 return CL_ASSIGN; 
    275         } 
    276  
    277         GotekPacket *gp = GotekPacket::createReadPacket(m_Buffer); 
    278  
    279         uint8_t command = gp->readUInt8(); 
    280         if ( command != (uint8_t)CCMD_LOGIN ) 
    281         { 
    282                 logInfo("Invalid CCMD_LOGIN %x\n",command); 
    283                 return CL_DROP; 
    284         } 
    285  
    286         uint32_t gotekpacketsize = gp->readUInt32(); 
    287         if (m_Buffer.size() <  gotekpacketsize + 5 ) 
    288         { 
    289                 *needmoredata = true; 
    290                 return CL_ASSIGN; 
    291         } 
    292  
    293      
    294 // parse the data 
    295         string hash; 
    296          
    297         try 
    298         { 
    299                 gp->readString(); 
    300                 gp->readString(); 
    301                 m_Username = gp->readString(); 
    302                 hash = gp->readRaw(64); 
    303                 command = gp->readUInt8(); 
    304         } 
    305         catch ( GotekException e ) 
    306         { 
    307                 delete gp; 
    308                 return CL_DROP; 
    309         } 
    310         delete gp; 
    311  
    312 // verfiy the data is sane 
    313         if ( command != CTYPE_CTRL && command != CTYPE_DATA ) 
    314         { 
    315                 return CL_DROP; 
    316         } 
    317  
    318         for ( char * c = (char *)m_Username.c_str(); * c; ++c ) 
    319                  if ( !isalpha(* c) && * c != '.' && * c != '-' ) 
    320                          return CL_DROP; 
    321  
    322 // create the query context so we know which query returns, so we can answer appropriate 
    323  
    324         SQLContext *sqlctx = new SQLContext(command,time(NULL),&hash); 
    325  
    326 // query  
    327          string query = "SELECT \"authkey\" FROM \"users-dp\".\"users\" WHERE username = '"; 
    328          query  += m_Username.c_str(); 
    329          query  += "';"; 
    330  
    331          SQLQuery *sqlquery = g_Library->getSQLMgr()->addQuery(&query,this,(void *)sqlctx); 
    332          m_QueryQueue.push_back(sqlquery); 
    333  
    334          m_Buffer.erase(0,gotekpacketsize+5); 
    335          return CL_ASSIGN; 
    336 
    337  
    338  
    339  
    340  
    341  
    342 ConsumeLevel ServiceGotek2Dialogue::processCTRL(bool *needmoredata) 
    343 
    344         logPF();  
    345  
    346         if (m_Buffer.size() < 1 ) 
    347         { 
    348                 *needmoredata = true; 
    349                 return CL_ASSIGN; 
    350         } 
    351  
    352         if (m_Buffer[0] == CCMD_NOOP) 
    353         { 
    354         m_Buffer.erase(0,1); 
    355                 return CL_ASSIGN; 
    356         } 
    357  
    358         if (m_Buffer.size() < 5) 
    359         { 
    360                 *needmoredata = true; 
    361                 return CL_ASSIGN; 
    362         } 
    363  
    364         ConsumeLevel cl = CL_ASSIGN; 
    365  
    366         GotekPacket *gp = GotekPacket::createReadPacket(m_Buffer); 
    367  
    368         uint8_t command = gp->readUInt8(); 
    369         uint32_t gotekpacketsize = gp->readUInt32(); 
    370  
    371         if (m_Buffer.size() <  + gotekpacketsize + 5  ) 
    372         { 
    373                 *needmoredata = true; 
    374                 delete gp; 
    375                  return CL_ASSIGN; 
    376         } 
    377  
    378         try 
    379         { 
    380                 switch ( command ) 
     224                if (*it == client) 
    381225                { 
    382                 case CCMD_CHECK_PRESENCE: 
    383                         cl = processCTRL_CHECK_PRESENCE(gp); 
    384                         break; 
    385  
    386                 case CCMD_STORE_MAGNET: 
    387                         cl = processCTRL_STORE_MAGNET(gp); 
    388                         break; 
    389  
    390                 case CCMD_REQUEST_NOTIFICATION: 
    391                         cl = processCTRL_REQUEST_NOTIFICATION(gp); 
    392                         break; 
    393  
    394                 case CCMD_REQUEST_SILENCE: 
    395                         cl = processCTRL_REQUEST_SILENCE(gp); 
    396                         break; 
    397  
    398                 case CCMD_REPORT_INSTANCE: 
    399                         cl = processCTRL_REPORT_INSTANCE(gp); 
    400                         break; 
     226                        m_Clients.erase(it); 
     227                        return; 
    401228                } 
    402229        } 
    403         catch ( GotekException e ) 
    404         { 
    405                 return CL_DROP; 
    406         } 
    407  
    408         m_Buffer.erase(0,gotekpacketsize + 5); 
    409  
    410         return cl; 
    411 } 
    412  
    413 ConsumeLevel ServiceGotek2Dialogue::processCTRL_CHECK_PRESENCE(GotekPacket *gp) 
    414 { 
    415         logPF(); 
    416         uint8_t hash_type = gp->readUInt8(); 
    417         if (hash_type != HTYPE_MD5 && hash_type != HTYPE_SHA512) 
    418         { 
    419                 m_Socket->doWrite(mapEnum(RPL_UNSUPPORTED),1); 
    420                 return CL_ASSIGN; 
    421         } 
    422  
    423          
    424 // create the query context so we know which query returns, so we can answer appropriate 
    425  
    426         SQLContext *sqlctx = new SQLContext(CCMD_CHECK_PRESENCE,time(NULL)); 
    427  
    428         string hash; 
    429         switch(hash_type) 
    430         { 
    431         case HTYPE_SHA512: 
    432                 { 
    433                         string hash = gp->readRaw(64); 
    434                         hash = SQL_ESCAPE_BINARY(&hash); 
    435                         string query = "SELECT \"sha512_hash\" FROM \"users-dp\".\"malware\" WHERE sha512_hash = '" + hash +"';"; 
    436                         SQLQuery *sqlquery = g_Library->getSQLMgr()->addQuery(&query,this,(void *)sqlctx); 
    437                         m_QueryQueue.push_back(sqlquery); 
    438                 } 
    439                 break; 
    440  
    441         case HTYPE_MD5: 
    442                 { 
    443                         string hash = gp->readRaw(16); 
    444                         hash = SQL_ESCAPE_BINARY(&hash); 
    445                         string query = "SELECT \"sha512_hash\" FROM \"users-dp\".\"malware\" WHERE sha512_hash = '" + hash +"';"; 
    446                         SQLQuery *sqlquery = g_Library->getSQLMgr()->addQuery(&query,this,(void *)sqlctx); 
    447                         m_QueryQueue.push_back(sqlquery); 
    448                 } 
    449                 break; 
    450  
    451         } 
    452  
    453         return CL_ASSIGN; 
    454 } 
    455  
    456 ConsumeLevel ServiceGotek2Dialogue::processCTRL_STORE_MAGNET(GotekPacket *gp) 
    457 { 
    458         logPF(); 
    459         string hash = gp->readRaw(64); 
    460         uint8_t writemode = gp->readUInt8(); 
    461         string name = gp->readString(); 
    462         uint16_t valuesize = gp->readUInt16(); 
    463         string value = gp->readRaw(valuesize); 
    464  
    465         name = SQL_ESCAPE_STRING(&name); 
    466         hash = SQL_ESCAPE_BINARY(&hash); 
    467         value = SQL_ESCAPE_STRING(&value); 
    468  
    469         writemode = 0; 
    470  
    471         SQLContext *sqlctx = new SQLContext(CCMD_STORE_MAGNET,time(NULL)); 
    472  
    473         string query = "INSERT INTO \"users-dp\".\"magnets\" (\"sha512_hash\",\"name\",\"value\") VALUES ('" + hash + "','" + name + "','" + value + "')"; 
    474  
    475         SQLQuery *sqlquery = g_Library->getSQLMgr()->addQuery(&query,this,(void *)sqlctx); 
    476         m_QueryQueue.push_back(sqlquery); 
    477  
    478  
    479         return CL_ASSIGN; 
    480 } 
    481  
    482 ConsumeLevel ServiceGotek2Dialogue::processCTRL_REQUEST_NOTIFICATION(GotekPacket *gp) 
    483 { 
    484         logPF();  
    485         return CL_DROP; 
    486 } 
    487  
    488 ConsumeLevel ServiceGotek2Dialogue::processCTRL_REQUEST_SILENCE(GotekPacket *gp) 
    489 { 
    490         logPF();  
    491         return CL_DROP; 
    492 } 
    493  
    494 ConsumeLevel ServiceGotek2Dialogue::processCTRL_REPORT_INSTANCE(GotekPacket *gp) 
    495 { 
    496         logPF();  
    497         string hash = gp->readRaw(64); 
    498         hash = SQL_ESCAPE_BINARY(&hash); 
    499  
    500         uint16_t num_magnets = gp->readUInt16(); 
    501  
    502         uint16_t i; 
    503  
    504         string insert_names; 
    505         string insert_values; 
    506  
    507         for (i=0;i<num_magnets;i++) 
    508         { 
    509  
    510                 string name = gp->readString(); 
    511                 name = SQL_ESCAPE_STRING(&name); 
    512  
    513                 insert_names += "\"" + name + "\""; 
    514  
    515                 if (i+1 < num_magnets ) 
    516                 { 
    517                         insert_names += ","; 
    518                 } 
    519  
    520  
    521                 uint16_t valuesize = gp->readUInt16(); 
    522                 string value = gp->readRaw(valuesize); 
    523         value = SQL_ESCAPE_STRING(&value); 
    524  
    525                 insert_values += "'" + value + "'"; 
    526  
    527                 if (i+1 < num_magnets ) 
    528                 { 
    529                         insert_values += ","; 
    530                 } 
    531         } 
    532  
    533  
    534         string query = "INSERT INTO \"users-dp\".\"instances\" (" + insert_names + ") VALUES (" + insert_values + ")"; 
    535  
    536  
    537         SQLContext *sqlctx = new SQLContext(CCMD_REPORT_INSTANCE,time(NULL)); 
    538  
    539         SQLQuery *sqlquery = g_Library->getSQLMgr()->addQuery(&query,this,sqlctx); 
    540         m_QueryQueue.push_back(sqlquery); 
    541  
    542         return CL_ASSIGN; 
    543 } 
    544  
    545  
    546  
    547  
    548 ConsumeLevel ServiceGotek2Dialogue::processDATA(bool *needmoredata) 
    549 { 
    550         logPF();  
    551         return CL_DROP; 
    552 } 
    553  
    554 ConsumeLevel ServiceGotek2Dialogue::processDATA_MAGNET(GotekPacket *gp) 
    555 { 
    556         logPF();  
    557         return CL_DROP; 
    558 } 
    559  
    560 ConsumeLevel ServiceGotek2Dialogue::processDATA_SAMPLE_GET(GotekPacket *gp) 
    561 { 
    562         logPF();  
    563         return CL_DROP; 
    564 } 
    565  
    566 ConsumeLevel ServiceGotek2Dialogue::processDATA_SAMPLE_ADD(GotekPacket *gp) 
    567 { 
    568         logPF();  
    569         return CL_DROP; 
    570 }      
    571  
    572 ConsumeLevel ServiceGotek2Dialogue::processDATA_SAMPLE_ADD_GZIP(GotekPacket *gp) 
    573 { 
    574         logPF();  
    575         return CL_DROP; 
    576 } 
    577  
    578  
    579  
    580 /** 
    581  * Dialogue::handleTimeout(Message *) 
    582  * as we are not interested in these socket actions  
    583  * we simply return CL_DROP to show the socket 
    584  *  
    585  * @param msg 
    586  *  
    587  * @return CL_DROP 
    588  */ 
    589 ConsumeLevel ServiceGotek2Dialogue::handleTimeout(Message *msg) 
    590 { 
    591         return CL_DROP; 
    592 } 
    593  
    594 /** 
    595  * Dialogue::connectionLost(Message *) 
    596  * as we are not interested in these socket actions  
    597  * we simply return CL_DROP to show the socket 
    598  *  
    599  * @param msg 
    600  *  
    601  * @return CL_DROP 
    602  */ 
    603 ConsumeLevel ServiceGotek2Dialogue::connectionLost(Message *msg) 
    604 { 
    605         return CL_DROP; 
    606 } 
    607  
    608 /** 
    609  * Dialogue::connectionShutdown(Message *) 
    610  * as we are not interested in these socket actions  
    611  * we simply return CL_DROP to show the socket 
    612  *  
    613  * @param msg 
    614  *  
    615  * @return CL_DROP 
    616  */ 
    617 ConsumeLevel ServiceGotek2Dialogue::connectionShutdown(Message *msg) 
    618 { 
    619         return CL_DROP; 
    620 } 
    621  
    622  
    623 bool ServiceGotek2Dialogue::sqlSuccess(SQLResult *sqlresult) 
    624 { 
    625         logPF(); 
    626  
    627  
    628         if ( m_QueryQueue.size() == 0 ) 
    629         { 
    630                 logCrit("unexpected Query callback for query %s context %x\n",sqlresult->getQuery().c_str(), sqlresult->getObject()); 
    631                 m_Socket->setStatus(SS_CLOSED); 
    632                 return false; 
    633         } 
    634  
    635         bool ret = false; 
    636  
    637         switch ( m_ClientType ) 
    638         { 
    639         case GCT_LOGIN: 
    640                 ret = sqlSuccessLOGIN(sqlresult); 
    641                 break; 
    642  
    643         case GCT_CONTROL: 
    644                 ret = sqlSuccessCTRL(sqlresult); 
    645                 break; 
    646  
    647         case GCT_DATA: 
    648                 ret = sqlSuccessDATA(sqlresult); 
    649                 break; 
    650         } 
    651  
    652         m_QueryQueue.pop_front(); 
    653         return ret; 
    654 } 
    655  
    656  
    657 bool ServiceGotek2Dialogue::sqlSuccessLOGIN(SQLResult *sqlresult) 
    658 { 
    659         logPF(); 
    660         vector< map<string,string> > result = *sqlresult->getResult(); 
    661  
    662         SQLContext *sqlctx = (SQLContext *)sqlresult->getObject(); 
    663  
    664         if ( result.size() == 1 ) 
    665         { 
    666                 string bkey(result[0]["authkey"].data(), result[0]["authkey"].size()); 
    667                 bkey.append((char *)&m_SessionKey,8); 
    668  
    669  
    670                 unsigned char hash[64]; 
    671                 g_Library->getUtilities()->sha512((unsigned char *)bkey.c_str(),bkey.size(),hash); 
    672                  
    673                 if ( memcmp(sqlctx->getString().c_str(),hash,64) == 0 ) 
    674                 { 
    675                          
    676                         switch ( sqlctx->getCommand() ) 
    677                         { 
    678                         case CTYPE_CTRL: 
    679                                 logInfo("Login success (ctrl) :%s\n",m_Username.c_str()); 
    680                                 m_ClientType = GCT_CONTROL; 
    681                                 break; 
    682                         case CTYPE_DATA: 
    683                                 logInfo("Login success (data) :%s\n",m_Username.c_str()); 
    684                                 m_ClientType = GCT_DATA; 
    685                                 break; 
    686                         default: 
    687                                 logCrit("Login error, invalid connection type requested %0x\n",sqlctx->getCommand()); 
    688                         } 
    689                         m_Socket->doWrite(mapEnum(RPL_AUTHORIZED),1); 
    690  
    691                 }else 
    692                 { 
    693                         // invalid login 
    694                         logInfo("Login failed: %s\n",m_Username.c_str()); 
    695                         m_Socket->doWrite(mapEnum(RPL_DENIED),1); 
    696                         m_Socket->setStatus(SS_CLEANQUIT); 
    697                 } 
    698  
    699         } else 
    700         { 
    701                 // invalid login 
    702                 logInfo("Login failed: %s\n",m_Username.c_str()); 
    703                 m_Socket->doWrite(mapEnum(RPL_DENIED),1); 
    704                 m_Socket->setStatus(SS_CLEANQUIT); 
    705         } 
    706  
    707         delete sqlctx; 
    708  
    709  
    710         return true; 
    711 } 
    712  
    713 bool ServiceGotek2Dialogue::sqlSuccessCTRL(SQLResult *sqlresult) 
    714 { 
    715         logPF(); 
    716         bool retval=false; 
    717         SQLContext *sqlctx = (SQLContext *)sqlresult->getObject(); 
    718         switch (sqlctx->getCommand()) 
    719         { 
    720         case CCMD_CHECK_PRESENCE: 
    721                 retval = sqlSuccessCTRL_CHECK_PRESENCE(sqlresult); 
    722                 break; 
    723  
    724         case CCMD_STORE_MAGNET: 
    725                 retval = sqlSuccessCTRL_STORE_MAGNET(sqlresult); 
    726                 break; 
    727  
    728         case CCMD_REPORT_INSTANCE: 
    729                 retval = sqlSuccessCTRL_REPORT_INSTANCE(sqlresult); 
    730                 break; 
    731         } 
    732  
    733         delete sqlctx; 
    734         return true; 
    735 } 
    736  
    737  
    738 bool ServiceGotek2Dialogue::sqlSuccessCTRL_CHECK_PRESENCE(SQLResult *sqlresult) 
    739 { 
    740         logPF(); 
    741         vector< map<string,string> > result = *sqlresult->getResult(); 
    742  
    743         if (result.size() >= 1) 
    744         { 
    745                 m_Socket->doWrite(mapEnum(RPL_PRESENT),1); 
    746         }else 
    747         { 
    748                 m_Socket->doWrite(mapEnum(RPL_NPRESENT),1); 
    749         } 
    750         return true; 
    751 } 
    752  
    753 bool ServiceGotek2Dialogue::sqlSuccessCTRL_STORE_MAGNET(SQLResult *sqlresult) 
    754 { 
    755         logPF(); 
    756         m_Socket->doWrite(mapEnum(RPL_ACK),1); 
    757         return true; 
    758 } 
    759  
    760 bool ServiceGotek2Dialogue::sqlSuccessCTRL_REPORT_INSTANCE(SQLResult *sqlresult) 
    761 { 
    762         logPF(); 
    763         m_Socket->doWrite(mapEnum(RPL_ACK),1); 
    764         return true; 
    765 } 
    766  
    767  
    768  
    769  
    770  
    771  
    772  
    773 bool ServiceGotek2Dialogue::sqlSuccessDATA(SQLResult *sqlresult) 
    774 { 
    775         logPF(); 
    776  
    777         return true; 
    778 } 
    779  
    780  
    781  
    782  
    783  
    784  
    785 bool ServiceGotek2Dialogue::sqlFailure(SQLResult *sqlresult) 
    786 { 
    787         logPF(); 
    788  
    789         if ( m_QueryQueue.size() == 0 ) 
    790         { 
    791                 logCrit("unexpected Query callback for query %s\n",sqlresult->getQuery().c_str()); 
    792                 m_Socket->setStatus(SS_CLOSED); 
    793                 return false; 
    794         } 
    795  
    796         bool ret = true; 
    797  
    798         switch ( m_ClientType ) 
    799         { 
    800         case GCT_LOGIN: 
    801                 ret = sqlFailureLOGIN(sqlresult); 
    802                 break; 
    803  
    804         case GCT_CONTROL: 
    805                 ret = sqlFailureCTRL(sqlresult); 
    806                 break; 
    807  
    808         case GCT_DATA: 
    809                 ret = sqlFailureDATA(sqlresult); 
    810                 break; 
    811         } 
    812  
    813         m_QueryQueue.pop_front(); 
    814         return ret; 
    815 } 
    816  
    817  
    818  
    819 bool ServiceGotek2Dialogue::sqlFailureLOGIN(SQLResult *sqlresult) 
    820 { 
    821         logPF(); 
    822  
    823         return true; 
    824 } 
    825  
    826  
    827 bool ServiceGotek2Dialogue::sqlFailureCTRL(SQLResult *sqlresult) 
    828 { 
    829         logPF(); 
    830  
    831         bool retval=false; 
    832  
    833         SQLContext *sqlctx = (SQLContext *)sqlresult->getObject(); 
    834         switch ( sqlctx->getCommand() ) 
    835         { 
    836         case CCMD_STORE_MAGNET: 
    837                 retval = sqlFailureCTRL_STORE_MAGNET(sqlresult); 
    838                 break; 
    839  
    840         case CCMD_REPORT_INSTANCE: 
    841                 retval = sqlFailureCTRL_REPORT_INSTANCE(sqlresult); 
    842                 break; 
    843         } 
    844  
    845         delete sqlctx; 
    846  
    847         return retval; 
    848 } 
    849  
    850 bool ServiceGotek2Dialogue::sqlFailureCTRL_STORE_MAGNET(SQLResult *sqlresult) 
    851 { 
    852         logPF(); 
    853         m_Socket->doWrite(mapEnum(RPL_NACK),1); 
    854         return true; 
    855 } 
    856  
    857 bool ServiceGotek2Dialogue::sqlFailureCTRL_REPORT_INSTANCE(SQLResult *sqlresult) 
    858 { 
    859         logPF(); 
    860         m_Socket->doWrite(mapEnum(RPL_NACK),1); 
    861         return true; 
    862 } 
    863  
    864  
    865 bool ServiceGotek2Dialogue::sqlFailureDATA(SQLResult *sqlresult) 
    866 { 
    867         logPF(); 
    868  
    869         return true; 
    870230} 
    871231 
  • library/trunk/modules/service-gotek2/service-gotek2.hpp

    r514 r595  
    3131#define HAVE_SERVICE_GOTEK2_HPP 
    3232 
     33#include "GotekDialogue.hpp" 
     34 
    3335#include "DialogueFactory.hpp" 
    3436#include "Module.hpp" 
     
    4143#include "SQLCallback.hpp" 
    4244 
     45#include <map> 
    4346 
    4447using namespace std; 
     
    5356        class SQLQuery; 
    5457        class GotekPacket; 
     58        class ServiceGotek2Dialogue; 
    5559 
    56         enum GotekClientType 
    57         { 
    58                 GCT_LOGIN, 
    59         GCT_CONTROL, 
    60             GCT_DATA, 
    61         }; 
    6260 
    6361 
     
    7270                CCMD_REQUEST_SILENCE, 
    7371                CCMD_REPORT_INSTANCE, 
     72                CCMD_MAGNET_ADD, 
     73                CCMD_SAMPLE_GET, 
     74                CCMD_SAMPLE_ADD, 
     75                CCMD_SAMPLE_ADD_GZIP, 
     76 
    7477 
    7578                CTYPE_CTRL, 
     
    7982                SCMD_PING, 
    8083                SCMD_NOTIFY, 
     84                SCMD_SAMPLE_SEND, 
    8185 
    8286                RPL_AUTHORIZED, 
     
    99103                WMODE_NEW, 
    100104                WMODE_OVERWRITE, 
    101                 WMODE_APPEND, 
     105                WMODE_APPEND 
    102106 
    103                 DTYPE_MAGNET, 
    104                 DTYPE_SAMPLE_GET, 
    105                 DTYPE_SAMPLE_ADD, 
    106                 DTYPE_SAMPLE_ADD_GZIP 
    107107        }GotekPacketType; 
    108108 
    109109        char *mapEnum(GotekPacketType t);  
    110110 
    111         class ServiceGotek2 : public Module , public DialogueFactory 
     111        class ServiceGotek2 : public Module , public DialogueFactory, public SQLCallback 
    112112        { 
    113113        public: 
    114114                ServiceGotek2(Library *); 
    115115                ~ServiceGotek2(); 
    116                 Dialogue *createDialogue(Socket *socket); 
    117116                bool Init(); 
    118117                bool Exit(); 
     118 
     119                Dialogue *createDialogue(Socket *socket); 
     120 
     121                void newSample(string &hash); 
     122                void newMagnet(string &hash); 
     123 
     124                bool sqlSuccess(SQLResult *result); 
     125                bool sqlFailure(SQLResult *result); 
     126 
     127                void addClient(ServiceGotek2Dialogue *client); 
     128                void delClient(ServiceGotek2Dialogue *client); 
     129 
     130        protected: 
     131                list <ServiceGotek2Dialogue *>  m_Clients; 
    119132        }; 
    120133 
    121134 
    122         class ServiceGotek2Dialogue : public Dialogue, public SQLCallback 
    123         { 
    124         public: 
    125                 ServiceGotek2Dialogue(Socket *socket); 
    126                 ~ServiceGotek2Dialogue(); 
     135
    127136 
    128                ConsumeLevel incomingData(Message *msg)
     137extern library::ServiceGotek2 *g_ServiceGotek2
    129138 
    130                 ConsumeLevel process(); 
    131  
    132                 ConsumeLevel processLOGIN(bool *needmoredata); 
    133  
    134                 ConsumeLevel processCTRL(bool *needmoredata); 
    135                 ConsumeLevel processCTRL_CHECK_PRESENCE(GotekPacket *gp); 
    136                 ConsumeLevel processCTRL_STORE_MAGNET(GotekPacket *gp); 
    137                 ConsumeLevel processCTRL_REQUEST_NOTIFICATION(GotekPacket *gp); 
    138                 ConsumeLevel processCTRL_REQUEST_SILENCE(GotekPacket *gp); 
    139                 ConsumeLevel processCTRL_REPORT_INSTANCE(GotekPacket *gp); 
    140  
    141                 ConsumeLevel processDATA(bool *needmoredata); 
    142                 ConsumeLevel processDATA_MAGNET(GotekPacket *gp); 
    143                 ConsumeLevel processDATA_SAMPLE_GET(GotekPacket *gp); 
    144                 ConsumeLevel processDATA_SAMPLE_ADD(GotekPacket *gp);        
    145                 ConsumeLevel processDATA_SAMPLE_ADD_GZIP(GotekPacket *gp); 
    146  
    147                 ConsumeLevel handleTimeout(Message *msg); 
    148                 ConsumeLevel connectionLost(Message *msg); 
    149                 ConsumeLevel connectionShutdown(Message *msg); 
    150  
    151  
    152                 bool sqlSuccess(SQLResult *result); 
    153                 bool sqlSuccessLOGIN(SQLResult *result); 
    154  
    155                 bool sqlSuccessCTRL(SQLResult *result); 
    156                 bool sqlSuccessCTRL_CHECK_PRESENCE(SQLResult *result); 
    157                 bool sqlSuccessCTRL_STORE_MAGNET(SQLResult *result); 
    158                 bool sqlSuccessCTRL_REPORT_INSTANCE(SQLResult *result); 
    159  
    160                 bool sqlSuccessDATA(SQLResult *result); 
    161  
    162                 bool sqlFailure(SQLResult *result); 
    163                 bool sqlFailureLOGIN(SQLResult *result); 
    164  
    165                 bool sqlFailureCTRL(SQLResult *result); 
    166                 bool sqlFailureCTRL_STORE_MAGNET(SQLResult *result); 
    167                 bool sqlFailureCTRL_REPORT_INSTANCE(SQLResult *result); 
    168  
    169  
    170                 bool sqlFailureDATA(SQLResult *result); 
    171  
    172         protected: 
    173                 string          m_Buffer; 
    174                 list<SQLQuery *>        m_QueryQueue; 
    175  
    176                 GotekClientType         m_ClientType; 
    177  
    178                 unsigned long long      m_SessionKey; 
    179  
    180                 string                          m_Username; 
    181         }; 
    182  
    183 } 
    184139extern library::Library *g_Library; 
    185140 
  • library/trunk/modules/sqlhandler-postgres/sqlhandler-postgres.cpp

    r514 r595  
    223223 
    224224        vector< map<string,string> >        result; 
     225        bool broken_query=false; 
    225226 
    226227        while ( (res = PQgetResult(m_PGConnection)) != NUL