Skip to content

Commit f968da1

Browse files
committed
Pairing can be very slow and sometimes iOS will send a TCP reset, courtesy of @jgstroud
Attempt keep the session alive while pairing Add optimistic_yields to try and prevent the session from timing out
1 parent 5f59512 commit f968da1

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/arduino_homekit_server.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3008,11 +3008,19 @@ client_context_t* homekit_server_accept_client(homekit_server_t *server) {
30083008
wifiClient->localIP().toString().c_str(), wifiClient->localPort(),
30093009
wifiClient->remoteIP().toString().c_str(), wifiClient->remotePort());
30103010

3011-
wifiClient->keepAlive(HOMEKIT_SOCKET_KEEPALIVE_IDLE_SEC,
3012-
HOMEKIT_SOCKET_KEEPALIVE_INTERVAL_SEC, HOMEKIT_SOCKET_KEEPALIVE_IDLE_COUNT);
30133011
wifiClient->setNoDelay(true);
3014-
wifiClient->setSync(false);
3015-
wifiClient->setTimeout(HOMEKIT_SOCKET_TIMEOUT);
3012+
wifiClient->setSync(true);
3013+
if (server->paired) {
3014+
wifiClient->keepAlive(HOMEKIT_SOCKET_KEEPALIVE_IDLE_SEC,
3015+
HOMEKIT_SOCKET_KEEPALIVE_INTERVAL_SEC, HOMEKIT_SOCKET_KEEPALIVE_IDLE_COUNT);
3016+
wifiClient->setTimeout(HOMEKIT_SOCKET_TIMEOUT);
3017+
INFO("Setting Timeout to 500ms");
3018+
} else {
3019+
// During the pairing process, relax the session timeout and be more agressive about keepalives
3020+
wifiClient->keepAlive(5, 5, 20);
3021+
wifiClient->setTimeout(90000);
3022+
INFO("Setting Timeout to 90 s");
3023+
}
30163024

30173025
client_context_t *context = client_context_new(wifiClient);
30183026
context->server = server;

src/wolfcrypt/src/integer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3535,7 +3535,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
35353535
* call `yield()` to run WiFi task,
35363536
* to prevent WiFi disconnection while heavy crypto computing.
35373537
*/
3538-
yield();
3538+
optimistic_yield(1000);
35393539
/* grab next digit as required */
35403540
if (--bitcnt == 0) {
35413541
/* if digidx == -1 we are out of digits */

src/wolfcrypt/src/srp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ int wc_SrpComputeKey(Srp* srp, byte* clientPubKey, word32 clientPubKeySz,
647647
r = mp_read_unsigned_bin(&temp1, srp->k, digestSz);
648648
if (!r) r = mp_iszero(&temp1) == MP_YES ? SRP_BAD_KEY_E : 0;
649649
if (!r) r = mp_exptmod(&srp->g, &srp->auth, &srp->N, &temp2);
650+
optimistic_yield(1000);
650651
if (!r) r = mp_mulmod(&temp1, &temp2, &srp->N, &s);
651652
if (!r) r = mp_read_unsigned_bin(&temp2, serverPubKey, serverPubKeySz);
652653
if (!r) r = mp_iszero(&temp2) == MP_YES ? SRP_BAD_KEY_E : 0;
@@ -659,10 +660,12 @@ int wc_SrpComputeKey(Srp* srp, byte* clientPubKey, word32 clientPubKeySz,
659660

660661
/* secret = temp1 ^ temp2 % N */
661662
if (!r) r = mp_exptmod(&temp1, &temp2, &srp->N, &s);
663+
optimistic_yield(1000);
662664

663665
} else if (!r && srp->side == SRP_SERVER_SIDE) {
664666
/* temp1 = v ^ u % N */
665667
r = mp_exptmod(&srp->auth, &u, &srp->N, &temp1);
668+
optimistic_yield(1000);
666669

667670
/* temp2 = A * temp1 % N; rejects A == 0, A >= N */
668671
if (!r) r = mp_read_unsigned_bin(&s, clientPubKey, clientPubKeySz);
@@ -678,6 +681,7 @@ int wc_SrpComputeKey(Srp* srp, byte* clientPubKey, word32 clientPubKeySz,
678681

679682
/* secret = temp2 * b % N */
680683
if (!r) r = mp_exptmod(&temp2, &srp->priv, &srp->N, &s);
684+
optimistic_yield(1000);
681685
}
682686

683687
/* building session key from secret */

0 commit comments

Comments
 (0)