void BSocket::reset(bool clear) { if(d->qsock) { d->qsock->disconnect(this); if(!clear && d->qsock->isOpen()) { // move remaining into the local queue QByteArray block(d->qsock->bytesAvailable()); d->qsock->readBlock(block.data(), block.size()); appendRead(block); } d->sd.deleteLater(d->qsock); d->qsock = 0; } else { if(clear) clearReadBuffer(); } if(d->srv.isBusy()) d->srv.stop(); #ifndef NO_NDNS if(d->ndns.isBusy()) d->ndns.stop(); #endif d->state = Idle; }
void KNetworkByteStream::slotReadyRead() { // stuff all available data into our buffers QByteArray readBuffer( socket()->bytesAvailable () ); socket()->readBlock( readBuffer.data (), readBuffer.size () ); appendRead( readBuffer ); emit readyRead(); }
void BConsole::sn_read() { QByteArray a(1024, 0); int r = ::read(0, a.data(), a.size()); if(r < 0) { error(ErrRead); } else if(r == 0) { connectionClosed(); } else { a.resize(r); appendRead(a); readyRead(); } }
void SocksClient::grantConnect() { if(d->step != StepRequest || !d->waiting) return; // response d->waiting = false; writeData(sp_set_request(d->rhost, d->rport, RET_SUCCESS)); setOpenMode(QIODevice::ReadWrite); #ifdef PROX_DEBUG fprintf(stderr, "SocksClient: server << Success >>\n"); #endif if(!d->recvBuf.isEmpty()) { appendRead(d->recvBuf); d->recvBuf.resize(0); readyRead(); } }
void SocksClient::sock_readyRead() { QByteArray block = d->sock.readAll(); //qDebug() << this << "::sock_readyRead " << block.size() << " bytes." << // "udp=" << d->udp << openMode(); if(!isOpen()) { if(d->incoming) processIncoming(block); else processOutgoing(block); } else { if(!d->udp) { appendRead(block); emit readyRead(); } } }
void HttpConnect::sock_readyRead() { QByteArray block = d->sock.read(); if(!d->active) { ByteStream::appendArray(&d->recvBuf, block); if(d->inHeader) { // grab available lines while(1) { bool found; QString line = extractLine(&d->recvBuf, &found); if(!found) break; if(line.isEmpty()) { d->inHeader = false; break; } d->headerLines += line; } // done with grabbing the header? if(!d->inHeader) { QString str = d->headerLines.first(); d->headerLines.takeFirst(); QString proto; int code; QString msg; if(!extractMainHeader(str, &proto, &code, &msg)) { #ifdef PROX_DEBUG fprintf(stderr, "HttpConnect: invalid header!\n"); #endif reset(true); error(ErrProxyNeg); return; } else { #ifdef PROX_DEBUG fprintf(stderr, "HttpConnect: header proto=[%s] code=[%d] msg=[%s]\n", qPrintable(proto), code, qPrintable(msg)); for(QStringList::ConstIterator it = d->headerLines.begin(); it != d->headerLines.end(); ++it) fprintf(stderr, "HttpConnect: * [%s]\n", qPrintable(*it)); #endif } if(code == 200) { // OK #ifdef PROX_DEBUG fprintf(stderr, "HttpConnect: << Success >>\n"); #endif d->active = true; connected(); if(!d->recvBuf.isEmpty()) { appendRead(d->recvBuf); d->recvBuf.resize(0); readyRead(); return; } } else { int err; QString errStr; if(code == 407) { // Authentication failed err = ErrProxyAuth; errStr = tr("Authentication failed"); } else if(code == 404) { // Host not found err = ErrHostNotFound; errStr = tr("Host not found"); } else if(code == 403) { // Access denied err = ErrProxyNeg; errStr = tr("Access denied"); } else if(code == 503) { // Connection refused err = ErrConnectionRefused; errStr = tr("Connection refused"); } else { // invalid reply err = ErrProxyNeg; errStr = tr("Invalid reply"); } #ifdef PROX_DEBUG fprintf(stderr, "HttpConnect: << Error >> [%s]\n", qPrintable(errStr)); #endif reset(true); error(err); return; } } } } else { appendRead(block); readyRead(); return; } }
void SocksClient::processOutgoing(const QByteArray &block) { #ifdef PROX_DEBUG // show hex fprintf(stderr, "SocksClient: client recv { "); for(int n = 0; n < (int)block.size(); ++n) fprintf(stderr, "%02X ", (unsigned char)block[n]); fprintf(stderr, " } \n"); #endif d->recvBuf += block; if(d->step == StepVersion) { SPSS_VERSION s; int r = sps_get_version(d->recvBuf, &s); if(r == -1) { resetConnection(true); setError(ErrProxyNeg); return; } else if(r == 1) { if(s.version != 0x05 || s.method == 0xff) { #ifdef PROX_DEBUG fprintf(stderr, "SocksClient: Method selection failed\n"); #endif resetConnection(true); setError(ErrProxyNeg); return; } QString str; if(s.method == 0x00) { str = "None"; d->authMethod = AuthNone; } else if(s.method == 0x02) { str = "Username/Password"; d->authMethod = AuthUsername; } else { #ifdef PROX_DEBUG fprintf(stderr, "SocksClient: Server wants to use unknown method '%02x'\n", s.method); #endif resetConnection(true); setError(ErrProxyNeg); return; } if(d->authMethod == AuthNone) { // no auth, go straight to the request do_request(); } else if(d->authMethod == AuthUsername) { d->step = StepAuth; #ifdef PROX_DEBUG fprintf(stderr, "SocksClient: Authenticating [Username] ...\n"); #endif writeData(spc_set_authUsername(d->user.toLatin1(), d->pass.toLatin1())); } } } if(d->step == StepAuth) { if(d->authMethod == AuthUsername) { SPSS_AUTHUSERNAME s; int r = sps_get_authUsername(d->recvBuf, &s); if(r == -1) { resetConnection(true); setError(ErrProxyNeg); return; } else if(r == 1) { if(s.version != 0x01) { resetConnection(true); setError(ErrProxyNeg); return; } if(!s.success) { resetConnection(true); setError(ErrProxyAuth); return; } do_request(); } } } else if(d->step == StepRequest) { SPS_CONNREQ s; int r = sp_get_request(d->recvBuf, &s); if(r == -1) { resetConnection(true); setError(ErrProxyNeg); return; } else if(r == 1) { if(s.cmd != RET_SUCCESS) { #ifdef PROX_DEBUG fprintf(stderr, "SocksClient: client << Error >> [%02x]\n", s.cmd); #endif resetConnection(true); if(s.cmd == RET_UNREACHABLE) setError(ErrHostNotFound); else if(s.cmd == RET_CONNREFUSED) setError(ErrConnectionRefused); else setError(ErrProxyNeg); return; } #ifdef PROX_DEBUG fprintf(stderr, "SocksClient: client << Success >>\n"); #endif if(d->udp) { if(s.address_type == 0x03) d->udpAddr = s.host; else d->udpAddr = s.addr.toString(); d->udpPort = s.port; } setOpenMode(QIODevice::ReadWrite); QPointer<QObject> self = this; setOpenMode(QIODevice::ReadWrite); emit connected(); if(!self) return; if(!d->recvBuf.isEmpty()) { appendRead(d->recvBuf); d->recvBuf.resize(0); readyRead(); } } } }
void HttpPoll::http_result() { // check for death :) QPointer<QObject> self = this; syncFinished(); if(!self) return; // get id and packet QString id; QString cookie = d->http.getHeader("Set-Cookie"); int n = cookie.indexOf("ID="); if(n == -1) { reset(); error(ErrRead); return; } n += 3; int n2 = cookie.indexOf(';', n); if(n2 != -1) id = cookie.mid(n, n2-n); else id = cookie.mid(n); QByteArray block = d->http.body(); // session error? if(id.right(2) == ":0") { if(id == "0:0" && d->state == 2) { reset(); connectionClosed(); return; } else { reset(); error(ErrRead); return; } } d->ident = id; bool justNowConnected = false; if(d->state == 1) { d->state = 2; justNowConnected = true; } // sync up again soon if(bytesToWrite() > 0 || !d->closing) { d->t->start(d->polltime * 1000); } // connecting if(justNowConnected) { connected(); } else { if(!d->out.isEmpty()) { int x = d->out.size(); d->out.resize(0); takeWrite(x); bytesWritten(x); } } if(!self) return; if(!block.isEmpty()) { appendRead(block); readyRead(); } if(!self) return; if(bytesToWrite() > 0) { do_sync(); } else { if(d->closing) { reset(); delayedCloseFinished(); return; } } }