Skip to content

Commit

Permalink
Bug 1162585: Set socket flags after socket has been created, r=kmachulis
Browse files Browse the repository at this point in the history
This patch moves the code for setting socket flags in the socket I/O
classes to the few locations were sockets are created. Any other socket
setup is redundant and has been removed.
  • Loading branch information
tdz committed May 8, 2015
1 parent e5ff47d commit 63a3cb9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 79 deletions.
64 changes: 27 additions & 37 deletions dom/bluetooth/bluez/BluetoothSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,6 @@ BluetoothSocket::BluetoothSocketIO::Listen()
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(mConnector);

// This will set things we don't particularly care about, but it will hand
// back the correct structure size which is what we do care about.
if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
NS_WARNING("Cannot create socket address!");
FireSocketError();
return;
}

if (!IsOpen()) {
int fd = mConnector->Create();
if (fd < 0) {
Expand All @@ -240,6 +232,18 @@ BluetoothSocket::BluetoothSocketIO::Listen()
FireSocketError();
return;
}
if (!mConnector->SetUpListenSocket(fd)) {
NS_WARNING("Could not set up listen socket!");
FireSocketError();
return;
}
// This will set things we don't particularly care about, but it will hand
// back the correct structure size which is what we do care about.
if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
NS_WARNING("Cannot create socket address!");
FireSocketError();
return;
}
SetFd(fd);

// calls OnListening on success, or OnError otherwise
Expand Down Expand Up @@ -267,15 +271,19 @@ BluetoothSocket::BluetoothSocketIO::Connect()
FireSocketError();
return;
}
if (!mConnector->SetUp(fd)) {
NS_WARNING("Could not set up socket!");
FireSocketError();
return;
}
if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) {
NS_WARNING("Cannot create socket address!");
FireSocketError();
return;
}
SetFd(fd);
}

if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) {
NS_WARNING("Cannot create socket address!");
FireSocketError();
return;
}

// calls OnConnected() on success, or OnError() otherwise
nsresult rv = UnixSocketWatcher::Connect(
reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize);
Expand All @@ -295,18 +303,6 @@ BluetoothSocket::BluetoothSocketIO::OnConnected()
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED);

if (!SetSocketFlags(GetFd())) {
NS_WARNING("Cannot set socket flags!");
FireSocketError();
return;
}

if (!mConnector->SetUp(GetFd())) {
NS_WARNING("Could not set up socket!");
FireSocketError();
return;
}

NS_DispatchToMainThread(
new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));

Expand All @@ -322,12 +318,6 @@ BluetoothSocket::BluetoothSocketIO::OnListening()
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);

if (!mConnector->SetUpListenSocket(GetFd())) {
NS_WARNING("Could not set up listen socket!");
FireSocketError();
return;
}

AddWatchers(READ_WATCHER, true);
}

Expand All @@ -346,24 +336,24 @@ BluetoothSocket::BluetoothSocketIO::OnSocketCanAcceptWithoutBlocking()
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);

RemoveWatchers(READ_WATCHER|WRITE_WATCHER);

socklen_t mAddrSize = sizeof(mAddr);
int fd = TEMP_FAILURE_RETRY(accept(GetFd(),
reinterpret_cast<struct sockaddr*>(&mAddr), &mAddrSize));
if (fd < 0) {
OnError("accept", errno);
return;
}

if (!SetSocketFlags(fd)) {
return;
}
if (!mConnector->SetUp(fd)) {
NS_WARNING("Could not set up socket!");
return;
}

RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
Close();
if (!SetSocketFlags(fd)) {
return;
}
SetSocket(fd, SOCKET_IS_CONNECTED);

NS_DispatchToMainThread(
Expand Down
28 changes: 13 additions & 15 deletions ipc/unixsocket/ListenSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,24 @@ ListenSocketIO::Listen(ConnectionOrientedSocketIO* aCOSocketIO)
FireSocketError();
return;
}
if (!mConnector->SetUpListenSocket(GetFd())) {
NS_WARNING("Could not set up listen socket!");
FireSocketError();
return;
}
// This will set things we don't particularly care about, but
// it will hand back the correct structure size which is what
// we do care about.
if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
NS_WARNING("Cannot create socket address!");
FireSocketError();
return;
}
SetFd(fd);
}

mCOSocketIO = aCOSocketIO;

// This will set things we don't particularly care about, but
// it will hand back the correct structure size which is what
// we do care about.
if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) {
NS_WARNING("Cannot create socket address!");
FireSocketError();
return;
}

// calls OnListening on success, or OnError otherwise
nsresult rv = UnixSocketWatcher::Listen(
reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize);
Expand All @@ -188,12 +192,6 @@ ListenSocketIO::OnListening()
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_LISTENING);

if (!mConnector->SetUpListenSocket(GetFd())) {
NS_WARNING("Could not set up listen socket!");
FireSocketError();
return;
}

AddWatchers(READ_WATCHER, true);

/* We signal a successful 'connection' to a local address for listening. */
Expand Down
45 changes: 18 additions & 27 deletions ipc/unixsocket/StreamSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,19 @@ StreamSocketIO::Connect()
FireSocketError();
return;
}
if (!mConnector->SetUp(GetFd())) {
NS_WARNING("Could not set up socket!");
FireSocketError();
return;
}
if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) {
NS_WARNING("Cannot create socket address!");
FireSocketError();
return;
}
SetFd(fd);
}

if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) {
NS_WARNING("Cannot create socket address!");
FireSocketError();
return;
}

// calls OnConnected() on success, or OnError() otherwise
nsresult rv = UnixSocketWatcher::Connect(
reinterpret_cast<struct sockaddr*>(&mAddr), mAddrSize);
Expand All @@ -279,18 +283,6 @@ StreamSocketIO::OnConnected()
MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED);

if (!SetSocketFlags(GetFd())) {
NS_WARNING("Cannot set socket flags!");
FireSocketError();
return;
}

if (!mConnector->SetUp(GetFd())) {
NS_WARNING("Could not set up socket!");
FireSocketError();
return;
}

NS_DispatchToMainThread(
new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));

Expand Down Expand Up @@ -408,30 +400,29 @@ StreamSocketIO::Accept(int aFd,

// File-descriptor setup

if (!SetSocketFlags(aFd)) {
return NS_ERROR_FAILURE;
}
if (!mConnector->SetUp(aFd)) {
NS_WARNING("Could not set up socket!");
return NS_ERROR_FAILURE;
}

if (!SetSocketFlags(aFd)) {
return NS_ERROR_FAILURE;
}
SetSocket(aFd, SOCKET_IS_CONNECTED);

AddWatchers(READ_WATCHER, true);
if (HasPendingData()) {
AddWatchers(WRITE_WATCHER, false);
}

// Address setup

memcpy(&mAddr, aAddr, aAddrLen);
mAddrSize = aAddrLen;

// Signal success
NS_DispatchToMainThread(
new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));

AddWatchers(READ_WATCHER, true);
if (HasPendingData()) {
AddWatchers(WRITE_WATCHER, false);
}

return NS_OK;
}

Expand Down

0 comments on commit 63a3cb9

Please sign in to comment.