void TCClientSocket::socketConnected() { emit sendText(QString("TraceClient connected to port %1").arg(peerPort())); }
void DkConnection::checkState() { if (mState == WaitingForGreeting) { if (mCurrentDataType != Greeting) { abort(); return; } if (!hasEnoughData()) return; mBuffer = read(mNumBytesForCurrentDataType); if (mBuffer.size() != mNumBytesForCurrentDataType) { abort(); return; } if (!isValid()) { abort(); return; } if (!mIsGreetingMessageSent) sendGreetingMessage(mCurrentTitle); mState = ReadyForUse; mPortOfPeer = peerPort(); // save peer port ... otherwise connections where this instance is server can not be removed from peerList readGreetingMessage(); mBuffer.clear(); mNumBytesForCurrentDataType = 0; mCurrentDataType = Undefined; return; } if (mState==ReadyForUse && mCurrentDataType == startSynchronize) { if (!hasEnoughData()) return; mBuffer = read(mNumBytesForCurrentDataType); if (mBuffer.size() != mNumBytesForCurrentDataType) { abort(); return; } QDataStream ds(mBuffer); QList<quint16> synchronizedPeersOfOtherInstance; quint16 numberOfSynchronizedPeers; ds >> numberOfSynchronizedPeers; //qDebug() << "other client is sychronized with: "; for (int i=0; i < numberOfSynchronizedPeers; i++) { quint16 peerId; ds >> peerId; synchronizedPeersOfOtherInstance.push_back(peerId); //qDebug() << peerId; } mCurrentDataType = Undefined; mNumBytesForCurrentDataType = 0; mBuffer.clear(); if (!isValid()) { abort(); return; } mState = Synchronized; if (!mIsSynchronizeMessageSent) sendStartSynchronizeMessage(); mSynchronizedTimer->stop(); emit connectionStartSynchronize(synchronizedPeersOfOtherInstance, this); return; } if (mState==Synchronized && mCurrentDataType == stopSynchronize) { mState=ReadyForUse; this->mIsSynchronizeMessageSent=false; emit connectionStopSynchronize(this); mBuffer = read(mNumBytesForCurrentDataType); if (mBuffer.size() != mNumBytesForCurrentDataType) { abort(); return; } mCurrentDataType = Undefined; mNumBytesForCurrentDataType = 0; mBuffer.clear(); return; } if (mCurrentDataType == GoodBye) { //qDebug() << "received GoodBye from " << peerAddress() << ":" << peerPort(); emit connectionGoodBye(this); mCurrentDataType = Undefined; mNumBytesForCurrentDataType = 0; mBuffer.clear(); abort(); return; } }
//### query management qint64 Session::sendQuery(OutboundPkt &outboundPkt, QueryMethods *methods, const QVariant &extra, const QString &name) { Q_ASSERT (m_sessionId); Q_ASSERT (m_dc->authKeyId()); qint32 *data = outboundPkt.buffer(); qint32 ints = outboundPkt.length(); qCDebug(TG_CORE_SESSION) << "Sending query of size" << 4 * ints << "to DC" << m_dc->id() << "at" << peerName() << ":" << peerPort() << "by session" << QString::number(m_sessionId, 16); Query *q = new Query(this); q->setData(data, ints); q->setMsgId(encryptSendMessage(data, ints, 1)); q->setSeqNo(m_seqNo - 1); qCDebug(TG_CORE_SESSION) << "msgId is" << QString::number(q->msgId(), 16); q->setMethods(methods); q->setAcked(false); q->setExtra(extra); q->setName(name); if (mSettings->resendQueries()) { connect(q, SIGNAL(timeout(Query*)), this, SLOT(resendQuery(Query*)), Qt::UniqueConnection); q->startTimer(QUERY_TIMEOUT); }
void ExtPlaneConnection::socketError(QAbstractSocket::SocketError err) { INFO << "Socket error:" << errorString(); server_ok = false; emit connectionMessage(errorString() + " : " + peerName() + ":" + QString::number(peerPort())); reconnectTimer.setInterval(5000); reconnectTimer.start(); }
void Connection::processReadyRead() { if (state == WaitingForGreeting) { if (!readProtocolHeader()) return; if (currentDataType != Greeting) { abort(); return; } state = ReadingGreeting; } if (state == ReadingGreeting) { if (!hasEnoughData()) return; buffer = read(numBytesForCurrentDataType); if (buffer.size() != numBytesForCurrentDataType) { abort(); return; } username = QString(buffer) + '@' + peerAddress().toString() + ':' + QString::number(peerPort()); currentDataType = Undefined; numBytesForCurrentDataType = 0; buffer.clear(); if (!isValid()) { abort(); return; } if (!isGreetingMessageSent) sendGreetingMessage(); pingTimer.start(); pongTime.start(); state = ReadyForUse; emit readyForUse(); } do { if (currentDataType == Undefined) { if (!readProtocolHeader()) return; } if (!hasEnoughData()) return; processData(); } while (bytesAvailable() > 0); }
void Socket::emitDisconnect() { emit sockDisconnect(socketID,peerAddress().toString(),peerPort()); }
static UDP_Assoc *newUA(Connection *Conn,UDP_Assoc *uav[],PCStr(clhost),int clport,PCStr(svhost),int svport) { int ux; UDP_Assoc *ua; int svsock; int rcode; CStr(msg,128); CStr(lhost,256); int lport; CStr(local,256); CStr(remote,256); int SOCKSctl = -1; CStr(rh,256); int rp; rcode = -1; if( !permitted(clhost,clport,svhost,svport) ) return NULL; sprintf(local,"*:*"); sprintf(remote,"%s:%d",svhost,svport); Conn->from_cached = 1; VA_setClientAddr(Conn,clhost,clport,0); Conn->no_dstcheck_proto = serviceport("tcprelay"); initConnect(Conn); setupConnect(Conn); ServerFlags |= PF_UDP; Conn->sv.p_SOCKSCTL = -1; svsock = connectViaSocks(Conn,svhost,svport,AVStr(rh),&rp); if( 0 <= svsock ){ sv1log("via SOCKS UDP:%d@%s CTL:%d\n",svsock, Conn->sv.p_SOCKSADDR,Conn->sv.p_SOCKSCTL); SOCKSctl = Conn->sv.p_SOCKSCTL; goto ADDUA; } svsock = VSocket(Conn,"CNCT/UDPRELAY",-1,AVStr(local),AVStr(remote),""); if( 0 <= svsock ){ /* setNonblockingIO("UDPRELAY-SV",svsock,1); */ SetNonblockingIO("UDPRELAY-SV",svsock,1); goto ADDUA; } strcpy(lhost,"*"); lport = 0; SRCIFfor(Conn,"udprelay",svhost,svport,AVStr(lhost),&lport); if( strcmp(lhost,"*") == 0 ) lhost[0] = 0; svsock = server_open("UDPRELAY",AVStr(lhost),lport,-1); if( svsock < 0 ){ if( uav[0] == 0 ){ return NULL; } ux = longestIdle(uav); sv1log("push out longest idle 1 [%d]\n",ux); delUA(uav,ux,"NoMoreSocket",1); svsock = server_open("UDPRELAY",AVStr(lhost),lport,-1); if( svsock < 0 ) return NULL; } if( UDPRELAY_RPORT_FIX ){ rcode = __connectServer(svsock,"UDPRELAY","udprelay",svhost,svport /*,1*/); if( rcode < 0 ){ sv1log("UDPRELAY: connect(%d) error %d\n",svsock,rcode); close(svsock); return NULL; } } ADDUA: for( ux = 0; uav[ux]; ux++ ) ; if( UDPRELAY_MAXASSOC <= ux ){ ux = longestIdle(uav); sv1log("push out longest idle 2 [%d]\n",ux); delUA(uav,ux,"NoMoreClient",0); } ua = (UDP_Assoc*)calloc(1,sizeof(UDP_Assoc)); ua->ua_id = ++uaid; ua->ua_ctime = time(0); ua->ua_clhost = stralloc(clhost); ua->ua_clport = clport; ua->ua_svsock = svsock; ua->ua_svbound = (0 < peerPort(svsock)); ua->ua_svtcp = !isUDPsock(svsock); ua->ua_svhost = stralloc(svhost); ua->ua_svport = svport; ua->ua_svSOCKS = SOCKSctl; VSA_aptosa(&ua->ua_SOCKSADDR,Conn->sv.p_SOCKSADDR); uav[ux] = ua; msghead(AVStr(msg),"start",ua,ux); sv1log("%s > %s:%d\n",msg,svhost,svport); return ua; }
//### query management qint64 Session::sendQuery(OutboundPkt &outboundPkt, QueryMethods *methods, const QVariant &extra, const QString &name) { Q_ASSERT (m_sessionId); Q_ASSERT (m_dc->authKeyId()); qint32 *data = outboundPkt.buffer(); qint32 ints = outboundPkt.length(); // prepend init connection header to outboundPkt if initConnectionNeeded // Note: 'wrap' needs to be declare outside of if to persist until encriptSendMessage is completed OutboundPkt wrap(mSettings); if (m_initConnectionNeeded) { wrap.initConnection(); wrap.appendOutboundPkt(outboundPkt); data = wrap.buffer(); ints = wrap.length(); m_initConnectionNeeded = false; } qCDebug(TG_CORE_SESSION) << "Sending query of size" << 4 * ints << "to DC" << m_dc->id() << "at" << peerName() << ":" << peerPort() << "by session" << QString::number(m_sessionId, 16); Query *q = new Query(this); q->setData(data, ints); q->setMsgId(encryptSendMessage(data, ints, 1)); q->setMainMsgId(q->msgId()); q->setSeqNo(m_seqNo - 1); qCDebug(TG_CORE_SESSION) << "msgId is" << QString::number(q->msgId(), 16); q->setMethods(methods); q->setAcked(false); q->setExtra(extra); q->setName(name); if (mSettings->resendQueries()) { connect(q, &Query::timeout, this, &Session::resendQuery, Qt::UniqueConnection); q->startTimer(QUERY_TIMEOUT); } m_pendingQueries.insert(q->msgId(), q); return q->msgId(); }