Mixe for Privacy and Anonymity in the Internet
CAInfoServiceAware.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 DYNAMIC_MIX
30 #include "CAInfoServiceAware.hpp"
31 #include "CASocket.hpp"
32 #include "CASocketAddrINet.hpp"
33 #include "CAHttpClient.hpp"
34 #include "CACmdLnOptions.hpp"
35 
47 SINT32 CAInfoServiceAware::sendInfoserviceRequest(CASocketAddrINet* a_addr, UINT8 *a_strRequest, DOM_Element *r_elemRoot, UINT8* a_postData, UINT32 a_postLen)
48 {
49  CASocket socket;
50  CAHttpClient httpClient;
51  UINT32 status, contentLength;
52  UINT8 *content = NULL;
53  SINT32 ret = E_UNKNOWN;
54  DOM_Document doc;
55  DOMParser oParser;
56  MemBufInputSource *oInput = NULL;
57 
58  if(socket.connect(*a_addr)!=E_SUCCESS)
59  return E_UNKNOWN;
60 
61 #ifdef DEBUG
62 // CAMsg::printMsg(LOG_DEBUG, "CAInfoServiceAware::sendInfoserviceRequest - connected to InfoService\n");
63 #endif
64  //Send request
65  httpClient.setSocket(&socket);
66  if( a_postData != NULL )
67  httpClient.sendPostRequest(a_strRequest, a_postData, a_postLen);
68  else
69  httpClient.sendGetRequest(a_strRequest);
70  httpClient.parseHTTPHeader(&contentLength, &status);
71 #ifdef DEBUG
72 // CAMsg::printMsg(LOG_DEBUG, "CAInfoServiceAware::sendInfoserviceRequest - Request sent, HTTP status: %i, content length: %i\n", status,
73 // contentLength);
74 #endif
75  if(status!=200|| contentLength>MAX_CONTENT_LENGTH)
76  {
77  return E_UNKNOWN;
78  }
79  content = new UINT8[contentLength];
80 
81  if(httpClient.getContent(content, &contentLength)!=E_SUCCESS)
82  {
83  delete []content;
84  content = NULL;
85  return E_UNKNOWN;
86  }
87 #ifdef DEBUG
88 // CAMsg::printMsg(LOG_DEBUG, "CAInfoServiceAware::sendInfoserviceRequest - Answer was %s\n", (char*)content);
89 #endif
90  socket.close();
91  //Parse XML
92  oInput = new MemBufInputSource( content, contentLength, "tmp" );
93  oParser.parse( *oInput );
94  delete[] content;
95  content = NULL;
96  delete oInput;
97  oInput = NULL;
98  doc = oParser.getDocument();
99  if(doc==NULL)
100  return E_UNKNOWN;
101  *r_elemRoot=doc.getDocumentElement();
102  return E_SUCCESS;
103 }
104 
115 SINT32 CAInfoServiceAware::sendInfoserviceGetRequest(CASocketAddrINet* a_addr, UINT8 *a_strRequest, DOM_Element *r_elemRoot)
116 {
117  return sendInfoserviceRequest(a_addr, a_strRequest, r_elemRoot, NULL, 0);
118 }
119 
129 SINT32 CAInfoServiceAware::sendRandomInfoserviceGetRequest(UINT8 *a_strRequest, DOM_Element *r_elemRoot)
130 {
131  return sendRandomInfoserviceRequest(a_strRequest, r_elemRoot, NULL, 0);
132 }
133 
145 SINT32 CAInfoServiceAware::sendRandomInfoserviceRequest(UINT8 *a_strRequest, DOM_Element *r_elemRoot, UINT8* a_postData, UINT32 a_postLen)
146 {
147  CASocketAddrINet *address;
148  if( pglobalOptions->getRandomInfoService(address) != E_SUCCESS)
149  {
150 // CAMsg::printMsg( LOG_ERR, "CAInfoServiceAware::sendInfoserviceRequest - Unable to get a random InfoService - This will cause problems! Check your configuration!\n");
151  return E_UNKNOWN;
152  }
153 
154  return sendInfoserviceRequest(address, a_strRequest, r_elemRoot, a_postData, a_postLen);
155 }
156 
157 #endif // DYNAMIC_MIX
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
Very simple http client.
SINT32 sendGetRequest(const UINT8 *url)
Sends a HTTP GET request to the server.
SINT32 sendPostRequest(const UINT8 *url, const UINT8 *data, const UINT32 dataLen)
Sends a HTTP POST request to the server.
SINT32 getContent(UINT8 *a_pContent, UINT32 *a_pLength)
Retruns the content of the response.
SINT32 parseHTTPHeader(UINT32 *contentLength, UINT32 *statusCode=NULL, UINT32 msTimeOut=3000)
receives the HTTP header and parses the content length
SINT32 setSocket(CASocket *pSocket)
This class represents a socket address for Internet (IP) connections.
virtual SINT32 close()
Definition: CASocket.cpp:351
virtual SINT32 connect(const CASocketAddr &psa)
Definition: CASocket.hpp:64
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3