Skip to content

Commit

Permalink
[dtls] address race with fd isset (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
DuaneEllis-TI authored and jwhui committed Aug 16, 2017
1 parent b8e6540 commit 4f29337
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/agent/dtls_mbedtls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,14 @@ void MbedtlsServer::UpdateFdSet(fd_set &aReadFdSet, fd_set &aWriteFdSet, fd_set
}
}

FD_SET(mSocket, &aReadFdSet);

if (aMaxFd < mSocket)
if (mSocket >= 0)
{
aMaxFd = mSocket;
FD_SET(mSocket, &aReadFdSet);

if (aMaxFd < mSocket)
{
aMaxFd = mSocket;
}
}

aTimeout.tv_sec = timeout / 1000;
Expand All @@ -486,13 +489,17 @@ void MbedtlsServer::ProcessServer(const fd_set &aReadFdSet, const fd_set &aWrite
{
uint8_t packet[kMaxSizeOfPacket];
uint8_t control[kMaxSizeOfControl];
otbrError error = OTBR_ERROR_ERRNO;
otbrError error = OTBR_ERROR_ERRNO; // Assume error
sockaddr_in6 src;
sockaddr_in6 dst;
struct msghdr msghdr;
struct iovec iov[1];

VerifyOrExit(FD_ISSET(mSocket, &aReadFdSet));
/* Connection is not alive yet, or is shut down */
VerifyOrExit(mSocket >= 0, error = OTBR_ERROR_NONE);

/* If this is not set, then some other handle became rd/wr able, it is not an error */
VerifyOrExit(FD_ISSET(mSocket, &aReadFdSet), error = OTBR_ERROR_NONE);

otbrLog(OTBR_LOG_INFO, "Trying to accept connection...");
memset(&src, 0, sizeof(src));
Expand Down Expand Up @@ -548,6 +555,7 @@ void MbedtlsServer::ProcessServer(const fd_set &aReadFdSet, const fd_set &aWrite
otbrLog(OTBR_LOG_ERR, "DTLS failed to initiate new session: %s.", otbrErrorString(error));
otbrLog(OTBR_LOG_INFO, "Trying to create new server socket...");
close(mSocket);
mSocket = -1;

if (Bind())
{
Expand Down
1 change: 1 addition & 0 deletions src/agent/dtls_mbedtls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class MbedtlsServer : public Server
*
*/
MbedtlsServer(uint16_t aPort, StateHandler aStateHandler, void *aContext) :
mSocket(-1),
mPort(aPort),
mStateHandler(aStateHandler),
mContext(aContext) {}
Expand Down
1 change: 1 addition & 0 deletions tests/meshcop/commissioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ int CommissionerServe(Context &aContext)
VerifyOrExit(aContext.mSocket != -1, ret = errno);
aContext.mDtlsServer = Dtls::Server::Create(kPortJoinerSession, HandleSessionChange, &aContext);
aContext.mDtlsServer->SetPSK(kPSKd, strlen(reinterpret_cast<const char *>(kPSKd)));
aContext.mDtlsServer->Start();

while (aContext.mState != kStateDone && aContext.mState != kStateError)
{
Expand Down

0 comments on commit 4f29337

Please sign in to comment.