Bug 843868 - Change sockaddr* to be a union of all possible sockaddr types. r=tzimmermann, a=tef+

This commit is contained in:
Kyle Machulis 2013-03-21 11:26:28 -04:00
Родитель 89940a07f8
Коммит 6252e70de1
4 изменённых файлов: 64 добавлений и 28 удалений

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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:

Просмотреть файл

@ -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

Просмотреть файл

@ -7,7 +7,16 @@
#ifndef mozilla_ipc_UnixSocket_h
#define mozilla_ipc_UnixSocket_h
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <netinet/in.h>
#ifdef MOZ_B2G_BT
#include <bluetooth/bluetooth.h>
#include <bluetooth/sco.h>
#include <bluetooth/rfcomm.h>
#endif
#include <stdlib.h>
#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;
};