Bug 800249 - Patch 1: Add ability to store/retrieve address to UnixSocket; r=cjones

This commit is contained in:
Kyle Machulis 2012-10-17 17:10:27 -07:00
parent 00fd9cf2f3
commit 0ca2bf3025
2 changed files with 38 additions and 9 deletions

View File

@ -151,6 +151,12 @@ public:
*/
bool SetNonblockFlags();
void GetSocketAddr(struct sockaddr& aAddr, socklen_t& aAddrSize)
{
aAddr = mAddr;
aAddrSize = mAddrSize;
}
/**
* Consumer pointer. Non-thread safe RefPtr, so should only be manipulated
* directly from main thread. All non-main-thread accesses should happen with
@ -227,6 +233,17 @@ private:
* Address we are connecting to, assuming we are creating a client connection.
*/
nsCString mAddress;
/**
* Size of the socket address struct
*/
socklen_t mAddrSize;
/**
* Address struct of the socket currently in use
*/
struct sockaddr mAddr;
};
static void
@ -404,8 +421,6 @@ void SocketConnectTask::Run() {
void
UnixSocketImpl::Accept()
{
socklen_t addr_sz;
struct sockaddr addr;
if (!mConnector) {
NS_WARNING("No connector object available!");
@ -414,7 +429,7 @@ 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, addr_sz, &addr, nullptr);
mConnector->CreateAddr(true, mAddrSize, &mAddr, nullptr);
if(mFd.get() < 0)
{
@ -427,7 +442,7 @@ UnixSocketImpl::Accept()
return;
}
if (bind(mFd.get(), &addr, addr_sz)) {
if (bind(mFd.get(), &mAddr, mAddrSize)) {
#ifdef DEBUG
LOG("...bind(%d) gave errno %d", mFd.get(), errno);
#endif
@ -444,7 +459,7 @@ UnixSocketImpl::Accept()
}
int client_fd;
client_fd = accept(mFd.get(), &addr, &addr_sz);
client_fd = accept(mFd.get(), &mAddr, &mAddrSize);
if (client_fd < 0) {
EnqueueTask(SOCKET_RETRY_TIME_MS, new SocketAcceptTask(this));
return;
@ -479,12 +494,10 @@ UnixSocketImpl::Connect()
}
int ret;
socklen_t addr_sz;
struct sockaddr addr;
mConnector->CreateAddr(false, addr_sz, &addr, mAddress.get());
mConnector->CreateAddr(false, mAddrSize, &mAddr, mAddress.get());
ret = connect(mFd.get(), &addr, addr_sz);
ret = connect(mFd.get(), &mAddr, mAddrSize);
if (ret) {
#if DEBUG
@ -693,6 +706,16 @@ UnixSocketImpl::OnFileCanWriteWithoutBlocking(int aFd)
}
}
void
UnixSocketConsumer::GetSocketAddr(struct sockaddr& aAddr, socklen_t &aAddrSize)
{
if (!mImpl) {
NS_WARNING("No socket currently open!");
return;
}
mImpl->GetSocketAddr(aAddr, aAddrSize);
}
void
UnixSocketConsumer::NotifySuccess()
{

View File

@ -212,6 +212,12 @@ public:
* Called by implementation to notify consumer of disconnect.
*/
void NotifyDisconnect();
/**
* Get the current sockaddr for the socket
*/
void GetSocketAddr(struct sockaddr& aAddr, socklen_t& aAddrSize);
private:
UnixSocketImpl* mImpl;
SocketConnectionStatus mConnectionStatus;