From fca6cc8861e7718ffd5db95e9bc90a6c9221393f Mon Sep 17 00:00:00 2001 From: drbob Date: Sun, 17 Jul 2011 23:29:53 +0000 Subject: [PATCH] Added Bandwidth and Delay parameters to BitDHT Connect Messages. * Bandwidth are for RELAY connections. * Delays are for Exclusive NAT Proxy connections. * Lots of changes to achieve this. (Mainly added "param" to msgs). * Various Bugfixes too. * Updated BITDHT Version to 01 as we've changed Message Format. * Increased Connection Timeout to 180 * Added extra checks to kill duplicate connections early. git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-netupgrade@4462 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- src/bitdht/bdconnection.cc | 353 +++++++++++++++++++++++++++++-------- src/bitdht/bdconnection.h | 38 ++-- src/bitdht/bdiface.h | 7 +- src/bitdht/bdmanager.cc | 15 +- src/bitdht/bdmanager.h | 8 +- src/bitdht/bdmsgs.cc | 23 +-- src/bitdht/bdmsgs.h | 2 +- src/bitdht/bdnode.cc | 39 +++- src/bitdht/bdnode.h | 12 +- src/udp/udpbitdht.cc | 16 +- src/udp/udpbitdht.h | 5 +- 11 files changed, 371 insertions(+), 147 deletions(-) diff --git a/src/bitdht/bdconnection.cc b/src/bitdht/bdconnection.cc index 0658b10..71d65f4 100644 --- a/src/bitdht/bdconnection.cc +++ b/src/bitdht/bdconnection.cc @@ -37,9 +37,11 @@ /* * #define DEBUG_NODE_CONNECTION 1 * #define DEBUG_NODE_CONNECTION_EXTRA 1 + * #define DEBUG_CONNECTION_DELAY 1 */ #define DEBUG_NODE_CONNECTION 1 +#define DEBUG_CONNECTION_DELAY 1 #define BITDHT_CR_PAUSE_SHORT_PERIOD 1 @@ -142,7 +144,7 @@ void bdConnectManager::printConnections() * 2) Using a Proxy. */ -int bdConnectManager::requestConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start) +int bdConnectManager::requestConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay, uint32_t start) { /* check if connection obj already exists */ #ifdef DEBUG_NODE_CONNECTION @@ -187,7 +189,7 @@ int bdConnectManager::requestConnection(struct sockaddr_in *laddr, bdNodeId *tar } else { - return requestConnection_proxy(laddr, target, mode); + return requestConnection_proxy(laddr, target, mode, delay); } } @@ -321,7 +323,7 @@ int bdConnectManager::requestConnection_direct(struct sockaddr_in *laddr, bdNode } -int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode) +int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay) { #ifdef DEBUG_NODE_CONNECTION @@ -339,7 +341,7 @@ int bdConnectManager::requestConnection_proxy(struct sockaddr_in *laddr, bdNodeI /* create a bdConnect, and put into the queue */ bdConnectionRequest connreq; - connreq.setupProxyConnection(laddr, target, mode); + connreq.setupProxyConnection(laddr, target, mode, delay); /* grab any peers from any existing query */ std::list::iterator pit; @@ -723,8 +725,9 @@ void bdConnectManager::iterateConnectionRequests() bdId destId; destId.id = it->second.mTarget; + int param = 0; callbackConnect(&srcId, &proxyId, &destId, it->second.mMode, - BD_PROXY_CONNECTION_START_POINT, + BD_PROXY_CONNECTION_START_POINT, param, BITDHT_CONNECT_CB_REQUEST, it->second.mErrCode); /* cleanup */ @@ -764,6 +767,19 @@ int bdConnectManager::startConnectionAttempt(bdConnectionRequest *req) int mode = req->mMode; + /* calculate the delay... accounting for the time since the request */ + time_t now = time(NULL); + int timeElapsed = (now - req->mRequestTS); + int delay = req->mDelay - timeElapsed; + int absDelay = 0; + if (delay > 0) + { + absDelay = delay; + } + std::cerr << "bdConnectManager::startConnectionAttempt() TimeSinceReq: " << timeElapsed << " Original Delay: " << req->mDelay; + std::cerr << " AbsDelay: " << absDelay; + std::cerr << std::endl; + destConnAddr.id = req->mTarget; bdsockaddr_clear(&(destConnAddr.addr)); @@ -803,7 +819,7 @@ int bdConnectManager::startConnectionAttempt(bdConnectionRequest *req) } - return startConnectionAttempt(&proxyId, &srcConnAddr, &destConnAddr, mode); + return startConnectionAttempt(&proxyId, &srcConnAddr, &destConnAddr, mode, absDelay); } @@ -825,13 +841,14 @@ int bdConnectManager::startConnectionAttempt(bdConnectionRequest *req) */ void bdConnectManager::callbackConnectRequest(bdId *srcId, bdId *proxyId, bdId *destId, - int mode, int point, int cbtype, int errcode) + int mode, int point, int param, int cbtype, int errcode) { /* Check if we are the originator of the Connect Request. If so, then we do stuff to the CR. */ std::cerr << "bdConnectManager::callbackConnectRequest() "; std::cerr << "mode: " << mode; std::cerr << " point: " << point; + std::cerr << " param: " << param; std::cerr << " cbtype: " << cbtype; std::cerr << " errcode: " << errcode; std::cerr << std::endl; @@ -885,7 +902,7 @@ void bdConnectManager::callbackConnectRequest(bdId *srcId, bdId *proxyId, bdId * std::cerr << std::endl; } - callbackConnect(srcId, proxyId, destId, mode, point, cbtype, errcode); + callbackConnect(srcId, proxyId, destId, mode, point, param, cbtype, errcode); return; } @@ -1140,7 +1157,7 @@ void bdConnectManager::callbackConnectRequest(bdId *srcId, bdId *proxyId, bdId * cr->mErrCode = errcode; /* just pass on the callbackConnect() */ - callbackConnect(srcId, proxyId, destId, mode, point, cbtype, errcode); + callbackConnect(srcId, proxyId, destId, mode, point, param, cbtype, errcode); return; // CALLBACK FINISHED for FAILURE CODES. @@ -1152,7 +1169,7 @@ void bdConnectManager::callbackConnectRequest(bdId *srcId, bdId *proxyId, bdId * cr->mState = BITDHT_CONNREQUEST_EXTCONNECT; cr->mStateTS = now; - callbackConnect(srcId, proxyId, destId, mode, point, cbtype, errcode); + callbackConnect(srcId, proxyId, destId, mode, point, param, cbtype, errcode); } break; @@ -1181,7 +1198,7 @@ void bdConnectManager::callbackConnectRequest(bdId *srcId, bdId *proxyId, bdId * * In all cases, destConnAddr doesn't need to contain a valid address. */ -int bdConnectManager::startConnectionAttempt(bdId *proxyId, bdId *srcConnAddr, bdId *destConnAddr, int mode) +int bdConnectManager::startConnectionAttempt(bdId *proxyId, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delay) { #ifdef DEBUG_NODE_CONNECTION std::cerr << "bdConnectManager::startConnectionAttempt()"; @@ -1210,12 +1227,24 @@ int bdConnectManager::startConnectionAttempt(bdId *proxyId, bdId *srcConnAddr, b return 0; } + + /* Switch the order of peers around to test for "opposite connections" */ + if (NULL != findSimilarConnection(&(destConnAddr->id), &(srcConnAddr->id))) + { + std::cerr << "bdConnectManager::startConnectAttempt() WARNING Found Similar Connection. returning 0"; + std::cerr << std::endl; + + return 0; + } + + { // DO A CALLBACK to TELL higher levels, we are starting a connection attempt. int point = BD_PROXY_CONNECTION_START_POINT; int cbtype = BITDHT_CONNECT_CB_REQUEST; int errcode = 0; - callbackConnect(srcConnAddr, proxyId, destConnAddr, mode, point, cbtype, errcode); + int param = 0; + callbackConnect(srcConnAddr, proxyId, destConnAddr, mode, point, param, cbtype, errcode); } /* INSTALL a NEW CONNECTION */ @@ -1229,14 +1258,14 @@ int bdConnectManager::startConnectionAttempt(bdId *proxyId, bdId *srcConnAddr, b } else { - conn->ConnectionSetup(proxyId, srcConnAddr, destConnAddr, mode); + conn->ConnectionSetup(proxyId, srcConnAddr, destConnAddr, mode, delay); } int msgtype = BITDHT_MSG_TYPE_CONNECT_REQUEST; int status = BITDHT_CONNECT_ANSWER_OKAY; mPub->send_connect_msg(&(conn->mProxyId), msgtype, - &(conn->mSrcConnAddr), &(conn->mDestConnAddr), conn->mMode, status); + &(conn->mSrcConnAddr), &(conn->mDestConnAddr), conn->mMode, conn->mMaxDelay, status); return 1; } @@ -1254,7 +1283,7 @@ int bdConnectManager::startConnectionAttempt(bdId *proxyId, bdId *srcConnAddr, b * */ -void bdConnectManager::AuthConnectionOk(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc) +void bdConnectManager::AuthConnectionOk(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc, int bandwidth, int delay) { #ifdef DEBUG_NODE_CONNECTION @@ -1309,13 +1338,13 @@ void bdConnectManager::AuthConnectionOk(bdId *srcId, bdId *proxyId, bdId *destId std::cerr << std::endl; #endif /*** XXX MUST RECEIVE THE ADDRESS FROM DEST for connection */ - conn->AuthoriseEndConnection(srcId, proxyId, destId, mode, loc); + conn->AuthoriseEndConnection(srcId, proxyId, destId, mode, loc, delay); /* we respond to the proxy which will finalise connection */ int msgtype = BITDHT_MSG_TYPE_CONNECT_REPLY; int status = BITDHT_CONNECT_ANSWER_OKAY; mPub->send_connect_msg(&(conn->mProxyId), msgtype, - &(conn->mSrcConnAddr), &(conn->mDestConnAddr), conn->mMode, status); + &(conn->mSrcConnAddr), &(conn->mDestConnAddr), conn->mMode, conn->mMaxDelay, status); return; } @@ -1339,13 +1368,13 @@ void bdConnectManager::AuthConnectionOk(bdId *srcId, bdId *proxyId, bdId *destId #endif /* SEARCH for IP:Port of destination is done before AUTH */ - - conn->AuthoriseProxyConnection(srcId, proxyId, destId, mode, loc); + conn->AuthoriseProxyConnection(srcId, proxyId, destId, mode, loc, bandwidth); int msgtype = BITDHT_MSG_TYPE_CONNECT_REQUEST; int status = BITDHT_CONNECT_ANSWER_OKAY; + int param = 0; mPub->send_connect_msg(&(conn->mDestId), msgtype, - &(conn->mSrcConnAddr), &(conn->mDestConnAddr), conn->mMode, status); + &(conn->mSrcConnAddr), &(conn->mDestConnAddr), conn->mMode, param, status); } else { @@ -1394,7 +1423,7 @@ void bdConnectManager::AuthConnectionNo(bdId *srcId, bdId *proxyId, bdId *destId std::cerr << std::endl; #endif mPub->send_connect_msg(&(conn->mSrcId), msgtype, - &(conn->mSrcConnAddr), &(conn->mDestConnAddr), mode, status); + &(conn->mSrcConnAddr), &(conn->mDestConnAddr), mode, 0, status); cleanConnection(&(srcId->id), &(proxyId->id), &(destId->id)); return; @@ -1408,7 +1437,7 @@ void bdConnectManager::AuthConnectionNo(bdId *srcId, bdId *proxyId, bdId *destId std::cerr << std::endl; #endif mPub->send_connect_msg(&(conn->mProxyId), msgtype, - &(conn->mSrcConnAddr), &(conn->mDestConnAddr), mode, status); + &(conn->mSrcConnAddr), &(conn->mDestConnAddr), mode, 0, status); cleanConnection(&(srcId->id), &(proxyId->id), &(destId->id)); @@ -1421,7 +1450,7 @@ void bdConnectManager::AuthConnectionNo(bdId *srcId, bdId *proxyId, bdId *destId std::cerr << std::endl; #endif mPub->send_connect_msg(&(conn->mSrcId), msgtype, - &(conn->mSrcConnAddr), &(conn->mDestConnAddr), mode, status); + &(conn->mSrcConnAddr), &(conn->mDestConnAddr), mode, 0, status); cleanConnection(&(srcId->id), &(proxyId->id), &(destId->id)); @@ -1467,8 +1496,9 @@ void bdConnectManager::iterateConnections() { uint32_t errCode = createConnectionErrorCode(0, BITDHT_CONNECT_ERROR_TIMEOUT,it->second.mPoint); + int param = 0; callbackConnect(&(it->second.mSrcId),&(it->second.mProxyId), - &(it->second.mDestId), it->second.mMode, it->second.mPoint, + &(it->second.mDestId), it->second.mMode, it->second.mPoint, param, BITDHT_CONNECT_CB_FAILED, errCode); } @@ -1483,19 +1513,41 @@ void bdConnectManager::iterateConnections() #endif it->second.mLastStart = now; it->second.mRetryCount++; + + int bandwidth = it->second.mBandwidth; + int delay = it->second.mMaxDelay; + time_t elapsedTime = (time(NULL) - it->second.mConnectionStartTS); + int remainingDelay = delay - elapsedTime; + if (remainingDelay < 0) + { + remainingDelay = 0; + } + + std::cerr << "bdConnectManager::iterateConnections() Start/ACK cycle: remaining Delay: " << remainingDelay; + std::cerr << std::endl; + std::cerr << "bdConnectManager::iterateConnections() Start/ACK cycle: Bandwidth: " << bandwidth; + std::cerr << std::endl; + + // Must calculate the correct delay's here!!!! + int delayOrBandwidth = remainingDelay; + if (it->second.mMode == BITDHT_CONNECT_MODE_RELAY) + { + delayOrBandwidth = bandwidth; + } + if (!it->second.mSrcAck) { int msgtype = BITDHT_MSG_TYPE_CONNECT_START; mPub->send_connect_msg(&(it->second.mSrcId), msgtype, &(it->second.mSrcConnAddr), &(it->second.mDestConnAddr), - it->second.mMode, it->second.mBandwidth); + it->second.mMode, delayOrBandwidth, BITDHT_CONNECT_ANSWER_OKAY); } if (!it->second.mDestAck) { int msgtype = BITDHT_MSG_TYPE_CONNECT_START; mPub->send_connect_msg(&(it->second.mDestId), msgtype, &(it->second.mSrcConnAddr), &(it->second.mDestConnAddr), - it->second.mMode, it->second.mBandwidth); + it->second.mMode, delayOrBandwidth, BITDHT_CONNECT_ANSWER_OKAY); } } } @@ -1521,10 +1573,10 @@ void bdConnectManager::iterateConnections() void bdConnectManager::callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId, - int mode, int point, int cbtype, int errcode) + int mode, int point, int param, int cbtype, int errcode) { /* This is overloaded at a higher level */ - mPub->callbackConnect(srcId, proxyId, destId, mode, point, cbtype, errcode); + mPub->callbackConnect(srcId, proxyId, destId, mode, point, param, cbtype, errcode); } @@ -1585,8 +1637,8 @@ bdConnection::bdConnection() */ /* Connection State, and TimeStamp of Update */ - int mState = 0; - time_t mLastEvent = 0; + mState = 0; + mLastEvent = 0; // ///* Addresses of Start/Proxy/End Nodes */ //bdId mSrcId; @@ -1596,24 +1648,27 @@ bdConnection::bdConnection() ///* Where we are in the connection, //* and what connection mode. //*/ - int mPoint = 0; - int mMode = 0; + mPoint = 0; + mMode = 0; // ///* must have ip:ports of connection ends (if proxied) */ //bdId mSrcConnAddr; //bdId mDestConnAddr; // - int mBandwidth = 0; + mBandwidth = 0; + mMaxDelay = 0; + mConnectionStartTS = 0; + // ///* START/ACK Finishing ****/ - time_t mLastStart = 0; /* timer for retries */ - int mRetryCount = 0; /* retry counter */ + mLastStart = 0; /* timer for retries */ + mRetryCount = 0; /* retry counter */ // - bool mSrcAck = false; - bool mDestAck = false; + mSrcAck = false; + mDestAck = false; // //// Completion TS. - time_t mCompletedTS = 0; + mCompletedTS = 0; } /* heavy check, used to check for alternative connections, coming from other direction @@ -1774,7 +1829,7 @@ int bdConnectManager::cleanConnectionBySender(bdId *sender, bdId *src, bdId *des -int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode) +int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delay) { #ifdef DEBUG_NODE_CONNECTION std::cerr << "bdConnectManager::recvedConnectionRequest()"; @@ -1792,8 +1847,9 @@ int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId int pos = determinePosition(&(id->id), &(srcConnAddr->id), &(destConnAddr->id)); uint32_t status = createConnectionErrorCode(0, BITDHT_CONNECT_ERROR_UNSUPPORTED, pos); + int param = 0; int msgtype = BITDHT_MSG_TYPE_CONNECT_REPLY; - mPub->send_connect_msg(id, msgtype, srcConnAddr, destConnAddr, mode, status); + mPub->send_connect_msg(id, msgtype, srcConnAddr, destConnAddr, mode, 0, status); return 0; } @@ -1812,7 +1868,8 @@ int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId uint32_t status = createConnectionErrorCode(0, BITDHT_CONNECT_ERROR_DUPLICATE, conn->mPoint); int msgtype = BITDHT_MSG_TYPE_CONNECT_REPLY; - mPub->send_connect_msg(id, msgtype, srcConnAddr, destConnAddr, mode, status); + int param = 0; + mPub->send_connect_msg(id, msgtype, srcConnAddr, destConnAddr, mode, param, status); return 0; } @@ -1829,7 +1886,8 @@ int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId uint32_t status = createConnectionErrorCode(0, BITDHT_CONNECT_ERROR_DUPLICATE, pos); int msgtype = BITDHT_MSG_TYPE_CONNECT_REPLY; - mPub->send_connect_msg(id, msgtype, srcConnAddr, destConnAddr, mode, status); + int param = 0; + mPub->send_connect_msg(id, msgtype, srcConnAddr, destConnAddr, mode, param, status); return 0; } @@ -1855,8 +1913,9 @@ int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId std::cerr << *conn; std::cerr << std::endl; #endif + int param = 0; callbackConnect(&(conn->mSrcId),&(conn->mProxyId),&(conn->mDestId), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_AUTH, + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_AUTH, BITDHT_CONNECT_ERROR_NONE); } else @@ -1906,19 +1965,20 @@ int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId std::cerr << "asking for AUTH to continue"; std::cerr << std::endl; - conn->ConnectionRequestProxy(id, srcConnAddr, &mOwnId, &destId, mode); + conn->ConnectionRequestProxy(id, srcConnAddr, &mOwnId, &destId, mode, delay); /* ALLOW AUTO AUTH for MID Proxy Connections. */ if ((mConfigAutoProxy) && (mode == BITDHT_CONNECT_MODE_PROXY)) { AuthConnectionOk(&(conn->mSrcId),&(conn->mProxyId),&(conn->mDestId), - conn->mMode, conn->mPoint); + conn->mMode, conn->mPoint, 0, 0); } else { + int param = 0; callbackConnect(&(conn->mSrcId),&(conn->mProxyId),&(conn->mDestId), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_AUTH, + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_AUTH, BITDHT_CONNECT_ERROR_NONE); } } @@ -1932,7 +1992,8 @@ int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId /* send FAIL message to SRC */ int msgtype = BITDHT_MSG_TYPE_CONNECT_REPLY; uint32_t status = createConnectionErrorCode(0, BITDHT_CONNECT_ERROR_NOADDRESS, point); - mPub->send_connect_msg(id, msgtype, srcConnAddr, destConnAddr, mode, status); + int param = 0; + mPub->send_connect_msg(id, msgtype, srcConnAddr, destConnAddr, mode, param, status); /* remove connection */ bdConnectManager::cleanConnectionBySender(id, srcConnAddr, destConnAddr); @@ -1948,8 +2009,9 @@ int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId conn->ConnectionRequestEnd(id, srcConnAddr, destConnAddr, mode); + int param = 0; callbackConnect(&(conn->mSrcId),&(conn->mProxyId),&(conn->mDestId), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_AUTH, + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_AUTH, BITDHT_CONNECT_ERROR_NONE); } } @@ -1963,7 +2025,7 @@ int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId * */ -int bdConnectManager::recvedConnectionReply(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int status) +int bdConnectManager::recvedConnectionReply(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delay, int status) { /* retrieve existing connection data */ bdConnection *conn = findExistingConnectionBySender(id, srcConnAddr, destConnAddr); @@ -2016,13 +2078,15 @@ int bdConnectManager::recvedConnectionReply(bdId *id, bdId *srcConnAddr, bdId *d if (conn->mPoint == BD_PROXY_CONNECTION_START_POINT) { /* As we started the connection, callback internally first! */ + int param = 0; callbackConnectRequest(&(conn->mSrcId),&(conn->mProxyId),&(conn->mDestId), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_FAILED, errCode); + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_FAILED, errCode); } else { + int param = 0; callbackConnect(&(conn->mSrcId),&(conn->mProxyId),&(conn->mDestId), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_FAILED, errCode); + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_FAILED, errCode); } /* Kill Connection always */ @@ -2044,14 +2108,15 @@ int bdConnectManager::recvedConnectionReply(bdId *id, bdId *srcConnAddr, bdId *d std::cerr << std::endl; /* Upgrade Connection to Finishing Mode */ - conn->upgradeProxyConnectionToFinish(id, srcConnAddr, destConnAddr, mode, status); + conn->upgradeProxyConnectionToFinish(id, srcConnAddr, destConnAddr, mode, delay, status); /* do Callback for Pending Connection */ /* DONT CALLBACK in AutoProxy Mode: (PROXY & mConfigAutoProxy) */ if ((conn->mMode != BITDHT_CONNECT_MODE_PROXY) || (!mConfigAutoProxy)) { + int param = 0; callbackConnect(&(conn->mSrcId),&(conn->mProxyId),&(conn->mDestId), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_PENDING, + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_PENDING, BITDHT_CONNECT_ERROR_NONE); } @@ -2079,14 +2144,16 @@ int bdConnectManager::recvedConnectionReply(bdId *id, bdId *srcConnAddr, bdId *d /* DONT CALLBACK in AutoProxy Mode: (PROXY & mConfigAutoProxy) */ if ((conn->mMode != BITDHT_CONNECT_MODE_PROXY) || (!mConfigAutoProxy)) { + int param = 0; callbackConnect(&(conn->mSrcId),&(conn->mProxyId),&(conn->mDestId), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_FAILED, errCode); + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_FAILED, errCode); } /* send on message to SRC */ int msgtype = BITDHT_MSG_TYPE_CONNECT_REPLY; - mPub->send_connect_msg(&(conn->mSrcId), msgtype, &(conn->mSrcConnAddr), &(conn->mDestConnAddr), mode, errCode); + int param = 0; + mPub->send_connect_msg(&(conn->mSrcId), msgtype, &(conn->mSrcConnAddr), &(conn->mDestConnAddr), mode, param, errCode); /* connection is killed */ cleanConnectionBySender(id, srcConnAddr, destConnAddr); @@ -2106,7 +2173,7 @@ int bdConnectManager::recvedConnectionReply(bdId *id, bdId *srcConnAddr, bdId *d * Acks are set, and connections completed if possible (including callback!). */ -int bdConnectManager::recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int bandwidth) +int bdConnectManager::recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delayOrBandwidth) { std::cerr << "bdConnectManager::recvedConnectionStart()"; std::cerr << std::endl; @@ -2147,7 +2214,7 @@ int bdConnectManager::recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *d int msgtype = BITDHT_MSG_TYPE_CONNECT_ACK; int status = BITDHT_CONNECT_ANSWER_OKAY; - mPub->send_connect_msg(id, msgtype, &(conn->mSrcId), &(conn->mDestId), mode, status); + mPub->send_connect_msg(id, msgtype, &(conn->mSrcId), &(conn->mDestId), mode, 0, status); /* do complete Callback */ @@ -2157,7 +2224,19 @@ int bdConnectManager::recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *d std::cerr << "bdConnectManager::recvedConnectionStart() Switching State to COMPLETED, doing callback"; std::cerr << std::endl; - conn->CompleteConnection(id, srcConnAddr, destConnAddr); + int param = delayOrBandwidth; + int bandwidth = 0; + int delay = 0; + if (conn->mMode == BITDHT_CONNECT_MODE_RELAY) + { + bandwidth = param; + } + else + { + delay = param; + } + + conn->CompleteConnection(id, srcConnAddr, destConnAddr, bandwidth, delay); std::cerr << "bdConnectManager::recvedConnectionStart() Connection State: "; std::cerr << *conn; @@ -2168,7 +2247,7 @@ int bdConnectManager::recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *d { /* internal callback first */ callbackConnectRequest(&(conn->mSrcConnAddr),&(conn->mProxyId),&(conn->mDestConnAddr), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_START, + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_START, BITDHT_CONNECT_ERROR_NONE); } @@ -2176,7 +2255,7 @@ int bdConnectManager::recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *d { /* internal callback first */ callbackConnectRequest(&(conn->mSrcConnAddr),&(conn->mProxyId),&(conn->mDestConnAddr), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_START, + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_START, BITDHT_CONNECT_ERROR_NONE); } @@ -2226,7 +2305,7 @@ int bdConnectManager::recvedConnectionAck(bdId *id, bdId *srcConnAddr, bdId *des if (conn->mState != BITDHT_CONNECTION_WAITING_ACK) { /* ERROR */ - std::cerr << "bdConnectManager::recvedConnectionAck() conn->mState != WAITING_ACK, actual State: " << conn->mState; + std::cerr << "bdConnectManager::recvedConnectionAck() ERROR conn->mState != WAITING_ACK, actual State: " << conn->mState; std::cerr << std::endl; return 0; @@ -2262,8 +2341,9 @@ int bdConnectManager::recvedConnectionAck(bdId *id, bdId *srcConnAddr, bdId *des // Also callback to ConnectionRequest first. // ACTUALLY we are END, so shouldn't (AT This Point do this). // MUST callback via ConnectRequest - to prevent duplicate REQUESTS. + int param = 0; callbackConnectRequest(&(conn->mSrcConnAddr),&(conn->mProxyId),&(conn->mDestId), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_START, + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_START, BITDHT_CONNECT_ERROR_NONE); } @@ -2272,8 +2352,9 @@ int bdConnectManager::recvedConnectionAck(bdId *id, bdId *srcConnAddr, bdId *des /* DONT CALLBACK in AutoProxy Mode: (PROXY & mConfigAutoProxy) */ if ((conn->mMode != BITDHT_CONNECT_MODE_PROXY) || (!mConfigAutoProxy)) { + int param = 0; callbackConnect(&(conn->mSrcId),&(conn->mProxyId),&(conn->mDestId), - conn->mMode, conn->mPoint, BITDHT_CONNECT_CB_PROXY, + conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_PROXY, BITDHT_CONNECT_ERROR_NONE); } @@ -2295,7 +2376,7 @@ int bdConnectManager::recvedConnectionAck(bdId *id, bdId *srcConnAddr, bdId *des // Initialise a new Connection (request by User) // Any Connection initialised at Source (START_POINT), prior to Auth. -int bdConnection::ConnectionSetup(bdId *proxyId, bdId *srcConnAddr, bdId *destId, int mode) +int bdConnection::ConnectionSetup(bdId *proxyId, bdId *srcConnAddr, bdId *destId, int mode, int delay) { mState = BITDHT_CONNECTION_WAITING_START; /* or REPLY, no AUTH required */ mLastEvent = time(NULL); @@ -2309,6 +2390,15 @@ int bdConnection::ConnectionSetup(bdId *proxyId, bdId *srcConnAddr, bdId *destId mSrcConnAddr = *srcConnAddr; /* self, full ID/IP */ mDestConnAddr = *destId; /* IP unknown */ + mBandwidth = 0; + mMaxDelay = delay; + mConnectionStartTS = time(NULL); + +#ifdef DEBUG_CONNECTION_DELAY + std::cerr << "bdConnection::ConnectionSetup(): delay: " << mMaxDelay; + std::cerr << std::endl; +#endif + /* clear IP Addresses to enforce this */ bdsockaddr_clear(&(mSrcId.addr)); bdsockaddr_clear(&(mDestId.addr)); @@ -2333,6 +2423,10 @@ int bdConnection::ConnectionSetupDirect(bdId *destId, bdId *srcConnAddr) mSrcConnAddr = *srcConnAddr; /* self, full ID/IP */ mDestConnAddr = *destId; /* IP unknown */ + mBandwidth = 0; + mMaxDelay = 0; + mConnectionStartTS = time(NULL); + /* clear IP Addresses to enforce this */ bdsockaddr_clear(&(mSrcId.addr)); bdsockaddr_clear(&(mDestConnAddr.addr)); @@ -2360,6 +2454,10 @@ int bdConnection::ConnectionRequestDirect(bdId *id, bdId *srcConnAddr, bdId *des mSrcConnAddr = *srcConnAddr; /* connect address ID/IP known */ mDestConnAddr = *destId; /* self IP unknown */ + mBandwidth = 0; + mMaxDelay = 0; + mConnectionStartTS = time(NULL); + /* clear IP Addresses to enforce this */ bdsockaddr_clear(&(mDestId.addr)); bdsockaddr_clear(&(mDestConnAddr.addr)); @@ -2371,7 +2469,7 @@ int bdConnection::ConnectionRequestDirect(bdId *id, bdId *srcConnAddr, bdId *des // Proxy Connection initialised at Proxy (MID_POINT), prior to Auth. -int bdConnection::ConnectionRequestProxy(bdId *id, bdId *srcConnAddr, bdNodeId *ownId, bdId *destId, int mode) +int bdConnection::ConnectionRequestProxy(bdId *id, bdId *srcConnAddr, bdNodeId *ownId, bdId *destId, int mode, int delay) { mState = BITDHT_CONNECTION_WAITING_AUTH; mLastEvent = time(NULL); @@ -2385,6 +2483,15 @@ int bdConnection::ConnectionRequestProxy(bdId *id, bdId *srcConnAddr, bdNodeId * mSrcConnAddr = *srcConnAddr; mDestConnAddr = *destId; /* other peer, IP unknown */ + mBandwidth = 0; + mMaxDelay = delay; + mConnectionStartTS = time(NULL); + +#ifdef DEBUG_CONNECTION_DELAY + std::cerr << "bdConnection::ConnectionRequestProxy(): received Initial delay: " << mMaxDelay; + std::cerr << std::endl; +#endif + /* clear IP Addresses to enforce this */ bdsockaddr_clear(&(mProxyId.addr)); bdsockaddr_clear(&(mDestConnAddr.addr)); @@ -2410,6 +2517,10 @@ int bdConnection::ConnectionRequestEnd(bdId *id, bdId *srcId, bdId *destId, int mSrcConnAddr = *srcId; /* ID, not IP */ mDestConnAddr = *destId; /* ID, not IP */ + mBandwidth = 0; + mMaxDelay = 0; + mConnectionStartTS = time(NULL); + /* clear IP Addresses to enforce this */ bdsockaddr_clear(&(mSrcId.addr)); bdsockaddr_clear(&(mDestId.addr)); @@ -2423,7 +2534,7 @@ int bdConnection::ConnectionRequestEnd(bdId *id, bdId *srcId, bdId *destId, int // Received AUTH, step up to next stage. // Search for dest ID/IP is done before AUTH. so actually nothing to do here, except set the state -int bdConnection::AuthoriseProxyConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc) +int bdConnection::AuthoriseProxyConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc, int bandwidth) { mState = BITDHT_CONNECTION_WAITING_REPLY; mLastEvent = time(NULL); @@ -2434,6 +2545,15 @@ int bdConnection::AuthoriseProxyConnection(bdId *srcId, bdId *proxyId, bdId *des // mPoint, mMode should be okay. + mBandwidth = bandwidth; + //mMaxDelay Already set. + //mConnectionStartTS Already Set + +#ifdef DEBUG_CONNECTION_DELAY + std::cerr << "bdConnection::AuthoriseProxyConnection(): received Bandwidth Limitation: " << mBandwidth; + std::cerr << std::endl; +#endif + // mSrcConnAddr (ID/IP known) // mDestConnAddr is still pending. @@ -2448,7 +2568,7 @@ int bdConnection::AuthoriseProxyConnection(bdId *srcId, bdId *proxyId, bdId *des /* we are end of a Proxy Connection */ -int bdConnection::AuthoriseEndConnection(bdId *srcId, bdId *proxyId, bdId *destConnAddr, int mode, int loc) +int bdConnection::AuthoriseEndConnection(bdId *srcId, bdId *proxyId, bdId *destConnAddr, int mode, int loc, int delay) { mState = BITDHT_CONNECTION_WAITING_START; mLastEvent = time(NULL); @@ -2459,6 +2579,17 @@ int bdConnection::AuthoriseEndConnection(bdId *srcId, bdId *proxyId, bdId *destC // mPoint, mMode should be okay. + //mBandwidth not set by us. + time_t elapsedTime = (time(NULL) - mConnectionStartTS); + mMaxDelay = delay + elapsedTime; // relative to mConnectionStartTS. + //mConnectionStartTS // Already Set + +#ifdef DEBUG_CONNECTION_DELAY + std::cerr << "bdConnection::AuthoriseEndConnection(): adjusting delay: " << delay << " by elapsedTime: " << elapsedTime; + std::cerr << " for MaxDelay: " << mMaxDelay; + std::cerr << std::endl; +#endif + // mSrcConnAddr should be okay. (IP unknown) // Install the correct destConnAddr. (just received) mDestConnAddr = *destConnAddr; @@ -2494,6 +2625,10 @@ int bdConnection::AuthoriseDirectConnection(bdId *srcId, bdId *proxyId, bdId *de // mPoint, mMode should be okay. + //mBandwidth doesn't matter + //mMaxDelay doesn't matter + //mConnectionStartTS // Already Set + // mSrcConnAddr should be okay. (ID/IP known) // Install the correct destConnAddr. (just received) mDestConnAddr = *destConnAddr; @@ -2515,7 +2650,7 @@ int bdConnection::AuthoriseDirectConnection(bdId *srcId, bdId *proxyId, bdId *de } // Proxy Connection => at Proxy, Ready to send out Start and get back ACKs!! -int bdConnection::upgradeProxyConnectionToFinish(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int status) +int bdConnection::upgradeProxyConnectionToFinish(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int secondDelay, int status) { mState = BITDHT_CONNECTION_WAITING_ACK; mLastEvent = time(NULL); @@ -2525,6 +2660,32 @@ int bdConnection::upgradeProxyConnectionToFinish(bdId *id, bdId *srcConnAddr, bd // mPoint, mMode should be okay. + //mBandwidth already set. + time_t elapsedTime = (time(NULL) - mConnectionStartTS); + int remainingDelayOne = mMaxDelay - elapsedTime; + if (secondDelay > remainingDelayOne) + { + /* maxDelay provided by Peer 2.... update calculations */ + mMaxDelay = secondDelay + elapsedTime; // relative to mConnectionStartTS. + } + //mConnectionStartTS // Already Set + +#ifdef DEBUG_CONNECTION_DELAY + std::cerr << "bdConnection::upgradeProxyConnectionToFinish(): elapsedTime: " << elapsedTime << " remainingDelayOne: "; + std::cerr << remainingDelayOne << " secondDelay: " << secondDelay << " => maxDelay: " << mMaxDelay; + std::cerr << std::endl; +#endif + + +#ifdef DEBUG_CONNECTION_DELAY + if ((secondDelay > 100 || mMaxDelay > 100) || (mBandwidth > 1050)) + { + std::cerr << "bdConnection::upgradeProxyConnectionToFinish(): ERROR Bandwidth or Delay rather large"; + std::cerr << std::endl; + } +#endif + + // mSrcConnAddr should be okay. (ID/IP known) // Install the correct destConnAddr. (just received) mDestConnAddr = *destConnAddr; @@ -2546,7 +2707,9 @@ int bdConnection::upgradeProxyConnectionToFinish(bdId *id, bdId *srcConnAddr, bd // Final Sorting out of Addresses. -int bdConnection::CompleteConnection(bdId *id, bdId *srcConnAddr, bdId *destConnAddr) +// This is called at the Connection Ends, on receiving the START message. +// This will contain either Bandwidth or Delay. +int bdConnection::CompleteConnection(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int bandwidth, int delay) { /* Store Final Addresses */ time_t now = time(NULL); @@ -2555,6 +2718,40 @@ int bdConnection::CompleteConnection(bdId *id, bdId *srcConnAddr, bdId *destConn mCompletedTS = now; mLastEvent = now; + mBandwidth = bandwidth; // Received from the Proxy (in the case of a Relay). + time_t elapsedTime = (time(NULL) - mConnectionStartTS); + int remainingOrigDelay = mMaxDelay - elapsedTime; + if (delay > remainingOrigDelay) + { + /* maxDelay provided by Peer 2.... update calculations */ + mMaxDelay = delay + elapsedTime; // relative to mConnectionStartTS. + } + + +#ifdef DEBUG_CONNECTION_DELAY + if ((delay > 100 || mMaxDelay > 100) || (mBandwidth > 1050)) + { + std::cerr << "bdConnection::CompleteConnection(): ERROR Bandwidth or Delay rather large"; + std::cerr << std::endl; + } +#endif + +#ifdef DEBUG_CONNECTION_DELAY + std::cerr << "bdConnection::CompleteConnection(): Bandwidth: " << mBandwidth; + std::cerr << std::endl; + std::cerr << "bdConnection::CompleteConnection(): elapsedTime: " << elapsedTime << " remainingOrigDelay: "; + std::cerr << remainingOrigDelay << " newDelay: " << delay << " => maxDelay: " << mMaxDelay; + std::cerr << std::endl; +#endif + + if (delay < remainingOrigDelay) + { + // ERROR + std::cerr << "bdConnection::CompleteConnection(): ERROR delay: " << delay << " is smaller than remainingOrigDelay: "; + std::cerr << remainingOrigDelay; + std::cerr << std::endl; + } + // Received Definitive Final Addresses from Proxy. // These have to be done by proxy, as its the only one who know both our addresses. @@ -2606,10 +2803,13 @@ int bdConnectionRequest::setupDirectConnection(struct sockaddr_in *laddr, bdNode mRecycled = 0; mErrCode = 0; + mDelay = 0; + mRequestTS = time(NULL); + return 1; } -int bdConnectionRequest::setupProxyConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode) +int bdConnectionRequest::setupProxyConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay) { mState = BITDHT_CONNREQUEST_READY; mStateTS = time(NULL); @@ -2620,6 +2820,9 @@ int bdConnectionRequest::setupProxyConnection(struct sockaddr_in *laddr, bdNodeI mRecycled = 0; mErrCode = 0; + mDelay = delay; + mRequestTS = time(NULL); + return 1; } @@ -2712,6 +2915,9 @@ std::ostream &operator<<(std::ostream &out, const bdConnectionRequest &req) out << std::endl; out << "\tMode: " << req.mMode; out << std::endl; + out << "\tDelay: " << req.mDelay; + out << " RequestTS: " << now - req.mRequestTS; + out << std::endl; out << "CurrentAttempt:"; bdStdPrintId(out, &(req.mCurrentAttempt)); @@ -2783,6 +2989,11 @@ std::ostream &operator<<(std::ostream &out, const bdConnection &conn) out << " srcAck: " << conn.mSrcAck; out << " destAck: " << conn.mDestAck; out << " completedTS: " << now - conn.mCompletedTS; + + out << "\tBandwidth: " << conn.mBandwidth; + out << " maxDelay: " << conn.mMaxDelay; + out << " startTS: " << now - conn.mConnectionStartTS; + out << std::endl; return out; @@ -2876,7 +3087,7 @@ std::string decodeConnectionErrorType(uint32_t errcode) std::string decodeConnectionErrorSource(uint32_t errcode) { uint32_t errsrc = errcode & BITDHT_CONNECT_ERROR_MASK_SOURCE; - std::string namedtype = "UNKNOWN"; + std::string namedtype = "ERROR SRC UNKNOWN"; switch(errsrc) { default: diff --git a/src/bitdht/bdconnection.h b/src/bitdht/bdconnection.h index 777b08d..9c8bfe9 100644 --- a/src/bitdht/bdconnection.h +++ b/src/bitdht/bdconnection.h @@ -42,7 +42,7 @@ class bdNodePublisher; #define BITDHT_CONNREQUEST_EXTCONNECT 4 #define BITDHT_CONNREQUEST_DONE 5 -#define BITDHT_CONNREQUEST_TIMEOUT_CONNECT 120 // MAKE THIS LARGE - SHOULD NEVER HAPPEN. +#define BITDHT_CONNREQUEST_TIMEOUT_CONNECT 180 // MAKE THIS LARGE - SHOULD NEVER HAPPEN. #define BITDHT_CONNREQUEST_TIMEOUT_INPROGRESS 30 #define BITDHT_CONNREQUEST_MAX_AGE 60 @@ -85,22 +85,22 @@ class bdConnection /** Functions to tweak the connection status */ // User initialised Connection. - int ConnectionSetup(bdId *proxyId, bdId *srcConnAddr, bdId *destConnAddr, int mode); + int ConnectionSetup(bdId *proxyId, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delay); int ConnectionSetupDirect(bdId *destId, bdId *srcConnAddr); // Initialise a new Connection. (receiving a Connection Request) int ConnectionRequestDirect(bdId *id, bdId *srcConnAddr, bdId *destConnAddr); - int ConnectionRequestProxy(bdId *id, bdId *srcConnAddr, bdNodeId *ownId, bdId *destConnAddr, int mode); + int ConnectionRequestProxy(bdId *id, bdId *srcConnAddr, bdNodeId *ownId, bdId *destConnAddr, int mode, int delay); int ConnectionRequestEnd(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode); // Setup Finishing Stage, (receiving a Connection Reply). - int upgradeProxyConnectionToFinish(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int status); + int upgradeProxyConnectionToFinish(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delay, int status); int AuthoriseDirectConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc); - int AuthoriseProxyConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc); - int AuthoriseEndConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc); + int AuthoriseProxyConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc, int bandwidth); + int AuthoriseEndConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc, int delay); - int CompleteConnection(bdId *id, bdId *srcConnAddr, bdId *destConnAddr); + int CompleteConnection(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int bandwidth, int delay); int checkForDefaultConnectAddress(); @@ -124,6 +124,8 @@ class bdConnection bdId mDestConnAddr; int mBandwidth; + int mMaxDelay; + time_t mConnectionStartTS; /* START/ACK Finishing ****/ time_t mLastStart; /* timer for retries */ @@ -142,7 +144,7 @@ class bdConnectionRequest { public: int setupDirectConnection(struct sockaddr_in *laddr, bdNodeId *target); - int setupProxyConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode); + int setupProxyConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay); int addGoodProxy(const bdId *srcId); int checkGoodProxyPeer(const bdId *Id); @@ -157,6 +159,8 @@ class bdConnectionRequest time_t mPauseTS; uint32_t mErrCode; + int mDelay; + time_t mRequestTS; // reference Time for mDelay. std::list mGoodProxies; std::list mPotentialProxies; @@ -199,9 +203,9 @@ class bdConnectManager /* Connections: Initiation */ - int requestConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start); + int requestConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay, uint32_t start); int requestConnection_direct(struct sockaddr_in *laddr, bdNodeId *target); - int requestConnection_proxy(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode); + int requestConnection_proxy(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay); int killConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode); @@ -217,12 +221,12 @@ class bdConnectManager // internal Callback -> normally continues to callbackConnect(). void callbackConnectRequest(bdId *srcId, bdId *proxyId, bdId *destId, - int mode, int point, int cbtype, int errcode); + int mode, int point, int param, int cbtype, int errcode); /* Connections: Outgoing */ - int startConnectionAttempt(bdId *proxyId, bdId *srcConnAddr, bdId *destConnAddr, int mode); - void AuthConnectionOk(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc); + int startConnectionAttempt(bdId *proxyId, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delay); + void AuthConnectionOk(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc, int bandwidth, int delay); void AuthConnectionNo(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc, int errcode); void iterateConnections(); @@ -243,12 +247,12 @@ class bdConnectManager // Overloaded Generalised Connection Callback. virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId, - int mode, int point, int cbtype, int errcode); + int mode, int point, int param, int cbtype, int errcode); /* Connections: */ - int recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode); - int recvedConnectionReply(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int status); - int recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int bandwidth); + int recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delay); + int recvedConnectionReply(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delay, int status); + int recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delayOrBandwidth); int recvedConnectionAck(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode); private: diff --git a/src/bitdht/bdiface.h b/src/bitdht/bdiface.h index ad31797..49fbb88 100644 --- a/src/bitdht/bdiface.h +++ b/src/bitdht/bdiface.h @@ -302,7 +302,7 @@ virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t statu // connection callback. Not required for basic behaviour, but forced for initial development. virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId, - uint32_t mode, uint32_t point, uint32_t cbtype, uint32_t errcode) = 0; /* { return 0; } */ + uint32_t mode, uint32_t point, uint32_t param, uint32_t cbtype, uint32_t errcode) = 0; /* { return 0; } */ }; @@ -317,8 +317,9 @@ virtual void removeFindNode(bdNodeId *id) = 0; virtual void findDhtValue(bdNodeId *id, std::string key, uint32_t mode) = 0; /***** Connections Requests *****/ -virtual bool ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start) = 0; -virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer) = 0; +virtual bool ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay, uint32_t start) = 0; +virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, + uint32_t bandwidth, uint32_t delay, uint32_t answer) = 0; virtual void ConnectionOptions(uint32_t allowedModes, uint32_t flags) = 0; diff --git a/src/bitdht/bdmanager.cc b/src/bitdht/bdmanager.cc index 7b9487b..4ad53eb 100644 --- a/src/bitdht/bdmanager.cc +++ b/src/bitdht/bdmanager.cc @@ -1255,22 +1255,22 @@ int bdDebugCallback::dhtValueCallback(const bdNodeId *id, std::string key, uint3 -bool bdNodeManager::ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start) +bool bdNodeManager::ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay, uint32_t start) { std::cerr << "bdNodeManager::ConnectionRequest()"; std::cerr << std::endl; - return mConnMgr->requestConnection(laddr, target, mode, start); + return mConnMgr->requestConnection(laddr, target, mode, delay, start); } -void bdNodeManager::ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer) +void bdNodeManager::ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t bandwidth, uint32_t delay, uint32_t answer) { std::cerr << "bdNodeManager::ConnectionAuth()"; std::cerr << std::endl; if (answer == BITDHT_CONNECT_ANSWER_OKAY) { - mConnMgr->AuthConnectionOk(srcId, proxyId, destId, mode, loc); + mConnMgr->AuthConnectionOk(srcId, proxyId, destId, mode, loc, bandwidth, delay); } else { @@ -1287,7 +1287,7 @@ void bdNodeManager::ConnectionOptions(uint32_t allowedModes, uint32_t flags) /***** Connections Requests *****/ // Overloaded from bdnode for external node callback. -void bdNodeManager::callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int point, int cbtype, int errcode) +void bdNodeManager::callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int point, int param, int cbtype, int errcode) { std::cerr << "bdNodeManager::callbackConnect()"; std::cerr << std::endl; @@ -1298,13 +1298,13 @@ void bdNodeManager::callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId, in std::list::iterator it; for(it = mCallbacks.begin(); it != mCallbacks.end(); it++) { - (*it)->dhtConnectCallback(srcId, proxyId, destId, mode, point, cbtype, errcode); + (*it)->dhtConnectCallback(srcId, proxyId, destId, mode, point, param, cbtype, errcode); } return; } int bdDebugCallback::dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId, - uint32_t mode, uint32_t point, uint32_t cbtype, uint32_t errcode) + uint32_t mode, uint32_t point, uint32_t param, uint32_t cbtype, uint32_t errcode) { #ifdef DEBUG_MGR std::cerr << "bdDebugCallback::dhtConnectCallback() Type: " << cbtype; @@ -1316,6 +1316,7 @@ int bdDebugCallback::dhtConnectCallback(const bdId *srcId, const bdId *proxyId, std::cerr << " destId: "; bdStdPrintId(std::cerr, destId); std::cerr << " mode: " << mode; + std::cerr << " param: " << param; std::cerr << " point: " << point << std::endl; #endif diff --git a/src/bitdht/bdmanager.h b/src/bitdht/bdmanager.h index 887e916..505c960 100644 --- a/src/bitdht/bdmanager.h +++ b/src/bitdht/bdmanager.h @@ -121,9 +121,9 @@ virtual int getDhtQueries(std::map &queries); virtual int getDhtQueryStatus(const bdNodeId *id, bdQuerySummary &query); /***** Connection Interface ****/ -virtual bool ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start); +virtual bool ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay, uint32_t start); virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, - uint32_t mode, uint32_t loc, uint32_t answer); + uint32_t mode, uint32_t loc, uint32_t bandwidth, uint32_t delay, uint32_t answer); virtual void ConnectionOptions(uint32_t allowedModes, uint32_t flags); @@ -139,7 +139,7 @@ virtual uint32_t statsBDVersionSize(); /* same version as us! */ virtual void addPeer(const bdId *id, uint32_t peerflags); // Overloaded from bdnode for external node callback. virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId, - int mode, int point, int cbtype, int errcode); + int mode, int point, int param, int cbtype, int errcode); int isBitDhtPacket(char *data, int size, struct sockaddr_in &from); private: @@ -188,7 +188,7 @@ class bdDebugCallback: public BitDhtCallback virtual int dhtPeerCallback(const bdId *id, uint32_t status); virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status); virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId, - uint32_t mode, uint32_t point, uint32_t cbtype, uint32_t errcode); + uint32_t mode, uint32_t point, uint32_t param, uint32_t cbtype, uint32_t errcode); }; diff --git a/src/bitdht/bdmsgs.cc b/src/bitdht/bdmsgs.cc index a11dead..c3a3caa 100644 --- a/src/bitdht/bdmsgs.cc +++ b/src/bitdht/bdmsgs.cc @@ -963,7 +963,7 @@ int bitdht_find_node_msg(bdToken *tid, bdNodeId *id, bdNodeId *target, */ -int bitdht_connect_genmsg(bdToken *tid, bdNodeId *id, int msgtype, bdId *src, bdId *dest, int mode, int status, char *msg, int avail) +int bitdht_connect_genmsg(bdToken *tid, bdNodeId *id, int msgtype, bdId *src, bdId *dest, int mode, int param, int status, char *msg, int avail) { #ifdef DEBUG_MSGS fprintf(stderr, "bitdht_connect_genmsg()\n"); @@ -983,34 +983,17 @@ int bitdht_connect_genmsg(bdToken *tid, bdNodeId *id, int msgtype, bdId *src, bd be_node *typenode = be_create_int(msgtype); be_node *statusnode = be_create_int(status); be_node *modenode = be_create_int(mode); + be_node *paramnode = be_create_int(param); be_node *tidnode = be_create_str_wlen((char *) tid->data, tid->len); be_node *yqrnode = be_create_str("q"); be_node *cmdnode = be_create_str("connect"); -#if 0 - be_node *modenode = NULL; - switch(mode) - { - case BITDHT_CONNECT_MODE_DIRECT: - modenode = be_create_str("d"); - break; - case BITDHT_CONNECT_MODE_PROXY: - modenode = be_create_str("p"); - break; - case BITDHT_CONNECT_MODE_RELAY: - modenode = be_create_str("r"); - break; - default: - modenode = be_create_str("u"); - break; - } -#endif - be_add_keypair(iddict, "id", idnode); be_add_keypair(iddict, "src", srcnode); be_add_keypair(iddict, "dest", destnode); be_add_keypair(iddict, "mode", modenode); + be_add_keypair(iddict, "param", paramnode); be_add_keypair(iddict, "status", statusnode); be_add_keypair(iddict, "type", typenode); diff --git a/src/bitdht/bdmsgs.h b/src/bitdht/bdmsgs.h index 738f007..0941994 100644 --- a/src/bitdht/bdmsgs.h +++ b/src/bitdht/bdmsgs.h @@ -93,7 +93,7 @@ int bitdht_reply_announce_msg(bdToken *tid, bdNodeId *id, // Extensions. -int bitdht_connect_genmsg(bdToken *tid, bdNodeId *id, int msgtype, bdId *src, bdId *dest, int mode, int status, char *msg, int avail); +int bitdht_connect_genmsg(bdToken *tid, bdNodeId *id, int msgtype, bdId *src, bdId *dest, int mode, int param, int status, char *msg, int avail); //int response_peers_message() diff --git a/src/bitdht/bdnode.cc b/src/bitdht/bdnode.cc index 23fbe4a..7b94ec6 100644 --- a/src/bitdht/bdnode.cc +++ b/src/bitdht/bdnode.cc @@ -380,14 +380,14 @@ void bdNode::send_query(bdId *id, bdNodeId *targetNodeId) } -void bdNode::send_connect_msg(bdId *id, int msgtype, bdId *srcAddr, bdId *destAddr, int mode, int status) +void bdNode::send_connect_msg(bdId *id, int msgtype, bdId *srcAddr, bdId *destAddr, int mode, int param, int status) { /* push out query */ bdToken transId; genNewTransId(&transId); //registerOutgoingMsg(&id, &transId, BITDHT_MSG_TYPE_FIND_NODE); - msgout_connect_genmsg(id, &transId, msgtype, srcAddr, destAddr, mode, status); + msgout_connect_genmsg(id, &transId, msgtype, srcAddr, destAddr, mode, param, status); #ifdef DEBUG_NODE_MSGS std::cerr << "bdNode::send_connect_msg() to: "; @@ -1190,12 +1190,14 @@ void bdNode::recvPkt(char *msg, int len, struct sockaddr_in addr) bdId connSrcAddr; bdId connDestAddr; uint32_t connMode; + uint32_t connParam = 0; uint32_t connStatus; uint32_t connType; be_node *be_ConnSrcAddr = NULL; be_node *be_ConnDestAddr = NULL; be_node *be_ConnMode = NULL; + be_node *be_ConnParam = NULL; be_node *be_ConnStatus = NULL; be_node *be_ConnType = NULL; if (beType == BITDHT_MSG_TYPE_CONNECT) @@ -1236,6 +1238,18 @@ void bdNode::recvPkt(char *msg, int len, struct sockaddr_in addr) return; } + /* Param */ + be_ConnParam = beMsgGetDictNode(be_data, "param"); + if (!be_ConnParam) + { +#ifdef DEBUG_NODE_PARSE + std::cerr << "bdNode::recvPkt() CONNECT Missing Param. Dropping Msg"; + std::cerr << std::endl; +#endif + be_free(node); + return; + } + /* Status */ be_ConnStatus = beMsgGetDictNode(be_data, "status"); if (!be_ConnStatus) @@ -1276,6 +1290,11 @@ void bdNode::recvPkt(char *msg, int len, struct sockaddr_in addr) beMsgGetUInt32(be_ConnMode, &connMode); } + if (be_ConnParam) + { + beMsgGetUInt32(be_ConnParam, &connParam); + } + if (be_ConnStatus) { beMsgGetUInt32(be_ConnStatus, &connStatus); @@ -1409,7 +1428,7 @@ void bdNode::recvPkt(char *msg, int len, struct sockaddr_in addr) //#endif msgin_connect_genmsg(&srcId, &transId, connType, &connSrcAddr, &connDestAddr, - connMode, connStatus); + connMode, connParam, connStatus); break; } default: @@ -1788,7 +1807,7 @@ std::string getConnectMsgType(int msgtype) } } -void bdNode::msgout_connect_genmsg(bdId *id, bdToken *transId, int msgtype, bdId *srcAddr, bdId *destAddr, int mode, int status) +void bdNode::msgout_connect_genmsg(bdId *id, bdToken *transId, int msgtype, bdId *srcAddr, bdId *destAddr, int mode, int param, int status) { std::cerr << "bdConnectManager::msgout_connect_genmsg() Type: " << getConnectMsgType(msgtype); std::cerr << " TransId: "; @@ -1800,6 +1819,7 @@ void bdNode::msgout_connect_genmsg(bdId *id, bdToken *transId, int msgtype, bdId std::cerr << " DestAddr: "; mFns->bdPrintId(std::cerr, destAddr); std::cerr << " Mode: " << mode; + std::cerr << " Param: " << param; std::cerr << " Status: " << status; std::cerr << std::endl; #ifdef DEBUG_NODE_MSGOUT @@ -1828,13 +1848,13 @@ void bdNode::msgout_connect_genmsg(bdId *id, bdToken *transId, int msgtype, bdId char msg[10240]; int avail = 10240; - int blen = bitdht_connect_genmsg(transId, &(mOwnId), msgtype, srcAddr, destAddr, mode, status, msg, avail-1); + int blen = bitdht_connect_genmsg(transId, &(mOwnId), msgtype, srcAddr, destAddr, mode, param, status, msg, avail-1); sendPkt(msg, blen, id->addr); } void bdNode::msgin_connect_genmsg(bdId *id, bdToken *transId, int msgtype, - bdId *srcAddr, bdId *destAddr, int mode, int status) + bdId *srcAddr, bdId *destAddr, int mode, int param, int status) { std::list::iterator it; @@ -1848,6 +1868,7 @@ void bdNode::msgin_connect_genmsg(bdId *id, bdToken *transId, int msgtype, std::cerr << " DestAddr: "; mFns->bdPrintId(std::cerr, destAddr); std::cerr << " Mode: " << mode; + std::cerr << " Param: " << param; std::cerr << " Status: " << status; std::cerr << std::endl; #ifdef DEBUG_NODE_MSGS @@ -1864,21 +1885,21 @@ void bdNode::msgin_connect_genmsg(bdId *id, bdToken *transId, int msgtype, mAccount.incCounter(BDACCOUNT_MSG_CONNECTREQUEST, false); - mConnMgr->recvedConnectionRequest(id, srcAddr, destAddr, mode); + mConnMgr->recvedConnectionRequest(id, srcAddr, destAddr, mode, param); break; case BITDHT_MSG_TYPE_CONNECT_REPLY: peerflags = BITDHT_PEER_STATUS_RECV_CONNECT_MSG; mAccount.incCounter(BDACCOUNT_MSG_CONNECTREPLY, false); - mConnMgr->recvedConnectionReply(id, srcAddr, destAddr, mode, status); + mConnMgr->recvedConnectionReply(id, srcAddr, destAddr, mode, param, status); break; case BITDHT_MSG_TYPE_CONNECT_START: peerflags = BITDHT_PEER_STATUS_RECV_CONNECT_MSG; mAccount.incCounter(BDACCOUNT_MSG_CONNECTSTART, false); - mConnMgr->recvedConnectionStart(id, srcAddr, destAddr, mode, status); + mConnMgr->recvedConnectionStart(id, srcAddr, destAddr, mode, param); break; case BITDHT_MSG_TYPE_CONNECT_ACK: diff --git a/src/bitdht/bdnode.h b/src/bitdht/bdnode.h index 3d43d1c..9bfbda7 100644 --- a/src/bitdht/bdnode.h +++ b/src/bitdht/bdnode.h @@ -102,11 +102,11 @@ class bdNodePublisher virtual void send_ping(bdId *id) = 0; /* message out */ virtual void send_query(bdId *id, bdNodeId *targetNodeId) = 0; /* message out */ virtual void send_connect_msg(bdId *id, int msgtype, - bdId *srcAddr, bdId *destAddr, int mode, int status) = 0; + bdId *srcAddr, bdId *destAddr, int mode, int param, int status) = 0; // internal Callback -> normally continues to callbackConnect(). virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId, - int mode, int point, int cbtype, int errcode) = 0; + int mode, int point, int param, int cbtype, int errcode) = 0; }; @@ -145,11 +145,11 @@ class bdNode: public bdNodePublisher virtual void send_ping(bdId *id); /* message out */ virtual void send_query(bdId *id, bdNodeId *targetNodeId); /* message out */ virtual void send_connect_msg(bdId *id, int msgtype, - bdId *srcAddr, bdId *destAddr, int mode, int status); + bdId *srcAddr, bdId *destAddr, int mode, int param, int status); // This is implemented in bdManager. // virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId, -// int mode, int point, int cbtype, int errcode); +// int mode, int point, int param, int cbtype, int errcode); /* interaction with outside world (Accessed by controller to deliver us msgs) */ int outgoingMsg(struct sockaddr_in *addr, char *msg, int *len); @@ -200,9 +200,9 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr); void msgin_reply_post(bdId *id, bdToken *transId); void msgout_connect_genmsg(bdId *id, bdToken *transId, int msgtype, - bdId *srcAddr, bdId *destAddr, int mode, int status); + bdId *srcAddr, bdId *destAddr, int mode, int param, int status); void msgin_connect_genmsg(bdId *id, bdToken *transId, int msgtype, - bdId *srcAddr, bdId *destAddr, int mode, int status); + bdId *srcAddr, bdId *destAddr, int mode, int param, int status); diff --git a/src/udp/udpbitdht.cc b/src/udp/udpbitdht.cc index 2ae81ce..ed8da74 100644 --- a/src/udp/udpbitdht.cc +++ b/src/udp/udpbitdht.cc @@ -50,9 +50,10 @@ #define BITDHT_VERSION_IDENTIFER 1 // Original RS 0.5.0/0.5.1 version, is un-numbered. -#define BITDHT_VERSION "00" // First Release of BitDHT with Connections (Proxy Support + Dht Stun) -//#define BITDHT_VERSION "01" // Full Connections -//#define BITDHT_VERSION "02" // Full DHT implementation. +//#define BITDHT_VERSION "00" // First Release of BitDHT with Connections (Proxy Support + Dht Stun) +#define BITDHT_VERSION "01" // Testing Connections +//#define BITDHT_VERSION "02" // Completed? Full Connections +//#define BITDHT_VERSION "04" // Full DHT implementation.? /*************************************/ @@ -125,20 +126,21 @@ void UdpBitDht::removeCallback(BitDhtCallback *cb) mBitDhtManager->removeCallback(cb); } -bool UdpBitDht::ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start) +bool UdpBitDht::ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay, uint32_t start) { bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/ - return mBitDhtManager->ConnectionRequest(laddr, target, mode, start); + return mBitDhtManager->ConnectionRequest(laddr, target, mode, delay, start); } -void UdpBitDht::ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer) +void UdpBitDht::ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, + uint32_t bandwidth, uint32_t delay, uint32_t answer) { bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/ - mBitDhtManager->ConnectionAuth(srcId, proxyId, destId, mode, loc, answer); + mBitDhtManager->ConnectionAuth(srcId, proxyId, destId, mode, loc, bandwidth, delay, answer); } void UdpBitDht::ConnectionOptions(uint32_t allowedModes, uint32_t flags) diff --git a/src/udp/udpbitdht.h b/src/udp/udpbitdht.h index c201659..510b2b8 100644 --- a/src/udp/udpbitdht.h +++ b/src/udp/udpbitdht.h @@ -68,8 +68,9 @@ virtual void addCallback(BitDhtCallback *cb); virtual void removeCallback(BitDhtCallback *cb); /***** Connections Requests *****/ -virtual bool ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t start); -virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer); +virtual bool ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode, uint32_t delay, uint32_t start); +virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, + uint32_t bandwidth, uint32_t delay, uint32_t answer); virtual void ConnectionOptions(uint32_t allowedModes, uint32_t flags); /***** Get Results Details *****/