1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
/***************************************************************************
polling.cpp - description
-------------------
begin : Sun Mar 17 2002
copyright : (C) 2002 by Vladimir Shutoff
email : vovan@shutoff.ru
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <qtimer.h>
#include <stdio.h>
#include "fetch.h"
#include "log.h"
#include "icqclient.h"
#include "polling.h"
using namespace std;
using namespace SIM;
const unsigned short HTTP_PROXY_VERSION = 0x0443;
const unsigned short HTTP_PROXY_HELLO = 2;
const unsigned short HTTP_PROXY_LOGIN = 3;
const unsigned short HTTP_PROXY_UNK1 = 4;
const unsigned short HTTP_PROXY_FLAP = 5;
const unsigned short HTTP_PROXY_CONNECT = 6;
const unsigned short HTTP_PROXY_UNK2 = 7;
class HttpPacket
{
COPY_RESTRICTED(HttpPacket)
public:
HttpPacket(const char *data, unsigned short size, unsigned short type, unsigned long nSock);
~HttpPacket();
char *data;
unsigned short size;
unsigned short type;
unsigned long nSock;
};
HttpPacket::HttpPacket(const char *_data, unsigned short _size, unsigned short _type, unsigned long _nSock)
{
size = _size;
type = _type;
nSock = _nSock;
data = NULL;
if (size){
data = new char[size];
memcpy(data, _data, size);
}
}
HttpPacket::~HttpPacket()
{
if (data) delete[] data;
}
// ___________________________________________________________________________________
static char ANSWER_ERROR[] = "Bad answer";
class HttpRequest : public FetchClient
{
public:
HttpRequest(HttpPool *pool);
void send();
protected:
virtual bool done(unsigned code, Buffer &data, const QString &headers);
virtual HttpPacket *packet() = 0;
virtual QString url() = 0;
virtual void data_ready(ICQBuffer*) = 0;
HttpPool *m_pool;
};
HttpRequest::HttpRequest(HttpPool *pool)
{
m_pool = pool;
}
void HttpRequest::send()
{
HttpPacket *p = packet();
ICQBuffer *postData = NULL;
if (p){
postData = new ICQBuffer;
unsigned short len = (unsigned short)(p->size + 12);
*postData
<< len
<< HTTP_PROXY_VERSION
<< p->type
<< 0x00000000L
<< p->nSock;
if (p->size)
postData->pack(p->data, p->size);
m_pool->queue.remove(p);
delete p;
}
char headers[] = "Cache-control: no-store, no-cache\n"
"Pragma: no-cache";
fetch(url(), headers, postData);
}
bool HttpRequest::done(unsigned code, Buffer &data, const QString &)
{
if (code != 200){
log(L_DEBUG, "Res: %u %s", code, static_cast<const char *>(url().local8Bit()));
m_pool->error(ANSWER_ERROR);
return false;
}
ICQBuffer d(data);
data_ready(&d);
return true;
}
unsigned long HttpPool::localHost()
{
return 0;
}
void HttpPool::pause(unsigned time)
{
QTimer::singleShot(time * 1000, this, SLOT(timeout()));
}
void HttpPool::timeout()
{
if (notify)
notify->write_ready();
}
// ______________________________________________________________________________________
class HelloRequest : public HttpRequest
{
public:
HelloRequest(HttpPool *poll, bool bAIM);
protected:
virtual HttpPacket *packet();
virtual QString url();
virtual void data_ready(ICQBuffer*);
bool m_bAIM;
};
HelloRequest::HelloRequest(HttpPool *poll, bool bAIM)
: HttpRequest(poll)
{
m_bAIM = bAIM;
send();
}
HttpPacket *HelloRequest::packet()
{
return NULL;
}
QString HelloRequest::url()
{
return m_bAIM ? "http://aimhttp.oscar.aol.com/hello" : "http://http.proxy.icq.com/hello";
}
void HelloRequest::data_ready(ICQBuffer *bIn)
{
m_pool->hello = NULL;
bIn->incReadPos(12);
unsigned long SID[4];
(*bIn) >> SID[0] >> SID[1] >> SID[2] >> SID[3];
char b[34];
snprintf(b, sizeof(b), "%08lx%08lx%08lx%08lx", SID[0], SID[1], SID[2], SID[3]);
m_pool->sid = b;
bIn->unpackStr(m_pool->m_host);
m_pool->request();
}
// ______________________________________________________________________________________
class MonitorRequest : public HttpRequest
{
public:
MonitorRequest(HttpPool *pool);
protected:
virtual HttpPacket *packet();
virtual QString url();
virtual void data_ready(ICQBuffer*);
};
MonitorRequest::MonitorRequest(HttpPool *pool)
: HttpRequest(pool)
{
send();
}
HttpPacket *MonitorRequest::packet()
{
return NULL;
}
QString MonitorRequest::url()
{
QString sURL;
sURL = "http://";
sURL += m_pool->m_host;
sURL += "/monitor?sid=";
sURL += m_pool->sid;
return sURL;
}
void MonitorRequest::data_ready(ICQBuffer *bIn)
{
m_pool->monitor = NULL;
m_pool->readn = 0;
while (bIn->readPos() < bIn->size()){
unsigned short len, ver, type;
(*bIn) >> len >> ver >> type;
bIn->incReadPos(8);
len -= 12;
if (len > (bIn->size() - bIn->readPos())){
m_pool->error(ANSWER_ERROR);
return;
}
if (ver != HTTP_PROXY_VERSION){
m_pool->error(ANSWER_ERROR);
return;
}
switch (type){
case HTTP_PROXY_FLAP:
if (len){
unsigned short nSock;
bIn->incReadPos(-2);
*bIn >> nSock;
if (nSock == m_pool->nSock){
char *data = bIn->data(bIn->readPos());
m_pool->readData.pack(data, len);
m_pool->readn += len;
}
bIn->incReadPos(len);
}
break;
case HTTP_PROXY_UNK1:
case HTTP_PROXY_UNK2:
if (len)
bIn->incReadPos(len);
break;
default:
m_pool->error(ANSWER_ERROR);
return;
}
}
m_pool->request();
}
// ______________________________________________________________________________________
class PostRequest : public HttpRequest
{
public:
PostRequest(HttpPool *proxy);
protected:
virtual HttpPacket *packet();
virtual QString url();
virtual void data_ready(ICQBuffer *b);
};
PostRequest::PostRequest(HttpPool *proxy)
: HttpRequest(proxy)
{
send();
}
HttpPacket *PostRequest::packet()
{
if (m_pool->queue.size())
return m_pool->queue.front();
return NULL;
}
QString PostRequest::url()
{
QString sURL;
sURL = "http://";
sURL += m_pool->m_host;
sURL += "/data?sid=";
sURL += m_pool->sid;
sURL += "&seq=";
char b[15];
snprintf(b, sizeof(b), "%u", ++m_pool->seq);
sURL += b;
return sURL;
}
void PostRequest::data_ready(ICQBuffer*)
{
m_pool->post = NULL;
m_pool->request();
}
// ______________________________________________________________________________________
HttpPool::HttpPool(bool bAIM)
{
m_bAIM = bAIM;
hello = NULL;
monitor = NULL;
post = NULL;
state = None;
seq = 0;
readn = 0;
nSock = 0;
}
HttpPool::~HttpPool()
{
if (hello) delete hello;
if (monitor) delete monitor;
if (post) delete post;
for (list<HttpPacket*>::iterator it = queue.begin(); it != queue.end(); ++it)
delete *it;
}
int HttpPool::read(char *buf, unsigned size)
{
unsigned tail = readData.size() - readData.readPos();
if (size > tail) size = tail;
if (size == 0) return 0;
readData.unpack(buf, size);
if (readData.readPos() == readData.size())
readData.init(0);
return size;
}
void HttpPool::write(const char *buf, unsigned size)
{
queue.push_back(new HttpPacket(buf, (unsigned short)size, HTTP_PROXY_FLAP, nSock));
request();
}
void HttpPool::close()
{
readData.init(0);
}
void HttpPool::connect(const QString &host, unsigned short port)
{
state = None;
ICQBuffer b;
unsigned short len = host.length();
b << len << host.local8Bit().data() << port;
nSock++;
queue.push_back(new HttpPacket(b.data(0), (unsigned short)(b.size()), HTTP_PROXY_LOGIN, nSock));
if (sid.length()){
unsigned char close_packet[] = { 0x2A, 0x04, 0x14, 0xAB, 0x00, 0x00 };
queue.push_back(new HttpPacket((char*)close_packet, sizeof(close_packet), HTTP_PROXY_FLAP, 1));
queue.push_back(new HttpPacket(NULL, 0, HTTP_PROXY_CONNECT, 1));
}
request();
}
void HttpPool::request()
{
if (sid.length() == 0){
if (hello == NULL)
hello = new HelloRequest(this, m_bAIM);
return;
}
if (monitor == NULL)
monitor = new MonitorRequest(this);
if (queue.size() && (post == NULL))
post = new PostRequest(this);
if (readn && notify){
if (state == None){
state = Connected;
notify->connect_ready();
}
readn = 0;
notify->read_ready();
}
}
Socket *ICQClient::createSocket()
{
m_bHTTP = getUseHTTP();
if (getAutoHTTP()){
m_bHTTP = m_bFirstTry;
if (!m_bFirstTry)
m_bFirstTry = true;
}
if (m_bHTTP)
return new HttpPool(m_bAIM);
return NULL;
}
#ifndef NO_MOC_INCLUDES
#include "polling.moc"
#endif
|