Skip to content
6 changes: 6 additions & 0 deletions include/stuntrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ struct hiutResult {
STUN_TRACECB traceCb;
};

bool
isDstUnreachable(const int32_t ICMPtype,
const u_int16_t addrFamily);

bool
isTimeExceeded(const int32_t ICMPtype,
const u_int16_t addrFamily);

int
StunTrace_startTrace(STUN_CLIENT_DATA* clientData,
Expand Down
38 changes: 13 additions & 25 deletions src/stunclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,7 @@ StunClient_startSTUNTrace(STUN_CLIENT_DATA* clientData,
StunMessage stunMsg;
uint8_t stunBuff[STUN_MAX_PACKET_SIZE];
uint32_t len;
if (clientData == NULL)
{
StunPrint(clientData->logUserData,
clientData->Log_cb,
StunInfoCategory_Error,
"<STUNCLIENT> startStuntrace() failed, Not initialised or no memory");
return 0;
}

memset( &m, 0, sizeof(m) );
m.userCtx = userCtx;
sockaddr_copy( (struct sockaddr*)&m.serverAddr, serverAddr );
Expand Down Expand Up @@ -461,7 +454,6 @@ StunClient_HandleIncResp(STUN_CLIENT_DATA* clientData,
return;
}
}

StunPrint(clientData->logUserData,
clientData->Log_cb,
StunInfoCategory_Trace,
Expand All @@ -478,14 +470,15 @@ StunClient_HandleICMP(STUN_CLIENT_DATA* clientData,
{
return;
}
/* Todo: Test if this is fr me.. */
/* Todo: Test if this is for me.. */
StunPrint(clientData->logUserData,
clientData->Log_cb,
StunInfoCategory_Trace,
"<STUNTRACE> StunClient_HandleICMP: Got ICMP type: %i\n ", ICMPtype);
clientData->Log_cb,
StunInfoCategory_Trace,
"<STUNTRACE> StunClient_HandleICMP: Got ICMP type: %i\n ",
ICMPtype);

if ( ( (ICMPtype == 11 || ICMPtype == 3) && (srcAddr->sa_family == AF_INET) ) ||
( (ICMPtype == 3 || ICMPtype == 1) && (srcAddr->sa_family == AF_INET6) ) )
if ( isTimeExceeded(ICMPtype, srcAddr->sa_family) ||
isDstUnreachable(ICMPtype,srcAddr->sa_family) )
{
for (int i = 0; i < MAX_STUN_TRANSACTIONS; i++)
{
Expand All @@ -505,15 +498,14 @@ StunClient_HandleICMP(STUN_CLIENT_DATA* clientData,

}
}
}
else
{
StunPrint(clientData->logUserData,
clientData->Log_cb,
StunInfoCategory_Trace,
"<STUNCLIENT> no instance with transId, discarding, ICMP message\n ");
}else{
StunPrint(clientData->logUserData,
clientData->Log_cb,
StunInfoCategory_Trace,
"<STUNTRACE> StunClient_HandleICMP: Ignoring ICMP Type, nothing to do\n ", ICMPtype);
"<STUNTRACE> StunClient_HandleICMP: Ignoring ICMP Type, nothing to do\n ",
ICMPtype);
}
}

Expand All @@ -530,10 +522,6 @@ StunClient_cancelBindingTransaction(STUN_CLIENT_DATA* clientData,
{
if (clientData == NULL)
{
StunPrint(clientData->logUserData,
clientData->Log_cb,
StunInfoCategory_Error,
"<STUNCLIENT> cancelBindingTransaction() failed, Not initialised or no memory");
return STUNCLIENT_CTX_UNKNOWN;
}

Expand Down
56 changes: 3 additions & 53 deletions src/stunlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,7 @@ stunDecodeUnknownAtr(StunAtrUnknown* pUnk,
int* nBufLen,
int atrLen)
{
uint32_t padLen = calcPadLen(atrLen, 4);
int i;
if (*nBufLen < atrLen)
{
Expand All @@ -1448,7 +1449,8 @@ stunDecodeUnknownAtr(StunAtrUnknown* pUnk,
read_16(pBuf, &pUnk->attrType[i]);
}
pUnk->numAttributes = i;
*nBufLen -= atrLen;
*nBufLen -= ( atrLen + padLen );
*pBuf += padLen;
if ( i < (atrLen / 2) )
{
*nBufLen -= (atrLen - 2 * i);
Expand Down Expand Up @@ -3464,58 +3466,6 @@ stunlib_setIP6Address(StunIPAddress* pIpAddr,
}
}

int
stunlib_compareIPAddresses(const StunIPAddress* pS1,
const StunIPAddress* pS2)
{
int res;
if (!pS1 && !pS2)
{
return 0;
}
if (!pS1)
{
return -1;
}
if (!pS2)
{
return 1;
}

if ( 0 != ( res = (pS1->familyType - pS2->familyType) ) )
{
return res;
}
if (pS1->familyType == STUN_ADDR_IPv4Family)
{
if ( 0 != ( res = (pS1->addr.v4.port - pS2->addr.v4.port) ) )
{
return res;
}
if ( 0 != ( res = (pS1->addr.v4.addr - pS2->addr.v4.addr) ) )
{
return res;
}
}
else
{
int i;
if ( 0 != ( res = (pS1->addr.v6.port - pS2->addr.v6.port) ) )
{
return res;
}
for (i = 0; i < 4; i++)
{
if ( 0 != (res = pS1->addr.v6.addr[i] - pS2->addr.v6.addr[i]) )
{
return res;
}
}
}
return 0;
}


uint32_t
stunlib_calculateFingerprint(const uint8_t* buf,
size_t len)
Expand Down
104 changes: 54 additions & 50 deletions src/stuntrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ void
StunStatusCallBack(void* userCtx,
StunCallBackData_T* stunCbData);

static bool
isDstUnreachable(int32_t ICMPtype,
u_int16_t addrFamily)
bool
isDstUnreachable(const int32_t ICMPtype,
const u_int16_t addrFamily)
{
if ( ( (ICMPtype == 3) && (addrFamily == AF_INET) ) ||
( (ICMPtype == 1) && (addrFamily == AF_INET6) ) )
Expand All @@ -24,9 +24,9 @@ isDstUnreachable(int32_t ICMPtype,
return false;
}

static bool
isTimeExceeded(int32_t ICMPtype,
u_int16_t addrFamily)
bool
isTimeExceeded(const int32_t ICMPtype,
const u_int16_t addrFamily)
{
if ( ( (ICMPtype == 11) && (addrFamily == AF_INET) ) ||
( (ICMPtype == 3) && (addrFamily == AF_INET6) ) )
Expand Down Expand Up @@ -118,9 +118,33 @@ void
handleStunNoAnswer(struct hiutResult* result)
{
result->pathElement[result->currentTTL].inactive = true;
if (result->currentTTL == MAX_TTL)
{
/* Part of far end alive test */
result->remoteAlive = false;
result->currentTTL = 1;

stunlib_createId(&result->currStunMsgId,
rand(), result->currentTTL);

StunClient_startSTUNTrace( (STUN_CLIENT_DATA*)result->stunCtx,
result,
(struct sockaddr*)&result->remoteAddr,
(struct sockaddr*)&result->localAddr,
false,
result->username,
result->password,
result->currentTTL,
result->currStunMsgId,
result->sockfd,
result->sendFunc,
StunStatusCallBack,
NULL );
return;
}
/* Hov many no answer in a row? */
if (numConcecutiveInactiveNodes(result) >= MAX_CONCECUTIVE_INACTIVE &&
!result->remoteAlive)
if ( (numConcecutiveInactiveNodes(result) >= MAX_CONCECUTIVE_INACTIVE) &&
!result->remoteAlive )
{
bool done = result->num_traces < result->max_recuring ? false : true;
result->path_max_ttl = result->currentTTL - MAX_CONCECUTIVE_INACTIVE;
Expand All @@ -143,7 +167,7 @@ handleStunNoAnswer(struct hiutResult* result)
false,
false);

if ( result->currentTTL < result->user_max_ttl )
if (result->currentTTL < result->user_max_ttl)
{
while (result->pathElement[result->currentTTL].inactive &&
result->currentTTL < result->path_max_ttl)
Expand All @@ -166,13 +190,6 @@ handleStunNoAnswer(struct hiutResult* result)
StunStatusCallBack,
NULL );
}
else
{
/* TODO: Callabck here */
/*
* stopAndExit(result);
*/
}
}

void
Expand All @@ -183,8 +200,6 @@ handleStunRespIcmp(struct hiutResult* result,
int rtt,
int retransmits)
{
STUN_CLIENT_DATA* clientData = (STUN_CLIENT_DATA*) result->stunCtx;

if ( (ttl == MAX_TTL) &&
isDstUnreachable(ICMPtype, srcAddr->sa_family) )
{
Expand Down Expand Up @@ -212,7 +227,7 @@ handleStunRespIcmp(struct hiutResult* result,
}
if ( isTimeExceeded(ICMPtype, srcAddr->sa_family) )
{
if (result->currentTTL < result->user_max_ttl)
if (result->currentTTL < result->user_max_ttl - 1)
{
result->currentTTL++;
while (result->pathElement[result->currentTTL].inactive &&
Expand Down Expand Up @@ -246,24 +261,19 @@ handleStunRespIcmp(struct hiutResult* result,
result->sendFunc,
StunStatusCallBack,
NULL );
}
else
{
bool done = result->num_traces < result->max_recuring ? false : true;
sendCallback(result,
srcAddr,
ttl,
rtt,
retransmits,
true,
done);
resartIfNotDone(result);
return;
}
}
else
{
/* do nothing */
}
bool done = result->num_traces < result->max_recuring ? false : true;
sendCallback(result,
srcAddr,
ttl,
rtt,
retransmits,
true,
done);
resartIfNotDone(result);

}
else if ( isDstUnreachable(ICMPtype,srcAddr->sa_family) )
{
Expand All @@ -284,15 +294,6 @@ handleStunRespIcmp(struct hiutResult* result,
resartIfNotDone(result);
}
}
else
{
StunPrint(clientData->logUserData,
clientData->Log_cb,
StunInfoCategory_Trace,
"<STUNTRACE> handleStunRespIcmp: Ignoring ICMP type: %i\n ",
ICMPtype);

}
}

void
Expand Down Expand Up @@ -327,7 +328,7 @@ handleStunRespSucsessfull(struct hiutResult* result,
result->sendFunc,
StunStatusCallBack,
NULL );
return;
return;
}

bool done = result->num_traces < result->max_recuring ? false : true;
Expand Down Expand Up @@ -358,11 +359,6 @@ StunStatusCallBack(void* userCtx,
{
struct hiutResult* result = (struct hiutResult*)userCtx;

if (result->pathElement[stunCbData->ttl].gotAnswer)
{
printf("Got this one already! Ignoring (%i)\n", stunCbData->ttl);
return;
}
result->pathElement[stunCbData->ttl].gotAnswer = true;

switch (stunCbData->stunResult)
Expand Down Expand Up @@ -403,6 +399,14 @@ StunTrace_startTrace(STUN_CLIENT_DATA* clientData,
STUN_TRACECB traceCbFunc,
STUN_SENDFUNC sendFunc)
{
if (clientData == NULL)
{
return 0;
}
if (!sendFunc || !toAddr)
{
return 0;
}
struct hiutResult* result;
uint32_t len;

Expand Down
Loading