Mixe for Privacy and Anonymity in the Internet
CAAccountingInstance.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 
29 
30 
31 #ifndef __CAACCOUNTINGINSTANCE__
32 #define __CAACCOUNTINGINSTANCE__
33 #include "doxygen.h"
34 #ifdef PAYMENT
35 #include "CAFirstMix.hpp"
37 #include "CASymCipher.hpp"
38 #include "CAQueue.hpp"
39 #include "CAThread.hpp"
40 #include "CATempIPBlockList.hpp"
41 #include "CAThreadPool.hpp"
46 #include "CACmdLnOptions.hpp"
47 #include "Hashtable.hpp"
48 #include "CAMix.hpp"
49 #include "xml/DOM_Output.hpp"
50 #include "CAStatusManager.hpp"
51 
52 // we want a costconfirmation from the user for every megabyte
53 // after 2megs of unconfirmed traffic we kick the user out
54 // todo put this in the configfile
55 //#define HARDLIMIT_UNCONFIRMED_BYTES 1024*1024*2
56 //#define SOFTLIMIT_UNCONFIRMED_BYTES 1024*512
57 
58 // the number of seconds that may pass between a pay request
59 // and the jap sending its answer
60 #define CHALLENGE_TIMEOUT 15
61 #define HARD_LIMIT_TIMEOUT 30
62 #define AUTH_TIMEOUT 15
63 #define CRITICAL_SUBSEQUENT_BI_CONN_ERRORS 5
64 
65 #define CLIENT_VERSION_STR_LEN 20
66 #define PREPAID_PROTO_CLIENT_VERSION "00.10.057"
67 
68 #define CRITICAL_SETTLE_FLAGS \
69  (AUTH_INVALID_ACCOUNT | AUTH_ACCOUNT_EMPTY | AUTH_BLOCKED | AUTH_UNKNOWN)
70 
71 struct t_fmhashtableentry;
72 
74 {
84 };
85 
87 {
95 };
96 
97 bool testAndSetLoginOwner(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
98 bool resetLoginOngoing(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
99 bool testLoginEntryOwner(struct AccountLoginHashEntry *loginEntry, fmHashTableEntry *ownerRef);
100 bool isLoginOngoing(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
101 inline bool testLoginEntryOwner_internal(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
102 
113 {
114  friend class CAFirstMixA;
115  friend class CAFirstMix;
116 public:
117 
121  static SINT32 init(CAFirstMix* callingMix)
122  {
123  ms_pInstance = new CAAccountingInstance(callingMix);
124  MONITORING_FIRE_PAY_EVENT(ev_pay_aiInited);
125  return E_SUCCESS;
126  }
127 
128  static SINT32 clean()
129  {
130  if(ms_pInstance != NULL)
131  {
132  delete ms_pInstance;
133  ms_pInstance=NULL;
134  }
135  if(m_preparedCCRequest != NULL)
136  {
137  m_preparedCCRequest->release();
138  m_preparedCCRequest = NULL;
139  }
140  MONITORING_FIRE_PAY_EVENT(ev_pay_aiShutdown);
141  return E_SUCCESS;
142  }
143 
147  static UINT32 getAuthFlags(fmHashTableEntry * pHashEntry);
148 
153  static SINT32 cleanupTableEntry(fmHashTableEntry * pHashEntry);
154  static SINT32 initTableEntry(fmHashTableEntry * pHashEntry);
155 
159  static SINT32 handleJapPacket(fmHashTableEntry *pHashEntry, bool a_bControlMessage, bool a_bMessageToJAP);
160 
167  static SINT32 isIPAddressBlocked(const UINT8 ip[4])
168  {
169  //return ms_pInstance->m_pIPBlockList->checkIP(ip);
170  return 0;
171  }
172 
180  static SINT32 processJapMessage(fmHashTableEntry * pHashEntry,const XERCES_CPP_NAMESPACE::DOMDocument* a_DomDoc);
181  static UINT32 getNrOfUsers();
182 
183  static SINT32 loginProcessStatus(fmHashTableEntry *pHashEntry);
184  static SINT32 finishLoginProcess(fmHashTableEntry *pHashEntry);
185  //static void forcedSettle();
186 
187  static SINT32 settlementTransaction();
188  static SettleEntry *__handleSettleResult(CAXMLCostConfirmation *pCC, CAXMLErrorMessage *pErrMsg, CAAccountingDBInterface *dbInterface,
189  UINT64 a_iSettlementTransactionNr);
190  static void __commitSettlementToDatabase(SettleEntry *entryList, CAAccountingDBInterface *dbInterface);
191  static void __commitSettlementToLoginTable(SettleEntry *entryList);
193  static SINT32 __newSettlementTransaction(UINT32 *nrOfSettledCCs);
194 
195 
196  static const SINT32 HANDLE_PACKET_CONNECTION_OK; // this packet has been checked and is OK
197  static const SINT32 HANDLE_PACKET_CONNECTION_UNCHECKED; // the packet might be OK (is it not checked)
198  static const SINT32 HANDLE_PACKET_HOLD_CONNECTION; // queue packets until JAP has authenticated
199  static const SINT32 HANDLE_PACKET_PREPARE_FOR_CLOSING_CONNECTION; // small grace period until kickout
200  static const SINT32 HANDLE_PACKET_CLOSE_CONNECTION; // this connection should be closed immediatly
201 
202 
203  static const UINT32 MAX_SETTLED_CCS; // do not settle more that X cost confirmations in one settle connection
204 private:
205 
206  CAAccountingInstance(CAFirstMix* callingMix); //Singleton!
208 
210  {
211  XERCES_CPP_NAMESPACE::DOMDocument* pDomDoc;
214  };
215  typedef struct t_aiqueueitem aiQueueItem;
216 
217  static SINT32 handleJapPacket_internal(fmHashTableEntry *pHashEntry, bool a_bControlMessage, bool a_bMessageToJAP);
218 
219  static UINT64 unlockLogin(fmHashTableEntry *ownerRef);
220  static void processJapMessageLoginHelper(fmHashTableEntry *pHashEntry,
221  UINT32 handlerReturnvalue,
222  bool finishLogin);
226  UINT32 handleCostConfirmation(tAiAccountingInfo* pAccInfo, DOMElement* root );
227  UINT32 handleCostConfirmation_internal(tAiAccountingInfo* pAccInfo, DOMElement* root );
228 
232  UINT32 handleAccountCertificate(tAiAccountingInfo* pAccInfo, DOMElement* root );
233  UINT32 handleAccountCertificate_internal(tAiAccountingInfo* pAccInfo, DOMElement* root );
234 
235 
239  UINT32 handleChallengeResponse(tAiAccountingInfo* pAccInfo, DOMElement* root);
240  UINT32 handleChallengeResponse_internal(tAiAccountingInfo* pAccInfo, DOMElement* root);
241 
242  bool cascadeMatchesCC(CAXMLCostConfirmation *pCC);
243 
244  static SINT32 getPrepaidBytes(tAiAccountingInfo* pAccInfos);
245  SINT32 prepareCCRequest(CAMix* callingMix, UINT8* a_AiName);
246  static SINT32 makeInitialCCRequest(CAXMLCostConfirmation *pCC, XERCES_CPP_NAMESPACE::DOMDocument* & doc, SINT32 prepaidBytes);
247  static SINT32 makeCCRequest( const UINT64 accountNumber, const UINT64 transferredBytes, XERCES_CPP_NAMESPACE::DOMDocument* & doc);
248  static SINT32 sendInitialCCRequest(tAiAccountingInfo* pAccInfo, CAXMLCostConfirmation *pCC, SINT32 prepaidBytes);
249  static SINT32 sendCCRequest(tAiAccountingInfo* pAccInfo);
250  static SINT32 makeAccountRequest( XERCES_CPP_NAMESPACE::DOMDocument* & doc);
251  static SINT32 sendAILoginConfirmation(tAiAccountingInfo* pAccInfo, const UINT32 code, UINT8 * message);
252 
253  //possible replies to a JAP
254  static SINT32 returnKickout(tAiAccountingInfo* pAccInfo);
256 
257  static void setPrepaidBytesToZero(tAiAccountingInfo* pAccInfo);
258 
259  static inline void setPrepaidBytesToZero_internal(tAiAccountingInfo* pAccInfo);
260 
265  //static THREAD_RETURN aiThreadMainLoop(void *param);
266 
268  static THREAD_RETURN processThread(void* a_param);
269 
271 
273 
274  static XERCES_CPP_NAMESPACE::DOMDocument* m_preparedCCRequest;
275 
278 
281 
282  Hashtable* m_certHashCC;
283 
287 
290 
292  // (same hash values as in CCs and in the JPI, as taken from the MixInfo)
293  // we concatenate price certificates rather than mix-skis because a cascade with changed prices counts as a new/different cascade
295 
299 
301  //CAAccountingDBInterface * m_dbInterface;
303 
306 
314 
319 
324  //CASignature * m_pJpiVerifyingInstance;
325 
328 
330 
332 
333  //bool m_loginHashTableChangeInProgress;
334 
335  /* For Thread synchronisation can only be set and read when m_pSettlementMutex lock is acquired */
339 
341 
343 };
344 
345 
346 #endif
347 #endif //Payment
bool testAndSetLoginOwner(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef)
if the current login entry isn't locked by a login thread the calling login-thread obtains ownership
bool resetLoginOngoing(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef)
indicates that the ongoing login process for this entry is finished but doesn't reset ownership.
bool testLoginEntryOwner(struct AccountLoginHashEntry *loginEntry, fmHashTableEntry *ownerRef)
tests whether the corresponding ownerEntry owns this loginEntry.
bool testLoginEntryOwner_internal(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef)
bool isLoginOngoing(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef)
test whether this entry is currently resrved by an ongoing login process.
#define MONITORING_FIRE_PAY_EVENT(e_type)
#define THREAD_RETURN
Definition: StdAfx.h:540
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
This class encapsulates the connection to the JPI.
This is the AI (accounting instance or abrechnungsinstanz in german) class.
static const SINT32 HANDLE_PACKET_CONNECTION_OK
static SettleEntry * __handleSettleResult(CAXMLCostConfirmation *pCC, CAXMLErrorMessage *pErrMsg, CAAccountingDBInterface *dbInterface, UINT64 a_iSettlementTransactionNr)
static const UINT32 MAX_SETTLED_CCS
UINT8 * m_currentCascade
current cascade (identified by the concatenated hash values of the price certificates)
static SINT32 getPrepaidBytes(tAiAccountingInfo *pAccInfos)
static SINT32 returnPrepareKickout(tAiAccountingInfo *pAccInfo, CAXMLErrorMessage *a_error)
UINT32 handleCostConfirmation(tAiAccountingInfo *pAccInfo, DOMElement *root)
Handles a cost confirmation sent by a jap.
static SINT32 sendAILoginConfirmation(tAiAccountingInfo *pAccInfo, const UINT32 code, UINT8 *message)
CAAccountingInstance(CAFirstMix *callingMix)
private Constructor
static SINT32 handleJapPacket_internal(fmHashTableEntry *pHashEntry, bool a_bControlMessage, bool a_bMessageToJAP)
Called by FirstMix for each incoming JAP packet.
static const SINT32 HANDLE_PACKET_CLOSE_CONNECTION
~CAAccountingInstance()
private destructor
static SINT32 init(CAFirstMix *callingMix)
Returns a reference to the Singleton instance.
static SINT32 makeInitialCCRequest(CAXMLCostConfirmation *pCC, XERCES_CPP_NAMESPACE::DOMDocument *&doc, SINT32 prepaidBytes)
new initialCCRequest containing the last CC and the prepaid bytes (this is a replacement for sending ...
static SINT32 returnKickout(tAiAccountingInfo *pAccInfo)
When receiving this message, the Mix should kick the user out immediately.
static SINT32 isIPAddressBlocked(const UINT8 ip[4])
Check if an IP address is temporarily blocked by the accounting instance.
static void setPrepaidBytesToZero_internal(tAiAccountingInfo *pAccInfo)
CAAccountingSettleThread * m_pSettleThread
Signature verifying instance for BI signatures.
CAMutex * m_pMutex
this is for synchronizing the write access to the HashEntries
static void __commitSettlementToLoginTable(SettleEntry *entryList)
only for internal use during the settleTransaction because no login table locks are acquired
static const SINT32 HANDLE_PACKET_HOLD_CONNECTION
static const UINT32 MAX_TOLERATED_MULTIPLE_LOGINS
static SINT32 processJapMessage(fmHashTableEntry *pHashEntry, const XERCES_CPP_NAMESPACE::DOMDocument *a_DomDoc)
Handle a user (xml) message sent to us by the Jap through the ControlChannel.
volatile UINT32 m_seqBIConnErrors
static SINT32 handleJapPacket(fmHashTableEntry *pHashEntry, bool a_bControlMessage, bool a_bMessageToJAP)
This should be called by the FirstMix for every incoming Jap packet.
static SINT32 makeAccountRequest(XERCES_CPP_NAMESPACE::DOMDocument *&doc)
static void setPrepaidBytesToZero(tAiAccountingInfo *pAccInfo)
static SINT32 loginProcessStatus(fmHashTableEntry *pHashEntry)
UINT32 handleCostConfirmation_internal(tAiAccountingInfo *pAccInfo, DOMElement *root)
Handles a cost confirmation sent by a jap.
CAThreadPool * m_aiThreadPool
reads messages from the queue and processes them
UINT32 handleChallengeResponse(tAiAccountingInfo *pAccInfo, DOMElement *root)
Checks the response of the challenge-response auth.
static SINT32 settlementTransaction()
CAAccountingBIInterface * m_pPiInterface
the interface to the database
static SINT32 initTableEntry(fmHashTableEntry *pHashEntry)
This must be called whenever a JAP is connecting to init our per-user data structures.
static SINT32 newSettlementTransaction()
static CAAccountingInstance * ms_pInstance
Singleton: This is the reference to the only instance of this class.
static void __commitSettlementToDatabase(SettleEntry *entryList, CAAccountingDBInterface *dbInterface)
only for internal use during the settleTransaction because the global settlement lock is not acquired
UINT8 ** m_allHashes
The hash values of the Mixes ordered beginning with the AI Mix.
UINT8 * m_AiName
the name of this accounting instance
static const UINT64 PACKETS_BEFORE_NEXT_CHECK
static volatile UINT64 m_iCurrentSettleTransactionNr
UINT32 handleChallengeResponse_internal(tAiAccountingInfo *pAccInfo, DOMElement *root)
Handles the response to our challenge.
static UINT64 unlockLogin(fmHashTableEntry *ownerRef)
release login (particularly for use in error case) this function is thread-safe.
SINT32 prepareCCRequest(CAMix *callingMix, UINT8 *a_AiName)
creating the xml of a new CC is really the responsability of the CAXMLCostConfirmation class knowledg...
UINT32 handleAccountCertificate_internal(tAiAccountingInfo *pAccInfo, DOMElement *root)
Handles an account certificate of a newly connected Jap.
static SINT32 sendCCRequest(tAiAccountingInfo *pAccInfo)
UINT32 handleAccountCertificate(tAiAccountingInfo *pAccInfo, DOMElement *root)
Handles an account certificate of a newly connected Jap.
static UINT32 getAuthFlags(fmHashTableEntry *pHashEntry)
static const SINT32 HANDLE_PACKET_CONNECTION_UNCHECKED
Hashtable * m_currentAccountsHashtable
Stores the account number of all users currently logged in.
static SINT32 __newSettlementTransaction(UINT32 *nrOfSettledCCs)
bool cascadeMatchesCC(CAXMLCostConfirmation *pCC)
static THREAD_RETURN processThread(void *a_param)
The main loop of the AI thread - reads messages from the queue and starts process threads for these m...
static SINT32 cleanupTableEntry(fmHashTableEntry *pHashEntry)
This should always be called when closing a JAP connection to cleanup the data structures.
static SINT32 makeCCRequest(const UINT64 accountNumber, const UINT64 transferredBytes, XERCES_CPP_NAMESPACE::DOMDocument *&doc)
static SINT32 sendInitialCCRequest(tAiAccountingInfo *pAccInfo, CAXMLCostConfirmation *pCC, SINT32 prepaidBytes)
CATempIPBlockList * m_pIPBlockList
Users that get kicked out because they sent no authentication certificate get their IP appended to th...
static const SINT32 HANDLE_PACKET_PREPARE_FOR_CLOSING_CONNECTION
CAConditionVariable * m_pSettlementMutex
static XERCES_CPP_NAMESPACE::DOMDocument * m_preparedCCRequest
static void processJapMessageLoginHelper(fmHashTableEntry *pHashEntry, UINT32 handlerReturnvalue, bool finishLogin)
static SINT32 finishLoginProcess(fmHashTableEntry *pHashEntry)
this method is for the corresponding CAFirstMix login thread to verify the result of the settlement.
A thread that settles CCs with the BI.
Definition: CAMix.hpp:49
The purpose of this class is storing the IPs of JAP users who tried to hack/attack the payment system...
This class bla bla.
This class encapsulates an error or success message.
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
UINT64 confirmedBytes
UINT32 count
bool loginOngoing
UINT32 authRemoveFlags
UINT64 userID
UINT64 accountNumber
struct t_fmhashtableentry * ownerRef
UINT32 authFlags
CAMutex * ownerLock
void(CAAccountingInstance::* handleFunc)(tAiAccountingInfo *, DOMElement *)
XERCES_CPP_NAMESPACE::DOMDocument * pDomDoc
SettleEntry * nextEntry
UINT32 authFlags
UINT32 authRemoveFlags
UINT64 accountNumber
UINT64 confirmedBytes
SINT32 storedStatus
UINT64 diffBytes
Structure that holds all per-user payment information Included in CAFirstMixChannelList (struct fmHas...
Definition: typedefs.hpp:301