Bug 720846 Scriptable PRNetAddr and scriptable access to Self/Peer address in SocketTransport

Bug 720846 Scriptable PRNetAddr and scriptable access to Self/Peer address in SocketTransport
This commit is contained in:
Derrick Rice 2012-02-29 06:19:00 -05:00
parent beab7f108b
commit 48b237aaa3
9 changed files with 552 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,157 @@
/* 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;
#if defined(XP_UNIX) || defined(XP_OS2)
case PR_AF_LOCAL:
aAddress.Assign(mAddr.local.path);
break;
#endif
// PR_AF_LOCAL falls through to default when not XP_UNIX || XP_OS2
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,186 @@
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");
/**
* TestServer: A single instance of this is created as |serv|. When created,
* it starts listening on the loopback address on port |serv.port|. Tests will
* connect to it after setting |serv.acceptCallback|, which is invoked after it
* accepts a connection.
*
* Within |serv.acceptCallback|, various properties of |serv| can be used to
* run checks. After the callback, the connection is closed, but the server
* remains listening until |serv.stop|
*
* Note: TestServer can only handle a single connection at a time. Tests
* should use run_next_test at the end of |serv.acceptCallback| to start the
* following test which creates a connection.
*/
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;
do_print('server: listening on', this.port);
this.listener.asyncListen(this);
}
TestServer.prototype = {
onSocketAccepted: function(socket, trans) {
do_print('server: got client connection');
// one connection at a time.
if (this.input !== null) {
try { socket.close(); } catch(ignore) {}
do_throw("Test written to handle one connection at a time.");
}
try {
this.input = trans.openInputStream(0, 0, 0);
this.output = trans.openOutputStream(0, 0, 0);
this.selfAddr = trans.getScriptableSelfAddr();
this.peerAddr = trans.getScriptablePeerAddr();
this.acceptCallback();
} catch(e) {
/* In a native callback such as onSocketAccepted, exceptions might not
* get output correctly or logged to test output. Send them through
* do_throw, which fails the test immediately. */
do_report_unexpected_exception(e, "in TestServer.onSocketAccepted");
}
this.reset();
} ,
onStopListening: function(socket) {} ,
/**
* Called to close a connection and clean up properties.
*/
reset: function() {
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;
} ,
/**
* Cleanup for TestServer and this test case.
*/
stop: function() {
this.reset();
try { this.listener.close(); } catch(ignore) {}
}
};
/**
* Helper function.
* Compares two nsINetAddr objects and ensures they are logically equivalent.
*/
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);
}
/* TODO: fully support ipv6 and local */
}
/**
* An instance of SocketTransportService, used to create connections.
*/
var sts;
/**
* Single instance of TestServer
*/
var serv;
/**
* Connections have 5 seconds to be made, or a timeout function fails this
* test. This prevents the test from hanging and bringing down the entire
* xpcshell test chain.
*/
var connectTimeout = 5*1000;
/**
* IPv4 test.
*/
function testIpv4() {
var transport;
serv.acceptCallback = function() {
// disable the timeoutCallback
serv.timeoutCallback = 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);
};
// Useful timeout for debugging test hangs
/*serv.timeoutCallback = function(tname) {
if (tname === 'testIpv4')
do_throw('testIpv4 never completed a connection to TestServ');
};
do_timeout(connectTimeout, function(){ serv.timeoutCallback('testIpv4'); });*/
transport = sts.createTransport(null, 0, '127.0.0.1', serv.port, null);
transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING,0,0);
/* NEXT:
* openOutputStream -> onSocketAccepted -> acceptedCallback -> run_next_test
* OR
* <connectTimeout lapses> -> timeoutCallback -> do_throw
*/
}
/**
* Running the tests.
*/
function run_test() {
sts = Cc["@mozilla.org/network/socket-transport-service;1"]
.getService(Ci.nsISocketTransportService);
serv = new TestServer();
do_register_cleanup(function(){ serv.stop(); });
add_test(testIpv4);
/* TODO: testIpv6 */
/* TODO: testLocal */
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]