diff --git a/dom/bluetooth/BluetoothUnixSocketConnector.cpp b/dom/bluetooth/BluetoothUnixSocketConnector.cpp index b6f136cd7338..1b36284a08ba 100644 --- a/dom/bluetooth/BluetoothUnixSocketConnector.cpp +++ b/dom/bluetooth/BluetoothUnixSocketConnector.cpp @@ -44,6 +44,7 @@ #define LOG(args...) if (BTDEBUG) printf(args); #endif +using namespace mozilla::ipc; USING_BLUETOOTH_NAMESPACE static const int RFCOMM_SO_SNDBUF = 70 * 1024; // 70 KB send buffer @@ -144,10 +145,10 @@ BluetoothUnixSocketConnector::Create() return fd; } -void +bool BluetoothUnixSocketConnector::CreateAddr(bool aIsServer, socklen_t& aAddrSize, - struct sockaddr* aAddr, + sockaddr_any& aAddr, const char* aAddress) { // Set to BDADDR_ANY, if it's not a server, we'll reset. @@ -156,7 +157,7 @@ BluetoothUnixSocketConnector::CreateAddr(bool aIsServer, if (!aIsServer && aAddress && strlen(aAddress) > 0) { if (get_bdaddr(aAddress, &bd_address_obj)) { NS_WARNING("Can't get bluetooth address!"); - return; + return false; } } @@ -164,31 +165,37 @@ BluetoothUnixSocketConnector::CreateAddr(bool aIsServer, case BluetoothSocketType::RFCOMM: struct sockaddr_rc addr_rc; aAddrSize = sizeof(addr_rc); - memset(aAddr, 0, aAddrSize); - addr_rc.rc_family = AF_BLUETOOTH; - addr_rc.rc_channel = mChannel; - memcpy(&addr_rc.rc_bdaddr, &bd_address_obj, sizeof(bdaddr_t)); - memcpy(aAddr, &addr_rc, sizeof(addr_rc)); + aAddr.rc.rc_family = AF_BLUETOOTH; + aAddr.rc.rc_channel = mChannel; + memcpy(&aAddr.rc.rc_bdaddr, &bd_address_obj, sizeof(bd_address_obj)); break; case BluetoothSocketType::SCO: struct sockaddr_sco addr_sco; aAddrSize = sizeof(addr_sco); - - memset(aAddr, 0, aAddrSize); - addr_sco.sco_family = AF_BLUETOOTH; - memcpy(&addr_sco.sco_bdaddr, &bd_address_obj, sizeof(bdaddr_t)); - memcpy(aAddr, &addr_sco, sizeof(addr_sco)); + aAddr.sco.sco_family = AF_BLUETOOTH; + memcpy(&aAddr.sco.sco_bdaddr, &bd_address_obj, sizeof(bd_address_obj)); break; default: NS_WARNING("Socket type unknown!"); + return false; } + return true; } void -BluetoothUnixSocketConnector::GetSocketAddr(const sockaddr& aAddr, +BluetoothUnixSocketConnector::GetSocketAddr(const sockaddr_any& aAddr, nsAString& aAddrStr) { char addr[18]; - get_bdaddr_as_string((bdaddr_t*)aAddr.sa_data, addr); + switch (mType) { + case BluetoothSocketType::RFCOMM: + get_bdaddr_as_string((bdaddr_t*)(&aAddr.rc.rc_bdaddr), addr); + break; + case BluetoothSocketType::SCO: + get_bdaddr_as_string((bdaddr_t*)(&aAddr.sco.sco_bdaddr), addr); + break; + default: + MOZ_NOT_REACHED("Socket should be either RFCOMM or SCO!"); + } aAddrStr.AssignASCII(addr); } diff --git a/dom/bluetooth/BluetoothUnixSocketConnector.h b/dom/bluetooth/BluetoothUnixSocketConnector.h index 89252b80dd7e..e46a9bd78f55 100644 --- a/dom/bluetooth/BluetoothUnixSocketConnector.h +++ b/dom/bluetooth/BluetoothUnixSocketConnector.h @@ -21,12 +21,12 @@ public: virtual ~BluetoothUnixSocketConnector() {} virtual int Create() MOZ_OVERRIDE; - virtual void CreateAddr(bool aIsServer, + virtual bool CreateAddr(bool aIsServer, socklen_t& aAddrSize, - struct sockaddr* aAddr, + mozilla::ipc::sockaddr_any& aAddr, const char* aAddress) MOZ_OVERRIDE; virtual bool SetUp(int aFd) MOZ_OVERRIDE; - virtual void GetSocketAddr(const sockaddr& aAddr, + virtual void GetSocketAddr(const mozilla::ipc::sockaddr_any& aAddr, nsAString& aAddrStr) MOZ_OVERRIDE; private: diff --git a/ipc/unixsocket/UnixSocket.cpp b/ipc/unixsocket/UnixSocket.cpp index 5d334ecf9125..2a47e016aa9b 100644 --- a/ipc/unixsocket/UnixSocket.cpp +++ b/ipc/unixsocket/UnixSocket.cpp @@ -255,7 +255,7 @@ private: /** * Address struct of the socket currently in use */ - sockaddr mAddr; + sockaddr_any mAddr; }; @@ -476,9 +476,12 @@ UnixSocketImpl::Accept() // 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. - mConnector->CreateAddr(true, mAddrSize, &mAddr, nullptr); + if (!mConnector->CreateAddr(true, mAddrSize, mAddr, nullptr)) { + NS_WARNING("Cannot create socket address!"); + return; + } - if(mFd.get() < 0) + if (mFd.get() < 0) { mFd = mConnector->Create(); if (mFd.get() < 0) { @@ -489,7 +492,7 @@ UnixSocketImpl::Accept() return; } - if (bind(mFd.get(), &mAddr, mAddrSize)) { + if (bind(mFd.get(), (struct sockaddr*)&mAddr, mAddrSize)) { #ifdef DEBUG LOG("...bind(%d) gave errno %d", mFd.get(), errno); #endif @@ -506,7 +509,7 @@ UnixSocketImpl::Accept() } int client_fd; - client_fd = accept(mFd.get(), &mAddr, &mAddrSize); + client_fd = accept(mFd.get(), (struct sockaddr*)&mAddr, &mAddrSize); if (client_fd < 0) { EnqueueTask(SOCKET_RETRY_TIME_MS, new SocketAcceptTask(this)); return; @@ -545,9 +548,12 @@ UnixSocketImpl::Connect() int ret; - mConnector->CreateAddr(false, mAddrSize, &mAddr, mAddress.get()); + if (!mConnector->CreateAddr(false, mAddrSize, mAddr, mAddress.get())) { + NS_WARNING("Cannot create socket address!"); + return; + } - ret = connect(mFd.get(), &mAddr, mAddrSize); + ret = connect(mFd.get(), (struct sockaddr*)&mAddr, mAddrSize); if (ret) { #if DEBUG diff --git a/ipc/unixsocket/UnixSocket.h b/ipc/unixsocket/UnixSocket.h index 7dee5c3fb421..0fc8e7a249d7 100644 --- a/ipc/unixsocket/UnixSocket.h +++ b/ipc/unixsocket/UnixSocket.h @@ -7,7 +7,16 @@ #ifndef mozilla_ipc_UnixSocket_h #define mozilla_ipc_UnixSocket_h + #include +#include +#include +#include +#ifdef MOZ_B2G_BT +#include +#include +#include +#endif #include #include "nsString.h" #include "nsAutoPtr.h" @@ -16,6 +25,18 @@ namespace mozilla { namespace ipc { +union sockaddr_any { + struct sockaddr_storage storage; // address-family only + struct sockaddr_un un; + struct sockaddr_in in; + struct sockaddr_in6 in6; +#ifdef MOZ_B2G_BT + struct sockaddr_sco sco; + struct sockaddr_rc rc; +#endif + // ... others +}; + class UnixSocketRawData { public: @@ -78,10 +99,12 @@ public: * @param aAddrSize Size of the struct * @param aAddr Struct to fill * @param aAddress If aIsServer is false, Address to connect to. nullptr otherwise. + * + * @return True if address is filled correctly, false otherwise */ - virtual void CreateAddr(bool aIsServer, + virtual bool CreateAddr(bool aIsServer, socklen_t& aAddrSize, - struct sockaddr *aAddr, + sockaddr_any& aAddr, const char* aAddress) = 0; /** @@ -100,7 +123,7 @@ public: * @param aAddr Address struct * @param aAddrStr String to store address to */ - virtual void GetSocketAddr(const sockaddr& aAddr, + virtual void GetSocketAddr(const sockaddr_any& aAddr, nsAString& aAddrStr) = 0; };