Skip to content

Commit bf0dd3a

Browse files
committed
Address review feedback
DtlsTimersRunning and DtlsTimerLeft returned either the timer state or BAD_MUTEX_E. They now return 0 or BAD_MUTEX_E and report the timers through an out parameter. Rename DtlsTimerLowRes to DtlsTimerIsLowRes, since the old name read like a variant of LowResTimer. Move the handShakeDone comment in EmbedReceiveFrom back above the check it describes.
1 parent e2b29ba commit bf0dd3a

5 files changed

Lines changed: 51 additions & 38 deletions

File tree

‎src/internal.c‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12082,7 +12082,7 @@ word32 DtlsTimerElapsedMs(word32 start)
1208212082
word32 elapsed = DtlsTimerNowMs() - start;
1208312083

1208412084
/* Both readings were truncated to whole seconds. */
12085-
if (DtlsTimerLowRes())
12085+
if (DtlsTimerIsLowRes())
1208612086
elapsed = (elapsed >= 1000) ? elapsed - 1000 : 0;
1208712087
return elapsed;
1208812088
}
@@ -12134,43 +12134,44 @@ static int DtlsMsgPoolWaiting(WOLFSSL* ssl)
1213412134
/* Get the DTLS timers that run.
1213512135
*
1213612136
* @param [in] ssl SSL/TLS object.
12137+
* @param [out] running DTLS_TIMER_RTX and DTLS_TIMER_ACK bits, 0 when none
12138+
* runs.
1213712139
* @param [out] ackStart Start of the DTLS 1.3 ack timer. May be NULL.
12138-
* @return DTLS_TIMER_RTX and DTLS_TIMER_ACK bits, 0 when none runs.
12140+
* @return 0 on success.
1213912141
* @return BAD_MUTEX_E when locking fails.
1214012142
*/
12141-
int DtlsTimersRunning(WOLFSSL* ssl, word32* ackStart)
12143+
int DtlsTimersRunning(WOLFSSL* ssl, int* running, word32* ackStart)
1214212144
{
12143-
int running = 0;
12144-
1214512145
(void)ackStart;
1214612146

12147+
*running = 0;
1214712148
#ifdef WOLFSSL_DTLS13
1214812149
if (IsAtLeastTLSv1_3(ssl->version)) {
1214912150
if (ssl->dtls13Rtx.rtxRecords != NULL)
12150-
running |= DTLS_TIMER_RTX;
12151+
*running |= DTLS_TIMER_RTX;
1215112152
#ifdef WOLFSSL_RW_THREADED
1215212153
if (wc_LockMutex(&ssl->dtls13Rtx.mutex) != 0)
1215312154
return BAD_MUTEX_E;
1215412155
#endif
1215512156
if (ssl->dtls13FastTimeout && Dtls13AckSendable(ssl))
12156-
running |= DTLS_TIMER_ACK;
12157+
*running |= DTLS_TIMER_ACK;
1215712158
#ifndef NO_ASN_TIME
1215812159
if (ackStart != NULL)
1215912160
*ackStart = ssl->dtls_ack_timer_start;
1216012161
#endif
1216112162
#ifdef WOLFSSL_RW_THREADED
1216212163
wc_UnLockMutex(&ssl->dtls13Rtx.mutex);
1216312164
#endif
12164-
return running;
12165+
return 0;
1216512166
}
1216612167
#endif /* WOLFSSL_DTLS13 */
1216712168

1216812169
if (ssl->options.handShakeState != HANDSHAKE_DONE &&
1216912170
(ssl->dtls_tx_msg_list != NULL || ssl->dtls_tx_msg != NULL) &&
1217012171
DtlsMsgPoolWaiting(ssl))
12171-
running |= DTLS_TIMER_RTX;
12172+
*running |= DTLS_TIMER_RTX;
1217212173

12173-
return running;
12174+
return 0;
1217412175
}
1217512176

1217612177

@@ -12939,7 +12940,7 @@ word32 DtlsTimerNowMs(void)
1293912940
}
1294012941

1294112942
/* Return 1 when the DTLS timer clock counts whole seconds. */
12942-
int DtlsTimerLowRes(void)
12943+
int DtlsTimerIsLowRes(void)
1294312944
{
1294412945
#if defined(MS_TIME_LOW_RES) || defined(MS_TIME_FALLBACK)
1294512946
return 1;

‎src/ssl_api_dtls.c‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,39 +1222,44 @@ static int Dtls13GotTimeout(WOLFSSL* ssl, int expired)
12221222
/* Get the time left on the DTLS timers.
12231223
*
12241224
* @param [in] ssl SSL/TLS object.
1225+
* @param [out] running 1 when a timer runs, 0 otherwise.
12251226
* @param [out] left Milliseconds until the first timer expires.
12261227
* @param [out] ackOnly 1 when only the DTLS 1.3 ack timer expired.
1227-
* @return 1 when a timer runs.
1228-
* @return 0 when no timer runs.
1228+
* @return 0 on success.
12291229
* @return BAD_MUTEX_E when locking fails.
12301230
*/
1231-
static int DtlsTimerLeft(WOLFSSL* ssl, word32* left, int* ackOnly)
1231+
static int DtlsTimerLeft(WOLFSSL* ssl, int* running, word32* left,
1232+
int* ackOnly)
12321233
{
12331234
#ifndef NO_ASN_TIME
1234-
int running;
1235+
int ret;
1236+
int timers = 0;
12351237
word32 timeout;
12361238
word32 elapsed;
12371239
#ifdef WOLFSSL_DTLS13
12381240
word32 ackLeft;
12391241
#endif
12401242
word32 ackStart = 0;
12411243

1244+
*running = 0;
12421245
if (!IsDtlsNotSctpMode(ssl) || ssl->dtls_timeout <= 0 ||
12431246
ssl->buffers.outputBuffer.length > 0)
12441247
return 0;
1245-
running = DtlsTimersRunning(ssl, &ackStart);
1248+
ret = DtlsTimersRunning(ssl, &timers, &ackStart);
1249+
if (ret != 0)
1250+
return ret;
12461251
#ifdef WOLFSSL_HAVE_HS_SUSPEND
12471252
/* A suspended handshake has not finished sending its flight. */
1248-
if (running > 0 && IsHsSuspendErr(ssl->error))
1249-
running &= ~DTLS_TIMER_RTX;
1253+
if (IsHsSuspendErr(ssl->error))
1254+
timers &= ~DTLS_TIMER_RTX;
12501255
#endif
1251-
if (running <= 0)
1252-
return running;
1256+
if (timers == 0)
1257+
return 0;
12531258

12541259
timeout = DtlsTimerTimeoutMs(ssl);
12551260
*left = timeout;
12561261
*ackOnly = 1;
1257-
if (running & DTLS_TIMER_RTX) {
1262+
if (timers & DTLS_TIMER_RTX) {
12581263
elapsed = DtlsTimerElapsedMs(ssl->dtls_timer_start);
12591264
*left = (elapsed < timeout) ? timeout - elapsed : 0;
12601265
#ifdef WOLFSSL_DTLS13
@@ -1264,16 +1269,18 @@ static int DtlsTimerLeft(WOLFSSL* ssl, word32* left, int* ackOnly)
12641269
*ackOnly = (*left > 0);
12651270
}
12661271
#ifdef WOLFSSL_DTLS13
1267-
if (running & DTLS_TIMER_ACK) {
1272+
if (timers & DTLS_TIMER_ACK) {
12681273
elapsed = DtlsTimerNowMs() - ackStart;
12691274
ackLeft = (elapsed < timeout / 4) ? timeout / 4 - elapsed : 0;
1270-
if (ackLeft < *left || !(running & DTLS_TIMER_RTX))
1275+
if (ackLeft < *left || !(timers & DTLS_TIMER_RTX))
12711276
*left = ackLeft;
12721277
}
12731278
#endif
1274-
return 1;
1279+
*running = 1;
1280+
return 0;
12751281
#else
12761282
(void)ssl;
1283+
*running = 0;
12771284
(void)left;
12781285
(void)ackOnly;
12791286
return 0;
@@ -1291,16 +1298,17 @@ int wolfSSL_DTLSv1_get_timeout(WOLFSSL* ssl, WOLFSSL_TIMEVAL* timeleft)
12911298
{
12921299
word32 left = 0;
12931300
int ackOnly = 0;
1301+
int running = 0;
12941302
int ret;
12951303

12961304
if (ssl == NULL || timeleft == NULL)
12971305
return 0;
12981306

12991307
XMEMSET(timeleft, 0, sizeof(WOLFSSL_TIMEVAL));
1300-
ret = DtlsTimerLeft(ssl, &left, &ackOnly);
1301-
if (ret < 0)
1308+
ret = DtlsTimerLeft(ssl, &running, &left, &ackOnly);
1309+
if (ret != 0)
13021310
WOLFSSL_MSG("Getting the DTLS timers failed");
1303-
if (ret <= 0)
1311+
if (ret != 0 || !running)
13041312
return 0;
13051313

13061314
timeleft->tv_sec = left / 1000;
@@ -1322,18 +1330,19 @@ int wolfSSL_DTLSv1_handle_timeout(WOLFSSL* ssl)
13221330
{
13231331
word32 left = 0;
13241332
int ackOnly = 0;
1333+
int running = 0;
13251334
int ret;
13261335

13271336
if (ssl == NULL)
13281337
return WOLFSSL_FATAL_ERROR;
13291338

1330-
ret = DtlsTimerLeft(ssl, &left, &ackOnly);
1331-
if (ret < 0) {
1339+
ret = DtlsTimerLeft(ssl, &running, &left, &ackOnly);
1340+
if (ret != 0) {
13321341
ssl->error = ret;
13331342
WOLFSSL_ERROR(ret);
13341343
return WOLFSSL_FATAL_ERROR;
13351344
}
1336-
if (ret == 0 || left > 0)
1345+
if (!running || left > 0)
13371346
return 0;
13381347

13391348
#ifdef WOLFSSL_DTLS13

‎src/wolfio.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ int EmbedReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
683683
int sd = dtlsCtx->rfd;
684684
int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
685685
byte doDtlsTimeout;
686+
int running = 0;
686687
SOCKADDR_S lclPeer;
687688
SOCKADDR_S* peer;
688689
XSOCKLENT peerSz = 0;
@@ -736,12 +737,13 @@ int EmbedReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
736737
if (ret != 0)
737738
return ret;
738739

740+
ret = DtlsTimersRunning(ssl, &running, NULL);
741+
if (ret != 0)
742+
return ret;
739743
/* Don't use ssl->options.handShakeDone since it is true even if
740744
* we are in the process of renegotiation */
741-
ret = DtlsTimersRunning(ssl, NULL);
742-
if (ret < 0)
743-
return ret;
744-
doDtlsTimeout = ssl->options.handShakeState != HANDSHAKE_DONE || ret != 0;
745+
doDtlsTimeout = ssl->options.handShakeState != HANDSHAKE_DONE ||
746+
running != 0;
745747

746748
do {
747749
int ignore = 0;

‎tests/api/test_dtls.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13861,7 +13861,7 @@ static int test_DTLSv1_timer(method_provider method_c,
1386113861
ExpectIntEQ(wolfSSL_DTLSv1_handle_timeout(ssl_c), 0);
1386213862
ExpectIntEQ(test_ctx.s_msg_count, 1);
1386313863

13864-
if (DtlsTimerLowRes()) {
13864+
if (DtlsTimerIsLowRes()) {
1386513865
/* A start one tick ago may be under a second, so 1 s hasn't passed. */
1386613866
for (i = 0; i < 3 && EXPECT_SUCCESS(); i++) {
1386713867
now = DtlsTimerNowMs();

‎wolfssl/internal.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8123,7 +8123,7 @@ WOLFSSL_LOCAL int Tls13KeyUpdateLimitReached(WOLFSSL* ssl);
81238123
#define DtlsMsgFind wolfSSL_DtlsMsgFind
81248124
#define DtlsMsgStore wolfSSL_DtlsMsgStore
81258125
#define DtlsTimerNowMs wolfSSL_DtlsTimerNowMs
8126-
#define DtlsTimerLowRes wolfSSL_DtlsTimerLowRes
8126+
#define DtlsTimerIsLowRes wolfSSL_DtlsTimerIsLowRes
81278127
#endif /* WOLFSSL_API_PREFIX_MAP */
81288128
WOLFSSL_LOCAL DtlsMsg* DtlsMsgNew(word32 sz, byte tx, void* heap);
81298129
WOLFSSL_LOCAL void DtlsMsgDelete(DtlsMsg* item, void* heap);
@@ -8154,10 +8154,11 @@ WOLFSSL_LOCAL int Tls13KeyUpdateLimitReached(WOLFSSL* ssl);
81548154
WOLFSSL_LOCAL int DtlsMsgPoolTimeout(WOLFSSL* ssl);
81558155
WOLFSSL_LOCAL void DtlsTimerStart(WOLFSSL* ssl);
81568156
WOLFSSL_LOCAL void DtlsTimeoutReset(WOLFSSL* ssl);
8157-
WOLFSSL_LOCAL int DtlsTimersRunning(WOLFSSL* ssl, word32* ackStart);
8157+
WOLFSSL_LOCAL int DtlsTimersRunning(WOLFSSL* ssl, int* running,
8158+
word32* ackStart);
81588159
#ifndef NO_ASN_TIME
81598160
WOLFSSL_TEST_VIS word32 DtlsTimerNowMs(void);
8160-
WOLFSSL_TEST_VIS int DtlsTimerLowRes(void);
8161+
WOLFSSL_TEST_VIS int DtlsTimerIsLowRes(void);
81618162
WOLFSSL_LOCAL word32 DtlsTimerElapsedMs(word32 start);
81628163
WOLFSSL_LOCAL word32 DtlsTimerTimeoutMs(const WOLFSSL* ssl);
81638164
#endif

0 commit comments

Comments
 (0)