Mixe for Privacy and Anonymity in the Internet
CAXMLErrorMessage.cpp
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 #include "StdAfx.h"
29 #ifdef PAYMENT
30 #ifndef ONLY_LOCAL_PROXY
31 #include "CAXMLErrorMessage.hpp"
32 
35 {
36  m_iErrorCode = errorCode;
37  m_strErrMsg = new UINT8[strlen((char *)message)+1];
38  strcpy((char *)m_strErrMsg, (char *)message);
39  m_messageObject = NULL;
40 }
41 
42 
43 
46 {
47  UINT8 *errors[] = {
48  (UINT8*)"Success",
49  (UINT8*)"Internal Server Error",
50  (UINT8*)"Wrong format",
51  (UINT8*)"Wrong Data",
52  (UINT8*)"Key not found",
53  (UINT8*)"Bad Signature",
54  (UINT8*)"Bad request",
55  (UINT8*)"You refused to send an account certificate. I will close the connection.",
56  (UINT8*)"You refused to send a current balance. I will close the connection.",
57  (UINT8*)"You refused to send a cost confirmation. I will close the connection.",
58  (UINT8*)"Your account is empty.",
59  (UINT8*)"Cascade is too long",
60  (UINT8*)"Database error",
61  (UINT8*)"Insufficient balance",
62  (UINT8*)"No flatrate offered",
63  (UINT8*)"Invalid code",
64  (UINT8*)"Costconfirmation is not valid, possible attempt at doublespending!",
65  (UINT8*)"One or more price certificates are invalid!",
66  (UINT8*)"User is logged in more than once!",
67  (UINT8*)"No database record for this cost confirmation was found!",
68  (UINT8*)"Operation may have succeded, but this is not clear!",
69  (UINT8*)"Account is blocked!"
70  };
71  m_iErrorCode = errorCode;
72  if (m_iErrorCode < 0 || m_iErrorCode >= 19)
73  {
74  UINT8 defaultMsg[] = "Unknown Error";
75  m_strErrMsg = new UINT8[strlen((char *)defaultMsg)+1];
76  strcpy((char *)m_strErrMsg, (char *)defaultMsg);
77  }
78  else
79  {
80  m_strErrMsg = new UINT8[strlen((char *)errors[errorCode])+1];
81  strcpy((char *)m_strErrMsg, (char *)errors[errorCode]);
82  }
83  m_messageObject = NULL;
84 }
85 
86 
87 
88 CAXMLErrorMessage::CAXMLErrorMessage(const UINT32 errorCode, UINT8* message, CAAbstractXMLEncodable* messageObject)
89 {
90  m_iErrorCode = errorCode;
91  m_strErrMsg = new UINT8[strlen((char *)message)+1];
92  strcpy((char *)m_strErrMsg, (char *)message);
93 
94  m_messageObject = messageObject;
95 }
96 
99 {
100  m_strErrMsg=NULL;
101  m_messageObject = NULL;
102 
103  if(elemRoot != NULL)
104  {
105  if (setValues(elemRoot) != E_SUCCESS)
106  {
108  }
109  }
110  else
111  {
113  }
114 }
115 
118 {
119  m_strErrMsg=NULL;
120  m_messageObject = NULL;
121 
122  XERCES_CPP_NAMESPACE::DOMDocument* doc = parseDOMDocument(strXmlData,strlen((char*)strXmlData));
123 
124  if(doc != NULL)
125  {
126  DOMElement* elemRoot = doc->getDocumentElement();
127  if (setValues(elemRoot) != E_SUCCESS)
128  {
130  }
131  if(doc != NULL)
132  {
133  doc->release();
134  doc = NULL;
135  }
136  }
137  else
138  {
140  }
141 }
142 
143 
145 {
146  UINT8 strGeneral[256];
147  UINT32 strGeneralLen = 256;
148 
149  SINT32 tmp;
150  SINT32 rc;
151 
152  if( ((rc=getDOMElementAttribute(elemRoot, "code", &tmp)) !=E_SUCCESS) ||
153  ((rc=getDOMElementValue(elemRoot, strGeneral, &strGeneralLen)) !=E_SUCCESS)
154  )
155  {
156  UINT8 buff[8192];
157  UINT32 len=8192;
158  DOM_Output::dumpToMem(elemRoot,buff,&len);
159  CAMsg::printMsg(LOG_DEBUG,(char*)buff);
160 
161  return rc;
162  }
163 
164  m_iErrorCode = (UINT32)tmp;
165  delete [] m_strErrMsg;
166  m_strErrMsg = NULL;
167  m_strErrMsg = new UINT8[strGeneralLen+1];
168  strcpy((char*)m_strErrMsg, (char*)strGeneral);
169 
170  /*if((rc=getDOMElementAttribute(elemRoot, "expires", (UINT8*) strExp, &strExpLen)) ==E_SUCCESS)
171  {
172  if(m_strExpires != NULL)
173  {
174  strncpy(m_strExpires, strExp, 10);
175  }
176  }*/
177  DOMElement* objectRootElem=NULL;
178  getDOMChildByName(elemRoot, "MessageObject", objectRootElem, false);
179 
180  //due to lack of RTTI, we need to hardcode how to deal with each specific object type
182  {
183  DOMElement* ccElem=NULL;
184  if (getDOMChildByName(objectRootElem,"CC",ccElem,true) == E_SUCCESS)
185  {
186  m_messageObject = CAXMLCostConfirmation::getInstance(ccElem);
187  }
188  }
189  else if (ERR_ACCOUNT_EMPTY == m_iErrorCode)
190  {
191  if (objectRootElem != NULL)
192  {
193  CAMsg::printMsg(LOG_INFO, "XMLErrorMessage: appended object found...\n");
194  UINT8* buff = DOM_Output::dumpToString(objectRootElem, true);
195  CAMsg::printMsg(LOG_DEBUG,(char*)buff);
196  delete[] buff;
197  }
198  DOMElement* confirmedElem=NULL;
199  if (getDOMChildByName(objectRootElem,"GenericText",confirmedElem,true) == E_SUCCESS)
200  {
201  m_messageObject = new UINT64;
202  if(getDOMElementValue(confirmedElem, (*(UINT64*)m_messageObject)) != E_SUCCESS)
203  {
204  delete (UINT64*)m_messageObject;
205  m_messageObject = NULL;
206  }
207  }
208  }
209  else
210  {
211  m_messageObject = NULL;
212  }
213  //add code to parse other types of objects here when adding new error codes with corresponding objects
214 
215  return E_SUCCESS;
216 }
217 
218 
220 {
221  if(m_strErrMsg)
222  {
223  delete [] m_strErrMsg;
224  m_strErrMsg = NULL;
225  }
226  if (m_messageObject != NULL)
227  {
228  delete m_messageObject;
229  m_messageObject = NULL;
230  }
231 }
232 
233 
234 SINT32 CAXMLErrorMessage::toXmlElement(XERCES_CPP_NAMESPACE::DOMDocument* a_doc, DOMElement* & elemRoot)
235 {
236  elemRoot = createDOMElement(a_doc, XML_ELEMENT_ERROR_MSG);
237  setDOMElementAttribute(elemRoot, "code", m_iErrorCode);
238  /*if(m_strExpires != NULL)
239  {
240  if(m_strExpires[0] != 0)
241  {
242  setDOMElementAttribute(elemRoot, "expires", (UINT8*)m_strExpires);
243  }
244  }*/
245  setDOMElementValue(elemRoot, m_strErrMsg);
246 
247  if (m_messageObject)
248  {
249  DOMElement* objectRoot = createDOMElement(a_doc,"MessageObject");
250  DOMElement* objectElem=NULL;
251  //WARNING: this will fail for CAXMLCostConfirmation!!! (since it is not a subclass of CAAbstractXMLEncodable)
253  encodableObject->toXmlElement(a_doc,objectElem);
254  objectRoot->appendChild(objectElem);
255  elemRoot->appendChild(objectRoot);
256  }
257 
258  return E_SUCCESS;
259 }
260 #endif //ONLY_LOCAL_PROXY
261 #endif //PAYMENT
SINT32 setDOMElementAttribute(DOMNode *pElem, const char *attrName, const char *value)
Definition: CAUtil.cpp:831
SINT32 getDOMElementValue(const DOMNode *const pElem, UINT8 *value, UINT32 *valuelen)
Returns the content of the text node(s) under elem as null-terminated C String.
Definition: CAUtil.cpp:746
SINT32 setDOMElementValue(DOMElement *pElem, SINT32 value)
Definition: CAUtil.cpp:939
XERCES_CPP_NAMESPACE::DOMDocument * parseDOMDocument(const UINT8 *const buff, UINT32 len)
Parses a buffer containing an XML document and returns this document.
Definition: CAUtil.cpp:663
DOMElement * createDOMElement(XERCES_CPP_NAMESPACE::DOMDocument *pOwnerDoc, const char *const name)
Creates a new DOMElement with the given name which belongs to the DOMDocument owernDoc.
Definition: CAUtil.cpp:814
SINT32 getDOMChildByName(const DOMNode *pNode, const char *const name, DOMElement *&child, bool deep)
Definition: CAUtil.cpp:458
SINT32 getDOMElementAttribute(const DOMNode *const elem, const char *attrName, UINT8 *value, UINT32 *len)
Definition: CAUtil.cpp:780
#define XML_ELEMENT_ERROR_MSG
signed int SINT32
Definition: basetypedefs.h:132
struct __UINT64__t_ UINT64
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
Abstract base class for classes which can be converted to an XML structure.
virtual SINT32 toXmlElement(XERCES_CPP_NAMESPACE::DOMDocument *a_pDoc, DOMElement *&pElemRoot)=0
Creates the XML structure inside an existing DOM_Document, but does not append it to any node.
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
CAXMLErrorMessage(const UINT32 errorCode, UINT8 *message)
Creates an errorMessage object.
static const UINT32 ERR_ACCOUNT_EMPTY
static const UINT32 ERR_NO_ERROR_GIVEN
SINT32 setValues(DOMElement *elemRoot)
static const UINT32 ERR_OUTDATED_CC
SINT32 toXmlElement(XERCES_CPP_NAMESPACE::DOMDocument *a_doc, DOMElement *&elemRoot)
Creates the XML structure inside an existing DOM_Document, but does not append it to any node.
static UINT8 * dumpToString(const DOMNode *node, bool a_bAddNewLine)
Dumps the Node an returns a pointer to a null terminated string.
Definition: DOM_Output.hpp:190
static SINT32 dumpToMem(const DOMNode *node, UINT8 *buff, UINT32 *size)
Dumps the node and all childs into buff.
Definition: DOM_Output.hpp:161
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
UINT16 len
Definition: typedefs.hpp:0