From 68d878f7e31ea49e27265bbb21c8aa23cf339f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Fri, 22 Apr 2016 10:50:25 +0200 Subject: [PATCH 01/15] Added transmit counters and done necessary cleanup to not store values twice. --- include/stun_intern.h | 8 +- include/stunclient.h | 2 +- include/stunlib.h | 14 +++- include/stuntrace.h | 2 +- src/stunclient.c | 139 +++++++++++++++++++----------- src/stunlib.c | 186 +++++++++++++++++++++++++++-------------- src/stuntrace.c | 35 ++++---- test/stunclient_test.c | 7 +- test/stuntrace_test.c | 36 +++++--- 9 files changed, 277 insertions(+), 152 deletions(-) diff --git a/include/stun_intern.h b/include/stun_intern.h index 9264769..ab47681 100644 --- a/include/stun_intern.h +++ b/include/stun_intern.h @@ -74,6 +74,7 @@ typedef struct { DiscussData* discussData; /*NULL allowed if none present */ bool addSoftware; bool stuntrace; + bool addTransCnt; } StunBindReqStruct; struct StunClientStats @@ -102,11 +103,6 @@ typedef struct uint32_t inst; StunBindReqStruct stunBindReq; - uint8_t stunReqMsgBuf[STUN_MAX_PACKET_SIZE]; /* encoded STUN request - * */ - int stunReqMsgBufLen; /* of encoded STUN - * request */ - STUN_USER_CREDENTIALS userCredentials; bool authenticated; @@ -123,7 +119,7 @@ typedef struct /* icmp */ uint32_t ICMPtype; - uint32_t ttl; + //uint32_t ttl; /* DISCUSS */ bool hasDiscuss; diff --git a/include/stunclient.h b/include/stunclient.h index 2ed0be5..ad51253 100644 --- a/include/stunclient.h +++ b/include/stunclient.h @@ -180,7 +180,7 @@ StunClient_startBindTransaction(STUN_CLIENT_DATA* clientData, STUN_SENDFUNC sendFunc, STUNCB stunCbFunc, DiscussData* discussData); -uint32_t +void StunClient_startSTUNTrace(STUN_CLIENT_DATA* clientData, void* userCtx, const struct sockaddr* serverAddr, diff --git a/include/stunlib.h b/include/stunlib.h index 4d7f711..64690b2 100644 --- a/include/stunlib.h +++ b/include/stunlib.h @@ -116,6 +116,9 @@ extern "C" { #define STUN_ATTR_ICEControlled 0x8029 #define STUN_ATTR_ICEControlling 0x802A +/* Draft loss and RTT calculation TODO:update actual calues*/ +#define STUN_ATTR_TransCount 0x8072 + /** IP Addr family **/ #define STUN_ADDR_IPv4Family 0x01 #define STUN_ADDR_IPv6Family 0x02 @@ -362,6 +365,14 @@ typedef struct } StunAtrNetworkStatus; +typedef struct +{ + uint16_t reserved; + uint8_t reqCnt; + uint8_t respCnt; +} +StunAtrTransCount; + typedef struct { uint32_t first; @@ -542,7 +553,8 @@ typedef struct bool hasCiscoNetFeedResp; StunAtrCiscoNetworkFeedback ciscoNetFeedResp; - + bool hasTransCount; + StunAtrTransCount transCount; /* No value, only flaged */ bool hasUseCandidate; diff --git a/include/stuntrace.h b/include/stuntrace.h index d83f4ea..ddeb880 100644 --- a/include/stuntrace.h +++ b/include/stuntrace.h @@ -62,7 +62,7 @@ struct hiutResult { /* Initial Length of first STUN packet (TTL=1) */ - uint32_t stunLen; + //uint32_t stunLen; struct hiutPathElement pathElement[MAX_TTL + 1]; bool remoteAlive; /* struct hiutTTLinfo ttlInfo[MAX_TTL]; */ diff --git a/src/stunclient.c b/src/stunclient.c index 10ac714..850b4f2 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -223,6 +223,14 @@ BuildStunBindReq(STUN_TRANSACTION_DATA* trans, { stunlib_addSoftware(stunReqMsg, SoftwareVersionStr, STUN_DFLT_PAD); } + + if (trans->stunBindReq.addTransCnt) + { + stunReqMsg->hasTransCount = true; + stunReqMsg->transCount.reserved = (uint16_t)0; + stunReqMsg->transCount.reqCnt = (uint8_t)trans->retransmits + 1; + stunReqMsg->transCount.respCnt = (uint8_t)0; + } } @@ -355,6 +363,8 @@ StunClient_startBindTransaction(STUN_CLIENT_DATA* clientData, m.discussData = discussData; m.addSoftware = true; + /*TODO: Let app overide this */ + m.addTransCnt = true; /* callback and data (owned by caller) */ m.stunCbFunc = stunCbFunc; @@ -364,7 +374,7 @@ StunClient_startBindTransaction(STUN_CLIENT_DATA* clientData, return 0; } -uint32_t +void StunClient_startSTUNTrace(STUN_CLIENT_DATA* clientData, void* userCtx, const struct sockaddr* serverAddr, @@ -384,8 +394,6 @@ StunClient_startSTUNTrace(STUN_CLIENT_DATA* clientData, StunBindReqStruct m; STUN_TRANSACTION_DATA trans; StunMessage stunMsg; - uint8_t stunBuff[STUN_MAX_PACKET_SIZE]; - uint32_t len; memset( &m, 0, sizeof(m) ); m.userCtx = userCtx; @@ -402,20 +410,15 @@ StunClient_startSTUNTrace(STUN_CLIENT_DATA* clientData, m.discussData = discussData; m.addSoftware = false; /* callback and data (owned by caller) */ - m.stunCbFunc = stunCbFunc; - m.stuntrace = true; + m.stunCbFunc = stunCbFunc; + m.stuntrace = true; + m.addTransCnt = false; + StoreStunBindReq(&trans, &m); BuildStunBindReq(&trans, &stunMsg); StunClientMain(clientData, STUNCLIENT_CTX_UNKNOWN, STUN_SIGNAL_BindReq, (uint8_t*)&m); - len = stunlib_encodeMessage(&stunMsg, - (uint8_t*)stunBuff, - STUN_MAX_PACKET_SIZE, - (unsigned char*)password, /* md5key */ - password ? strlen(password) : 0, /* keyLen */ - NULL); - return len; } @@ -923,7 +926,7 @@ StartTimer(STUN_TRANSACTION_DATA* trans, { STUN_CLIENT_DATA* client = trans->client; - StunPrint(client->logUserData, client->Log_cb, StunInfoCategory_Trace, + StunPrint(client->logUserData, client->Log_cb, StunInfoCategory_Trace, " StartTimer(%s, %dms)", trans->inst, StunsigToStr(sig), durationMsec); @@ -948,7 +951,7 @@ StopTimer(STUN_TRANSACTION_DATA* trans, { STUN_CLIENT_DATA* client = trans->client; - StunPrint( client->logUserData, client->Log_cb, StunInfoCategory_Trace, + StunPrint( client->logUserData, client->Log_cb, StunInfoCategory_Trace, " StopTimer(%s)", trans->inst, StunsigToStr(sig) ); @@ -1033,36 +1036,39 @@ SendStunReq(STUN_TRANSACTION_DATA* trans, StunMessage* stunReqMsg) { STUN_CLIENT_DATA* client = trans->client; + uint8_t stunReqMsgBuf[STUN_MAX_PACKET_SIZE]; /* encoded STUN request + * */ + int stunReqMsgBufLen; /* of encoded STUN + * request */ + /* encode the BindReq */ if (strlen(trans->stunBindReq.password) > 0) { - trans->stunReqMsgBufLen = stunlib_encodeMessage(stunReqMsg, - (unsigned char*) (trans-> - stunReqMsgBuf), - STUN_MAX_PACKET_SIZE, - (unsigned char*)&trans->stunBindReq.password, - /* key */ - strlen(trans->stunBindReq. - password), - /* keyLen - * */ - NULL); + stunReqMsgBufLen = stunlib_encodeMessage(stunReqMsg, + (unsigned char*) (stunReqMsgBuf), + STUN_MAX_PACKET_SIZE, + (unsigned char*)&trans->stunBindReq.password, + /* key */ + strlen(trans->stunBindReq. + password), + /* keyLen + * */ + NULL); } else { - trans->stunReqMsgBufLen = stunlib_encodeMessage(stunReqMsg, - (unsigned char*) (trans-> - stunReqMsgBuf), - STUN_MAX_PACKET_SIZE, - NULL, - /* key */ - 0, - /* keyLen */ - NULL); + stunReqMsgBufLen = stunlib_encodeMessage(stunReqMsg, + (unsigned char*) (stunReqMsgBuf), + STUN_MAX_PACKET_SIZE, + NULL, + /* key */ + 0, + /* keyLen */ + NULL); } - if (!trans->stunReqMsgBufLen) + if (!stunReqMsgBufLen) { StunPrint(client->logUserData, client->Log_cb, @@ -1078,8 +1084,8 @@ SendStunReq(STUN_TRANSACTION_DATA* trans, { trans->stunBindReq.sendFunc(trans->client->userCtx, trans->stunBindReq.sockhandle, - trans->stunReqMsgBuf, - trans->stunReqMsgBufLen, + stunReqMsgBuf, + stunReqMsgBufLen, (struct sockaddr*)&trans->stunBindReq.serverAddr, trans->stunBindReq.proto, trans->stunBindReq.useRelay, @@ -1117,13 +1123,48 @@ StunClientFsm(STUN_TRANSACTION_DATA* trans, static void RetransmitLastReq(STUN_TRANSACTION_DATA* trans, + StunMessage* stunReqMsg, struct sockaddr_storage* destAddr) { + + /* We need to recalculate Integrity Attribute due to the change in reqCnt*/ + /* encode the BindReq */ + uint8_t stunReqMsgBuf[STUN_MAX_PACKET_SIZE]; /* encoded STUN request + * */ + int stunReqMsgBufLen; /* of encoded STUN + * request */ + + if (strlen(trans->stunBindReq.password) > 0) + { + stunReqMsgBufLen = stunlib_encodeMessage(stunReqMsg, + (unsigned char*) (stunReqMsgBuf), + STUN_MAX_PACKET_SIZE, + (unsigned char*)&trans->stunBindReq.password, + /* key */ + strlen(trans->stunBindReq. + password), + /* keyLen + * */ + NULL); + } + else + { + stunReqMsgBufLen = stunlib_encodeMessage(stunReqMsg, + (unsigned char*) (stunReqMsgBuf), + STUN_MAX_PACKET_SIZE, + NULL, + /* key */ + 0, + /* keyLen */ + NULL); + + } + gettimeofday(&trans->start[trans->retransmits + 1], NULL); trans->stunBindReq.sendFunc(trans->client->userCtx, trans->stunBindReq.sockhandle, - trans->stunReqMsgBuf, - trans->stunReqMsgBufLen, + stunReqMsgBuf, + stunReqMsgBufLen, (struct sockaddr*)destAddr, trans->stunBindReq.proto, trans->stunBindReq.useRelay, @@ -1213,8 +1254,10 @@ CommonRetryTimeoutHandler(STUN_TRANSACTION_DATA* trans, StunPrint(client->logUserData, client->Log_cb, StunInfoCategory_Trace, " Retrans %s Retry: %d to %s", - trans->inst, errStr, trans->retransmits + 1, peer); - RetransmitLastReq(trans, &trans->stunBindReq.serverAddr); + trans->inst, errStr, trans->retransmits, peer); + StunMessage stunReqMsg; + BuildStunBindReq(trans, &stunReqMsg); + RetransmitLastReq(trans, &stunReqMsg, &trans->stunBindReq.serverAddr); StartNextRetransmitTimer(trans); trans->retransmits++; trans->stats.Retransmits++; @@ -1232,7 +1275,7 @@ static void CancelRetryTimeoutHandler(STUN_TRANSACTION_DATA* trans) { STUN_CLIENT_DATA* client = trans->client; - uint32_t max; + uint32_t max; if (trans->stunBindReq.stuntrace) { @@ -1371,7 +1414,7 @@ ICMPRespCallback(STUN_TRANSACTION_DATA* trans, res.stunResult = StunResult_ICMPResp; res.ICMPtype = trans->ICMPtype; - res.ttl = trans->ttl; + res.ttl = trans->stunBindReq.ttl; res.rtt = getRTTvalue(trans); res.retransmits = trans->retransmits; @@ -1462,7 +1505,7 @@ StunState_WaitBindResp(STUN_TRANSACTION_DATA* trans, StunMessage* pResp = &pMsgIn->stunRespMessage; StopTimer(trans, STUN_SIGNAL_TimerRetransmit); - trans->ttl = pMsgIn->ttl; + trans->stunBindReq.ttl = pMsgIn->ttl; if ( StoreBindResp(trans, pResp) ) { BindRespCallback(trans, (struct sockaddr*)&pMsgIn->srcAddr); @@ -1480,7 +1523,7 @@ StunState_WaitBindResp(STUN_TRANSACTION_DATA* trans, { StunRespStruct* pMsgIn = (StunRespStruct*)payload; trans->ICMPtype = pMsgIn->ICMPtype; - trans->ttl = pMsgIn->ttl; + trans->stunBindReq.ttl = pMsgIn->ttl; ICMPRespCallback(trans, (struct sockaddr*)&pMsgIn->srcAddr); trans->stats.ICMPReceived++; SetNextState(trans, STUN_STATE_Idle); @@ -1543,7 +1586,7 @@ StunState_Cancelled(STUN_TRANSACTION_DATA* trans, StunMessage* pResp = &pMsgIn->stunRespMessage; StopTimer(trans, STUN_SIGNAL_TimerRetransmit); - trans->ttl = pMsgIn->ttl; + trans->stunBindReq.ttl = pMsgIn->ttl; if ( StoreBindResp(trans, pResp) ) { BindRespCallback(trans, (struct sockaddr*)&pMsgIn->srcAddr); @@ -1597,7 +1640,7 @@ StunClient_clearStats(STUN_CLIENT_DATA* clientData) return; } - memset(&clientData->stats, 0, sizeof clientData->stats); + memset(&clientData->stats, 0, sizeof clientData->stats); for (int i = 0; i < MAX_STUN_TRANSACTIONS; i++) { @@ -1615,7 +1658,7 @@ StunClient_dumpStats (STUN_CLIENT_DATA* clientData, struct StunClientStats* ptr = &clientData->stats; int usedCnt = 0; - memset(&stats, 0, sizeof stats); + memset(&stats, 0, sizeof stats); stats.InProgress += ptr->InProgress; stats.BindReqSent += ptr->BindReqSent; diff --git a/src/stunlib.c b/src/stunlib.c index 7bcda96..4c06701 100644 --- a/src/stunlib.c +++ b/src/stunlib.c @@ -284,10 +284,10 @@ dumpbuff(char* s, uint32_t len) { uint32_t i; - printError("%s\n", s); + printError("%s\n", s); for (i = 0; i < len; i++) { - printError("%02x,", buf[i]); + printError("%02x,", buf[i]); if (i % 10 == 0) { printError("\n"); @@ -732,11 +732,11 @@ stunEncodeUnknownAtr(StunAtrUnknown* pUnk, { return false; } - write_16( pBuf, STUN_ATTR_UnknownAttribute); - write_16( pBuf, ( (pUnk->numAttributes) * 2 ) ); + write_16( pBuf, STUN_ATTR_UnknownAttribute); + write_16( pBuf, ( (pUnk->numAttributes) * 2 ) ); for (i = 0; i < pUnk->numAttributes; i++) { - write_16( pBuf, pUnk->attrType[i]); + write_16(pBuf, pUnk->attrType[i]); } if (rest) { @@ -911,6 +911,25 @@ stunEncodeNetworkStatus(StunAtrNetworkStatus* pNetworkStatus, return true; } +static bool +stunEncodeTransCount(StunAtrTransCount* pTransCount, + uint8_t** pBuf, + int* nBufLen) +{ + if (*nBufLen < 24) + { + return false; + } + write_16(pBuf, STUN_ATTR_TransCount); /* Attr type */ + write_16(pBuf, 4); /* Length */ + write_16(pBuf, pTransCount->reserved); + write_8(pBuf, pTransCount->reqCnt); + write_8(pBuf, pTransCount->respCnt); + + *nBufLen -= 8; + return true; +} + static bool stunEncodeTTL(StunAtrTTL* pTTL, uint8_t** pBuf, @@ -1011,7 +1030,7 @@ stunlib_EncodeIndication(uint8_t msgType, /* STD TURN: sendInd(XorPeerAddr, Data) no integrity */ stunMsg.msgHdr.msgType = msgType; stunMsg.xorPeerAddrEntries = 1; - memcpy( &stunMsg.xorPeerAddress[0], &activeDstAddr, sizeof(StunIPAddress) ); + memcpy( &stunMsg.xorPeerAddress[0], &activeDstAddr, sizeof(StunIPAddress) ); stunMsg.hasData = true; stunMsg.data.dataLen = payloadLength; stunMsg.data.pData = dataBuf; /*The data (RTP packet) follows @@ -1251,7 +1270,7 @@ stunDecodeIPAddrAtr(StunIPAddress* pAddr, return false; } - read_16(pBuf, &flagtype); + read_16(pBuf, &flagtype); pAddr->familyType = (flagtype & 0xff); if (pAddr->familyType == STUN_ADDR_IPv4Family) @@ -1500,7 +1519,7 @@ stunDecodeStreamType(StunAtrStreamType* streamTypeAtr, - read_16(pBuf, &streamTypeAtr->type); + read_16(pBuf, &streamTypeAtr->type); read_8(pBuf, &streamTypeAtr->interactivity); read_8(pBuf, &streamTypeAtr->pad); *nBufLen -= 4; @@ -1528,6 +1547,25 @@ stunDecodeNetworkStatus(StunAtrNetworkStatus* networkStatusAtr, return true; } +static bool +stunDecodeTransCount(StunAtrTransCount* transCountAtr, + const uint8_t** pBuf, + int* nBufLen) +{ + if (*nBufLen < 4) + { + return false; + } + read_16(pBuf, &transCountAtr->reserved); + read_8(pBuf, &transCountAtr->reqCnt); + read_8(pBuf, &transCountAtr->respCnt); + + *nBufLen -= 8; + return true; +} + + + static bool stunDecodeCiscoNetworkFeedback(StunAtrCiscoNetworkFeedback* ciscoNetFeed, const uint8_t** pBuf, @@ -1589,7 +1627,7 @@ stun_printIP4Address(FILE* stream, char const* szHead, const StunAddress4* pAdr) { - printError(stream, " %s \t= {%d.%d.%d.%d:%d}\n", szHead, + printError(stream, " %s \t= {%d.%d.%d.%d:%d}\n", szHead, pAdr->addr >> 24 & 0xff, pAdr->addr >> 16 & 0xff, pAdr->addr >> 8 & 0xff, @@ -1709,7 +1747,7 @@ stun_printErrorCode(FILE* stream, char buf[1512]; memcpy(buf, pErr->reason, pErr->sizeReason); buf[pErr->sizeReason] = '\0'; - printError(stream, " error = {%d %d, \"%s\"[%d]}\n", + printError(stream, " error = {%d %d, \"%s\"[%d]}\n", pErr->errorClass, pErr->number, buf, pErr->sizeReason); } @@ -1718,7 +1756,7 @@ stun_printUnknown(FILE* stream, const StunAtrUnknown* pUnk) { int i; - printError(stream, " unknownAttribute = [%d]{", pUnk->numAttributes); + printError(stream, " unknownAttribute = [%d]{", pUnk->numAttributes); for (i = 0; i < pUnk->numAttributes; i++) { printError(stream, @@ -1726,7 +1764,7 @@ stun_printUnknown(FILE* stream, (i ? ',' : ' '), pUnk->attrType[i]); } - printError(stream, "\n"); + printError(stream, "\n"); } @@ -1739,7 +1777,7 @@ stun_printData(FILE* stream, { return; } - printError(stream, + printError(stream, " %s \t= %p (%d)\n", szHead, pData->pData, @@ -1769,10 +1807,10 @@ stun_printMessage(FILE* stream, printError(stream, "NULL\n"); return; } - printError(stream, "{\n"); - printError(stream, " msgHdr.type \t= %d\n", pMsg->msgHdr.msgType); - printError(stream, " msgHdr.length \t= %d\n", pMsg->msgHdr.msgLength); - printError(stream, " msgHdr.id[] \t = "); + printError(stream, "{\n"); + printError(stream, " msgHdr.type \t= %d\n", pMsg->msgHdr.msgType); + printError(stream, " msgHdr.length \t= %d\n", pMsg->msgHdr.msgLength); + printError(stream, " msgHdr.id[] \t = "); stun_printTransId(stream, &pMsg->msgHdr.id); printError(stream, "\n"); @@ -1841,7 +1879,7 @@ stun_printMessage(FILE* stream, if (message->hasAlternateServer) { - stun_printIPAddress(stream, "alternateServer", &message->alternateServer); + stun_printIPAddress(stream, "alternateServer", &message->alternateServer); } if (message->xorPeerAddrEntries) @@ -1896,18 +1934,18 @@ stun_printMessage(FILE* stream, if (message->hasMessageIntegrity) { - printError(stream, + printError(stream, " integrity.offset = %02u", message->messageIntegrity.offset); - printError(stream, " integrity.hash[] = "); + printError(stream, " integrity.hash[] = "); for (i = 0; i < 20; i++) { printError(stream, "%02x ", message->messageIntegrity.hash[i]); } - printError(stream, "\n"); + printError(stream, "\n"); } - printError(stream, "}\n"); + printError(stream, "}\n"); } void @@ -1919,7 +1957,7 @@ stunlib_printBuffer(FILE* stream, int i; int linecnt = 0; - printError(stream, "%s Buffer (%i) = [\n", szHead, len); + printError(stream, "%s Buffer (%i) = [\n", szHead, len); for (i = 0; i < len; i++, linecnt++) { if (linecnt == 4) @@ -1930,11 +1968,11 @@ stunlib_printBuffer(FILE* stream, } else { - printError( stream, "%c", (linecnt ? ',' : ' ') ); + printError( stream, "%c", (linecnt ? ',' : ' ') ); } - printError( stream, " %02x", (uint8_t)( *( (pBuf + i) ) ) ); + printError( stream, " %02x", (uint8_t)( *( (pBuf + i) ) ) ); } - printError( stream, "];\n"); + printError(stream, "];\n"); } char const* @@ -2093,7 +2131,7 @@ stunlib_DecodeMessage(const uint8_t* buf, } if (restlen < message->msgHdr.msgLength) { - printError(stream, + printError(stream, "stunlib_DecodeMessage: The length in msg (%d) is larger than rest of buffer (%d)!\n", message->msgHdr.msgLength, restlen); @@ -2104,7 +2142,7 @@ stunlib_DecodeMessage(const uint8_t* buf, { if (stream) { - printError(stream, + printError(stream, "Parsing attribute head with restlen=%d at %p\n", restlen, pCurrPtr); @@ -2121,7 +2159,7 @@ stunlib_DecodeMessage(const uint8_t* buf, } if (stream) { - printError(stream, + printError(stream, "Attribute Header parsed: type == %d, length == %d\n", sAtr.type, sAtr.length); @@ -2407,6 +2445,17 @@ stunlib_DecodeMessage(const uint8_t* buf, message->hasBandwidthUsage = true; break; + case STUN_ATTR_TransCount: + if ( !stunDecodeTransCount(&message->transCount, + &pCurrPtr, + &restlen) ) + { + return false; + } + message->hasTransCount = true; + break; + + case STUN_ATTR_NetworkStatus: if (message->hasMessageIntegrity) { @@ -2669,7 +2718,7 @@ stunlib_encodeStunKeepAliveResp(StunMsgId* transId, { StunMsgHdr h; - memcpy( &h.id, transId, sizeof(h.id) ); + memcpy( &h.id, transId, sizeof(h.id) ); h.msgType = STUN_MSG_BindResponseMsg; h.msgLength = (srvrRflxAddr->familyType == STUN_ADDR_IPv4Family) ? 12 : 24; @@ -2729,7 +2778,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, + printError(stream, "invalid arguments (%p, %p, %d)\n", message, buf, @@ -2749,7 +2798,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Software (Name)\n"); + printError(stream, "Invalid Software (Name)\n"); } return 0; } @@ -2761,7 +2810,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Priority attribute\n"); + printError(stream, "Invalid Priority attribute\n"); } return 0; } @@ -2773,7 +2822,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid ICEControlled\n"); + printError(stream, "Invalid ICEControlled\n"); } return 0; } @@ -2785,7 +2834,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Username\n"); + printError(stream, "Invalid Username\n"); } return 0; } @@ -2797,7 +2846,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Nonce attribute\n"); + printError(stream, "Invalid Nonce attribute\n"); } return 0; } @@ -2810,7 +2859,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Realm attribute\n"); + printError(stream, "Invalid Realm attribute\n"); } return 0; } @@ -2822,7 +2871,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Lifetime attribute\n"); + printError(stream, "Invalid Lifetime attribute\n"); } return 0; } @@ -2834,7 +2883,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid RequestedTransport attribute\n"); + printError(stream, "Invalid RequestedTransport attribute\n"); } return 0; } @@ -2848,7 +2897,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid RequestedAddressFamily attribute\n"); + printError(stream, "Invalid RequestedAddressFamily attribute\n"); } return 0; } @@ -2862,7 +2911,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid RequestedAddressFamily attribute\n"); + printError(stream, "Invalid RequestedAddressFamily attribute\n"); } return 0; } @@ -2875,7 +2924,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid IceControlling\n"); + printError(stream, "Invalid IceControlling\n"); } return 0; } @@ -2888,7 +2937,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "mappedAddress failed \n"); + printError(stream, "mappedAddress failed \n"); } return 0; } @@ -2899,7 +2948,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Error attribute\n"); + printError(stream, "Invalid Error attribute\n"); } return 0; } @@ -2910,7 +2959,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid unknown attribute\n"); + printError(stream, "Invalid unknown attribute\n"); } return 0; } @@ -2925,7 +2974,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid xorMappedAddress\n"); + printError(stream, "Invalid xorMappedAddress\n"); } return 0; } @@ -2937,7 +2986,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid ChannelNumber attribute\n"); + printError(stream, "Invalid ChannelNumber attribute\n"); } return 0; } @@ -2951,7 +3000,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Alternate Server\n"); + printError(stream, "Invalid Alternate Server\n"); } return 0; } @@ -2981,7 +3030,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "xorRelayAddressIPv4 failed \n"); + printError(stream, "xorRelayAddressIPv4 failed \n"); } return 0; } @@ -2996,7 +3045,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "xorRelayAddressIPv6 failed \n"); + printError(stream, "xorRelayAddressIPv6 failed \n"); } return 0; } @@ -3007,7 +3056,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid UseCandidate\n"); + printError(stream, "Invalid UseCandidate\n"); } return 0; } @@ -3018,7 +3067,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid DontFragment\n"); + printError(stream, "Invalid DontFragment\n"); } return 0; } @@ -3029,7 +3078,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid EvenPort attribute\n"); + printError(stream, "Invalid EvenPort attribute\n"); } return 0; } @@ -3042,7 +3091,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Reservation Token attribute\n"); + printError(stream, "Invalid Reservation Token attribute\n"); } return 0; } @@ -3054,7 +3103,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid StreamType attribute\n"); + printError(stream, "Invalid StreamType attribute\n"); } return 0; } @@ -3067,7 +3116,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid BandwidthUsage attribute\n"); + printError(stream, "Invalid BandwidthUsage attribute\n"); } return 0; } @@ -3079,7 +3128,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid TTL attribute\n"); + printError(stream, "Invalid TTL attribute\n"); } return 0; } @@ -3091,7 +3140,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Network Status attribute\n"); + printError(stream, "Invalid Network Status attribute\n"); } return 0; } @@ -3103,11 +3152,22 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Cisco Network Feedback attribute\n"); + printError(stream, "Invalid Cisco Network Feedback attribute\n"); } return 0; } + if ( message->hasTransCount && + !stunEncodeTransCount(&message->transCount, + &pCurrPtr, + &restlen) ) + { + if (stream) + { + printError(stream, "Invalid TransCount attribute\n"); + } + return 0; + } /* note: DATA should be the last attribute */ if ( message->hasData && !stunEncodeDataAtr(&message->data, @@ -3116,7 +3176,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Data attribute\n"); + printError(stream, "Invalid Data attribute\n"); } return 0; } @@ -3150,7 +3210,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Network Status attribute\n"); + printError(stream, "Invalid Network Status attribute\n"); } return 0; } @@ -3162,7 +3222,7 @@ stunlib_encodeMessage(StunMessage* message, { if (stream) { - printError(stream, "Invalid Cisco Network Feedback attribute\n"); + printError(stream, "Invalid Cisco Network Feedback attribute\n"); } return 0; } @@ -3236,7 +3296,7 @@ stunlib_encodeMessage(StunMessage* message, } if (stream) { - printError(stream, "STUN_encode, messages to encode: \n"); + printError(stream, "STUN_encode, messages to encode: \n"); stun_printMessage(stream, message); printError(stream, "STUN_encode, buffer encoded: \n"); stunlib_printBuffer(stream, (uint8_t*)buf, msglen, "STUN"); diff --git a/src/stuntrace.c b/src/stuntrace.c index 9f76650..36fd4ae 100644 --- a/src/stuntrace.c +++ b/src/stuntrace.c @@ -359,7 +359,9 @@ StunStatusCallBack(void* userCtx, { struct hiutResult* result = (struct hiutResult*)userCtx; - result->pathElement[stunCbData->ttl].gotAnswer = true; + if(stunCbData->ttl <= MAX_TTL){ + result->pathElement[stunCbData->ttl].gotAnswer = true; + } switch (stunCbData->stunResult) { @@ -408,7 +410,6 @@ StunTrace_startTrace(STUN_CLIENT_DATA* clientData, return 0; } struct hiutResult* result; - uint32_t len; result = &clientData->traceResult; @@ -437,21 +438,19 @@ StunTrace_startTrace(STUN_CLIENT_DATA* clientData, strncpy(result->username, ufrag, sizeof(result->username) - 1); strncpy(result->password, password, sizeof(result->password) - 1); - len = StunClient_startSTUNTrace(result->stunCtx, - result, - toAddr, - fromAddr, - false, - result->username, - result->password, - result->currentTTL, - result->currStunMsgId, - result->sockfd, - result->sendFunc, - StunStatusCallBack, - NULL); - result->stunLen = len; - - return len; + StunClient_startSTUNTrace(result->stunCtx, + result, + toAddr, + fromAddr, + false, + result->username, + result->password, + result->currentTTL, + result->currStunMsgId, + result->sockfd, + result->sendFunc, + StunStatusCallBack, + NULL); + return 1; } diff --git a/test/stunclient_test.c b/test/stunclient_test.c index 96bd530..708d56a 100644 --- a/test/stunclient_test.c +++ b/test/stunclient_test.c @@ -48,9 +48,6 @@ char logStr[200]; CTEST_DATA(data) { int a; - - - }; #define STUN_TICK_INTERVAL_MS 50 @@ -114,6 +111,7 @@ stundbg(void* ctx, (void) category; (void) ctx; strncpy(logStr, errStr, sizeof logStr); + //printf("%s\n", errStr); } static int @@ -360,6 +358,9 @@ CTEST(stunclient, WaitBindRespNotAut_Timeout) { ASSERT_TRUE(stunResult == StunResult_Empty); StunClient_Alloc(&stunInstance); + StunClient_RegisterLogger(stunInstance, + stundbg, + NULL); sockaddr_initFromString( (struct sockaddr*)&stunServerAddr, "193.200.93.152:3478" ); diff --git a/test/stuntrace_test.c b/test/stuntrace_test.c index b22f446..6144b70 100644 --- a/test/stuntrace_test.c +++ b/test/stuntrace_test.c @@ -20,6 +20,19 @@ static const uint8_t StunCookie[] = STUN_MAGIC_COOKIE_ARRAY; const uint64_t test_addr_ipv4 = 1009527574; /* "60.44.43.22"); */ const uint32_t test_port_ipv4 = 43000; + +void +stundbg(void* ctx, + StunInfoCategory_T category, + char* errStr) +{ + (void) category; + (void) ctx; + (void) errStr; +// strncpy(logStr, errStr, sizeof logStr); +// printf("%s\n", errStr); +} + static void sendPacket(void* ctx, int sockfd, @@ -537,8 +550,9 @@ CTEST(stuntrace, run_IPv4_Stunresp) "192.168.1.34:45674" ); StunClient_Alloc(&clientData); - - + StunClient_RegisterLogger(clientData, + stundbg, + NULL); int len = StunTrace_startTrace(clientData, &someData, (const struct sockaddr*)&remoteAddr, @@ -561,22 +575,20 @@ CTEST(stuntrace, run_IPv4_Stunresp) m.xorMappedAddress.familyType = STUN_ADDR_IPv4Family; m.xorMappedAddress.addr.v4.addr = test_addr_ipv4; m.xorMappedAddress.addr.v4.port = test_port_ipv4; - StunClient_HandleIncResp(clientData, &m, NULL); - /* First hop.. */ - ASSERT_TRUE(LastTTL == 1); + ASSERT_TRUE(LastTTL == 40); sockaddr_initFromString( (struct sockaddr*)&hop1Addr, "192.168.1.1:45674" ); - StunClient_HandleICMP(clientData, + StunClient_HandleICMP(clientData, (struct sockaddr*)&hop1Addr, 11); - ASSERT_TRUE( sockaddr_alike( (struct sockaddr*)&LastHopAddr, + ASSERT_FALSE( sockaddr_alike( (struct sockaddr*)&LastHopAddr, (struct sockaddr*)&hop1Addr ) ); - ASSERT_TRUE( LastTTL == 2); + ASSERT_TRUE( LastTTL == 40); sockaddr_initFromString( (struct sockaddr*)&hop2Addr, "193.200.93.152:45674" ); @@ -584,6 +596,8 @@ CTEST(stuntrace, run_IPv4_Stunresp) StunClient_HandleICMP(clientData, (struct sockaddr*)&hop2Addr, 3); + + ASSERT_TRUE( sockaddr_alike( (struct sockaddr*)&LastHopAddr, (struct sockaddr*)&hop2Addr ) ); ASSERT_TRUE( Done); @@ -711,7 +725,7 @@ CTEST(stuntrace, run_IPv4_Stunresp_end) NULL); /* First hop.. */ - ASSERT_TRUE(LastTTL == 1); + ASSERT_TRUE(LastTTL == 40); sockaddr_initFromString( (struct sockaddr*)&hop1Addr, "192.168.1.1:45674" ); StunClient_HandleICMP(clientData, @@ -720,7 +734,7 @@ CTEST(stuntrace, run_IPv4_Stunresp_end) ASSERT_TRUE( sockaddr_alike( (struct sockaddr*)&LastHopAddr, (struct sockaddr*)&hop1Addr ) ); - ASSERT_TRUE( LastTTL == 2); + ASSERT_TRUE( LastTTL == 40); memcpy(&m.msgHdr.id, &LastTransId, STUN_MSG_ID_SIZE); StunClient_HandleIncResp(clientData, @@ -783,7 +797,7 @@ CTEST(stuntrace, run_IPv4_Stunresp_max_ttl) } /* First hop.. */ - ASSERT_TRUE(LastTTL == 39); + ASSERT_TRUE(LastTTL == 40); sockaddr_initFromString( (struct sockaddr*)&hop1Addr, "192.168.1.1:45674" ); StunClient_HandleICMP(clientData, From bdeb2f602f96ce0879d7484d41be4f3a827653a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Fri, 22 Apr 2016 14:29:11 +0200 Subject: [PATCH 02/15] Setting transmit counter in attribute tested. --- include/stun_intern.h | 5 +++++ include/stunclient.h | 2 ++ src/stunclient.c | 16 ++++++++++---- test/stunclient_test.c | 49 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/include/stun_intern.h b/include/stun_intern.h index ab47681..d5690da 100644 --- a/include/stun_intern.h +++ b/include/stun_intern.h @@ -113,10 +113,15 @@ typedef struct int32_t TimerRetransmit; uint32_t retransmits; + uint32_t reqTransCnt; + uint32_t respTransCnt; + /* RTT Info */ struct timeval start[STUNCLIENT_MAX_RETRANSMITS]; struct timeval stop[STUNCLIENT_MAX_RETRANSMITS]; + + /* icmp */ uint32_t ICMPtype; //uint32_t ttl; diff --git a/include/stunclient.h b/include/stunclient.h index ad51253..f853417 100644 --- a/include/stunclient.h +++ b/include/stunclient.h @@ -63,6 +63,8 @@ typedef struct struct sockaddr_storage dstBaseAddr; /* The destination seen from the * sender of the response */ uint32_t rtt; /* Rtt in microseconds */ + uint32_t reqTransCnt; + uint32_t respTransCnt; uint32_t retransmits; uint32_t ICMPtype; uint32_t ttl; diff --git a/src/stunclient.c b/src/stunclient.c index 850b4f2..a1c2687 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -1333,8 +1333,6 @@ StoreBindResp(STUN_TRANSACTION_DATA* trans, resp->xorMappedAddress.addr.v6.addr, htons(resp->xorMappedAddress.addr.v6.port) ); } - - return true; } else { @@ -1343,6 +1341,13 @@ StoreBindResp(STUN_TRANSACTION_DATA* trans, trans->inst); return false; } + + if (resp->hasTransCount) + { + trans->reqTransCnt = resp->transCount.reqCnt; + trans->respTransCnt = resp->transCount.respCnt; + } + return true; } static int @@ -1386,6 +1391,9 @@ BindRespCallback(STUN_TRANSACTION_DATA* trans, res.rtt = getRTTvalue(trans); res.ttl = trans->stunBindReq.ttl; + res.respTransCnt = trans->respTransCnt; + res.reqTransCnt = trans->reqTransCnt; + StunPrint( client->logUserData, client->Log_cb, StunInfoCategory_Info, " BindResp from src: %s", trans->inst, @@ -1522,8 +1530,8 @@ StunState_WaitBindResp(STUN_TRANSACTION_DATA* trans, case STUN_SIGNAL_ICMPResp: { StunRespStruct* pMsgIn = (StunRespStruct*)payload; - trans->ICMPtype = pMsgIn->ICMPtype; - trans->stunBindReq.ttl = pMsgIn->ttl; + trans->ICMPtype = pMsgIn->ICMPtype; + trans->stunBindReq.ttl = pMsgIn->ttl; ICMPRespCallback(trans, (struct sockaddr*)&pMsgIn->srcAddr); trans->stats.ICMPReceived++; SetNextState(trans, STUN_STATE_Idle); diff --git a/test/stunclient_test.c b/test/stunclient_test.c index 708d56a..9020906 100644 --- a/test/stunclient_test.c +++ b/test/stunclient_test.c @@ -45,6 +45,10 @@ DiscussData discussData; char logStr[200]; +int lastReqCnt; +int lastTranspRespCnt; +int lastTranspReqCnt; + CTEST_DATA(data) { int a; @@ -58,6 +62,8 @@ StunStatusCallBack(void* ctx, { (void)ctx; stunResult = retData->stunResult; + lastTranspRespCnt = retData->respTransCnt; + lastTranspReqCnt = retData->reqTransCnt; /* printf("Got STUN status callback\n");// (Result (%i)\n", * retData->stunResult); */ } @@ -99,6 +105,15 @@ SendRawStun(void* ctx, sockaddr_toString(addr, addr_str, SOCKADDR_MAX_STRLEN, true); + // And then we deencode to chek if correct values are set.. + StunMessage stunMsg; + stunlib_DecodeMessage(buf, len, &stunMsg, NULL, + NULL); + lastReqCnt = 0; + if(stunMsg.hasTransCount){ + lastReqCnt = stunMsg.transCount.reqCnt; + //lastTranspRespCnt = stunMsg.transCount.respCnt; + } /* printf("Sendto: '%s'\n", addr_str); */ } @@ -223,7 +238,9 @@ SimBindSuccessResp(bool IPv6, m.xorMappedAddress.addr.v4.addr = test_addr_ipv4; m.xorMappedAddress.addr.v4.port = test_port_ipv4; } - + m.hasTransCount = true; + m.transCount.respCnt = 2; + m.transCount.reqCnt = lastReqCnt; StunClient_HandleIncResp(stunInstance, &m, NULL); } @@ -384,7 +401,7 @@ CTEST(stunclient, WaitBindRespNotAut_Timeout) { StunClient_HandleTick(stunInstance, STUN_TICK_INTERVAL_MS); } - + ASSERT_TRUE(lastReqCnt == 9); ASSERT_TRUE(stunResult == StunResult_BindFailNoAnswer); StunClient_free(stunInstance); } @@ -400,8 +417,36 @@ CTEST(stunclient, WaitBindRespNotAut_BindSuccess) /*Just quick sanity if NULL ihandled propperly */ StunClient_HandleIncResp(NULL, NULL, NULL); SimBindSuccessRespWrongId(runningAsIPv6, true); + SimBindSuccessResp(runningAsIPv6, true); + ASSERT_TRUE(lastReqCnt == 1); + ASSERT_TRUE(stunResult == StunResult_BindOk); + StunClient_free(stunInstance); +} + +CTEST(stunclient, BindReq_TranportCnt) +{ + StunClient_Alloc(&stunInstance); + sockaddr_initFromString( (struct sockaddr*)&stunServerAddr, + "193.200.93.152:3478" ); + + StartBindTransaction(0); + ASSERT_TRUE(lastReqCnt == 1); + for (int i = 0; i < 5; i++) + { + StunClient_HandleTick(stunInstance, STUN_TICK_INTERVAL_MS); + } + ASSERT_TRUE(lastReqCnt == 2); + + for (int i = 0; i < 5; i++) + { + StunClient_HandleTick(stunInstance, STUN_TICK_INTERVAL_MS); + } + ASSERT_TRUE(lastReqCnt == 3); + SimBindSuccessResp(runningAsIPv6, true); ASSERT_TRUE(stunResult == StunResult_BindOk); + ASSERT_TRUE(lastTranspReqCnt == 3); + ASSERT_TRUE(lastTranspRespCnt == 2); StunClient_free(stunInstance); } From f86238625dfcd3e7300037bdfee8087c7e148105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Fri, 22 Apr 2016 15:03:16 +0200 Subject: [PATCH 03/15] Added RTT calculation. --- src/stunclient.c | 26 ++++++++++++++++++++------ test/stunclient_test.c | 4 ++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/stunclient.c b/src/stunclient.c index a1c2687..1fc9d39 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -1353,12 +1353,26 @@ StoreBindResp(STUN_TRANSACTION_DATA* trans, static int getRTTvalue(STUN_TRANSACTION_DATA* trans) { - int32_t stop = (trans->stop[trans->retransmits].tv_sec * 1000000 + - trans->stop[trans->retransmits].tv_usec); - /* Always use the first stored value for start. */ - int32_t start = (trans->start[0].tv_sec * 1000000 + - trans->start[0].tv_usec); + int32_t start, stop = 0; + + if ( trans->reqTransCnt > 0 && trans->reqTransCnt < STUNCLIENT_MAX_RETRANSMITS ) + { + stop = (trans->stop[trans->reqTransCnt].tv_sec * 1000000 + + trans->stop[trans->reqTransCnt].tv_usec); + /* Always use the first stored value for start. */ + start = (trans->start[trans->reqTransCnt].tv_sec * 1000000 + + trans->start[trans->reqTransCnt].tv_usec); + } + else + { + stop = (trans->stop[trans->retransmits].tv_sec * 1000000 + + trans->stop[trans->retransmits].tv_usec); + /* Always use the first stored value for start. */ + start = (trans->start[0].tv_sec * 1000000 + + trans->start[0].tv_usec); + + } return stop - start; @@ -1392,7 +1406,7 @@ BindRespCallback(STUN_TRANSACTION_DATA* trans, res.ttl = trans->stunBindReq.ttl; res.respTransCnt = trans->respTransCnt; - res.reqTransCnt = trans->reqTransCnt; + res.reqTransCnt = trans->reqTransCnt; StunPrint( client->logUserData, client->Log_cb, StunInfoCategory_Info, " BindResp from src: %s", diff --git a/test/stunclient_test.c b/test/stunclient_test.c index 9020906..cd751c1 100644 --- a/test/stunclient_test.c +++ b/test/stunclient_test.c @@ -332,8 +332,8 @@ CTEST(stunclient, logger) { StunClient_Alloc(&stunInstance); StunClient_RegisterLogger(stunInstance, - stundbg, - NULL); + stundbg, + NULL); StartBindTransaction(0); ASSERT_TRUE( 0 == strncmp("", logStr, 15) ); From ba19d7ab328f2dc359664e4d343b9a3642716903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Mon, 25 Apr 2016 11:02:39 +0200 Subject: [PATCH 04/15] Moved some src out of the client files. --- include/stunclient.h | 21 ---- include/stunserver.h | 40 ++++++ src/CMakeLists.txt | 1 + src/stunclient.c | 268 +-------------------------------------- src/stunserver.c | 277 +++++++++++++++++++++++++++++++++++++++++ test/stunserver_test.c | 2 +- 6 files changed, 320 insertions(+), 289 deletions(-) create mode 100644 include/stunserver.h create mode 100644 src/stunserver.c diff --git a/include/stunclient.h b/include/stunclient.h index f853417..e630b22 100644 --- a/include/stunclient.h +++ b/include/stunclient.h @@ -232,27 +232,6 @@ StunClient_cancelBindingTransaction(STUN_CLIENT_DATA* clientData, StunMsgId transactionId); -/********* Server handling: send STUN BIND RESP *************/ -bool -StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA* clientData, - int32_t globalSocketId, - StunMsgId transactionId, - const char* password, - const struct sockaddr* mappedAddr, - const struct sockaddr* dstAddr, - void* userData, - STUN_SENDFUNC sendFunc, - int proto, - bool useRelay, - uint32_t responseCode, - DiscussData* discussData); - -/********** Server handling: incoming STUN BIND REQ **********/ -bool -StunServer_HandleStunIncomingBindReqMsg(STUN_CLIENT_DATA* clientData, - STUN_INCOMING_REQ_DATA* pReq, - const StunMessage* stunMsg, - bool fromRelay); void StunClient_clearStats(STUN_CLIENT_DATA* clientData); diff --git a/include/stunserver.h b/include/stunserver.h new file mode 100644 index 0000000..3838d76 --- /dev/null +++ b/include/stunserver.h @@ -0,0 +1,40 @@ + + +#ifndef STUNSERVER_H +#define STUNSERVER_H + + +#include "stunlib.h" /* stun enc/dec and msg formats*/ +#include +#include "sockaddr_util.h" +#include "stunclient.h" +#include "stun_intern.h" + +#ifdef __cplusplus +extern "C" { +#else +#include +#endif + +/********* Server handling: send STUN BIND RESP *************/ +bool +StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA* clientData, + int32_t globalSocketId, + StunMsgId transactionId, + const char* password, + const struct sockaddr* mappedAddr, + const struct sockaddr* dstAddr, + void* userData, + STUN_SENDFUNC sendFunc, + int proto, + bool useRelay, + uint32_t responseCode, + DiscussData* discussData); + +/********** Server handling: incoming STUN BIND REQ **********/ +bool +StunServer_HandleStunIncomingBindReqMsg(STUN_CLIENT_DATA* clientData, + STUN_INCOMING_REQ_DATA* pReq, + const StunMessage* stunMsg, + bool fromRelay); +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4c0d68a..8ca317f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,6 +4,7 @@ set ( stunlib_srcs stunclient.c + stunserver.c stunlib.c turnclient.c stuntrace.c diff --git a/src/stunclient.c b/src/stunclient.c index 1fc9d39..337a8fa 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -32,6 +32,7 @@ #include #include "stunclient.h" + #include "stun_intern.h" @@ -532,275 +533,8 @@ StunClient_cancelBindingTransaction(STUN_CLIENT_DATA* clientData, } -static bool -CreateConnectivityBindingResp(StunMessage* stunMsg, - StunMsgId transactionId, - const struct sockaddr* mappedSockAddr, - uint16_t response, - uint32_t responseCode, - DiscussData* discussData) -{ - StunIPAddress mappedAddr; - - if ( !sockaddr_isSet(mappedSockAddr) ) - { - return false; - } - - memset(stunMsg, 0, sizeof *stunMsg); - stunMsg->msgHdr.msgType = response; - - if (mappedSockAddr->sa_family == AF_INET) - { - mappedAddr.familyType = STUN_ADDR_IPv4Family; - mappedAddr.addr.v4.port = ntohs( - ( (struct sockaddr_in*)mappedSockAddr )->sin_port); - mappedAddr.addr.v4.addr = ntohl( - ( (struct sockaddr_in*)mappedSockAddr )->sin_addr.s_addr); - - } - else if (mappedSockAddr->sa_family == AF_INET6) - { - mappedAddr.familyType = STUN_ADDR_IPv6Family; - mappedAddr.addr.v6.port = ntohs( - ( (struct sockaddr_in6*)mappedSockAddr )->sin6_port); - - /*TODO: will this be correct ? */ - memcpy( mappedAddr.addr.v6.addr, - ( (struct sockaddr_in6*)mappedSockAddr )->sin6_addr.s6_addr, - sizeof(mappedAddr.addr.v6.addr) ); - } - else - { - return false; - } - - /*id*/ - stunMsg->msgHdr.id = transactionId; - - /* The XOR address MUST be added according to the RFC */ - stunMsg->hasXorMappedAddress = true; - stunMsg->xorMappedAddress = mappedAddr; - if (discussData != NULL) - { - stunMsg->hasStreamType = true; - stunMsg->streamType.type = discussData->streamType; - stunMsg->streamType.interactivity = discussData->interactivity; - - stunMsg->hasNetworkStatus = true; - stunMsg->networkStatus.flags = 0; - stunMsg->networkStatus.nodeCnt = 0; - stunMsg->networkStatus.upMaxBandwidth = 0; - stunMsg->networkStatus.downMaxBandwidth = 0; - stunMsg->hasNetworkStatusResp = true; - stunMsg->networkStatusResp.flags = - discussData->networkStatusResp_flags; - stunMsg->networkStatusResp.nodeCnt = - discussData->networkStatusResp_nodeCnt; - stunMsg->networkStatusResp.upMaxBandwidth = - discussData->networkStatusResp_upMaxBandwidth; - stunMsg->networkStatusResp.downMaxBandwidth = - discussData->networkStatusResp_downMaxBandwidth; - } - if (responseCode != 200) - { - stunMsg->hasErrorCode = true; - stunMsg->errorCode.errorClass = responseCode / 100; - stunMsg->errorCode.number = (uint8_t) (responseCode % 100); - if (responseCode == 487) - { - strncpy( stunMsg->errorCode.reason, "Role Conflict", - sizeof (stunMsg->errorCode.reason) ); - stunMsg->errorCode.sizeReason = strlen(stunMsg->errorCode.reason); - } - else if (responseCode == 400) - { - strncpy( stunMsg->errorCode.reason, "Bad Request", - sizeof (stunMsg->errorCode.reason) ); - stunMsg->errorCode.sizeReason = strlen(stunMsg->errorCode.reason); - } - } - - return true; -} - - -static bool -SendConnectivityBindResponse(STUN_CLIENT_DATA* clientData, - int32_t globalSocketId, - StunMessage* stunRespMsg, - const char* password, - const struct sockaddr* dstAddr, - void* userData, - STUN_SENDFUNC sendFunc, - int proto, - bool useRelay) -{ - uint8_t stunBuff[STUN_MAX_PACKET_SIZE]; - int stunLen; - - (void) userData; - /* encode bind Response */ - stunLen = stunlib_encodeMessage(stunRespMsg, - (uint8_t*)stunBuff, - STUN_MAX_PACKET_SIZE, - (unsigned char*)password, /* md5key - **/ - password ? strlen(password) : 0, /* keyLen - **/ - NULL); - if (!stunLen) - { - StunPrint(clientData->logUserData, - clientData->Log_cb, - StunInfoCategory_Error, - " Failed to encode Binding request response\n"); - return false; - } - - /* send */ - /* sendFunc(globalSocketId, stunBuff, stunLen, dstAddr, useRelay, 0); */ - sendFunc(clientData->userCtx, - globalSocketId, - stunBuff, - stunLen, - dstAddr, - proto, - useRelay, - 0); - clientData->stats.BindRespSent++; - return true; -} - - -/********* Server handling of STUN BIND RESP *************/ -bool -StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA* clientData, - int32_t globalSocketId, - StunMsgId transactionId, - const char* password, - const struct sockaddr* mappedAddr, - const struct sockaddr* dstAddr, - void* userData, - STUN_SENDFUNC sendFunc, - int proto, - bool useRelay, - uint32_t responseCode, - DiscussData* discussData) -{ - StunMessage stunRespMsg; - - /* format */ - if ( CreateConnectivityBindingResp(&stunRespMsg, - transactionId, - mappedAddr, - (responseCode == - 200) ? STUN_MSG_BindResponseMsg : - STUN_MSG_BindErrorResponseMsg, - responseCode, - discussData) ) - { - /* encode and send */ - if ( SendConnectivityBindResponse(clientData, - globalSocketId, - &stunRespMsg, - password, - dstAddr, - userData, - sendFunc, - proto, - useRelay) ) - { - return true; - } - } - return false; -} - - -/********** Server handling of incoming STUN BIND REQ **********/ -bool -StunServer_HandleStunIncomingBindReqMsg(STUN_CLIENT_DATA* clientData, - STUN_INCOMING_REQ_DATA* pReq, - const StunMessage* stunMsg, - bool fromRelay) -{ - if (!clientData) - { - return false; - } - - memcpy( &pReq->transactionId, &stunMsg->msgHdr.id, sizeof(StunMsgId) ); - - pReq->fromRelay = fromRelay; - - if (stunMsg->hasUsername) - { - strncpy( pReq->ufrag, stunMsg->username.value, - min(stunMsg->username.sizeValue, STUN_MAX_STRING) ); - if (stunMsg->username.sizeValue < STUN_MAX_STRING) - { - pReq->ufrag[stunMsg->username.sizeValue] = '\0'; - } - else - { - pReq->ufrag[STUN_MAX_STRING - 1] = '\0'; - } - } - else - { - StunPrint(clientData->logUserData, - clientData->Log_cb, - StunInfoCategory_Error, - " Missing Username in Binding Request\n"); - return false; - } - - if (stunMsg->hasPriority) - { - pReq->peerPriority = stunMsg->priority.value; - } - else - { - StunPrint(clientData->logUserData, - clientData->Log_cb, - StunInfoCategory_Error, - " Missing Priority in Binding Request\n"); - return false; - } - - pReq->useCandidate = stunMsg->hasUseCandidate; - - if (stunMsg->hasControlling) - { - pReq->iceControlling = true; - pReq->tieBreaker = stunMsg->controlling.value; - } - else - { - pReq->iceControlling = false; - } - - if (stunMsg->hasControlled) - { - pReq->iceControlled = true; - pReq->tieBreaker = stunMsg->controlled.value; - } - else - { - pReq->iceControlled = false; - } - - if (fromRelay) - { - clientData->stats.BindReqReceived_ViaRelay++; - } - clientData->stats.BindReqReceived++; - - return true; -} /*************************************************************************/ diff --git a/src/stunserver.c b/src/stunserver.c new file mode 100644 index 0000000..acc10b7 --- /dev/null +++ b/src/stunserver.c @@ -0,0 +1,277 @@ + +#include +#include + +#include "stunserver.h" + + + +static bool +CreateConnectivityBindingResp(StunMessage* stunMsg, + StunMsgId transactionId, + const struct sockaddr* mappedSockAddr, + uint16_t response, + uint32_t responseCode, + DiscussData* discussData) +{ + StunIPAddress mappedAddr; + + if ( !sockaddr_isSet(mappedSockAddr) ) + { + return false; + } + + memset(stunMsg, 0, sizeof *stunMsg); + stunMsg->msgHdr.msgType = response; + + if (mappedSockAddr->sa_family == AF_INET) + { + mappedAddr.familyType = STUN_ADDR_IPv4Family; + mappedAddr.addr.v4.port = ntohs( + ( (struct sockaddr_in*)mappedSockAddr )->sin_port); + mappedAddr.addr.v4.addr = ntohl( + ( (struct sockaddr_in*)mappedSockAddr )->sin_addr.s_addr); + + } + else if (mappedSockAddr->sa_family == AF_INET6) + { + mappedAddr.familyType = STUN_ADDR_IPv6Family; + mappedAddr.addr.v6.port = ntohs( + ( (struct sockaddr_in6*)mappedSockAddr )->sin6_port); + + /*TODO: will this be correct ? */ + memcpy( mappedAddr.addr.v6.addr, + ( (struct sockaddr_in6*)mappedSockAddr )->sin6_addr.s6_addr, + sizeof(mappedAddr.addr.v6.addr) ); + } + else + { + return false; + } + + /*id*/ + stunMsg->msgHdr.id = transactionId; + + /* The XOR address MUST be added according to the RFC */ + stunMsg->hasXorMappedAddress = true; + stunMsg->xorMappedAddress = mappedAddr; + + if (discussData != NULL) + { + stunMsg->hasStreamType = true; + stunMsg->streamType.type = discussData->streamType; + stunMsg->streamType.interactivity = discussData->interactivity; + + stunMsg->hasNetworkStatus = true; + stunMsg->networkStatus.flags = 0; + stunMsg->networkStatus.nodeCnt = 0; + stunMsg->networkStatus.upMaxBandwidth = 0; + stunMsg->networkStatus.downMaxBandwidth = 0; + + stunMsg->hasNetworkStatusResp = true; + stunMsg->networkStatusResp.flags = + discussData->networkStatusResp_flags; + stunMsg->networkStatusResp.nodeCnt = + discussData->networkStatusResp_nodeCnt; + stunMsg->networkStatusResp.upMaxBandwidth = + discussData->networkStatusResp_upMaxBandwidth; + stunMsg->networkStatusResp.downMaxBandwidth = + discussData->networkStatusResp_downMaxBandwidth; + } + if (responseCode != 200) + { + stunMsg->hasErrorCode = true; + stunMsg->errorCode.errorClass = responseCode / 100; + stunMsg->errorCode.number = (uint8_t) (responseCode % 100); + if (responseCode == 487) + { + strncpy( stunMsg->errorCode.reason, "Role Conflict", + sizeof (stunMsg->errorCode.reason) ); + stunMsg->errorCode.sizeReason = strlen(stunMsg->errorCode.reason); + } + else if (responseCode == 400) + { + strncpy( stunMsg->errorCode.reason, "Bad Request", + sizeof (stunMsg->errorCode.reason) ); + stunMsg->errorCode.sizeReason = strlen(stunMsg->errorCode.reason); + } + } + + return true; +} + + +static bool +SendConnectivityBindResponse(STUN_CLIENT_DATA* clientData, + int32_t globalSocketId, + StunMessage* stunRespMsg, + const char* password, + const struct sockaddr* dstAddr, + void* userData, + STUN_SENDFUNC sendFunc, + int proto, + bool useRelay) +{ + uint8_t stunBuff[STUN_MAX_PACKET_SIZE]; + int stunLen; + + (void) userData; + /* encode bind Response */ + stunLen = stunlib_encodeMessage(stunRespMsg, + (uint8_t*)stunBuff, + STUN_MAX_PACKET_SIZE, + (unsigned char*)password, /* md5key + **/ + password ? strlen(password) : 0, /* keyLen + **/ + NULL); + if (!stunLen) + { + StunPrint(clientData->logUserData, + clientData->Log_cb, + StunInfoCategory_Error, + " Failed to encode Binding request response\n"); + return false; + } + + /* send */ + /* sendFunc(globalSocketId, stunBuff, stunLen, dstAddr, useRelay, 0); */ + sendFunc(clientData->userCtx, + globalSocketId, + stunBuff, + stunLen, + dstAddr, + proto, + useRelay, + 0); + clientData->stats.BindRespSent++; + return true; +} + + +/********* Server handling of STUN BIND RESP *************/ +bool +StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA* clientData, + int32_t globalSocketId, + StunMsgId transactionId, + const char* password, + const struct sockaddr* mappedAddr, + const struct sockaddr* dstAddr, + void* userData, + STUN_SENDFUNC sendFunc, + int proto, + bool useRelay, + uint32_t responseCode, + DiscussData* discussData) +{ + StunMessage stunRespMsg; + + /* format */ + if ( CreateConnectivityBindingResp(&stunRespMsg, + transactionId, + mappedAddr, + (responseCode == + 200) ? STUN_MSG_BindResponseMsg : + STUN_MSG_BindErrorResponseMsg, + responseCode, + discussData) ) + { + /* encode and send */ + if ( SendConnectivityBindResponse(clientData, + globalSocketId, + &stunRespMsg, + password, + dstAddr, + userData, + sendFunc, + proto, + useRelay) ) + { + return true; + } + } + return false; +} + + +/********** Server handling of incoming STUN BIND REQ **********/ +bool +StunServer_HandleStunIncomingBindReqMsg(STUN_CLIENT_DATA* clientData, + STUN_INCOMING_REQ_DATA* pReq, + const StunMessage* stunMsg, + bool fromRelay) +{ + if (!clientData) + { + return false; + } + + memcpy( &pReq->transactionId, &stunMsg->msgHdr.id, sizeof(StunMsgId) ); + + pReq->fromRelay = fromRelay; + + if (stunMsg->hasUsername) + { + strncpy( pReq->ufrag, stunMsg->username.value, + min(stunMsg->username.sizeValue, STUN_MAX_STRING) ); + if (stunMsg->username.sizeValue < STUN_MAX_STRING) + { + pReq->ufrag[stunMsg->username.sizeValue] = '\0'; + } + else + { + pReq->ufrag[STUN_MAX_STRING - 1] = '\0'; + } + } + else + { + StunPrint(clientData->logUserData, + clientData->Log_cb, + StunInfoCategory_Error, + " Missing Username in Binding Request\n"); + return false; + } + + if (stunMsg->hasPriority) + { + pReq->peerPriority = stunMsg->priority.value; + } + else + { + StunPrint(clientData->logUserData, + clientData->Log_cb, + StunInfoCategory_Error, + " Missing Priority in Binding Request\n"); + return false; + } + + pReq->useCandidate = stunMsg->hasUseCandidate; + + if (stunMsg->hasControlling) + { + pReq->iceControlling = true; + pReq->tieBreaker = stunMsg->controlling.value; + } + else + { + pReq->iceControlling = false; + } + + if (stunMsg->hasControlled) + { + pReq->iceControlled = true; + pReq->tieBreaker = stunMsg->controlled.value; + } + else + { + pReq->iceControlled = false; + } + + if (fromRelay) + { + clientData->stats.BindReqReceived_ViaRelay++; + } + clientData->stats.BindReqReceived++; + + return true; +} diff --git a/test/stunserver_test.c b/test/stunserver_test.c index fc7627c..6ed653a 100644 --- a/test/stunserver_test.c +++ b/test/stunserver_test.c @@ -3,7 +3,7 @@ #include -#include "stunclient.h" +#include "stunserver.h" #include "sockaddr_util.h" #include "test_utils.h" From 795afdc8855bcf39df4fd794e4b5a5af33699eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Mon, 25 Apr 2016 13:19:45 +0200 Subject: [PATCH 05/15] tore rtt and retransmits in callback. --- src/stunclient.c | 55 +++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/src/stunclient.c b/src/stunclient.c index 337a8fa..e6fa444 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -938,6 +938,33 @@ StartNextRetransmitTimer(STUN_TRANSACTION_DATA* trans) } } +static int +getRTTvalue(STUN_TRANSACTION_DATA* trans) +{ + int32_t start, stop = 0; + + + if ( trans->reqTransCnt > 0 && trans->reqTransCnt < STUNCLIENT_MAX_RETRANSMITS ) + { + stop = (trans->stop[trans->reqTransCnt].tv_sec * 1000000 + + trans->stop[trans->reqTransCnt].tv_usec); + /* Always use the first stored value for start. */ + start = (trans->start[trans->reqTransCnt].tv_sec * 1000000 + + trans->start[trans->reqTransCnt].tv_usec); + } + else + { + stop = (trans->stop[trans->retransmits].tv_sec * 1000000 + + trans->stop[trans->retransmits].tv_usec); + /* Always use the first stored value for start. */ + start = (trans->start[0].tv_sec * 1000000 + + trans->start[0].tv_usec); + + } + return stop - start; + + +} static void CallBack(STUN_TRANSACTION_DATA* trans, @@ -949,6 +976,8 @@ CallBack(STUN_TRANSACTION_DATA* trans, memcpy( &res.msgId, &trans->stunBindReq.transactionId, sizeof(StunMsgId) ); res.stunResult = stunResult; res.ttl = trans->stunBindReq.ttl; + res.rtt = getRTTvalue(trans); + res.retransmits = trans->retransmits; if (trans->stunBindReq.stunCbFunc) { @@ -1084,33 +1113,7 @@ StoreBindResp(STUN_TRANSACTION_DATA* trans, return true; } -static int -getRTTvalue(STUN_TRANSACTION_DATA* trans) -{ - int32_t start, stop = 0; - - if ( trans->reqTransCnt > 0 && trans->reqTransCnt < STUNCLIENT_MAX_RETRANSMITS ) - { - stop = (trans->stop[trans->reqTransCnt].tv_sec * 1000000 + - trans->stop[trans->reqTransCnt].tv_usec); - /* Always use the first stored value for start. */ - start = (trans->start[trans->reqTransCnt].tv_sec * 1000000 + - trans->start[trans->reqTransCnt].tv_usec); - } - else - { - stop = (trans->stop[trans->retransmits].tv_sec * 1000000 + - trans->stop[trans->retransmits].tv_usec); - /* Always use the first stored value for start. */ - start = (trans->start[0].tv_sec * 1000000 + - trans->start[0].tv_usec); - - } - return stop - start; - - -} static void BindRespCallback(STUN_TRANSACTION_DATA* trans, const struct sockaddr* srcAddr) From ef0b8a71d830fb7eee7315a5d726826abef0dc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Mon, 25 Apr 2016 13:28:06 +0200 Subject: [PATCH 06/15] More retransmit updates.. --- src/stunclient.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/stunclient.c b/src/stunclient.c index e6fa444..3739317 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -1141,6 +1141,7 @@ BindRespCallback(STUN_TRANSACTION_DATA* trans, res.rtt = getRTTvalue(trans); res.ttl = trans->stunBindReq.ttl; + res.retransmits = trans->retransmits; res.respTransCnt = trans->respTransCnt; res.reqTransCnt = trans->reqTransCnt; From 4600aebcac2501f74671c879dd57101d1a2e193d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Mon, 25 Apr 2016 14:23:06 +0200 Subject: [PATCH 07/15] Adding transport counters. --- include/stunserver.h | 2 ++ src/stunserver.c | 12 ++++++++++++ test/stunserver_test.c | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/include/stunserver.h b/include/stunserver.h index 3838d76..b67a389 100644 --- a/include/stunserver.h +++ b/include/stunserver.h @@ -24,6 +24,8 @@ StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA* clientData, const char* password, const struct sockaddr* mappedAddr, const struct sockaddr* dstAddr, + uint8_t reqTrnspCnt, + uint8_t respTrnspCnt, void* userData, STUN_SENDFUNC sendFunc, int proto, diff --git a/src/stunserver.c b/src/stunserver.c index acc10b7..e2f8293 100644 --- a/src/stunserver.c +++ b/src/stunserver.c @@ -10,6 +10,8 @@ static bool CreateConnectivityBindingResp(StunMessage* stunMsg, StunMsgId transactionId, const struct sockaddr* mappedSockAddr, + uint8_t reqTrnspCnt, + uint8_t respTrnspCnt, uint16_t response, uint32_t responseCode, DiscussData* discussData) @@ -24,6 +26,12 @@ CreateConnectivityBindingResp(StunMessage* stunMsg, memset(stunMsg, 0, sizeof *stunMsg); stunMsg->msgHdr.msgType = response; + if(reqTrnspCnt != 0 ){ + stunMsg->hasTransCount = true; + stunMsg->transCount.respCnt = respTrnspCnt; + stunMsg->transCount.reqCnt = reqTrnspCnt; + } + if (mappedSockAddr->sa_family == AF_INET) { mappedAddr.familyType = STUN_ADDR_IPv4Family; @@ -157,6 +165,8 @@ StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA* clientData, const char* password, const struct sockaddr* mappedAddr, const struct sockaddr* dstAddr, + uint8_t reqTrnspCnt, + uint8_t respTrnspCnt, void* userData, STUN_SENDFUNC sendFunc, int proto, @@ -170,6 +180,8 @@ StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA* clientData, if ( CreateConnectivityBindingResp(&stunRespMsg, transactionId, mappedAddr, + reqTrnspCnt, + respTrnspCnt, (responseCode == 200) ? STUN_MSG_BindResponseMsg : STUN_MSG_BindErrorResponseMsg, diff --git a/test/stunserver_test.c b/test/stunserver_test.c index 6ed653a..2b96763 100644 --- a/test/stunserver_test.c +++ b/test/stunserver_test.c @@ -126,6 +126,8 @@ CTEST(stunserver, SendResp_Valid) mappedAddr, (struct sockaddr*)& servAddr, + 0, + 0, NULL, SendRawStun, 0, @@ -142,6 +144,8 @@ CTEST(stunserver, SendResp_Valid) mappedAddr, (struct sockaddr*)& servAddr, + 2, + 3, NULL, SendRawStun, 0, @@ -170,6 +174,8 @@ CTEST(stunserver, SendResp_Valid_IPv6) mappedAddr, (struct sockaddr*)& servAddr, + 0, + 0, NULL, SendRawStun, 0, @@ -186,6 +192,8 @@ CTEST(stunserver, SendResp_Valid_IPv6) mappedAddr, (struct sockaddr*)& servAddr, + 0, + 0, NULL, SendRawStun, 0, @@ -221,6 +229,8 @@ CTEST(stunserver, SendDiscussResp_Valid) stunServerAddr, (struct sockaddr*)& stunServerAddr, + 2, + 2, NULL, SendRawStun, 0, From 710026e9a09a143577474992235b4480b3bf13c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Tue, 26 Apr 2016 10:14:27 +0200 Subject: [PATCH 08/15] Fixed bug in decoding transpcnt attribute --- include/stunserver.h | 9 +++++++++ src/stunlib.c | 6 ++---- src/stunserver.c | 2 +- test/stunserver_test.c | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/include/stunserver.h b/include/stunserver.h index b67a389..819a6b7 100644 --- a/include/stunserver.h +++ b/include/stunserver.h @@ -16,6 +16,15 @@ extern "C" { #include #endif +bool +CreateConnectivityBindingResp(StunMessage* stunMsg, + StunMsgId transactionId, + const struct sockaddr* mappedSockAddr, + uint8_t reqTrnspCnt, + uint8_t respTrnspCnt, + uint16_t response, + uint32_t responseCode, + DiscussData* discussData); /********* Server handling: send STUN BIND RESP *************/ bool StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA* clientData, diff --git a/src/stunlib.c b/src/stunlib.c index 4c06701..2a46f9d 100644 --- a/src/stunlib.c +++ b/src/stunlib.c @@ -1560,7 +1560,7 @@ stunDecodeTransCount(StunAtrTransCount* transCountAtr, read_8(pBuf, &transCountAtr->reqCnt); read_8(pBuf, &transCountAtr->respCnt); - *nBufLen -= 8; + *nBufLen -= 4; return true; } @@ -2553,7 +2553,7 @@ stunlib_DecodeMessage(const uint8_t* buf, { if (stream) { - fprintf(stream, " Message length or attribute length error.\n"); + fprintf(stream, " Message length or attribute length error(%i).\n", restlen); } return false; } @@ -3505,12 +3505,10 @@ bool stunlib_checkFingerPrint(const uint8_t* buf, uint32_t fpOffset) { - uint32_t crc = stunlib_calculateFingerprint(buf, fpOffset - 4); uint32_t val; const uint8_t* pos = buf + fpOffset; read_32(&pos, &val); - if (crc == val) { return true; diff --git a/src/stunserver.c b/src/stunserver.c index e2f8293..96c48b0 100644 --- a/src/stunserver.c +++ b/src/stunserver.c @@ -6,7 +6,7 @@ -static bool +bool CreateConnectivityBindingResp(StunMessage* stunMsg, StunMsgId transactionId, const struct sockaddr* mappedSockAddr, diff --git a/test/stunserver_test.c b/test/stunserver_test.c index 2b96763..3a25e05 100644 --- a/test/stunserver_test.c +++ b/test/stunserver_test.c @@ -20,12 +20,15 @@ static struct sockaddr_storage LastAddress; StunResult_T stunResult; struct sockaddr_storage stunServerAddr; +struct sockaddr_storage mappedAddr; DiscussData discussData; STUN_CLIENT_DATA* stunInstance; #define STUN_TICK_INTERVAL_MS 50 +const char passwd[] ="testtest"; + static void SendRawStun(void* ctx, int sockfd, @@ -55,6 +58,42 @@ SendRawStun(void* ctx, } + +CTEST(stunserver, Encode_decode) +{ + StunMessage stunMsg; + StunMessage stunResponse; + StunMsgId stunId; + + uint8_t stunBuff[STUN_MAX_PACKET_SIZE]; + stunlib_createId(&stunId, + 232323, + 3); + sockaddr_initFromString( (struct sockaddr*)&mappedAddr, + "193.200.93.152:3478" ); + CreateConnectivityBindingResp(&stunMsg, + stunId, + (struct sockaddr *)&mappedAddr, + 1, + 1, + STUN_MSG_BindResponseMsg, + 200, + NULL); + + int len = stunlib_encodeMessage(&stunMsg, + (uint8_t*)stunBuff, + STUN_MAX_PACKET_SIZE, + (unsigned char *) passwd, + strlen(passwd), + NULL); + ASSERT_TRUE(len == 72); + + ASSERT_TRUE( stunlib_DecodeMessage(stunBuff, len, + &stunResponse, + NULL, NULL /*stdout for debug*/)); + +} + CTEST(stunserver, HandleReq_Valid) { STUN_INCOMING_REQ_DATA pReq; From e6bf09e2a68069c5a158fd92651528b0eadfc519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Tue, 26 Apr 2016 12:11:15 +0200 Subject: [PATCH 09/15] Fixed RTT calculation. (Retransmits vs sent packes is still a mess). --- src/stunclient.c | 21 ++++++++++----------- test/stunclient_test.c | 4 ++++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/stunclient.c b/src/stunclient.c index 3739317..119c067 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -813,7 +813,7 @@ SendStunReq(STUN_TRANSACTION_DATA* trans, } /*Store Time so we can messure RTT */ - gettimeofday(&trans->start[trans->retransmits], NULL); + gettimeofday(&trans->start[0], NULL); if (trans->stunBindReq.sendFunc != NULL) { trans->stunBindReq.sendFunc(trans->client->userCtx, @@ -893,8 +893,7 @@ RetransmitLastReq(STUN_TRANSACTION_DATA* trans, NULL); } - - gettimeofday(&trans->start[trans->retransmits + 1], NULL); + gettimeofday(&trans->start[trans->retransmits +1], NULL); trans->stunBindReq.sendFunc(trans->client->userCtx, trans->stunBindReq.sockhandle, stunReqMsgBuf, @@ -946,11 +945,11 @@ getRTTvalue(STUN_TRANSACTION_DATA* trans) if ( trans->reqTransCnt > 0 && trans->reqTransCnt < STUNCLIENT_MAX_RETRANSMITS ) { - stop = (trans->stop[trans->reqTransCnt].tv_sec * 1000000 + - trans->stop[trans->reqTransCnt].tv_usec); + stop = (trans->stop[trans->reqTransCnt-1].tv_sec * 1000000 + + trans->stop[trans->reqTransCnt-1].tv_usec); /* Always use the first stored value for start. */ - start = (trans->start[trans->reqTransCnt].tv_sec * 1000000 + - trans->start[trans->reqTransCnt].tv_usec); + start = (trans->start[trans->reqTransCnt-1].tv_sec * 1000000 + + trans->start[trans->reqTransCnt-1].tv_usec); } else { @@ -962,8 +961,6 @@ getRTTvalue(STUN_TRANSACTION_DATA* trans) } return stop - start; - - } static void @@ -1005,7 +1002,7 @@ CommonRetryTimeoutHandler(STUN_TRANSACTION_DATA* trans, max = STUNCLIENT_MAX_RETRANSMITS; } - if ( (trans->retransmits < max) + if ( (trans->retransmits < (max-1)) && (stunTimeoutList[trans->retransmits] != 0) ) /* can be 0 terminated if * using fewer * retransmits @@ -1019,10 +1016,11 @@ CommonRetryTimeoutHandler(STUN_TRANSACTION_DATA* trans, " Retrans %s Retry: %d to %s", trans->inst, errStr, trans->retransmits, peer); StunMessage stunReqMsg; + trans->retransmits++; BuildStunBindReq(trans, &stunReqMsg); RetransmitLastReq(trans, &stunReqMsg, &trans->stunBindReq.serverAddr); StartNextRetransmitTimer(trans); - trans->retransmits++; + trans->stats.Retransmits++; } else @@ -1057,6 +1055,7 @@ CancelRetryTimeoutHandler(STUN_TRANSACTION_DATA* trans) **/ { StartNextRetransmitTimer(trans); + trans->retransmits++; } else diff --git a/test/stunclient_test.c b/test/stunclient_test.c index cd751c1..04960d6 100644 --- a/test/stunclient_test.c +++ b/test/stunclient_test.c @@ -48,6 +48,7 @@ char logStr[200]; int lastReqCnt; int lastTranspRespCnt; int lastTranspReqCnt; +int lastRTT; CTEST_DATA(data) { @@ -64,6 +65,7 @@ StunStatusCallBack(void* ctx, stunResult = retData->stunResult; lastTranspRespCnt = retData->respTransCnt; lastTranspReqCnt = retData->reqTransCnt; + lastRTT = retData->rtt; /* printf("Got STUN status callback\n");// (Result (%i)\n", * retData->stunResult); */ } @@ -445,6 +447,8 @@ CTEST(stunclient, BindReq_TranportCnt) SimBindSuccessResp(runningAsIPv6, true); ASSERT_TRUE(stunResult == StunResult_BindOk); + + ASSERT_TRUE(lastRTT > 2); ASSERT_TRUE(lastTranspReqCnt == 3); ASSERT_TRUE(lastTranspRespCnt == 2); StunClient_free(stunInstance); From 9d474aff9f817119da1cb4c14202300fd13b9724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Tue, 26 Apr 2016 14:00:42 +0200 Subject: [PATCH 10/15] Moved transaction id comp to API. (usefull for others as well) --- include/stunlib.h | 5 +++++ src/stunclient.c | 16 +++------------- src/stunlib.c | 7 +++++++ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/include/stunlib.h b/include/stunlib.h index 64690b2..a9a125a 100644 --- a/include/stunlib.h +++ b/include/stunlib.h @@ -587,6 +587,11 @@ typedef void (* STUN_ERR_FUNC)(const char* fmt, ***** API Funcs ****** **********************/ +/* transaction id compare */ +bool +stunlib_transIdIsEqual(const StunMsgId* a, + const StunMsgId* b); + /***********************************************/ /************* Decode functions ***************/ /***********************************************/ diff --git a/src/stunclient.c b/src/stunclient.c index 119c067..436d09c 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -133,16 +133,6 @@ StunMsgToInternalStunSig(const StunMessage* msg) } -/* transaction id compare */ -static bool -TransIdIsEqual(const StunMsgId* a, - const StunMsgId* b) -{ - return (memcmp(a, b, STUN_MSG_ID_SIZE) == 0); -} - - - static void StoreStunBindReq(STUN_TRANSACTION_DATA* trans, StunBindReqStruct* pMsgIn) @@ -437,7 +427,7 @@ StunClient_HandleIncResp(STUN_CLIENT_DATA* clientData, { STUN_TRANSACTION_DATA* trans = &clientData->data[i]; if ( trans->inUse && - TransIdIsEqual(&msg->msgHdr.id, &trans->stunBindReq.transactionId) ) + stunlib_transIdIsEqual(&msg->msgHdr.id, &trans->stunBindReq.transactionId) ) { StunRespStruct m; gettimeofday(&trans->stop[trans->retransmits], NULL); @@ -477,7 +467,7 @@ StunClient_HandleICMP(STUN_CLIENT_DATA* clientData, { STUN_TRANSACTION_DATA* trans = &clientData->data[i]; if ( trans->inUse && - TransIdIsEqual(&clientData->traceResult.currStunMsgId, + stunlib_transIdIsEqual(&clientData->traceResult.currStunMsgId, &trans->stunBindReq.transactionId) ) { StunRespStruct m; @@ -522,7 +512,7 @@ StunClient_cancelBindingTransaction(STUN_CLIENT_DATA* clientData, { STUN_TRANSACTION_DATA* trans = &clientData->data[i]; if ( trans->inUse && - TransIdIsEqual(&transactionId, &trans->stunBindReq.transactionId) ) + stunlib_transIdIsEqual(&transactionId, &trans->stunBindReq.transactionId) ) { StunClientMain(clientData, i, STUN_SIGNAL_Cancel, NULL); return i; diff --git a/src/stunlib.c b/src/stunlib.c index 2a46f9d..8feaa0f 100644 --- a/src/stunlib.c +++ b/src/stunlib.c @@ -3448,6 +3448,13 @@ stunlib_addChannelNumber(StunMessage* stunMsg, return true; } +/* transaction id compare */ +bool +stunlib_transIdIsEqual(const StunMsgId* a, + const StunMsgId* b) +{ + return (memcmp(a, b, STUN_MSG_ID_SIZE) == 0); +} /***** * Create our magic id.... From 29c9c4625c8f3fcf52456b48876ffbeed9eaa502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Tue, 26 Apr 2016 15:36:57 +0200 Subject: [PATCH 11/15] use arc4random. --- include/stunlib.h | 5 ++--- src/stunlib.c | 15 ++++----------- src/stuntrace.c | 20 ++++++++------------ src/turnclient.c | 23 +++++------------------ test/stunserver_test.c | 5 ++--- 5 files changed, 21 insertions(+), 47 deletions(-) diff --git a/include/stunlib.h b/include/stunlib.h index a9a125a..8689329 100644 --- a/include/stunlib.h +++ b/include/stunlib.h @@ -863,9 +863,8 @@ stunlib_printBuffer(FILE* stream, void -stunlib_createId(StunMsgId* pId, - long randval, - unsigned char retries); +stunlib_createId(StunMsgId* pId); + bool stunlib_addRealm(StunMessage* stunMsg, const char* realm, diff --git a/src/stunlib.c b/src/stunlib.c index 8feaa0f..9ddd6ee 100644 --- a/src/stunlib.c +++ b/src/stunlib.c @@ -1000,7 +1000,7 @@ stunlib_EncodeIndication(uint8_t msgType, int length = 0; memset( &stunMsg, 0, sizeof(StunMessage) ); - stunlib_createId(&stunMsg.msgHdr.id, rand(), 0); + stunlib_createId(&stunMsg.msgHdr.id); if (dstAddr->sa_family == AF_INET) { @@ -3460,16 +3460,9 @@ stunlib_transIdIsEqual(const StunMsgId* a, * Create our magic id.... *********/ void -stunlib_createId(StunMsgId* pId, - long randval, - unsigned char retries) -{ - char xorval[16] = "cafeogslikt"; - uint8_t* pIdBuf = pId->octet; - write_32(&pIdBuf, randval); - write_16(&pIdBuf, retries); - write_16(&pIdBuf, 0); - write_32_xor(&pIdBuf, randval, (uint8_t*)xorval); +stunlib_createId(StunMsgId* pId) +{ + arc4random_buf(pId, STUN_MSG_ID_SIZE); } diff --git a/src/stuntrace.c b/src/stuntrace.c index 36fd4ae..33b25f9 100644 --- a/src/stuntrace.c +++ b/src/stuntrace.c @@ -124,9 +124,8 @@ handleStunNoAnswer(struct hiutResult* result) result->remoteAlive = false; result->currentTTL = 1; - stunlib_createId(&result->currStunMsgId, - rand(), result->currentTTL); - + stunlib_createId(&result->currStunMsgId); + StunClient_startSTUNTrace( (STUN_CLIENT_DATA*)result->stunCtx, result, (struct sockaddr*)&result->remoteAddr, @@ -174,8 +173,8 @@ handleStunNoAnswer(struct hiutResult* result) { result->currentTTL++; } - stunlib_createId(&result->currStunMsgId, - rand(), result->currentTTL); + stunlib_createId(&result->currStunMsgId); + StunClient_startSTUNTrace( (STUN_CLIENT_DATA*)result->stunCtx, result, (struct sockaddr*)&result->remoteAddr, @@ -207,8 +206,7 @@ handleStunRespIcmp(struct hiutResult* result, result->remoteAlive = true; result->currentTTL = 1; - stunlib_createId(&result->currStunMsgId, - rand(), result->currentTTL); + stunlib_createId(&result->currStunMsgId); StunClient_startSTUNTrace( (STUN_CLIENT_DATA*)result->stunCtx, result, @@ -245,8 +243,7 @@ handleStunRespIcmp(struct hiutResult* result, false, false); - stunlib_createId(&result->currStunMsgId, - rand(), result->currentTTL); + stunlib_createId(&result->currStunMsgId); StunClient_startSTUNTrace( (STUN_CLIENT_DATA*)result->stunCtx, result, @@ -312,8 +309,7 @@ handleStunRespSucsessfull(struct hiutResult* result, result->remoteAlive = true; result->currentTTL = 1; - stunlib_createId(&result->currStunMsgId, - rand(), result->currentTTL); + stunlib_createId(&result->currStunMsgId); StunClient_startSTUNTrace( (STUN_CLIENT_DATA*)result->stunCtx, result, @@ -415,7 +411,7 @@ StunTrace_startTrace(STUN_CLIENT_DATA* clientData, result->currentTTL = MAX_TTL; result->userCtx = userCtx; - stunlib_createId(&result->currStunMsgId, rand(), 1); + stunlib_createId(&result->currStunMsgId); result->stunCtx = clientData; /* Fill inn the hiut struct so we get something back in the CB */ /* TODO: Fix the struct so we do not store information twice!! */ diff --git a/src/turnclient.c b/src/turnclient.c index 668ca25..88d2805 100644 --- a/src/turnclient.c +++ b/src/turnclient.c @@ -219,13 +219,6 @@ TurnTransactionIdString(char* dst, } } -static int -TurnRand(void) -{ - return rand(); -} - - static int getAddrFamily(const TURN_INSTANCE_DATA* pInst) { @@ -1271,11 +1264,9 @@ static void BuildInitialAllocateReq(TURN_INSTANCE_DATA* pInst, StunMessage* pReq) { - unsigned long rndval = TurnRand(); - memset( pReq, 0, sizeof(StunMessage) ); pReq->msgHdr.msgType = STUN_MSG_AllocateRequestMsg; - stunlib_createId(&pReq->msgHdr.id, rndval, 0); + stunlib_createId(&pReq->msgHdr.id); stunlib_addSoftware(pReq, pInst->softwareVersionStr, STUN_DFLT_PAD); stunlib_addRequestedTransport(pReq, STUN_REQ_TRANSPORT_UDP); @@ -1316,11 +1307,9 @@ static void BuildNewAllocateReq(TURN_INSTANCE_DATA* pInst, StunMessage* pReq) { - unsigned long rndval = TurnRand(); - memset( pReq, 0, sizeof(StunMessage) ); pReq->msgHdr.msgType = STUN_MSG_AllocateRequestMsg; - stunlib_createId(&pReq->msgHdr.id, rndval, 1); + stunlib_createId(&pReq->msgHdr.id); stunlib_addRealm(pReq, pInst->userCredentials.realm, STUN_DFLT_PAD); stunlib_addUserName(pReq, pInst->userCredentials.stunUserName, STUN_DFLT_PAD); stunlib_addNonce(pReq, pInst->userCredentials.nonce, STUN_DFLT_PAD); @@ -1363,10 +1352,8 @@ BuildRefreshAllocateReq(TURN_INSTANCE_DATA* pInst, StunMessage* pReq, uint32_t lifetimeSec) { - unsigned long rndval = TurnRand(); - memset( pReq, 0, sizeof(StunMessage) ); - stunlib_createId(&pReq->msgHdr.id, rndval, 1); + stunlib_createId(&pReq->msgHdr.id); pReq->hasLifetime = true; pReq->lifetime.value = lifetimeSec; @@ -1396,7 +1383,7 @@ BuildChannelBindReq(TURN_INSTANCE_DATA* pInst, memset( pReq, 0, sizeof(StunMessage) ); pReq->msgHdr.msgType = STUN_MSG_ChannelBindRequestMsg; - stunlib_createId(&pReq->msgHdr.id, TurnRand(), 0); + stunlib_createId(&pReq->msgHdr.id); if (peerAddr->sa_family == AF_INET) { @@ -1444,7 +1431,7 @@ BuildCreatePermReq(TURN_INSTANCE_DATA* pInst, memset( pReq, 0, sizeof(StunMessage) ); pReq->msgHdr.msgType = STUN_MSG_CreatePermissionRequestMsg; - stunlib_createId(&pReq->msgHdr.id, TurnRand(), 0); + stunlib_createId(&pReq->msgHdr.id); /* peer address(es) */ for (i = 0; i < pInst->createPermInfo.numberOfPeers; i++) diff --git a/test/stunserver_test.c b/test/stunserver_test.c index 3a25e05..abfa635 100644 --- a/test/stunserver_test.c +++ b/test/stunserver_test.c @@ -66,9 +66,8 @@ CTEST(stunserver, Encode_decode) StunMsgId stunId; uint8_t stunBuff[STUN_MAX_PACKET_SIZE]; - stunlib_createId(&stunId, - 232323, - 3); + stunlib_createId(&stunId); + sockaddr_initFromString( (struct sockaddr*)&mappedAddr, "193.200.93.152:3478" ); CreateConnectivityBindingResp(&stunMsg, From 9bd25bc2ea6c832a00a1e9f02a0a57bc75f5c3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Tue, 26 Apr 2016 20:40:40 +0200 Subject: [PATCH 12/15] Adding libbsd-dev --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 61f9bdd..10bb8b1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,7 @@ addons: packages: - cmake - cmake-data + - libbsd-dev before_install: - export PATH=$HOME/.local/bin:$PATH - pip install cpp-coveralls --user `whoami` From 5bddbe5f49396d67f91fa86435ddd1dea2f4be2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Tue, 26 Apr 2016 12:22:50 -0700 Subject: [PATCH 13/15] Compiling on linux with arc4random --- src/CMakeLists.txt | 4 ++++ src/stunlib.c | 1 + 2 files changed, 5 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8ca317f..a6217f6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -37,6 +37,10 @@ if( OPENSSL_FOUND ) list(APPEND ADDITIONAL_LIBS ${OPENSSL_LIBRARIES}) endif( OPENSSL_FOUND ) +# Todo fix propper library discovery. +if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + list(APPEND ADDITIONAL_LIBS "bsd") +endif() target_link_libraries ( stunlib PRIVATE sockaddrutil ${ADDITIONAL_LIBS}) diff --git a/src/stunlib.c b/src/stunlib.c index 9ddd6ee..3dceaf8 100644 --- a/src/stunlib.c +++ b/src/stunlib.c @@ -10,6 +10,7 @@ # define SHA1 CC_SHA1 #else +#include #include #include #include From 25db2c84bc2bccc185ea139b395128b52c1fc538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Wed, 27 Apr 2016 13:56:49 +0200 Subject: [PATCH 14/15] Fix yet another RTT calculation bug. --- src/stunclient.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stunclient.c b/src/stunclient.c index 436d09c..b35d0d6 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -883,7 +883,7 @@ RetransmitLastReq(STUN_TRANSACTION_DATA* trans, NULL); } - gettimeofday(&trans->start[trans->retransmits +1], NULL); + gettimeofday(&trans->start[trans->retransmits], NULL); trans->stunBindReq.sendFunc(trans->client->userCtx, trans->stunBindReq.sockhandle, stunReqMsgBuf, From 1665ba99831d72124b0db73783e4a87a7232bc4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Wed, 27 Apr 2016 13:58:24 +0200 Subject: [PATCH 15/15] leanup after stupid atom bautifier changed bahavior. Now running uncrustify from cmd line. --- include/stun_intern.h | 2 +- include/stunlib.h | 7 ++-- include/stunserver.h | 58 ++++++++++++++++----------------- include/stuntrace.h | 2 +- include/turn_intern.h | 28 ++++++++-------- include/turnclient.h | 76 ++++++++++++++++++++++--------------------- src/stunclient.c | 29 +++++++++-------- src/stunlib.c | 8 +++-- src/stunserver.c | 7 ++-- src/stuntrace.c | 5 +-- src/turnclient.c | 54 +++++++++++++++--------------- 11 files changed, 144 insertions(+), 132 deletions(-) diff --git a/include/stun_intern.h b/include/stun_intern.h index d5690da..16cd89a 100644 --- a/include/stun_intern.h +++ b/include/stun_intern.h @@ -124,7 +124,7 @@ typedef struct /* icmp */ uint32_t ICMPtype; - //uint32_t ttl; + /* uint32_t ttl; */ /* DISCUSS */ bool hasDiscuss; diff --git a/include/stunlib.h b/include/stunlib.h index 8689329..cc3a1c5 100644 --- a/include/stunlib.h +++ b/include/stunlib.h @@ -191,6 +191,7 @@ extern "C" { * * * + * *msec **/ #define STUNCLIENT_DFLT_TICK_TIMER_MS 50 @@ -553,7 +554,7 @@ typedef struct bool hasCiscoNetFeedResp; StunAtrCiscoNetworkFeedback ciscoNetFeedResp; - bool hasTransCount; + bool hasTransCount; StunAtrTransCount transCount; /* No value, only flaged */ @@ -590,7 +591,7 @@ typedef void (* STUN_ERR_FUNC)(const char* fmt, /* transaction id compare */ bool stunlib_transIdIsEqual(const StunMsgId* a, - const StunMsgId* b); + const StunMsgId* b); /***********************************************/ /************* Decode functions ***************/ @@ -863,7 +864,7 @@ stunlib_printBuffer(FILE* stream, void -stunlib_createId(StunMsgId* pId); +stunlib_createId(StunMsgId* pId); bool stunlib_addRealm(StunMessage* stunMsg, diff --git a/include/stunserver.h b/include/stunserver.h index 819a6b7..2494e04 100644 --- a/include/stunserver.h +++ b/include/stunserver.h @@ -16,36 +16,36 @@ extern "C" { #include #endif -bool -CreateConnectivityBindingResp(StunMessage* stunMsg, - StunMsgId transactionId, - const struct sockaddr* mappedSockAddr, - uint8_t reqTrnspCnt, - uint8_t respTrnspCnt, - uint16_t response, - uint32_t responseCode, - DiscussData* discussData); + bool + CreateConnectivityBindingResp(StunMessage * stunMsg, + StunMsgId transactionId, + const struct sockaddr* mappedSockAddr, + uint8_t reqTrnspCnt, + uint8_t respTrnspCnt, + uint16_t response, + uint32_t responseCode, + DiscussData * discussData); /********* Server handling: send STUN BIND RESP *************/ -bool -StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA* clientData, - int32_t globalSocketId, - StunMsgId transactionId, - const char* password, - const struct sockaddr* mappedAddr, - const struct sockaddr* dstAddr, - uint8_t reqTrnspCnt, - uint8_t respTrnspCnt, - void* userData, - STUN_SENDFUNC sendFunc, - int proto, - bool useRelay, - uint32_t responseCode, - DiscussData* discussData); + bool + StunServer_SendConnectivityBindingResp(STUN_CLIENT_DATA * clientData, + int32_t globalSocketId, + StunMsgId transactionId, + const char* password, + const struct sockaddr* mappedAddr, + const struct sockaddr* dstAddr, + uint8_t reqTrnspCnt, + uint8_t respTrnspCnt, + void* userData, + STUN_SENDFUNC sendFunc, + int proto, + bool useRelay, + uint32_t responseCode, + DiscussData * discussData); /********** Server handling: incoming STUN BIND REQ **********/ -bool -StunServer_HandleStunIncomingBindReqMsg(STUN_CLIENT_DATA* clientData, - STUN_INCOMING_REQ_DATA* pReq, - const StunMessage* stunMsg, - bool fromRelay); + bool + StunServer_HandleStunIncomingBindReqMsg(STUN_CLIENT_DATA * clientData, + STUN_INCOMING_REQ_DATA * pReq, + const StunMessage * stunMsg, + bool fromRelay); #endif diff --git a/include/stuntrace.h b/include/stuntrace.h index ddeb880..0be12b5 100644 --- a/include/stuntrace.h +++ b/include/stuntrace.h @@ -62,7 +62,7 @@ struct hiutResult { /* Initial Length of first STUN packet (TTL=1) */ - //uint32_t stunLen; + /* uint32_t stunLen; */ struct hiutPathElement pathElement[MAX_TTL + 1]; bool remoteAlive; /* struct hiutTTLinfo ttlInfo[MAX_TTL]; */ diff --git a/include/turn_intern.h b/include/turn_intern.h index 57e811a..36aa9af 100644 --- a/include/turn_intern.h +++ b/include/turn_intern.h @@ -14,7 +14,7 @@ extern "C" { /* make sure both the following timers are < 5 minutes */ #define TURN_REFRESH_CHANNEL_TIMER_SEC (3 * 60) /* 3 min (spec. is 10 min) - **/ + **/ #define TURN_REFRESH_PERMISSION_TIMER_SEC (4 * 60) /* 4 min (spec. is 5 min) * */ @@ -103,19 +103,21 @@ struct TURN_INSTANCE_DATA unsigned long id; TURN_INFO_FUNC infoFunc; - TURN_STATE state; - bool inUse; - TurnAllocateReqStuct turnAllocateReq; - StunMsgId StunReqTransId; /* transaction - *id of request + TURN_STATE state; + bool inUse; + TurnAllocateReqStuct turnAllocateReq; + StunMsgId StunReqTransId; /* transaction + * id of + *request **/ - StunMsgId PrevRespTransId; /* transaction - *id of last - *received */ - uint8_t stunReqMsgBuf[STUN_MAX_PACKET_SIZE]; /* encoded STUN - *request */ - int stunReqMsgBufLen; /* of encoded - *STUN request + StunMsgId PrevRespTransId; /* transaction + * id of last + * received */ + uint8_t stunReqMsgBuf[STUN_MAX_PACKET_SIZE]; /* encoded STUN + * request + * */ + int stunReqMsgBufLen; /* of encoded + * STUN request **/ bool pendingChannelBind; STUN_USER_CREDENTIALS userCredentials; diff --git a/include/turnclient.h b/include/turnclient.h index 1a15f08..b8f82a1 100644 --- a/include/turnclient.h +++ b/include/turnclient.h @@ -5,7 +5,7 @@ #define TURNCLIENT_H -//#include +/* #include */ #include #include "stunlib.h" /* stun enc/dec and msg formats*/ @@ -18,7 +18,7 @@ extern "C" { enum { TURN_DFLT_PORT = 3478, TURN_MAX_PERMISSION_PEERS = 12 /* max. number of Peers in a - *createPermissionRequest */ + * createPermissionRequest */ }; @@ -34,34 +34,34 @@ typedef enum TurnResult_AllocOk, /* Turn allocation was successful */ TurnResult_AllocFail, /* Turn Allocation failed */ TurnResult_AllocFailNoAnswer, /* Turn Allocation failed - no - *contact with turn server */ + * contact with turn server */ TurnResult_AllocUnauthorised, /* passwd/username is incorrect */ TurnResult_CreatePermissionOk, /* successfull CreatePermission */ TurnResult_CreatePermissionFail, /* Failed CreatePermission - no - *contact with turn server */ + * contact with turn server */ TurnResult_CreatePermissionNoAnswer, /* CreatePermission failed */ TurnResult_CreatePermissionQuotaReached, /* Quouta reached */ TurnResult_PermissionRefreshFail, /* Refresh Permission failed */ TurnResult_ChanBindOk, /* successful Channel Bind */ TurnResult_ChanBindFail, /* Channel Bind failed */ TurnResult_ChanBindFailNoanswer, /* Channel bind failed - no contact - *with turn server */ + * with turn server */ TurnResult_RefreshFail, /* Allocation Refresh failed */ TurnResult_RefreshFailNoAnswer, /* Allocation Refresh failed */ TurnResult_RelayReleaseComplete, /* Relay has been sucessfully - *released */ + * released */ TurnResult_RelayReleaseFailed, /* Relay released failed */ TurnResult_InternalError, TurnResult_MalformedRespWaitAlloc /* server problem occurred when - *waiting for alloc resp */ + * waiting for alloc resp */ } TurnResult_T; /* * Result of successful Turn allocation (TurnResult_AllocOk) has the following - *format. + * format. * srflxAddr - Server Reflexive Address/port. This is source addr/port of - *the AllocateRequest as seen by the turn server + * the AllocateRequest as seen by the turn server * relAddr - Relay Address/Port. As allocated on the turn server. */ typedef struct @@ -75,7 +75,7 @@ typedef struct /* Signalled back to the caller as a parameter in the TURN callback (see TURNCB) - **/ +**/ typedef struct { TurnResult_T turnResult; @@ -114,27 +114,27 @@ TurnStats_T; /* Defines how a user of turn sends data on e.g. socket */ typedef void (* TURN_SEND_FUNC)(const uint8_t* buffer, /* ptr to - *buffer to - *send */ + * buffer to + * send */ size_t bufLen, /* length of - *send - *buffer */ + * send + * buffer */ const struct sockaddr* dstAddr, /* Optional, - *if - *connected - *to socket + * if + * connected + * to socket **/ void* userCtx); /* context - - *e.g. - *socket - *handle */ + * e.g. + * socket + * handle */ /* Signalling back to user e.g. result of AllocateResp, ChanBindResp etc... * userCtx - User provided context, as provided in - *TurnClient_startAllocateTransaction(userCtx,...) + * TurnClient_startAllocateTransaction(userCtx,...) * TurnCbData - User provided turn callback data. Turn writes status here. - *e.g. Alloc ok + reflexive + relay address + * e.g. Alloc ok + reflexive + relay address */ typedef void (* TURN_CB_FUNC)(void* userCtx, TurnCallBackData_T* turnCbData); @@ -149,27 +149,27 @@ typedef void (* TURN_INFO_FUNC)(void* userCtx, * Initiate a Turn Allocate Transaction * instance - instance data * tickMsec - Tells turnclient how often TurnClient_HandleTick() is - *called. + * called. * funcPtr - Will be called by Turn when it outputs management - *info and trace. + * info and trace. * SwVerStr - Software version string to be sent in TURN Requests* * turnServerAddr - Address of TURN server * userName - \0 terminated string. Max - *STUN_MSG_MAX_USERNAME_LENGTH-1 chars. + * STUN_MSG_MAX_USERNAME_LENGTH-1 chars. * password - \0 terminated string. Max - *STUN_MSG_MAX_PASSWORD_LENGTH-1 chars. + * STUN_MSG_MAX_PASSWORD_LENGTH-1 chars. * ai_family - requested address family (AF_INET or AF_INET6) * sendFunc - function used to send STUN packet. - *send(sockhandle,buff, len, turnServerAddr, userCtx) + * send(sockhandle,buff, len, turnServerAddr, userCtx) * turnCbFunc - user provided callback function used by turn to - *signal the result of an allocation or channel bind etc... + * signal the result of an allocation or channel bind etc... * TurnCbData - user provided callback turn data. turn writes to this - *data area. + * data area. * evenPortAndReserve - reserve an even port n and next port n+1 * reservationToken - request a previously reserved port for the allocation * returns - Turn instance/context. Application should store this - *in further calls to TurnClient_StartChannelBindReq(), - *TurnClient_HandleIncResp(). + * in further calls to TurnClient_StartChannelBindReq(), + * TurnClient_HandleIncResp(). */ bool TurnClient_StartAllocateTransaction(TURN_INSTANCE_DATA** instp, @@ -200,15 +200,15 @@ TurnClient_StartChannelBindReq(TURN_INSTANCE_DATA* inst, /* * Create a permission in turn server. i.e. CreatePermission(List of - *RemotePeers). + * RemotePeers). * This will enable the turn server to route DataIndicatins from the Remote - *peers. + * peers. * * instance - instance pointer * noOfPeers - Number of peer addresses in peerTrnspAddrStr string * array * peerTrnspAddrStr - Pointer to array of strings in format "a.b.c.d:port". - *Note - Port is not used in create permission. + * Note - Port is not used in create permission. * */ bool @@ -255,7 +255,7 @@ TurnClient_SendPacket(TURN_INSTANCE_DATA* inst, uint32_t dataLen, uint32_t offset, const struct sockaddr* peerAddr, - bool needChannelDataPadding); + bool needChannelDataPadding); /* * handle received turn packets @@ -280,8 +280,10 @@ TurnClient_HandleIncResp(TURN_INSTANCE_DATA* inst, StunMessage* msg, uint8_t* buf); -bool TurnClient_hasBeenRedirected(TURN_INSTANCE_DATA *pInst); -const struct sockaddr * TurnClient_getRedirectedServerAddr(TURN_INSTANCE_DATA *pInst); +bool +TurnClient_hasBeenRedirected(TURN_INSTANCE_DATA* pInst); +const struct sockaddr* +TurnClient_getRedirectedServerAddr(TURN_INSTANCE_DATA* pInst); diff --git a/src/stunclient.c b/src/stunclient.c index b35d0d6..53bcb10 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -427,7 +427,8 @@ StunClient_HandleIncResp(STUN_CLIENT_DATA* clientData, { STUN_TRANSACTION_DATA* trans = &clientData->data[i]; if ( trans->inUse && - stunlib_transIdIsEqual(&msg->msgHdr.id, &trans->stunBindReq.transactionId) ) + stunlib_transIdIsEqual(&msg->msgHdr.id, + &trans->stunBindReq.transactionId) ) { StunRespStruct m; gettimeofday(&trans->stop[trans->retransmits], NULL); @@ -468,7 +469,7 @@ StunClient_HandleICMP(STUN_CLIENT_DATA* clientData, STUN_TRANSACTION_DATA* trans = &clientData->data[i]; if ( trans->inUse && stunlib_transIdIsEqual(&clientData->traceResult.currStunMsgId, - &trans->stunBindReq.transactionId) ) + &trans->stunBindReq.transactionId) ) { StunRespStruct m; gettimeofday(&trans->stop[trans->retransmits], NULL); @@ -512,7 +513,8 @@ StunClient_cancelBindingTransaction(STUN_CLIENT_DATA* clientData, { STUN_TRANSACTION_DATA* trans = &clientData->data[i]; if ( trans->inUse && - stunlib_transIdIsEqual(&transactionId, &trans->stunBindReq.transactionId) ) + stunlib_transIdIsEqual(&transactionId, + &trans->stunBindReq.transactionId) ) { StunClientMain(clientData, i, STUN_SIGNAL_Cancel, NULL); return i; @@ -933,13 +935,14 @@ getRTTvalue(STUN_TRANSACTION_DATA* trans) int32_t start, stop = 0; - if ( trans->reqTransCnt > 0 && trans->reqTransCnt < STUNCLIENT_MAX_RETRANSMITS ) + if ( (trans->reqTransCnt > 0) && + (trans->reqTransCnt < STUNCLIENT_MAX_RETRANSMITS) ) { - stop = (trans->stop[trans->reqTransCnt-1].tv_sec * 1000000 + - trans->stop[trans->reqTransCnt-1].tv_usec); + stop = (trans->stop[trans->reqTransCnt - 1].tv_sec * 1000000 + + trans->stop[trans->reqTransCnt - 1].tv_usec); /* Always use the first stored value for start. */ - start = (trans->start[trans->reqTransCnt-1].tv_sec * 1000000 + - trans->start[trans->reqTransCnt-1].tv_usec); + start = (trans->start[trans->reqTransCnt - 1].tv_sec * 1000000 + + trans->start[trans->reqTransCnt - 1].tv_usec); } else { @@ -961,8 +964,8 @@ CallBack(STUN_TRANSACTION_DATA* trans, memset( &res, 0, sizeof (StunCallBackData_T) ); memcpy( &res.msgId, &trans->stunBindReq.transactionId, sizeof(StunMsgId) ); - res.stunResult = stunResult; - res.ttl = trans->stunBindReq.ttl; + res.stunResult = stunResult; + res.ttl = trans->stunBindReq.ttl; res.rtt = getRTTvalue(trans); res.retransmits = trans->retransmits; @@ -992,7 +995,7 @@ CommonRetryTimeoutHandler(STUN_TRANSACTION_DATA* trans, max = STUNCLIENT_MAX_RETRANSMITS; } - if ( (trans->retransmits < (max-1)) + if ( ( trans->retransmits < (max - 1) ) && (stunTimeoutList[trans->retransmits] != 0) ) /* can be 0 terminated if * using fewer * retransmits @@ -1128,8 +1131,8 @@ BindRespCallback(STUN_TRANSACTION_DATA* trans, /* So did we loose a packet, or got an answer to the first response?*/ - res.rtt = getRTTvalue(trans); - res.ttl = trans->stunBindReq.ttl; + res.rtt = getRTTvalue(trans); + res.ttl = trans->stunBindReq.ttl; res.retransmits = trans->retransmits; res.respTransCnt = trans->respTransCnt; diff --git a/src/stunlib.c b/src/stunlib.c index 3dceaf8..2b6454a 100644 --- a/src/stunlib.c +++ b/src/stunlib.c @@ -2554,7 +2554,9 @@ stunlib_DecodeMessage(const uint8_t* buf, { if (stream) { - fprintf(stream, " Message length or attribute length error(%i).\n", restlen); + fprintf(stream, + " Message length or attribute length error(%i).\n", + restlen); } return false; } @@ -3452,7 +3454,7 @@ stunlib_addChannelNumber(StunMessage* stunMsg, /* transaction id compare */ bool stunlib_transIdIsEqual(const StunMsgId* a, - const StunMsgId* b) + const StunMsgId* b) { return (memcmp(a, b, STUN_MSG_ID_SIZE) == 0); } @@ -3461,7 +3463,7 @@ stunlib_transIdIsEqual(const StunMsgId* a, * Create our magic id.... *********/ void -stunlib_createId(StunMsgId* pId) +stunlib_createId(StunMsgId* pId) { arc4random_buf(pId, STUN_MSG_ID_SIZE); } diff --git a/src/stunserver.c b/src/stunserver.c index 96c48b0..f92c3f6 100644 --- a/src/stunserver.c +++ b/src/stunserver.c @@ -26,10 +26,11 @@ CreateConnectivityBindingResp(StunMessage* stunMsg, memset(stunMsg, 0, sizeof *stunMsg); stunMsg->msgHdr.msgType = response; - if(reqTrnspCnt != 0 ){ - stunMsg->hasTransCount = true; + if (reqTrnspCnt != 0) + { + stunMsg->hasTransCount = true; stunMsg->transCount.respCnt = respTrnspCnt; - stunMsg->transCount.reqCnt = reqTrnspCnt; + stunMsg->transCount.reqCnt = reqTrnspCnt; } if (mappedSockAddr->sa_family == AF_INET) diff --git a/src/stuntrace.c b/src/stuntrace.c index 33b25f9..45ccadc 100644 --- a/src/stuntrace.c +++ b/src/stuntrace.c @@ -125,7 +125,7 @@ handleStunNoAnswer(struct hiutResult* result) result->currentTTL = 1; stunlib_createId(&result->currStunMsgId); - + StunClient_startSTUNTrace( (STUN_CLIENT_DATA*)result->stunCtx, result, (struct sockaddr*)&result->remoteAddr, @@ -355,7 +355,8 @@ StunStatusCallBack(void* userCtx, { struct hiutResult* result = (struct hiutResult*)userCtx; - if(stunCbData->ttl <= MAX_TTL){ + if (stunCbData->ttl <= MAX_TTL) + { result->pathElement[stunCbData->ttl].gotAnswer = true; } diff --git a/src/turnclient.c b/src/turnclient.c index 88d2805..f2f989f 100644 --- a/src/turnclient.c +++ b/src/turnclient.c @@ -392,7 +392,7 @@ ChannelBindReqParamsOk(TURN_INSTANCE_DATA* pInst, if ( !IsValidBindChannelNumber(channelNumber) ) /* channel number ignored if * creating a permission */ { - TurnPrint(pInst, + TurnPrint(pInst, TurnInfoCategory_Error, " ChannelBindReq - illegal channel number %0X ", pInst->id, @@ -402,7 +402,7 @@ ChannelBindReqParamsOk(TURN_INSTANCE_DATA* pInst, if ( !sockaddr_isSet(peerTrnspAddr) ) { - TurnPrint(pInst, + TurnPrint(pInst, TurnInfoCategory_Error, " ChannelBindReq - illegal peerTRansport Address ", pInst->id); @@ -636,25 +636,25 @@ TurnClientGetStats(const TURN_INSTANCE_DATA* pInst, { if (pInst->state >= TURN_STATE_Allocated) { - sockaddr_copy( (struct sockaddr*)&Stats->AllocResp.srflxAddr, + sockaddr_copy( (struct sockaddr*)&Stats->AllocResp.srflxAddr, (struct sockaddr*)&pInst->srflxAddr ); - sockaddr_copy( (struct sockaddr*)&Stats->AllocResp.relAddrIPv4, + sockaddr_copy( (struct sockaddr*)&Stats->AllocResp.relAddrIPv4, (struct sockaddr*)&pInst->relAddr_IPv4 ); - sockaddr_copy( (struct sockaddr*)&Stats->AllocResp.relAddrIPv6, + sockaddr_copy( (struct sockaddr*)&Stats->AllocResp.relAddrIPv6, (struct sockaddr*)&pInst->relAddr_IPv6 ); Stats->channelBound = pInst->channelBound; if (pInst->channelBound) { Stats->channelNumber = pInst->channelBindInfo.channelNumber; - sockaddr_copy( (struct sockaddr*)&Stats->BoundPeerTrnspAddr, + sockaddr_copy( (struct sockaddr*)&Stats->BoundPeerTrnspAddr, (struct sockaddr*)&pInst->channelBindInfo.peerTrnspAddr ); /* a bound channel also creates a permission, so show this */ Stats->permissionsInstalled = 1; Stats->numberOfPeers = 1; - sockaddr_copy( (struct sockaddr*)&Stats->PermPeerTrnspAddr[0], + sockaddr_copy( (struct sockaddr*)&Stats->PermPeerTrnspAddr[0], (struct sockaddr*)&pInst->channelBindInfo.peerTrnspAddr ); } else @@ -779,7 +779,7 @@ TurnResultToStr(TurnResult_T res) static void StopAllTimers(TURN_INSTANCE_DATA* pInst) { - TurnPrint(pInst, + TurnPrint(pInst, TurnInfoCategory_Trace, " StopAllTimers", pInst->id); @@ -877,7 +877,7 @@ StartTimer(TURN_INSTANCE_DATA* pInst, TURN_SIGNAL sig, uint32_t durationMsec) { - TurnPrint(pInst, + TurnPrint(pInst, TurnInfoCategory_Trace, " StartTimer(%s, %dms)", pInst->id, @@ -917,7 +917,7 @@ static void StopTimer(TURN_INSTANCE_DATA* pInst, TURN_SIGNAL sig) { - TurnPrint( pInst, TurnInfoCategory_Trace, " StopTimer(%s)", + TurnPrint( pInst, TurnInfoCategory_Trace, " StopTimer(%s)", pInst->id, TurnsigToStr(sig) ); switch (sig) @@ -968,13 +968,13 @@ StoreAllocateReq(TURN_INSTANCE_DATA* pInst, TurnAllocateReqStuct* pMsgIn) { /* copy whole msg */ - memcpy( &pInst->turnAllocateReq, pMsgIn, + memcpy( &pInst->turnAllocateReq, pMsgIn, sizeof(TurnAllocateReqStuct) ); /* copy crendentials to seperate area */ - memcpy( pInst->userCredentials.stunUserName, pMsgIn->username, + memcpy( pInst->userCredentials.stunUserName, pMsgIn->username, sizeof(pInst->userCredentials.stunUserName) ); - memcpy( pInst->userCredentials.stunPassword, pMsgIn->password, + memcpy( pInst->userCredentials.stunPassword, pMsgIn->password, sizeof(pInst->userCredentials.stunPassword) ); } @@ -982,7 +982,7 @@ static void StoreChannelBindReq(TURN_INSTANCE_DATA* pInst, TurnChannelBindInfo_T* pMsgIn) { - memcpy( &pInst->channelBindInfo, pMsgIn, sizeof(pInst->channelBindInfo) ); + memcpy( &pInst->channelBindInfo, pMsgIn, sizeof(pInst->channelBindInfo) ); } static bool @@ -995,7 +995,7 @@ static void StoreCreatePermReq(TURN_INSTANCE_DATA* pInst, TurnCreatePermissionInfo_T* pMsgIn) { - memcpy( &pInst->createPermInfo, pMsgIn, sizeof(pInst->createPermInfo) ); + memcpy( &pInst->createPermInfo, pMsgIn, sizeof(pInst->createPermInfo) ); } static bool @@ -1205,7 +1205,7 @@ StoreRelayAddressStd(TURN_INSTANCE_DATA* pInst, } else { - TurnPrint(pInst, + TurnPrint(pInst, TurnInfoCategory_Error, " Missing Xor RelayAddress AllocResp", pInst->id); @@ -1233,7 +1233,7 @@ StoreLifetime(TURN_INSTANCE_DATA* pInst, } else { - TurnPrint(pInst, + TurnPrint(pInst, TurnInfoCategory_Error, " Missing Lifetime in AllocResp", pInst->id); @@ -1406,7 +1406,7 @@ BuildChannelBindReq(TURN_INSTANCE_DATA* pInst, } - memcpy( &pReq->xorPeerAddress[0], &peerTrnspAddr, sizeof(StunIPAddress) ); + memcpy( &pReq->xorPeerAddress[0], &peerTrnspAddr, sizeof(StunIPAddress) ); pReq->xorPeerAddrEntries = 1; /* Channel No */ @@ -1459,7 +1459,7 @@ BuildCreatePermReq(TURN_INSTANCE_DATA* pInst, } - memcpy( &pReq->xorPeerAddress[i], &peerTrnspAddr, sizeof(StunIPAddress) ); + memcpy( &pReq->xorPeerAddress[i], &peerTrnspAddr, sizeof(StunIPAddress) ); pReq->xorPeerAddrEntries++; } @@ -1535,7 +1535,7 @@ GetServerAddrFromAltServer(TURN_INSTANCE_DATA* pInst, } else { - TurnPrint(pInst, + TurnPrint(pInst, TurnInfoCategory_Error, " Missing Alternative Server in AllocRespErr", pInst->id); @@ -1577,7 +1577,7 @@ CheckRefreshRespError(TURN_INSTANCE_DATA* pInst, } else { - TurnPrint(pInst, + TurnPrint(pInst, TurnInfoCategory_Error, " No error code in RefreshRespError", pInst->id); @@ -1806,7 +1806,7 @@ RetransmitLastReq(TURN_INSTANCE_DATA* pInst) static void StartAllocRefreshTimer(TURN_INSTANCE_DATA* pInst) { - StartTimer(pInst, + StartTimer(pInst, TURN_SIGNAL_TimerRefreshAlloc, (pInst->lifetime / 2) * 1000); } @@ -1814,7 +1814,7 @@ StartAllocRefreshTimer(TURN_INSTANCE_DATA* pInst) static void StartChannelBindRefreshTimer(TURN_INSTANCE_DATA* pInst) { - StartTimer(pInst, + StartTimer(pInst, TURN_SIGNAL_TimerRefreshChannel, TURN_REFRESH_CHANNEL_TIMER_SEC * 1000); } @@ -1822,7 +1822,7 @@ StartChannelBindRefreshTimer(TURN_INSTANCE_DATA* pInst) static void StartCreatePermissionRefreshTimer(TURN_INSTANCE_DATA* pInst) { - StartTimer(pInst, + StartTimer(pInst, TURN_SIGNAL_TimerRefreshPermission, TURN_REFRESH_PERMISSION_TIMER_SEC * 1000); } @@ -1844,7 +1844,7 @@ static void StartFirstRetransmitTimer(TURN_INSTANCE_DATA* pInst) { pInst->retransmits = 0; - StartTimer(pInst, TURN_SIGNAL_TimerRetransmit, + StartTimer(pInst, TURN_SIGNAL_TimerRetransmit, stunTimeoutList[pInst->retransmits]); } @@ -1852,7 +1852,7 @@ StartFirstRetransmitTimer(TURN_INSTANCE_DATA* pInst) static void StartNextRetransmitTimer(TURN_INSTANCE_DATA* pInst) { - StartTimer(pInst, TURN_SIGNAL_TimerRetransmit, + StartTimer(pInst, TURN_SIGNAL_TimerRetransmit, stunTimeoutList[pInst->retransmits]); } @@ -1946,7 +1946,7 @@ TurnClientFsm(TURN_INSTANCE_DATA* pInst, } else if (sig == TURN_SIGNAL_DeAllocate) { - CallBack(pInst, TurnResult_RelayReleaseFailed); + CallBack(pInst, TurnResult_RelayReleaseFailed); } else {