Bug 1296464 - Part 1 - Add init2 method to nsIUDPSocket to allow opening sockets on specific IPs. r=hurley
authorKannan Vijayan <kvijayan@mozilla.com>
Tue, 13 Sep 2016 11:16:22 -0400
changeset 354951 f2a771eb073aff5c9b0dd63c48f27fabab280417
parent 354950 682489fca27b508ecd41dd27815cf317604d7a46
child 354952 16e3043a7ab7332a83ae8c392a2c170aacb8efba
push id6570
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:26:13 +0000
treeherdermozilla-beta@f455459b2ae5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershurley
bugs1296464
milestone51.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1296464 - Part 1 - Add init2 method to nsIUDPSocket to allow opening sockets on specific IPs. r=hurley
netwerk/base/nsIUDPSocket.idl
netwerk/base/nsUDPSocket.cpp
--- a/netwerk/base/nsIUDPSocket.idl
+++ b/netwerk/base/nsIUDPSocket.idl
@@ -52,16 +52,21 @@ interface nsIUDPSocket : nsISupports
      *        If true, the socket is allowed to be bound to an address that is
      *        already in use. Default is true.
      */
     [optional_argc] void init(in long aPort,
                               in boolean aLoopbackOnly,
                               in nsIPrincipal aPrincipal,
                               [optional] in boolean aAddressReuse);
 
+    [optional_argc] void init2(in AUTF8String aAddr,
+                               in long aPort,
+                               in nsIPrincipal aPrincipal,
+                               [optional] in boolean aAddressReuse);
+
     /**
      * initWithAddress
      *
      * This method initializes a UDP socket, and binds it to a particular
      * local address (and hence a particular local network interface).
      *
      * @param aAddr
      *        The address to which this UDP socket should be bound.
--- a/netwerk/base/nsUDPSocket.cpp
+++ b/netwerk/base/nsUDPSocket.cpp
@@ -568,16 +568,41 @@ nsUDPSocket::Init(int32_t aPort, bool aL
     addr.inet.ip = htonl(INADDR_LOOPBACK);
   else
     addr.inet.ip = htonl(INADDR_ANY);
 
   return InitWithAddress(&addr, aPrincipal, aAddressReuse, aOptionalArgc);
 }
 
 NS_IMETHODIMP
+nsUDPSocket::Init2(const nsACString& aAddr, int32_t aPort, nsIPrincipal *aPrincipal,
+                   bool aAddressReuse, uint8_t aOptionalArgc)
+{
+  if (NS_WARN_IF(aAddr.IsEmpty())) {
+    return NS_ERROR_INVALID_ARG;
+  }
+
+  PRNetAddr prAddr;
+  if (PR_StringToNetAddr(aAddr.BeginReading(), &prAddr) != PR_SUCCESS) {
+    return NS_ERROR_FAILURE;
+  }
+
+  NetAddr addr;
+
+  if (aPort < 0)
+    aPort = 0;
+
+  addr.raw.family = AF_INET;
+  addr.inet.port = htons(aPort);
+  addr.inet.ip = prAddr.inet.ip;
+
+  return InitWithAddress(&addr, aPrincipal, aAddressReuse, aOptionalArgc);
+}
+
+NS_IMETHODIMP
 nsUDPSocket::InitWithAddress(const NetAddr *aAddr, nsIPrincipal *aPrincipal,
                              bool aAddressReuse, uint8_t aOptionalArgc)
 {
   NS_ENSURE_TRUE(mFD == nullptr, NS_ERROR_ALREADY_INITIALIZED);
 
   if (gIOService->IsNetTearingDown()) {
     return NS_ERROR_FAILURE;
   }