Skip to content

Commit

Permalink
Merge pull request #11 from palerikm/master
Browse files Browse the repository at this point in the history
Fixed DecodeTTL attribute. Removed TTLString padding.
  • Loading branch information
palerikm committed Feb 5, 2016
2 parents 3090763 + dd982a3 commit b59deb6
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 95 deletions.
9 changes: 0 additions & 9 deletions include/stunlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ extern "C" {

/*STUNTrace Attributes (Experimental) */
#define STUN_ATTR_TTL 0x8055
#define STUN_ATTR_TTLString 0x8056

/* STUN attributes (TURN extensions) */
#define STUN_ATTR_ChannelNumber 0x000c
Expand Down Expand Up @@ -529,9 +528,6 @@ typedef struct
bool hasTTL;
StunAtrTTL ttl;

bool hasTTLString;
StunAtrString TTLString;

/*After Integrity attr*/
bool hasNetworkStatus;
StunAtrNetworkStatus networkStatus;
Expand Down Expand Up @@ -890,11 +886,6 @@ bool
stunlib_checkFingerPrint(const uint8_t* buf,
uint32_t fpOffset);

bool
stunlib_addTTLString(StunMessage* stunMsg,
const char* TTLString,
char padChar);


/* Concat username+realm+passwd into string "<username>:<realm>:<password>"
* then run the MD5 alg.
Expand Down
24 changes: 12 additions & 12 deletions src/stunclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,8 @@ BuildStunBindReq(STUN_TRANSACTION_DATA* trans,
/* ttl */
if (trans->stunBindReq.ttl > 0)
{
char ttlString[200];
char iTTL[5] = "0000\0";
stunReqMsg->hasTTL = true;
stunReqMsg->ttl.ttl = trans->stunBindReq.ttl;

sprintf(iTTL, "%.4i", trans->stunBindReq.ttl);
ttlString[0] = '\0';
for (int i = 0; i < trans->stunBindReq.ttl; i++)
{
strncat(ttlString,iTTL, 4);
}

stunlib_addTTLString(stunReqMsg, ttlString, 'a');
}


Expand Down Expand Up @@ -1243,8 +1232,19 @@ static void
CancelRetryTimeoutHandler(STUN_TRANSACTION_DATA* trans)
{
STUN_CLIENT_DATA* client = trans->client;
uint32_t max;

if (trans->stunBindReq.stuntrace)
{
max = STUNTRACE_MAX_RETRANSMITS;
}
else
{
max = STUNCLIENT_MAX_RETRANSMITS;
}

if ( (trans->retransmits < STUNCLIENT_MAX_RETRANSMITS)

if ( (trans->retransmits < max)
&& (stunTimeoutList[trans->retransmits] != 0) ) /* can be 0 terminated if
* using fewer
* retransmits
Expand Down
61 changes: 14 additions & 47 deletions src/stunlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ stunDecodeUnknownAtr(StunAtrUnknown* pUnk,
int atrLen)
{
uint32_t padLen = calcPadLen(atrLen, 4);
int i;
int i;
if (*nBufLen < atrLen)
{
return false;
Expand All @@ -1449,8 +1449,8 @@ stunDecodeUnknownAtr(StunAtrUnknown* pUnk,
read_16(pBuf, &pUnk->attrType[i]);
}
pUnk->numAttributes = i;
*nBufLen -= ( atrLen + padLen );
*pBuf += padLen;
*nBufLen -= (atrLen + padLen);
*pBuf += padLen;
if ( i < (atrLen / 2) )
{
*nBufLen -= (atrLen - 2 * i);
Expand Down Expand Up @@ -1577,7 +1577,7 @@ stunDecodeTTL(StunAtrTTL* ttl,
read_8(pBuf, &ttl->pad_8);
read_16(pBuf, &ttl->pad_16);

*nBufLen -= 8;
*nBufLen -= 4;
return true;
}

Expand Down Expand Up @@ -2111,9 +2111,12 @@ stunlib_DecodeMessage(const uint8_t* buf,
}
if ( !stunDecodeAttributeHead(&sAtr, &pCurrPtr, &restlen) )
{
if (stream)
{
printError(stream,
"stunlib_DecodeMessage: Failed to parse Attribute head (%d)\n",
restlen);
"stunlib_DecodeMessage: Failed to parse Attribute head (%d)\n",
restlen);
}
return false;
}
if (stream)
Expand Down Expand Up @@ -2384,17 +2387,6 @@ stunlib_DecodeMessage(const uint8_t* buf,
message->hasTTL = true;
break;

case STUN_ATTR_TTLString:
if ( !stunDecodeStringAtr(&message->TTLString,
&pCurrPtr,
&restlen,
sAtr.length) )
{
return false;
}
message->hasTTLString = true;
break;

case STUN_ATTR_StreamType:
if ( !stunDecodeStreamType(&message->streamType,
&pCurrPtr,
Expand Down Expand Up @@ -3092,18 +3084,6 @@ stunlib_encodeMessage(StunMessage* message,
return 0;
}

if ( message->hasTTLString && !stunEncodeStringAtr(&message->TTLString,
STUN_ATTR_TTLString,
&pCurrPtr,
&restlen) )
{
if (stream)
{
printError(stream, "Invalid TTLString\n");
}
return 0;
}

if ( message->hasNetworkStatusResp &&
!stunEncodeNetworkStatus(&message->networkStatusResp,
&pCurrPtr,
Expand Down Expand Up @@ -3243,7 +3223,10 @@ stunlib_encodeMessage(StunMessage* message,
&pCurrPtr,
&restlen) )
{
printError(stream, "Faild to add CRC Fingerprint\n");
if (stream)
{
printError(stream, "Faild to add CRC Fingerprint\n");
}
}
else
{
Expand All @@ -3253,7 +3236,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");
Expand Down Expand Up @@ -3303,22 +3286,6 @@ stunlib_addUserName(StunMessage* stunMsg,
return true;
}

bool
stunlib_addTTLString(StunMessage* stunMsg,
const char* TTLString,
char padChar)
{
if (strlen(TTLString) > STUN_MSG_MAX_USERNAME_LENGTH)
{
return false;
}

stunMsg->hasTTLString = true;
stunSetString(&stunMsg->TTLString, TTLString, padChar);
return true;
}


bool
stunlib_addRealm(StunMessage* stunMsg,
const char* realm,
Expand Down
17 changes: 9 additions & 8 deletions test/stuntrace_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ CTEST(stuntrace, run_IPv4)
1,
StunTraceCallBack,
sendPacket);

/* First alive probe */
ASSERT_TRUE(len == 224);
ASSERT_TRUE(len != 0);
ASSERT_TRUE(LastTTL == 40);


Expand Down Expand Up @@ -218,7 +219,7 @@ CTEST(stuntrace, run_IPv4_unhandled_ICMP)
StunTraceCallBack,
sendPacket);
/* First alive probe */
ASSERT_TRUE(len == 224);
ASSERT_TRUE(len != 0);
ASSERT_TRUE(LastTTL == 40);


Expand Down Expand Up @@ -285,7 +286,7 @@ CTEST(stuntrace, recurring_IPv4)
2,
StunTraceCallBack,
sendPacket);
ASSERT_TRUE(len == 224);
ASSERT_TRUE(len != 0);
ASSERT_TRUE(LastTTL == 40);

StunClient_HandleICMP(clientData,
Expand Down Expand Up @@ -362,7 +363,7 @@ CTEST(stuntrace, no_answer_IPv4)
1,
StunTraceCallBack,
sendPacket);
ASSERT_TRUE(len == 224);
ASSERT_TRUE(len != 0);
StunClient_HandleICMP(clientData,
(struct sockaddr*)&remoteAddr,
3);
Expand Down Expand Up @@ -440,7 +441,7 @@ CTEST(stuntrace, no_answer_recurring_IPv4)
2,
StunTraceCallBack,
sendPacket);
ASSERT_TRUE(len == 224);
ASSERT_TRUE(len != 0);

StunClient_HandleICMP(clientData,
(struct sockaddr*)&remoteAddr,
Expand Down Expand Up @@ -549,7 +550,7 @@ CTEST(stuntrace, run_IPv4_Stunresp)
StunTraceCallBack,
sendPacket);
/* First alive probe */
ASSERT_TRUE(len == 224);
ASSERT_TRUE(len != 0);
ASSERT_TRUE(LastTTL == 40);
StunMessage m;
memset( &m, 0, sizeof(m) );
Expand Down Expand Up @@ -693,7 +694,7 @@ CTEST(stuntrace, run_IPv4_Stunresp_end)
StunTraceCallBack,
sendPacket);
/* First alive probe */
ASSERT_TRUE(len == 224);
ASSERT_TRUE(len != 0);
ASSERT_TRUE(LastTTL == 40);
StunMessage m;
memset( &m, 0, sizeof(m) );
Expand Down Expand Up @@ -759,7 +760,7 @@ CTEST(stuntrace, run_IPv4_Stunresp_max_ttl)
StunTraceCallBack,
sendPacket);
/* First alive probe */
ASSERT_TRUE(len == 224);
ASSERT_TRUE(len != 0);
ASSERT_TRUE(LastTTL == 40);
StunMessage m;
memset( &m, 0, sizeof(m) );
Expand Down
34 changes: 34 additions & 0 deletions test/testvector_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,3 +1091,37 @@ CTEST(testvector, stun_msg_len)
{
ASSERT_TRUE(stunlib_StunMsgLen(unknwn) == 88);
}

CTEST(testvector, encode_decode_ttl)
{
StunMessage stunMsg;
unsigned char stunBuf[120];

for (int ttl = 1; ttl < 20; ttl++)
{
memset( &stunMsg, 0, sizeof(StunMessage) );
stunMsg.msgHdr.msgType = STUN_MSG_BindRequestMsg;
memcpy(&stunMsg.msgHdr.id.octet,&idOctet,12);

ASSERT_TRUE( stunlib_addUserName(&stunMsg, username, '\x20') );
ASSERT_TRUE( stunlib_addSoftware(&stunMsg, software, '\x20') );
stunMsg.hasTTL = true;
stunMsg.ttl.ttl = ttl;
ASSERT_TRUE( stunlib_encodeMessage(&stunMsg,
stunBuf,
120,
(unsigned char*)password,
strlen(password),
NULL) );

memset(&stunMsg, 0, sizeof stunMsg);

ASSERT_TRUE( stunlib_DecodeMessage(stunBuf,
120,
&stunMsg,
NULL,
NULL) );
ASSERT_TRUE(stunMsg.hasTTL);
ASSERT_TRUE(stunMsg.ttl.ttl == ttl);
}
}
Loading

0 comments on commit b59deb6

Please sign in to comment.