Bug 720846 Scriptable PRNetAddr and scriptable access to Self/Peer address in SocketTransport. r=biesi sr=bz

This commit is contained in:
Derrick Rice 2012-02-28 05:34:00 -05:00
parent d9f9fced62
commit 3d5a2ca05e
9 changed files with 532 additions and 1 deletions

View File

@ -86,6 +86,7 @@ XPIDLSRCS = \
nsIIOService2.idl \
nsIIPCSerializable.idl \
nsIMIMEInputStream.idl \
nsINetAddr.idl \
nsINetworkLinkService.idl \
nsIPermission.idl \
nsIPermissionManager.idl \

View File

@ -0,0 +1,100 @@
/* vim: et ts=4 sw=4 tw=80
*/
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code
*
* The Initial Developer of the Original Code is
* Derrick Rice <derrick.rice@gmail.com>
*
* Contributor(s):
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
native PRNetAddr(union PRNetAddr);
/**
* nsINetAddr
*
* This interface represents a (native) PRNetAddr struct in a readonly
* interface.
*/
[scriptable, uuid(c407ab6c-c3ca-4cb2-a99b-a7dfbb88af33)]
interface nsINetAddr : nsISupports
{
/**
* @return the address family of the network address, which corresponds to
* one of the FAMILY_ constants.
*/
readonly attribute unsigned short family;
/**
* @return Either the IP address (FAMILY_INET, FAMILY_INET6) or the path
* (FAMILY_LOCAL) in string form. IP addresses are in the format produced by
* PR_NetAddrToString.
*
* Note: Paths for FAMILY_LOCAL may have length limitations which are
* implementation dependent and not documented as part of this interface.
*/
readonly attribute AUTF8String address;
/**
* @return the port number for a FAMILY_INET or FAMILY_INET6 address.
*
* @throws NS_ERROR_NOT_AVAILABLE if the address family is not FAMILY_INET or
* FAMILY_INET6.
*/
readonly attribute unsigned short port;
/**
* @return the flow label for a FAMILY_INET6 address.
*
* @see http://www.ietf.org/rfc/rfc3697.txt
*
* @throws NS_ERROR_NOT_AVAILABLE if the address family is not FAMILY_INET6
*/
readonly attribute unsigned long flow;
/**
* @return the address scope of a FAMILY_INET6 address.
*
* @see http://tools.ietf.org/html/rfc4007
*
* @throws NS_ERROR_NOT_AVAILABLE if the address family is not FAMILY_INET6
*/
readonly attribute unsigned long scope;
/**
* Network address families. These correspond to all the network address
* families supported by the PRNetAddr struct.
*/
const unsigned long FAMILY_INET = 1;
const unsigned long FAMILY_INET6 = 2;
const unsigned long FAMILY_LOCAL = 3;
};

View File

@ -36,6 +36,7 @@
* ***** END LICENSE BLOCK ***** */
#include "nsITransport.idl"
#include "nsINetAddr.idl"
interface nsIInterfaceRequestor;
@ -51,7 +52,7 @@ native PRNetAddr(union PRNetAddr);
* NOTE: This is a free-threaded interface, meaning that the methods on
* this interface may be called from any thread.
*/
[scriptable, uuid(19c37caa-fb41-4c32-bbf1-c6b31b75d789)]
[scriptable, uuid(704c0859-cb64-43bd-bfdf-c8b28427f812)]
interface nsISocketTransport : nsITransport
{
/**
@ -76,6 +77,18 @@ interface nsISocketTransport : nsITransport
*/
[noscript] PRNetAddr getSelfAddr();
/**
* Returns a scriptable version of getPeerAddr. This attribute is defined
* only once a connection has been established.
*/
nsINetAddr getScriptablePeerAddr();
/**
* Returns a scriptable version of getSelfAddr. This attribute is defined
* only once a connection has been established.
*/
nsINetAddr getScriptableSelfAddr();
/**
* Security info object returned from the secure socket provider. This
* object supports nsISSLSocketControl, nsITransportSecurityInfo, and

View File

@ -86,6 +86,7 @@ CPPSRCS = \
nsURIChecker.cpp \
nsURLHelper.cpp \
nsURLParsers.cpp \
nsNetAddr.cpp \
nsNetStrings.cpp \
nsBase64Encoder.cpp \
nsSerializationHelper.cpp \

View File

@ -0,0 +1,154 @@
/* vim: et ts=2 sw=2 tw=80
*/
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code
*
* The Initial Developer of the Original Code is
* Derrick Rice <derrick.rice@gmail.com>
*
* Contributor(s):
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsNetAddr.h"
#include "nsString.h"
#include "prnetdb.h"
NS_IMPL_ISUPPORTS1(nsNetAddr, nsINetAddr)
/* Makes a copy of |addr| */
nsNetAddr::nsNetAddr(PRNetAddr* addr)
{
NS_ASSERTION(addr, "null addr");
mAddr = *addr;
}
/* readonly attribute unsigned short family; */
NS_IMETHODIMP nsNetAddr::GetFamily(PRUint16 *aFamily)
{
switch(mAddr.raw.family) {
case PR_AF_INET:
*aFamily = nsINetAddr::FAMILY_INET;
break;
case PR_AF_INET6:
*aFamily = nsINetAddr::FAMILY_INET6;
break;
case PR_AF_LOCAL:
*aFamily = nsINetAddr::FAMILY_LOCAL;
break;
default:
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
/* readonly attribute AUTF8String address; */
NS_IMETHODIMP nsNetAddr::GetAddress(nsACString & aAddress)
{
switch(mAddr.raw.family) {
/* PR_NetAddrToString can handle INET and INET6, but not LOCAL. */
case PR_AF_INET:
if(!aAddress.SetCapacity(16))
return NS_ERROR_OUT_OF_MEMORY;
PR_NetAddrToString(&mAddr, aAddress.BeginWriting(), 16);
aAddress.SetLength(strlen(aAddress.BeginReading()));
break;
case PR_AF_INET6:
if(!aAddress.SetCapacity(46))
return NS_ERROR_OUT_OF_MEMORY;
PR_NetAddrToString(&mAddr, aAddress.BeginWriting(), 46);
aAddress.SetLength(strlen(aAddress.BeginReading()));
break;
case PR_AF_LOCAL:
aAddress.Assign(mAddr.local.path);
break;
default:
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
/* readonly attribute unsigned short port; */
NS_IMETHODIMP nsNetAddr::GetPort(PRUint16 *aPort)
{
switch(mAddr.raw.family) {
case PR_AF_INET:
*aPort = PR_ntohs(mAddr.inet.port);
break;
case PR_AF_INET6:
*aPort = PR_ntohs(mAddr.ipv6.port);
break;
case PR_AF_LOCAL:
// There is no port number for local / connections.
return NS_ERROR_NOT_AVAILABLE;
default:
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
/* readonly attribute unsigned long flow; */
NS_IMETHODIMP nsNetAddr::GetFlow(PRUint32 *aFlow)
{
switch(mAddr.raw.family) {
case PR_AF_INET6:
*aFlow = PR_ntohl(mAddr.ipv6.flowinfo);
break;
case PR_AF_INET:
case PR_AF_LOCAL:
// only for IPv6
return NS_ERROR_NOT_AVAILABLE;
default:
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
/* readonly attribute unsigned long scope; */
NS_IMETHODIMP nsNetAddr::GetScope(PRUint32 *aScope)
{
switch(mAddr.raw.family) {
case PR_AF_INET6:
*aScope = PR_ntohl(mAddr.ipv6.scope_id);
break;
case PR_AF_INET:
case PR_AF_LOCAL:
// only for IPv6
return NS_ERROR_NOT_AVAILABLE;
default:
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}

View File

@ -0,0 +1,59 @@
/* vim: et ts=2 sw=2 tw=80
*/
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code
*
* The Initial Developer of the Original Code is
* Derrick Rice <derrick.rice@gmail.com>
*
* Contributor(s):
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsNetAddr_h__
#define nsNetAddr_h__
#include "nsINetAddr.h"
#include "prio.h"
class nsNetAddr : public nsINetAddr
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSINETADDR
nsNetAddr(PRNetAddr* addr);
private:
PRNetAddr mAddr;
protected:
/* additional members */
};
#endif // !nsNetAddr_h__

View File

@ -47,6 +47,7 @@
#include "nsIOService.h"
#include "nsStreamUtils.h"
#include "nsNetSegmentUtils.h"
#include "nsNetAddr.h"
#include "nsTransportUtils.h"
#include "nsProxyInfo.h"
#include "nsNetCID.h"
@ -1958,6 +1959,38 @@ nsSocketTransport::GetSelfAddr(PRNetAddr *addr)
return rv;
}
/* nsINetAddr getScriptablePeerAddr (); */
NS_IMETHODIMP
nsSocketTransport::GetScriptablePeerAddr(nsINetAddr * *addr NS_OUTPARAM)
{
PRNetAddr rawAddr;
nsresult rv;
rv = GetPeerAddr(&rawAddr);
if (NS_FAILED(rv))
return rv;
NS_ADDREF(*addr = new nsNetAddr(&rawAddr));
return NS_OK;
}
/* nsINetAddr getScriptableSelfAddr (); */
NS_IMETHODIMP
nsSocketTransport::GetScriptableSelfAddr(nsINetAddr * *addr NS_OUTPARAM)
{
PRNetAddr rawAddr;
nsresult rv;
rv = GetSelfAddr(&rawAddr);
if (NS_FAILED(rv))
return rv;
NS_ADDREF(*addr = new nsNetAddr(&rawAddr));
return NS_OK;
}
NS_IMETHODIMP
nsSocketTransport::GetTimeout(PRUint32 type, PRUint32 *value)
{

View File

@ -0,0 +1,169 @@
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
const CC = Components.Constructor;
const ServerSocket = CC("@mozilla.org/network/server-socket;1",
"nsIServerSocket",
"init");
function TestServer() {
this.reset();
// start server.
// any port (-1), loopback only (true), default backlog (-1)
this.listener = ServerSocket(-1, true, -1);
this.port = this.listener.port;
print('server: listening on', this.port);
this.listener.asyncListen(this);
}
TestServer.prototype = {
onSocketAccepted: function(socket, trans) {
print('server: got client connection');
// one connection at a time.
if (this.input !== null) {
do_throw("Test written to handle one connection at a time");
socket.close();
return;
}
this.input = trans.openInputStream(0, 0, 0);
this.output = trans.openOutputStream(0, 0, 0);
this.selfAddr = trans.getScriptableSelfAddr();
this.peerAddr = trans.getScriptablePeerAddr();
this.acceptCallback();
this.reset();
} ,
onStopListening: function(socket) {} ,
reset: function() {
print('server: reset');
if (this.input)
try { this.input.close(); } catch(ignore) {}
if (this.output)
try { this.output.close(); } catch(ignore) {}
this.input = null;
this.output = null;
this.acceptCallback = null;
this.selfAddr = null;
this.peerAddr = null;
} ,
stop: function() {
this.reset();
try { this.listener.close(); } catch(ignore) {}
}
};
var sts, serv;
function checkAddrEqual(lhs, rhs) {
do_check_eq(lhs.family, rhs.family);
if (lhs.family === Ci.nsINetAddr.FAMILY_INET) {
do_check_eq(lhs.address, rhs.address);
do_check_eq(lhs.port, rhs.port);
} else if (lhs.family === Ci.nsINetAddr.FAMILY_IPV6) {
do_check_eq(lhs.address, rhs.address);
do_check_eq(lhs.port, rhs.port);
do_check_eq(lhs.flow, rhs.flow);
do_check_eq(lhs.scope, rhs.scope);
}
}
function testIpv4() {
var transport;
serv.acceptCallback = function() {
var selfAddr = transport.getScriptableSelfAddr();
var peerAddr = transport.getScriptablePeerAddr();
// check peerAddr against expected values
do_check_eq(peerAddr.family, Ci.nsINetAddr.FAMILY_INET);
do_check_eq(peerAddr.port, transport.port);
do_check_eq(peerAddr.port, serv.port);
do_check_eq(peerAddr.address, "127.0.0.1");
// check selfAddr against expected values
do_check_eq(selfAddr.family, Ci.nsINetAddr.FAMILY_INET);
do_check_eq(selfAddr.address, "127.0.0.1");
// check that selfAddr = server.peerAddr and vice versa.
checkAddrEqual(selfAddr, serv.peerAddr);
checkAddrEqual(peerAddr, serv.selfAddr);
do_execute_soon(run_next_test);
};
transport = sts.createTransport(null, 0, '127.0.0.1', serv.port, null);
transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING,0,0);
}
function testIpv6() {
var transport;
serv.acceptCallback = function() {
var selfAddr = transport.getScriptableSelfAddr();
var peerAddr = transport.getScriptablePeerAddr();
// check peerAddr against expected values
do_check_eq(peerAddr.family, Ci.nsINetAddr.FAMILY_INET6);
do_check_eq(peerAddr.port, transport.port);
do_check_eq(peerAddr.port, serv.port);
// peerAddr is specifically requested as ::1 when connecting.
do_check_eq(peerAddr.address, "::1");
// check selfAddr against expected values
do_check_eq(selfAddr.family, Ci.nsINetAddr.FAMILY_INET6);
/* XXX: There are differences in the results here depending on whether the
* environment prefers to interpret ipv6 addresses as compatible ipv4
* address.
*
* do_check_true(selfAddr.address === "::1");
* - selfAddr.address might be "::ffff:127.0.0.1"
*
* checkAddrEqual(peerAddr, serv.selfAddr);
* - serv.selfAddr.family may be FAMILY_INET
* - serv.selfAddr.address might be any of:
* INET: "127.0.0.1"
* INET6: "::f", "::ffff:127.0.0.1"
* - the ports should match.
*
* checkAddrEqual(selfAddr, serv.peerAddr);
* - serv.peerAddr.family might be FAMILY_INET
* - serv.peerAddr.address might be any of:
* INET: "127.0.0.1"
* INET6: "::f", "::ffff:127.0.0.1"
* - the ports should match.
*/
do_check_eq(peerAddr.port, serv.selfAddr.port);
do_check_eq(selfAddr.port, serv.peerAddr.port);
do_execute_soon(run_next_test);
};
transport = sts.createTransport(null, 0, '::1', serv.port, null);
transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING,0,0);
}
function run_test() {
sts = Cc["@mozilla.org/network/socket-transport-service;1"]
.getService(Ci.nsISocketTransportService);
serv = new TestServer();
transport = null;
add_test(testIpv4);
add_test(testIpv6);
// XXX: no testing for local / unix
run_next_test();
}

View File

@ -135,6 +135,7 @@ skip-if = os == "android"
[test_multipart_streamconv.js]
[test_multipart_streamconv_missing_lead_boundary.js]
[test_nestedabout_serialize.js]
[test_net_addr.js]
[test_nojsredir.js]
[test_offline_status.js]
[test_parse_content_type.js]