Mixe for Privacy and Anonymity in the Internet
CAAccountingInstance.hpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000, The JAP-Team
00003 All rights reserved.
00004 Redistribution and use in source and binary forms, with or without modification,
00005 are permitted provided that the following conditions are met:
00006 
00007   - Redistributions of source code must retain the above copyright notice,
00008     this list of conditions and the following disclaimer.
00009 
00010   - Redistributions in binary form must reproduce the above copyright notice,
00011     this list of conditions and the following disclaimer in the documentation and/or
00012     other materials provided with the distribution.
00013 
00014   - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
00015     may be used to endorse or promote products derived from this software without specific
00016     prior written permission.
00017 
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00027 */
00028 
00029 
00030 
00031 #ifndef __CAACCOUNTINGINSTANCE__
00032 #define __CAACCOUNTINGINSTANCE__
00033 #include "doxygen.h"
00034 #ifdef PAYMENT
00035 #include "CAFirstMix.hpp"
00036 #include "CAFirstMixChannelList.hpp"
00037 #include "CASymCipher.hpp"
00038 #include "CAQueue.hpp"
00039 #include "CAThread.hpp"
00040 #include "CATempIPBlockList.hpp"
00041 #include "CAThreadPool.hpp"
00042 #include "CAAccountingDBInterface.hpp"
00043 #include "CAAccountingBIInterface.hpp"
00044 #include "CAAccountingControlChannel.hpp"
00045 #include "CAAccountingSettleThread.hpp"
00046 #include "CACmdLnOptions.hpp"
00047 #include "Hashtable.hpp"
00048 #include "CAMix.hpp"
00049 #include "xml/DOM_Output.hpp"
00050 #include "CAStatusManager.hpp"
00051 
00052 // we want a costconfirmation from the user for every megabyte
00053 // after 2megs of unconfirmed traffic we kick the user out
00054 // todo put this in the configfile
00055 //#define HARDLIMIT_UNCONFIRMED_BYTES 1024*1024*2
00056 //#define SOFTLIMIT_UNCONFIRMED_BYTES 1024*512
00057 
00058 // the number of seconds that may pass between a pay request
00059 // and the jap sending its answer
00060 #define CHALLENGE_TIMEOUT 15
00061 #define HARD_LIMIT_TIMEOUT 30
00062 #define AUTH_TIMEOUT 15
00063 #define CRITICAL_SUBSEQUENT_BI_CONN_ERRORS 5
00064 
00065 #define CLIENT_VERSION_STR_LEN 20
00066 #define PREPAID_PROTO_CLIENT_VERSION "00.10.057"
00067 
00068 #define CRITICAL_SETTLE_FLAGS \
00069   (AUTH_INVALID_ACCOUNT | AUTH_ACCOUNT_EMPTY | AUTH_BLOCKED | AUTH_UNKNOWN)
00070 
00071 struct t_fmhashtableentry;
00072 
00073 struct AccountLoginHashEntry
00074 {
00075   UINT64 accountNumber;
00076   UINT64 confirmedBytes;
00077   UINT64 userID;
00078   UINT32 authFlags;
00079   UINT32 authRemoveFlags;
00080   UINT32 count;
00081   CAMutex *ownerLock;
00082   struct t_fmhashtableentry *ownerRef;
00083   bool loginOngoing;
00084 };
00085 
00086 struct SettleEntry
00087 {
00088   UINT64 accountNumber;
00089   UINT32 authFlags;
00090   UINT32 authRemoveFlags;
00091   UINT64 confirmedBytes;
00092   UINT64 diffBytes;
00093   SettleEntry* nextEntry;
00094   SINT32 storedStatus;
00095 };
00096 
00097 bool testAndSetLoginOwner(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
00098 bool resetLoginOngoing(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
00099 bool testLoginEntryOwner(struct AccountLoginHashEntry *loginEntry, fmHashTableEntry *ownerRef);
00100 bool isLoginOngoing(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
00101 inline bool testLoginEntryOwner_internal(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
00102 
00112 class CAAccountingInstance
00113 {
00114   friend class CAFirstMixA;
00115   friend class CAFirstMix;
00116 public:
00117 
00121   static SINT32 init(CAFirstMix* callingMix)
00122     {
00123         ms_pInstance = new CAAccountingInstance(callingMix);
00124         MONITORING_FIRE_PAY_EVENT(ev_pay_aiInited);
00125         return E_SUCCESS;
00126     }
00127 
00128   static SINT32 clean()
00129     {
00130       if(ms_pInstance != NULL)
00131       {
00132         delete ms_pInstance;
00133         ms_pInstance=NULL;
00134       }
00135       if(m_preparedCCRequest != NULL)
00136       {
00137         m_preparedCCRequest->release();
00138         m_preparedCCRequest = NULL;
00139       }
00140       MONITORING_FIRE_PAY_EVENT(ev_pay_aiShutdown);
00141       return E_SUCCESS;
00142     }
00143 
00147   static UINT32 getAuthFlags(fmHashTableEntry * pHashEntry);
00148 
00153   static SINT32 cleanupTableEntry(fmHashTableEntry * pHashEntry);
00154   static SINT32 initTableEntry(fmHashTableEntry * pHashEntry);
00155 
00159   static SINT32 handleJapPacket(fmHashTableEntry *pHashEntry, bool a_bControlMessage, bool a_bMessageToJAP);
00160 
00167   static SINT32 isIPAddressBlocked(const UINT8 ip[4])
00168   {
00169     //return ms_pInstance->m_pIPBlockList->checkIP(ip);
00170     return 0;
00171   }
00172 
00180   static SINT32 processJapMessage(fmHashTableEntry * pHashEntry,const  XERCES_CPP_NAMESPACE::DOMDocument* a_DomDoc);
00181   static UINT32 getNrOfUsers();
00182 
00183   static SINT32 loginProcessStatus(fmHashTableEntry *pHashEntry);
00184   static SINT32 finishLoginProcess(fmHashTableEntry *pHashEntry);
00185   //static void forcedSettle();
00186 
00187   static SINT32 settlementTransaction();
00188   static SettleEntry *__handleSettleResult(CAXMLCostConfirmation *pCC, CAXMLErrorMessage *pErrMsg, CAAccountingDBInterface *dbInterface,
00189     UINT64 a_iSettlementTransactionNr);
00190   static void __commitSettlementToDatabase(SettleEntry *entryList, CAAccountingDBInterface *dbInterface);
00191   static void __commitSettlementToLoginTable(SettleEntry *entryList);
00192   static SINT32 newSettlementTransaction();
00193   static SINT32 __newSettlementTransaction(UINT32 *nrOfSettledCCs);
00194 
00195 
00196   static const SINT32 HANDLE_PACKET_CONNECTION_OK; // this packet has been checked and is OK
00197   static const SINT32 HANDLE_PACKET_CONNECTION_UNCHECKED; // the packet might be OK (is it not checked)
00198   static const SINT32 HANDLE_PACKET_HOLD_CONNECTION; // queue packets until JAP has authenticated
00199   static const SINT32 HANDLE_PACKET_PREPARE_FOR_CLOSING_CONNECTION; // small grace period until kickout
00200   static const SINT32 HANDLE_PACKET_CLOSE_CONNECTION; // this connection should be closed immediatly
00201 
00202 
00203   static const UINT32 MAX_SETTLED_CCS; // do not settle more that X cost confirmations in one settle connection
00204 private:
00205 
00206   CAAccountingInstance(CAFirstMix* callingMix); //Singleton!
00207   ~CAAccountingInstance();
00208 
00209   struct t_aiqueueitem
00210   {
00211     XERCES_CPP_NAMESPACE::DOMDocument*      pDomDoc;
00212     tAiAccountingInfo*    pAccInfo;
00213     void (CAAccountingInstance::*handleFunc)(tAiAccountingInfo*,DOMElement*);
00214   };
00215   typedef struct t_aiqueueitem aiQueueItem;
00216 
00217   static SINT32 handleJapPacket_internal(fmHashTableEntry *pHashEntry, bool a_bControlMessage, bool a_bMessageToJAP);
00218 
00219   static UINT64 unlockLogin(fmHashTableEntry *ownerRef);
00220   static void processJapMessageLoginHelper(fmHashTableEntry *pHashEntry,
00221                        UINT32 handlerReturnvalue,
00222                        bool finishLogin);
00226   UINT32 handleCostConfirmation(tAiAccountingInfo* pAccInfo, DOMElement* root );
00227   UINT32 handleCostConfirmation_internal(tAiAccountingInfo* pAccInfo, DOMElement* root );
00228 
00232   UINT32 handleAccountCertificate(tAiAccountingInfo* pAccInfo, DOMElement* root );
00233   UINT32 handleAccountCertificate_internal(tAiAccountingInfo* pAccInfo, DOMElement* root );
00234 
00235 
00239   UINT32 handleChallengeResponse(tAiAccountingInfo* pAccInfo, DOMElement* root);
00240   UINT32 handleChallengeResponse_internal(tAiAccountingInfo* pAccInfo, DOMElement* root);
00241 
00242   bool cascadeMatchesCC(CAXMLCostConfirmation *pCC);
00243 
00244   static SINT32 getPrepaidBytes(tAiAccountingInfo* pAccInfos);
00245   SINT32 prepareCCRequest(CAMix* callingMix, UINT8* a_AiName);
00246   static SINT32 makeInitialCCRequest(CAXMLCostConfirmation *pCC, XERCES_CPP_NAMESPACE::DOMDocument* & doc, SINT32 prepaidBytes);
00247   static SINT32 makeCCRequest( const UINT64 accountNumber, const UINT64 transferredBytes,  XERCES_CPP_NAMESPACE::DOMDocument* & doc);
00248   static SINT32 sendInitialCCRequest(tAiAccountingInfo* pAccInfo, CAXMLCostConfirmation *pCC, SINT32 prepaidBytes);
00249   static SINT32 sendCCRequest(tAiAccountingInfo* pAccInfo);
00250   static SINT32 makeAccountRequest( XERCES_CPP_NAMESPACE::DOMDocument* & doc);
00251   static SINT32 sendAILoginConfirmation(tAiAccountingInfo* pAccInfo, const UINT32 code, UINT8 * message);
00252 
00253   //possible replies to a JAP
00254   static SINT32 returnKickout(tAiAccountingInfo* pAccInfo);
00255   static SINT32 returnPrepareKickout(tAiAccountingInfo* pAccInfo, CAXMLErrorMessage* a_error);
00256 
00257   static void setPrepaidBytesToZero(tAiAccountingInfo* pAccInfo);
00258 
00259   static inline void setPrepaidBytesToZero_internal(tAiAccountingInfo* pAccInfo);
00260 
00265   //static THREAD_RETURN aiThreadMainLoop(void *param);
00266 
00268   static THREAD_RETURN processThread(void* a_param);
00269 
00270   static const UINT64 PACKETS_BEFORE_NEXT_CHECK;
00271 
00272   static const UINT32 MAX_TOLERATED_MULTIPLE_LOGINS;
00273 
00274   static XERCES_CPP_NAMESPACE::DOMDocument* m_preparedCCRequest;
00275 
00277   CAThreadPool* m_aiThreadPool;
00278 
00280   CAMutex *m_pMutex;
00281 
00282   Hashtable* m_certHashCC;
00283 
00285   Hashtable* m_currentAccountsHashtable;
00286   CAFirstMix *m_mix;
00287 
00289   UINT8* m_AiName;
00290 
00292   // (same hash values as in CCs and in the JPI, as taken from the MixInfo)
00293   // we concatenate price certificates rather than mix-skis because a cascade with changed prices counts as a new/different cascade
00294   UINT8* m_currentCascade;
00295 
00297   UINT8** m_allHashes;
00298   UINT32 m_allHashesLen;
00299 
00301   //CAAccountingDBInterface * m_dbInterface;
00302   CAAccountingBIInterface *m_pPiInterface;
00303 
00304   UINT32 m_iSoftLimitBytes;
00305   UINT32 m_iHardLimitBytes;
00306 
00313   CATempIPBlockList * m_pIPBlockList;
00314 
00318   static CAAccountingInstance * ms_pInstance;
00319 
00324   //CASignature * m_pJpiVerifyingInstance;
00325 
00327   CAAccountingSettleThread * m_pSettleThread;
00328 
00329   static SINT32 m_prepaidBytesMinimum;
00330 
00331   bool m_bThreadRunning;
00332 
00333   //bool m_loginHashTableChangeInProgress;
00334 
00335   /* For Thread synchronisation can only be set and read when m_pSettlementMutex lock is acquired */
00336   volatile UINT64 m_nextSettleNr;
00337   volatile UINT64 m_settleWaitNr;
00338   CAConditionVariable *m_pSettlementMutex;
00339 
00340   volatile UINT32 m_seqBIConnErrors;
00341 
00342   static volatile UINT64 m_iCurrentSettleTransactionNr;
00343 };
00344 
00345 
00346 #endif
00347 #endif //Payment