Bug 800249 - Patch 2: Add ability to retreive connected socket address as a string; r=cjones

This commit is contained in:
Kyle Machulis 2012-10-17 17:11:05 -07:00
Родитель f7cbae787d
Коммит 0d5aa7e492
4 изменённых файлов: 44 добавлений и 9 удалений

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

@ -60,6 +60,13 @@ int get_bdaddr(const char *str, bdaddr_t *ba)
return 0;
}
static
void get_bdaddr_as_string(const bdaddr_t *ba, char *str) {
const uint8_t *b = (const uint8_t *)ba;
sprintf(str, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
b[5], b[4], b[3], b[2], b[1], b[0]);
}
BluetoothUnixSocketConnector::BluetoothUnixSocketConnector(
BluetoothSocketType aType,
int aChannel,
@ -179,3 +186,11 @@ BluetoothUnixSocketConnector::CreateAddr(bool aIsServer,
}
}
void
BluetoothUnixSocketConnector::GetSocketAddr(const sockaddr& aAddr,
nsAString& aAddrStr)
{
char addr[18];
get_bdaddr_as_string((bdaddr_t*)&aAddr, addr);
aAddrStr.AssignASCII(addr);
}

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

@ -26,6 +26,9 @@ public:
struct sockaddr* aAddr,
const char* aAddress) MOZ_OVERRIDE;
virtual bool SetUp(int aFd) MOZ_OVERRIDE;
virtual void GetSocketAddr(const sockaddr& aAddr,
nsAString& aAddrStr) MOZ_OVERRIDE;
private:
BluetoothSocketType mType;
int mChannel;

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

@ -151,10 +151,15 @@ public:
*/
bool SetNonblockFlags();
void GetSocketAddr(struct sockaddr& aAddr, socklen_t& aAddrSize)
void GetSocketAddr(nsAString& aAddrStr)
{
aAddr = mAddr;
aAddrSize = mAddrSize;
if (!mConnector)
{
NS_WARNING("No connector to get socket address from!");
aAddrStr = nsString();
return;
}
mConnector->GetSocketAddr(mAddr, aAddrStr);
}
/**
@ -242,7 +247,7 @@ private:
/**
* Address struct of the socket currently in use
*/
struct sockaddr mAddr;
sockaddr mAddr;
};
@ -707,13 +712,14 @@ UnixSocketImpl::OnFileCanWriteWithoutBlocking(int aFd)
}
void
UnixSocketConsumer::GetSocketAddr(struct sockaddr& aAddr, socklen_t &aAddrSize)
UnixSocketConsumer::GetSocketAddr(nsAString& aAddrStr)
{
if (!mImpl) {
if (!mImpl || mConnectionStatus != SOCKET_CONNECTED) {
NS_WARNING("No socket currently open!");
aAddrStr = nsString();
return;
}
mImpl->GetSocketAddr(aAddr, aAddrSize);
mImpl->GetSocketAddr(aAddrStr);
}
void

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

@ -101,6 +101,17 @@ public:
* @return true is successful, false otherwise
*/
virtual bool SetUp(int aFd) = 0;
/**
* Get address of socket we're currently connected to. Return null string if
* not connected.
*
* @param aAddr Address struct
* @param aAddrStr String to store address to
*/
virtual void GetSocketAddr(const sockaddr& aAddr,
nsAString& aAddrStr) = 0;
};
enum SocketConnectionStatus {
@ -216,7 +227,7 @@ public:
/**
* Get the current sockaddr for the socket
*/
void GetSocketAddr(struct sockaddr& aAddr, socklen_t& aAddrSize);
void GetSocketAddr(nsAString& aAddrStr);
private:
UnixSocketImpl* mImpl;