Bug 800249 - Patch 2: Add ability to retreive connected socket address as a string; r=cjones
authorKyle Machulis <kyle@nonpolynomial.com>
Wed, 17 Oct 2012 17:11:05 -0700
changeset 110754 3b0b4ccb241f77772938e5a96885eb28aca01201
parent 110753 054604aa00bd159fcd06a54efdaafef35ec40e79
child 110755 4686dc4138ba97ce3e138c73b6c05bc541c3bd3b
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewerscjones
bugs800249
milestone19.0a1
Bug 800249 - Patch 2: Add ability to retreive connected socket address as a string; r=cjones
dom/bluetooth/BluetoothUnixSocketConnector.cpp
dom/bluetooth/BluetoothUnixSocketConnector.h
ipc/unixsocket/UnixSocket.cpp
ipc/unixsocket/UnixSocket.h
--- a/dom/bluetooth/BluetoothUnixSocketConnector.cpp
+++ b/dom/bluetooth/BluetoothUnixSocketConnector.cpp
@@ -55,16 +55,23 @@ int get_bdaddr(const char *str, bdaddr_t
   for (int i = 0; i < 6; i++) {
     *d-- = strtol(str, &endp, 16);
     MOZ_ASSERT(!(*endp != ':' && i != 5));
     str = endp + 1;
   }
   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,
   bool aAuth,
   bool aEncrypt) : mType(aType)
                  , mChannel(aChannel)
                  , mAuth(aAuth)
                  , mEncrypt(aEncrypt)
@@ -174,8 +181,16 @@ BluetoothUnixSocketConnector::CreateAddr
     memcpy(&addr_sco.sco_bdaddr, &bd_address_obj, sizeof(bdaddr_t));
     memcpy(aAddr, &addr_sco, sizeof(addr_sco));
     break;
   default:
     NS_WARNING("Socket type unknown!");
   }
 }
 
+void
+BluetoothUnixSocketConnector::GetSocketAddr(const sockaddr& aAddr,
+                                            nsAString& aAddrStr)
+{
+  char addr[18];
+  get_bdaddr_as_string((bdaddr_t*)&aAddr, addr);
+  aAddrStr.AssignASCII(addr);
+}
--- a/dom/bluetooth/BluetoothUnixSocketConnector.h
+++ b/dom/bluetooth/BluetoothUnixSocketConnector.h
@@ -21,16 +21,19 @@ public:
   virtual ~BluetoothUnixSocketConnector()
   {}
   virtual int Create() MOZ_OVERRIDE;
   virtual void CreateAddr(bool aIsServer,
                           socklen_t& aAddrSize,
                           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;
   bool mAuth;
   bool mEncrypt;
 };
 
 END_BLUETOOTH_NAMESPACE
--- a/ipc/unixsocket/UnixSocket.cpp
+++ b/ipc/unixsocket/UnixSocket.cpp
@@ -146,20 +146,25 @@ public:
 
   /** 
    * Set up nonblocking flags on whatever our current file descriptor is.
    *
    * @return true if successful, false otherwise
    */
   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);
   }
 
   /**
    * Consumer pointer. Non-thread safe RefPtr, so should only be manipulated
    * directly from main thread. All non-main-thread accesses should happen with
    * mImpl as container.
    */
   RefPtr<UnixSocketConsumer> mConsumer;
@@ -237,17 +242,17 @@ private:
   /**
    * Size of the socket address struct
    */
   socklen_t mAddrSize;
 
   /**
    * Address struct of the socket currently in use
    */
-  struct sockaddr mAddr;
+  sockaddr mAddr;
 
 };
 
 static void
 DestroyImpl(UnixSocketImpl* impl)
 {
   MOZ_ASSERT(impl);
   delete impl;
@@ -702,23 +707,24 @@ UnixSocketImpl::OnFileCanWriteWithoutBlo
       return;
     }
     mOutgoingQ.RemoveElementAt(0);
     delete data;
   }
 }
 
 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
 UnixSocketConsumer::NotifySuccess()
 {
   MOZ_ASSERT(NS_IsMainThread());
   mConnectionStatus = SOCKET_CONNECTED;
   OnConnectSuccess();
--- a/ipc/unixsocket/UnixSocket.h
+++ b/ipc/unixsocket/UnixSocket.h
@@ -96,16 +96,27 @@ public:
   /** 
    * Does any socket type specific setup that may be needed
    *
    * @param aFd File descriptor for opened socket
    *
    * @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 {
   SOCKET_DISCONNECTED = 0,
   SOCKET_CONNECTING = 1,
   SOCKET_CONNECTED = 2
 };
 
@@ -211,18 +222,18 @@ 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);
-  
+  void GetSocketAddr(nsAString& aAddrStr);
+
 private:
   UnixSocketImpl* mImpl;
   SocketConnectionStatus mConnectionStatus;
 };
 
 } // namespace ipc
 } // namepsace mozilla