Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dtls] address race with fd isset #90

Merged
merged 11 commits into from
Aug 16, 2017
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra space.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit fixed


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