Bug 800249 - Patch 2: Add ability to retreive connected socket address as a string. r=cjones, a=blocking-basecamp
authorKyle Machulis <kyle@nonpolynomial.com>
Wed, 17 Oct 2012 17:11:05 -0700
changeset 116771 435f85f6a54a55a0dbe63e6e55a7041e1e2b49c7
parent 116770 105181ba3578be7bcd7b9b4347aa7faa672552b0
child 116772 c31b9aa6d4ad763218ae754c89135ac48d5d179b
push id239
push userakeybl@mozilla.com
push dateThu, 03 Jan 2013 21:54:43 +0000
treeherdermozilla-release@3a7b66445659 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscjones, blocking-basecamp
bugs800249
milestone18.0a2
Bug 800249 - Patch 2: Add ability to retreive connected socket address as a string. r=cjones, a=blocking-basecamp
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