Back out fix for bug 807678 again, cset ec4e88c2c77a.

This commit is contained in:
Josh Aas 2012-12-20 17:39:30 -05:00
parent a9a22ee366
commit 9a4cf133f0
27 changed files with 362 additions and 777 deletions

View File

@ -6,10 +6,12 @@
#include "nsISupports.idl"
native PRNetAddr(union PRNetAddr);
/**
* nsINetAddr
*
* This interface represents a native NetAddr struct in a readonly
* This interface represents a (native) PRNetAddr struct in a readonly
* interface.
*/
[scriptable, uuid(c407ab6c-c3ca-4cb2-a99b-a7dfbb88af33)]
@ -24,7 +26,7 @@ interface nsINetAddr : nsISupports
/**
* @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
* mozilla::net::NetAddrToString.
* PR_NetAddrToString.
*
* Note: Paths for FAMILY_LOCAL may have length limitations which are
* implementation dependent and not documented as part of this interface.
@ -59,7 +61,7 @@ interface nsINetAddr : nsISupports
/**
* Network address families. These correspond to all the network address
* families supported by the NetAddr struct.
* families supported by the PRNetAddr struct.
*/
const unsigned long FAMILY_INET = 1;
const unsigned long FAMILY_INET6 = 2;

View File

@ -8,10 +8,7 @@
interface nsIInterfaceRequestor;
%{ C++
#include "mozilla/net/DNS.h"
%}
native NetAddr(mozilla::net::NetAddr);
native PRNetAddr(union PRNetAddr);
/**
* nsISocketTransport
@ -23,7 +20,7 @@ native NetAddr(mozilla::net::NetAddr);
* NOTE: This is a free-threaded interface, meaning that the methods on
* this interface may be called from any thread.
*/
[scriptable, uuid(A8318027-0DDC-4E60-A89B-D81AFE3B5020)]
[scriptable, uuid(91ca3523-fc52-4dea-b167-ef8f56473dde)]
interface nsISocketTransport : nsITransport
{
/**
@ -40,13 +37,13 @@ interface nsISocketTransport : nsITransport
* Returns the IP address of the socket connection peer. This
* attribute is defined only once a connection has been established.
*/
[noscript] NetAddr getPeerAddr();
[noscript] PRNetAddr getPeerAddr();
/**
* Returns the IP address of the initiating end. This attribute
* is defined only once a connection has been established.
*/
[noscript] NetAddr getSelfAddr();
[noscript] PRNetAddr getSelfAddr();
/**
* Returns a scriptable version of getPeerAddr. This attribute is defined

View File

@ -15,7 +15,6 @@
#include "nsJSUtils.h"
#include "prnetdb.h"
#include "nsITimer.h"
#include "mozilla/net/DNS.h"
namespace mozilla {
namespace net {
@ -314,7 +313,7 @@ PACErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
// timeout of 0 means the normal necko timeout strategy, otherwise the dns request
// will be canceled after aTimeout milliseconds
static
JSBool PACResolve(const nsCString &aHostName, NetAddr *aNetAddr,
JSBool PACResolve(const nsCString &aHostName, PRNetAddr *aNetAddr,
unsigned int aTimeout)
{
if (!sRunning) {
@ -327,7 +326,7 @@ JSBool PACResolve(const nsCString &aHostName, NetAddr *aNetAddr,
bool
ProxyAutoConfig::ResolveAddress(const nsCString &aHostName,
NetAddr *aNetAddr,
PRNetAddr *aNetAddr,
unsigned int aTimeout)
{
nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID);
@ -367,12 +366,12 @@ bool PACResolveToString(const nsCString &aHostName,
nsCString &aDottedDecimal,
unsigned int aTimeout)
{
NetAddr netAddr;
PRNetAddr netAddr;
if (!PACResolve(aHostName, &netAddr, aTimeout))
return false;
char dottedDecimal[128];
if (!NetAddrToString(&netAddr, dottedDecimal, sizeof(dottedDecimal)))
if (PR_NetAddrToString(&netAddr, dottedDecimal, sizeof(dottedDecimal)) != PR_SUCCESS)
return false;
aDottedDecimal.Assign(dottedDecimal);
@ -687,16 +686,14 @@ ProxyAutoConfig::Shutdown()
}
bool
ProxyAutoConfig::SrcAddress(const NetAddr *remoteAddress, nsCString &localAddress)
ProxyAutoConfig::SrcAddress(const PRNetAddr *remoteAddress, nsCString &localAddress)
{
PRFileDesc *fd;
fd = PR_OpenUDPSocket(remoteAddress->raw.family);
if (!fd)
return false;
PRNetAddr prRemoteAddress;
NetAddrToPRNetAddr(remoteAddress, &prRemoteAddress);
if (PR_Connect(fd, &prRemoteAddress, 0) != PR_SUCCESS) {
if (PR_Connect(fd, remoteAddress, 0) != PR_SUCCESS) {
PR_Close(fd);
return false;
}
@ -727,7 +724,7 @@ ProxyAutoConfig::MyIPAddressTryHost(const nsCString &hostName,
unsigned int timeout,
jsval *vp)
{
NetAddr remoteAddress;
PRNetAddr remoteAddress;
nsAutoCString localDottedDecimal;
JSContext *cx = mJSRuntime->Context();

View File

@ -12,7 +12,6 @@
#include "prio.h"
#include "nsITimer.h"
#include "nsAutoPtr.h"
#include "mozilla/net/DNS.h"
namespace mozilla { namespace net {
@ -39,7 +38,7 @@ public:
void GC();
bool MyIPAddress(jsval *vp);
bool ResolveAddress(const nsCString &aHostName,
NetAddr *aNetAddr, unsigned int aTimeout);
PRNetAddr *aNetAddr, unsigned int aTimeout);
/**
* Get the proxy string for the specified URI. The proxy string is
@ -85,7 +84,7 @@ private:
// used to compile the PAC file and setup the execution context
nsresult SetupJS();
bool SrcAddress(const NetAddr *remoteAddress, nsCString &localAddress);
bool SrcAddress(const PRNetAddr *remoteAddress, nsCString &localAddress);
bool MyIPAddressTryHost(const nsCString &hostName, unsigned int timeout,
jsval *vp);

View File

@ -6,14 +6,13 @@
#include "nsNetAddr.h"
#include "nsString.h"
#include "prnetdb.h"
using namespace mozilla::net;
#include "prnetdb.h"
NS_IMPL_ISUPPORTS1(nsNetAddr, nsINetAddr)
/* Makes a copy of |addr| */
nsNetAddr::nsNetAddr(NetAddr* addr)
nsNetAddr::nsNetAddr(PRNetAddr* addr)
{
NS_ASSERTION(addr, "null addr");
mAddr = *addr;
@ -23,17 +22,15 @@ nsNetAddr::nsNetAddr(NetAddr* addr)
NS_IMETHODIMP nsNetAddr::GetFamily(uint16_t *aFamily)
{
switch(mAddr.raw.family) {
case AF_INET:
case PR_AF_INET:
*aFamily = nsINetAddr::FAMILY_INET;
break;
case AF_INET6:
case PR_AF_INET6:
*aFamily = nsINetAddr::FAMILY_INET6;
break;
#if defined(XP_UNIX) || defined(XP_OS2)
case AF_LOCAL:
case PR_AF_LOCAL:
*aFamily = nsINetAddr::FAMILY_LOCAL;
break;
#endif
default:
return NS_ERROR_UNEXPECTED;
}
@ -46,18 +43,18 @@ NS_IMETHODIMP nsNetAddr::GetAddress(nsACString & aAddress)
{
switch(mAddr.raw.family) {
/* PR_NetAddrToString can handle INET and INET6, but not LOCAL. */
case AF_INET:
aAddress.SetCapacity(kIPv4CStrBufSize);
NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv4CStrBufSize);
case PR_AF_INET:
aAddress.SetCapacity(16);
PR_NetAddrToString(&mAddr, aAddress.BeginWriting(), 16);
aAddress.SetLength(strlen(aAddress.BeginReading()));
break;
case AF_INET6:
aAddress.SetCapacity(kIPv6CStrBufSize);
NetAddrToString(&mAddr, aAddress.BeginWriting(), kIPv6CStrBufSize);
case PR_AF_INET6:
aAddress.SetCapacity(46);
PR_NetAddrToString(&mAddr, aAddress.BeginWriting(), 46);
aAddress.SetLength(strlen(aAddress.BeginReading()));
break;
#if defined(XP_UNIX) || defined(XP_OS2)
case AF_LOCAL:
case PR_AF_LOCAL:
aAddress.Assign(mAddr.local.path);
break;
#endif
@ -73,17 +70,15 @@ NS_IMETHODIMP nsNetAddr::GetAddress(nsACString & aAddress)
NS_IMETHODIMP nsNetAddr::GetPort(uint16_t *aPort)
{
switch(mAddr.raw.family) {
case AF_INET:
*aPort = ntohs(mAddr.inet.port);
case PR_AF_INET:
*aPort = PR_ntohs(mAddr.inet.port);
break;
case AF_INET6:
*aPort = ntohs(mAddr.inet6.port);
case PR_AF_INET6:
*aPort = PR_ntohs(mAddr.ipv6.port);
break;
#if defined(XP_UNIX) || defined(XP_OS2)
case AF_LOCAL:
case PR_AF_LOCAL:
// There is no port number for local / connections.
return NS_ERROR_NOT_AVAILABLE;
#endif
default:
return NS_ERROR_UNEXPECTED;
}
@ -95,13 +90,11 @@ NS_IMETHODIMP nsNetAddr::GetPort(uint16_t *aPort)
NS_IMETHODIMP nsNetAddr::GetFlow(uint32_t *aFlow)
{
switch(mAddr.raw.family) {
case AF_INET6:
*aFlow = ntohl(mAddr.inet6.flowinfo);
case PR_AF_INET6:
*aFlow = PR_ntohl(mAddr.ipv6.flowinfo);
break;
case AF_INET:
#if defined(XP_UNIX) || defined(XP_OS2)
case AF_LOCAL:
#endif
case PR_AF_INET:
case PR_AF_LOCAL:
// only for IPv6
return NS_ERROR_NOT_AVAILABLE;
default:
@ -115,13 +108,11 @@ NS_IMETHODIMP nsNetAddr::GetFlow(uint32_t *aFlow)
NS_IMETHODIMP nsNetAddr::GetScope(uint32_t *aScope)
{
switch(mAddr.raw.family) {
case AF_INET6:
*aScope = ntohl(mAddr.inet6.scope_id);
case PR_AF_INET6:
*aScope = PR_ntohl(mAddr.ipv6.scope_id);
break;
case AF_INET:
#if defined(XP_UNIX) || defined(XP_OS2)
case AF_LOCAL:
#endif
case PR_AF_INET:
case PR_AF_LOCAL:
// only for IPv6
return NS_ERROR_NOT_AVAILABLE;
default:

View File

@ -8,7 +8,7 @@
#define nsNetAddr_h__
#include "nsINetAddr.h"
#include "mozilla/net/DNS.h"
#include "prio.h"
#include "mozilla/Attributes.h"
class nsNetAddr MOZ_FINAL : public nsINetAddr
@ -17,10 +17,10 @@ public:
NS_DECL_ISUPPORTS
NS_DECL_NSINETADDR
nsNetAddr(mozilla::net::NetAddr* addr);
nsNetAddr(PRNetAddr* addr);
private:
mozilla::net::NetAddr mAddr;
PRNetAddr mAddr;
protected:
/* additional members */

View File

@ -13,10 +13,8 @@
#include "prnetdb.h"
#include "prio.h"
#include "mozilla/Attributes.h"
#include "mozilla/net/DNS.h"
using namespace mozilla;
using namespace mozilla::net;
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
@ -169,11 +167,9 @@ nsServerSocket::OnSocketReady(PRFileDesc *fd, int16_t outFlags)
}
PRFileDesc *clientFD;
PRNetAddr prClientAddr;
NetAddr clientAddr;
PRNetAddr clientAddr;
clientFD = PR_Accept(mFD, &prClientAddr, PR_INTERVAL_NO_WAIT);
PRNetAddrToNetAddr(&prClientAddr, &clientAddr);
clientFD = PR_Accept(mFD, &clientAddr, PR_INTERVAL_NO_WAIT);
if (!clientFD)
{
NS_WARNING("PR_Accept failed");

View File

@ -39,7 +39,6 @@
#endif
using namespace mozilla;
using namespace mozilla::net;
//-----------------------------------------------------------------------------
@ -807,22 +806,22 @@ nsSocketTransport::Init(const char **types, uint32_t typeCount,
}
nsresult
nsSocketTransport::InitWithConnectedSocket(PRFileDesc *fd, const NetAddr *addr)
nsSocketTransport::InitWithConnectedSocket(PRFileDesc *fd, const PRNetAddr *addr)
{
NS_ASSERTION(!mFD, "already initialized");
char buf[kIPv6CStrBufSize];
NetAddrToString(addr, buf, sizeof(buf));
char buf[64];
PR_NetAddrToString(addr, buf, sizeof(buf));
mHost.Assign(buf);
uint16_t port;
if (addr->raw.family == AF_INET)
if (addr->raw.family == PR_AF_INET)
port = addr->inet.port;
else
port = addr->inet6.port;
mPort = ntohs(port);
port = addr->ipv6.port;
mPort = PR_ntohs(port);
memcpy(&mNetAddr, addr, sizeof(NetAddr));
memcpy(&mNetAddr, addr, sizeof(PRNetAddr));
mPollFlags = (PR_POLL_READ | PR_POLL_WRITE | PR_POLL_EXCEPT);
mPollTimeout = mTimeouts[TIMEOUT_READ_WRITE];
@ -908,9 +907,7 @@ nsSocketTransport::ResolveHost()
// we send it when it's created, rather than the empty address
// we send with the connect call.
mState = STATE_RESOLVING;
mNetAddr.raw.family = AF_INET;
mNetAddr.inet.port = htons(SocketPort());
mNetAddr.inet.ip = htonl(INADDR_ANY);
PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, SocketPort(), &mNetAddr);
return PostEvent(MSG_DNS_LOOKUP_COMPLETE, NS_OK, nullptr);
}
}
@ -1055,7 +1052,7 @@ nsSocketTransport::InitiateSocket()
nsresult rv;
if (gIOService->IsOffline() &&
!IsLoopBackAddress(&mNetAddr))
!PR_IsNetAddrType(&mNetAddr, PR_IpAddrLoopback))
return NS_ERROR_OFFLINE;
//
@ -1161,8 +1158,8 @@ nsSocketTransport::InitiateSocket()
#if defined(PR_LOGGING)
if (SOCKET_LOG_ENABLED()) {
char buf[kIPv6CStrBufSize];
NetAddrToString(&mNetAddr, buf, sizeof(buf));
char buf[64];
PR_NetAddrToString(&mNetAddr, buf, sizeof(buf));
SOCKET_LOG((" trying address: %s\n", buf));
}
#endif
@ -1170,9 +1167,7 @@ nsSocketTransport::InitiateSocket()
//
// Initiate the connect() to the host...
//
PRNetAddr prAddr;
NetAddrToPRNetAddr(&mNetAddr, &prAddr);
status = PR_Connect(fd, &prAddr, NS_SOCKET_CONNECT_TIMEOUT);
status = PR_Connect(fd, &mNetAddr, NS_SOCKET_CONNECT_TIMEOUT);
if (status == PR_SUCCESS) {
//
// we are connected!
@ -1684,7 +1679,7 @@ nsSocketTransport::IsLocal(bool *aIsLocal)
{
{
MutexAutoLock lock(mLock);
*aIsLocal = IsLoopBackAddress(&mNetAddr);
*aIsLocal = PR_IsNetAddrType(&mNetAddr, PR_IpAddrLoopback);
}
}
@ -1905,7 +1900,7 @@ nsSocketTransport::GetPort(int32_t *port)
}
NS_IMETHODIMP
nsSocketTransport::GetPeerAddr(NetAddr *addr)
nsSocketTransport::GetPeerAddr(PRNetAddr *addr)
{
// once we are in the connected state, mNetAddr will not change.
// so if we can verify that we are in the connected state, then
@ -1918,12 +1913,12 @@ nsSocketTransport::GetPeerAddr(NetAddr *addr)
return NS_ERROR_NOT_AVAILABLE;
}
memcpy(addr, &mNetAddr, sizeof(NetAddr));
memcpy(addr, &mNetAddr, sizeof(mNetAddr));
return NS_OK;
}
NS_IMETHODIMP
nsSocketTransport::GetSelfAddr(NetAddr *addr)
nsSocketTransport::GetSelfAddr(PRNetAddr *addr)
{
// we must not call any PR methods on our file descriptor
// while holding mLock since those methods might re-enter
@ -1935,14 +1930,11 @@ nsSocketTransport::GetSelfAddr(NetAddr *addr)
fd = GetFD_Locked();
}
if (!fd) {
if (!fd)
return NS_ERROR_NOT_CONNECTED;
}
PRNetAddr prAddr;
nsresult rv =
(PR_GetSockName(fd, &prAddr) == PR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
PRNetAddrToNetAddr(&prAddr, addr);
(PR_GetSockName(fd, addr) == PR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
{
MutexAutoLock lock(mLock);
@ -1956,7 +1948,7 @@ nsSocketTransport::GetSelfAddr(NetAddr *addr)
NS_IMETHODIMP
nsSocketTransport::GetScriptablePeerAddr(nsINetAddr * *addr)
{
NetAddr rawAddr;
PRNetAddr rawAddr;
nsresult rv;
rv = GetPeerAddr(&rawAddr);
@ -1972,7 +1964,7 @@ nsSocketTransport::GetScriptablePeerAddr(nsINetAddr * *addr)
NS_IMETHODIMP
nsSocketTransport::GetScriptableSelfAddr(nsINetAddr * *addr)
{
NetAddr rawAddr;
PRNetAddr rawAddr;
nsresult rv;
rv = GetSelfAddr(&rawAddr);

View File

@ -22,7 +22,6 @@
#include "nsIDNSRecord.h"
#include "nsICancelable.h"
#include "nsIClassInfo.h"
#include "mozilla/net/DNS.h"
class nsSocketTransport;
@ -122,7 +121,7 @@ public:
// this method instructs the socket transport to use an already connected
// socket with the given address.
nsresult InitWithConnectedSocket(PRFileDesc *socketFD,
const mozilla::net::NetAddr *addr);
const PRNetAddr *addr);
// nsASocketHandler methods:
void OnSocketReady(PRFileDesc *, int16_t outFlags);
@ -200,7 +199,7 @@ private:
// mNetAddr is valid from GetPeerAddr() once we have
// reached STATE_TRANSFERRING. It must not change after that.
mozilla::net::NetAddr mNetAddr;
PRNetAddr mNetAddr;
bool mNetAddrIsSet;
// socket methods (these can only be called on the socket thread):

View File

@ -1,205 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/net/DNS.h"
#include "mozilla/Assertions.h"
#include "mozilla/mozalloc.h"
#include <string.h>
#ifdef XP_WIN
#include "Ws2tcpip.h"
#endif
namespace mozilla {
namespace net {
const char *inet_ntop_internal(int af, const void *src, char *dst, socklen_t size)
{
#ifdef XP_WIN
if (af == AF_INET) {
struct sockaddr_in s;
memset(&s, 0, sizeof(s));
s.sin_family = AF_INET;
memcpy(&s.sin_addr, src, sizeof(struct in_addr));
int result = getnameinfo((struct sockaddr *)&s, sizeof(struct sockaddr_in),
dst, size, nullptr, 0, NI_NUMERICHOST);
if (result == 0) {
return dst;
}
}
else if (af == AF_INET6) {
struct sockaddr_in6 s;
memset(&s, 0, sizeof(s));
s.sin6_family = AF_INET6;
memcpy(&s.sin6_addr, src, sizeof(struct in_addr6));
int result = getnameinfo((struct sockaddr *)&s, sizeof(struct sockaddr_in6),
dst, size, nullptr, 0, NI_NUMERICHOST);
if (result == 0) {
return dst;
}
}
return nullptr;
#else
return inet_ntop(af, src, dst, size);
#endif
}
// Copies the contents of a PRNetAddr to a NetAddr.
// Does not do a ptr safety check!
void PRNetAddrToNetAddr(const PRNetAddr *prAddr, NetAddr *addr)
{
if (prAddr->raw.family == PR_AF_INET) {
addr->inet.family = AF_INET;
addr->inet.port = prAddr->inet.port;
addr->inet.ip = prAddr->inet.ip;
}
else if (prAddr->raw.family == PR_AF_INET6) {
addr->inet6.family = AF_INET6;
addr->inet6.port = prAddr->ipv6.port;
addr->inet6.flowinfo = prAddr->ipv6.flowinfo;
memcpy(&addr->inet6.ip, &prAddr->ipv6.ip, sizeof(addr->inet6.ip.u8));
addr->inet6.scope_id = prAddr->ipv6.scope_id;
}
#if defined(XP_UNIX) || defined(XP_OS2)
else if (prAddr->raw.family == PR_AF_LOCAL) {
addr->local.family = AF_LOCAL;
memcpy(addr->local.path, prAddr->local.path, sizeof(addr->local.path));
}
#endif
}
// Copies the contents of a NetAddr to a PRNetAddr.
// Does not do a ptr safety check!
void NetAddrToPRNetAddr(const NetAddr *addr, PRNetAddr *prAddr)
{
if (addr->raw.family == AF_INET) {
prAddr->inet.family = PR_AF_INET;
prAddr->inet.port = addr->inet.port;
prAddr->inet.ip = addr->inet.ip;
}
else if (addr->raw.family == AF_INET6) {
prAddr->ipv6.family = PR_AF_INET6;
prAddr->ipv6.port = addr->inet6.port;
prAddr->ipv6.flowinfo = addr->inet6.flowinfo;
memcpy(&prAddr->ipv6.ip, &addr->inet6.ip, sizeof(addr->inet6.ip.u8));
prAddr->ipv6.scope_id = addr->inet6.scope_id;
}
#if defined(XP_UNIX) || defined(XP_OS2)
else if (addr->raw.family == AF_LOCAL) {
prAddr->local.family = PR_AF_LOCAL;
memcpy(prAddr->local.path, addr->local.path, sizeof(addr->local.path));
}
#endif
}
bool NetAddrToString(const NetAddr *addr, char *buf, uint32_t bufSize)
{
if (addr->raw.family == AF_INET) {
if (bufSize < INET_ADDRSTRLEN) {
return false;
}
struct in_addr nativeAddr = {};
nativeAddr.s_addr = addr->inet.ip;
return !!inet_ntop_internal(AF_INET, &nativeAddr, buf, bufSize);
}
else if (addr->raw.family == AF_INET6) {
if (bufSize < INET6_ADDRSTRLEN) {
return false;
}
struct in6_addr nativeAddr = {};
memcpy(&nativeAddr.s6_addr, &addr->inet6.ip, sizeof(addr->inet6.ip.u8));
return !!inet_ntop_internal(AF_INET6, &nativeAddr, buf, bufSize);
}
#if defined(XP_UNIX) || defined(XP_OS2)
else if (addr->raw.family == AF_LOCAL) {
if (bufSize < sizeof(addr->local.path)) {
return false;
}
memcpy(buf, addr->local.path, bufSize);
return true;
}
#endif
return false;
}
bool IsLoopBackAddress(const NetAddr *addr)
{
if (addr->raw.family == AF_INET) {
return (addr->inet.ip == htonl(INADDR_LOOPBACK));
}
else if (addr->raw.family == AF_INET6) {
if (IPv6ADDR_IS_LOOPBACK(&addr->inet6.ip)) {
return true;
} else if (IPv6ADDR_IS_V4MAPPED(&addr->inet6.ip) &&
IPv6ADDR_V4MAPPED_TO_IPADDR(&addr->inet6.ip) == htonl(INADDR_LOOPBACK)) {
return true;
}
}
return false;
}
bool IsIPAddrAny(const NetAddr *addr)
{
if (addr->raw.family == AF_INET) {
if (addr->inet.ip == htonl(INADDR_ANY)) {
return true;
}
}
else if (addr->raw.family == AF_INET6) {
if (IPv6ADDR_IS_UNSPECIFIED(&addr->inet6.ip)) {
return true;
} else if (IPv6ADDR_IS_V4MAPPED(&addr->inet6.ip) &&
IPv6ADDR_V4MAPPED_TO_IPADDR(&addr->inet6.ip) == htonl(INADDR_ANY)) {
return true;
}
}
return false;
}
bool IsIPAddrV4Mapped(const NetAddr *addr)
{
if (addr->raw.family == AF_INET6) {
return IPv6ADDR_IS_V4MAPPED(&addr->inet6.ip);
}
return false;
}
NetAddrElement::NetAddrElement(const PRNetAddr *prNetAddr)
{
PRNetAddrToNetAddr(prNetAddr, &mAddress);
}
NetAddrElement::~NetAddrElement()
{
}
AddrInfo::AddrInfo(const char *host, const PRAddrInfo *prAddrInfo)
{
size_t hostlen = strlen(host);
mHostName = static_cast<char*>(moz_xmalloc(hostlen + 1));
memcpy(mHostName, host, hostlen + 1);
PRNetAddr tmpAddr;
void *iter = nullptr;
do {
iter = PR_EnumerateAddrInfo(iter, prAddrInfo, 0, &tmpAddr);
if (iter) {
NetAddrElement *addrElement = new NetAddrElement(&tmpAddr);
mAddresses.insertBack(addrElement);
}
} while (iter);
}
AddrInfo::~AddrInfo()
{
NetAddrElement *addrElement;
while ((addrElement = mAddresses.popLast())) {
delete addrElement;
}
free(mHostName);
}
} // namespace dns
} // namespace mozilla

View File

@ -1,141 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef DNS_h_
#define DNS_h_
#include "nscore.h"
#include "prio.h"
#include "prnetdb.h"
#include "mozilla/LinkedList.h"
#if !defined(XP_WIN) && !defined(XP_OS2)
#include <arpa/inet.h>
#endif
#ifdef XP_WIN
#include "Winsock2.h"
#endif
#define IPv6ADDR_IS_LOOPBACK(a) \
(((a)->u32[0] == 0) && \
((a)->u32[1] == 0) && \
((a)->u32[2] == 0) && \
((a)->u8[12] == 0) && \
((a)->u8[13] == 0) && \
((a)->u8[14] == 0) && \
((a)->u8[15] == 0x1U))
#define IPv6ADDR_IS_V4MAPPED(a) \
(((a)->u32[0] == 0) && \
((a)->u32[1] == 0) && \
((a)->u8[8] == 0) && \
((a)->u8[9] == 0) && \
((a)->u8[10] == 0xff) && \
((a)->u8[11] == 0xff))
#define IPv6ADDR_V4MAPPED_TO_IPADDR(a) ((a)->u32[3])
#define IPv6ADDR_IS_UNSPECIFIED(a) \
(((a)->u32[0] == 0) && \
((a)->u32[1] == 0) && \
((a)->u32[2] == 0) && \
((a)->u32[3] == 0))
namespace mozilla {
namespace net {
// Required buffer size for text form of an IP address.
// Includes space for null termination.
const int kIPv4CStrBufSize = 16;
const int kIPv6CStrBufSize = 46;
// This was all created at a time in which we were using NSPR for host
// resolution and we were propagating NSPR types like "PRAddrInfo" and
// "PRNetAddr" all over Gecko. This made it hard to use another host
// resolver -- we were locked into NSPR. The goal here is to get away
// from that. We'll translate what we get from NSPR or any other host
// resolution library into the types below and use them in Gecko.
union IPv6Addr {
uint8_t u8[16];
uint16_t u16[8];
uint32_t u32[4];
uint64_t u64[2];
};
// This struct is similar to operating system structs like "sockaddr", used for
// things like "connect" and "getsockname". When tempted to cast or do dumb
// copies of this struct to another struct, bear compiler-computed padding
// in mind. The size of this struct, and the layout of the data in it, may
// not be what you expect.
union NetAddr {
struct {
uint16_t family; /* address family (0x00ff maskable) */
char data[14]; /* raw address data */
} raw;
struct {
uint16_t family; /* address family (AF_INET) */
uint16_t port; /* port number */
uint32_t ip; /* The actual 32 bits of address */
} inet;
struct {
uint16_t family; /* address family (AF_INET6) */
uint16_t port; /* port number */
uint32_t flowinfo; /* routing information */
IPv6Addr ip; /* the actual 128 bits of address */
uint32_t scope_id; /* set of interfaces for a scope */
} inet6;
#if defined(XP_UNIX) || defined(XP_OS2)
struct { /* Unix domain socket address */
uint16_t family; /* address family (AF_UNIX) */
#ifdef XP_OS2
char path[108]; /* null-terminated pathname */
#else
char path[104]; /* null-terminated pathname */
#endif
} local;
#endif
};
// This class wraps a NetAddr union to provide C++ linked list
// capabilities and other methods. It is created from a PRNetAddr,
// which is converted to a mozilla::dns::NetAddr.
class NetAddrElement : public LinkedListElement<NetAddrElement> {
public:
NetAddrElement(const PRNetAddr *prNetAddr);
~NetAddrElement();
NetAddr mAddress;
};
class AddrInfo {
public:
AddrInfo(const char *host, const PRAddrInfo *prAddrInfo);
~AddrInfo();
char *mHostName;
LinkedList<NetAddrElement> mAddresses;
};
// Copies the contents of a PRNetAddr to a NetAddr.
// Does not do a ptr safety check!
void PRNetAddrToNetAddr(const PRNetAddr *prAddr, NetAddr *addr);
// Copies the contents of a NetAddr to a PRNetAddr.
// Does not do a ptr safety check!
void NetAddrToPRNetAddr(const NetAddr *addr, PRNetAddr *prAddr);
bool NetAddrToString(const NetAddr *addr, char *buf, uint32_t bufSize);
bool IsLoopBackAddress(const NetAddr *addr);
bool IsIPAddrAny(const NetAddr *addr);
bool IsIPAddrV4Mapped(const NetAddr *addr);
} // namespace net
} // namespace mozilla
#endif // DNS_h_

View File

@ -27,10 +27,9 @@ XPIDLSRCS = \
$(NULL)
CPPSRCS = \
DNS.cpp \
nsIDNService.cpp \
nsHostResolver.cpp \
nsDNSService2.cpp \
nsHostResolver.cpp \
nsEffectiveTLDService.cpp \
$(NULL)
@ -40,12 +39,6 @@ CSRCS = \
punycode.c \
$(NULL)
EXPORTS_NAMESPACES = mozilla/net
EXPORTS_mozilla/net = \
DNS.h \
$(null)
# we don't want the shared lib, but we want to force the creation of a
# static lib.
FORCE_STATIC_LIB = 1

View File

@ -30,7 +30,6 @@
#include "mozilla/Attributes.h"
using namespace mozilla;
using namespace mozilla::net;
static const char kPrefDnsCacheEntries[] = "network.dnsCacheEntries";
static const char kPrefDnsCacheExpiration[] = "network.dnsCacheExpiration";
@ -52,6 +51,7 @@ public:
nsDNSRecord(nsHostRecord *hostRecord)
: mHostRecord(hostRecord)
, mIter(nullptr)
, mLastIter(nullptr)
, mIterGenCnt(-1)
, mDone(false) {}
@ -59,7 +59,9 @@ private:
virtual ~nsDNSRecord() {}
nsRefPtr<nsHostRecord> mHostRecord;
NetAddrElement *mIter;
void *mIter; // enum ptr for PR_EnumerateAddrInfo
void *mLastIter; // previous enum ptr, for use in
// getting addrinfo in ReportUnusable
int mIterGenCnt; // the generation count of
// mHostRecord->addr_info when we
// start iterating
@ -81,7 +83,7 @@ nsDNSRecord::GetCanonicalName(nsACString &result)
{
MutexAutoLock lock(mHostRecord->addr_info_lock);
if (mHostRecord->addr_info)
cname = mHostRecord->addr_info->mHostName;
cname = PR_GetCanonNameFromAddrInfo(mHostRecord->addr_info);
else
cname = mHostRecord->host;
result.Assign(cname);
@ -90,45 +92,46 @@ nsDNSRecord::GetCanonicalName(nsACString &result)
}
NS_IMETHODIMP
nsDNSRecord::GetNextAddr(uint16_t port, NetAddr *addr)
nsDNSRecord::GetNextAddr(uint16_t port, PRNetAddr *addr)
{
if (mDone) {
// not a programming error to poke the DNS record when it has no more
// entries. just fail without any debug warnings. this enables consumers
// to enumerate the DNS record without calling HasMore.
if (mDone)
return NS_ERROR_NOT_AVAILABLE;
}
mHostRecord->addr_info_lock.Lock();
bool startedFresh = !mIter;
if (mHostRecord->addr_info) {
if (mIterGenCnt != mHostRecord->addr_info_gencnt) {
// mHostRecord->addr_info has changed, restart the iteration.
if (!mIter)
mIterGenCnt = mHostRecord->addr_info_gencnt;
else if (mIterGenCnt != mHostRecord->addr_info_gencnt) {
// mHostRecord->addr_info has changed, so mIter is invalid.
// Restart the iteration. Alternatively, we could just fail.
mIter = nullptr;
mIterGenCnt = mHostRecord->addr_info_gencnt;
startedFresh = true;
}
bool startedFresh = !mIter;
do {
if (!mIter) {
mIter = mHostRecord->addr_info->mAddresses.getFirst();
} else {
mIter = mIter->getNext();
}
mLastIter = mIter;
mIter = PR_EnumerateAddrInfo(mIter, mHostRecord->addr_info,
port, addr);
}
while (mIter && mHostRecord->Blacklisted(&mIter->mAddress));
while (mIter && mHostRecord->Blacklisted(addr));
if (!mIter && startedFresh) {
// If everything was blacklisted we want to reset the blacklist (and
if (startedFresh && !mIter) {
// if everything was blacklisted we want to reset the blacklist (and
// likely relearn it) and return the first address. That is better
// than nothing.
// than nothing
mHostRecord->ResetBlacklist();
mIter = mHostRecord->addr_info->mAddresses.getFirst();
mLastIter = nullptr;
mIter = PR_EnumerateAddrInfo(nullptr, mHostRecord->addr_info,
port, addr);
}
if (mIter) {
memcpy(addr, &mIter->mAddress, sizeof(NetAddr));
}
mHostRecord->addr_info_lock.Unlock();
if (!mIter) {
mDone = true;
return NS_ERROR_NOT_AVAILABLE;
@ -136,61 +139,57 @@ nsDNSRecord::GetNextAddr(uint16_t port, NetAddr *addr)
}
else {
mHostRecord->addr_info_lock.Unlock();
if (!mHostRecord->addr) {
// Both mHostRecord->addr_info and mHostRecord->addr are null.
// This can happen if mHostRecord->addr_info expired and the
// attempt to reresolve it failed.
return NS_ERROR_NOT_AVAILABLE;
}
memcpy(addr, mHostRecord->addr, sizeof(NetAddr));
mDone = true;
memcpy(addr, mHostRecord->addr, sizeof(PRNetAddr));
// set given port
port = PR_htons(port);
if (addr->raw.family == PR_AF_INET)
addr->inet.port = port;
else
addr->ipv6.port = port;
mDone = true; // no iterations
}
// set given port
port = htons(port);
if (addr->raw.family == AF_INET) {
addr->inet.port = port;
}
else if (addr->raw.family == AF_INET6) {
addr->inet6.port = port;
}
return NS_OK;
return NS_OK;
}
NS_IMETHODIMP
nsDNSRecord::GetNextAddrAsString(nsACString &result)
{
NetAddr addr;
PRNetAddr addr;
nsresult rv = GetNextAddr(0, &addr);
if (NS_FAILED(rv)) return rv;
char buf[kIPv6CStrBufSize];
if (NetAddrToString(&addr, buf, sizeof(buf))) {
char buf[64];
if (PR_NetAddrToString(&addr, buf, sizeof(buf)) == PR_SUCCESS) {
result.Assign(buf);
return NS_OK;
}
NS_ERROR("NetAddrToString failed unexpectedly");
NS_ERROR("PR_NetAddrToString failed unexpectedly");
return NS_ERROR_FAILURE; // conversion failed for some reason
}
NS_IMETHODIMP
nsDNSRecord::HasMore(bool *result)
{
if (mDone) {
if (mDone)
*result = false;
return NS_OK;
else {
// unfortunately, NSPR does not provide a way for us to determine if
// there is another address other than to simply get the next address.
void *iterCopy = mIter;
void *iterLastCopy = mLastIter;
PRNetAddr addr;
*result = NS_SUCCEEDED(GetNextAddr(0, &addr));
mIter = iterCopy; // backup iterator
mLastIter = iterLastCopy; // backup iterator
mDone = false;
}
NetAddrElement *iterCopy = mIter;
NetAddr addr;
*result = NS_SUCCEEDED(GetNextAddr(0, &addr));
mIter = iterCopy;
mDone = false;
return NS_OK;
}
@ -198,6 +197,7 @@ NS_IMETHODIMP
nsDNSRecord::Rewind()
{
mIter = nullptr;
mLastIter = nullptr;
mIterGenCnt = -1;
mDone = false;
return NS_OK;
@ -208,7 +208,7 @@ nsDNSRecord::ReportUnusable(uint16_t aPort)
{
// right now we don't use the port in the blacklist
MutexAutoLock lock(mHostRecord->addr_info_lock);
mHostRecord->addr_info_lock.Lock();
// Check that we are using a real addr_info (as opposed to a single
// constant address), and that the generation count is valid. Otherwise,
@ -216,9 +216,14 @@ nsDNSRecord::ReportUnusable(uint16_t aPort)
if (mHostRecord->addr_info &&
mIterGenCnt == mHostRecord->addr_info_gencnt) {
mHostRecord->ReportUnusable(&mIter->mAddress);
PRNetAddr addr;
void *id = PR_EnumerateAddrInfo(mLastIter, mHostRecord->addr_info,
aPort, &addr);
if (id)
mHostRecord->ReportUnusable(&addr);
}
mHostRecord->addr_info_lock.Unlock();
return NS_OK;
}

View File

@ -185,7 +185,7 @@ nsHostRecord::~nsHostRecord()
}
bool
nsHostRecord::Blacklisted(NetAddr *aQuery)
nsHostRecord::Blacklisted(PRNetAddr *aQuery)
{
// must call locked
LOG(("Checking blacklist for host [%s], host record [%p].\n", host, this));
@ -195,8 +195,8 @@ nsHostRecord::Blacklisted(NetAddr *aQuery)
return false;
}
char buf[kIPv6CStrBufSize];
if (!NetAddrToString(aQuery, buf, sizeof(buf))) {
char buf[64];
if (PR_NetAddrToString(aQuery, buf, sizeof(buf)) != PR_SUCCESS) {
return false;
}
nsDependentCString strQuery(buf);
@ -212,13 +212,13 @@ nsHostRecord::Blacklisted(NetAddr *aQuery)
}
void
nsHostRecord::ReportUnusable(NetAddr *aAddress)
nsHostRecord::ReportUnusable(PRNetAddr *aAddress)
{
// must call locked
LOG(("Adding address to blacklist for host [%s], host record [%p].\n", host, this));
char buf[kIPv6CStrBufSize];
if (NetAddrToString(aAddress, buf, sizeof(buf))) {
char buf[64];
if (PR_NetAddrToString(aAddress, buf, sizeof(buf)) == PR_SUCCESS) {
LOG(("Successfully adding address [%s] to blacklist for host [%s].\n", buf, host));
mBlacklistedItems.AppendElement(nsCString(buf));
}
@ -275,20 +275,21 @@ HostDB_ClearEntry(PLDHashTable *table,
nsHostDBEnt *he = static_cast<nsHostDBEnt *>(entry);
LOG(("Clearing cache db entry for host [%s].\n", he->rec->host));
#if defined(DEBUG) && defined(PR_LOGGING)
{
MutexAutoLock lock(he->rec->addr_info_lock);
if (!he->rec->addr_info) {
LOG(("No address info for host [%s].\n", he->rec->host));
} else {
int32_t now = (int32_t) NowInMinutes();
int32_t diff = (int32_t) he->rec->expiration - now;
LOG(("Record for [%s] expires in %d minute(s).\n", he->rec->host, diff));
NetAddrElement *addrElement;
char buf[kIPv6CStrBufSize];
while ((addrElement = he->rec->addr_info->mAddresses.popLast())) {
NetAddrToString(&addrElement->mAddress, buf, sizeof(buf));
LOG((" [%s]\n", buf));
}
if (!he->rec->addr_info) {
LOG(("No address info for host [%s].\n", he->rec->host));
} else {
int32_t now = (int32_t) NowInMinutes();
int32_t diff = (int32_t) he->rec->expiration - now;
LOG(("Record for [%s] expires in %d minute(s).\n", he->rec->host, diff));
void *iter = nullptr;
PRNetAddr addr;
char buf[64];
for (;;) {
iter = PR_EnumerateAddrInfo(iter, he->rec->addr_info, 0, &addr);
if (!iter)
break;
PR_NetAddrToString(&addr, buf, sizeof(buf));
LOG((" [%s]\n", buf));
}
}
#endif
@ -505,10 +506,10 @@ nsHostResolver::ResolveHost(const char *host,
if (mShutdown)
rv = NS_ERROR_NOT_INITIALIZED;
else {
// Used to try to parse to an IP address literal.
PRNetAddr tempAddr;
// Unfortunately, PR_StringToNetAddr does not properly initialize
// the output buffer in the case of IPv6 input. See bug 223145.
// unfortunately, PR_StringToNetAddr does not properly initialize
// the output buffer in the case of IPv6 input. see bug 223145.
memset(&tempAddr, 0, sizeof(PRNetAddr));
// check to see if there is already an entry for this |host|
@ -572,8 +573,11 @@ nsHostResolver::ResolveHost(const char *host,
else if (PR_StringToNetAddr(host, &tempAddr) == PR_SUCCESS) {
// ok, just copy the result into the host record, and be done
// with it! ;-)
he->rec->addr = new NetAddr();
PRNetAddrToNetAddr(&tempAddr, he->rec->addr);
he->rec->addr = (PRNetAddr *) malloc(sizeof(PRNetAddr));
if (!he->rec->addr)
status = NS_ERROR_OUT_OF_MEMORY;
else
memcpy(he->rec->addr, &tempAddr, sizeof(PRNetAddr));
// put reference to host record on stack...
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
METHOD_LITERAL);
@ -817,7 +821,7 @@ nsHostResolver::GetHostToLookup(nsHostRecord **result)
}
void
nsHostResolver::OnLookupComplete(nsHostRecord *rec, nsresult status, AddrInfo *result)
nsHostResolver::OnLookupComplete(nsHostRecord *rec, nsresult status, PRAddrInfo *result)
{
// get the list of pending callbacks for this lookup, and notify
// them that the lookup is complete.
@ -831,15 +835,15 @@ nsHostResolver::OnLookupComplete(nsHostRecord *rec, nsresult status, AddrInfo *r
// update record fields. We might have a rec->addr_info already if a
// previous lookup result expired and we're reresolving it..
AddrInfo *old_addr_info;
PRAddrInfo *old_addr_info;
{
MutexAutoLock lock(rec->addr_info_lock);
old_addr_info = rec->addr_info;
rec->addr_info = result;
rec->addr_info_gencnt++;
}
delete old_addr_info;
if (old_addr_info)
PR_FreeAddrInfo(old_addr_info);
rec->expiration = NowInMinutes();
if (result) {
rec->expiration += mMaxCacheLifetime;
@ -955,7 +959,7 @@ nsHostResolver::ThreadFunc(void *arg)
#endif
nsHostResolver *resolver = (nsHostResolver *)arg;
nsHostRecord *rec;
PRAddrInfo *prai = nullptr;
PRAddrInfo *ai;
while (resolver->GetHostToLookup(&rec)) {
LOG(("Calling getaddrinfo for host [%s].\n", rec->host));
@ -965,21 +969,18 @@ nsHostResolver::ThreadFunc(void *arg)
TimeStamp startTime = TimeStamp::Now();
prai = PR_GetAddrInfoByName(rec->host, rec->af, flags);
ai = PR_GetAddrInfoByName(rec->host, rec->af, flags);
#if defined(RES_RETRY_ON_FAILURE)
if (!prai && rs.Reset())
prai = PR_GetAddrInfoByName(rec->host, rec->af, flags);
if (!ai && rs.Reset())
ai = PR_GetAddrInfoByName(rec->host, rec->af, flags);
#endif
TimeDuration elapsed = TimeStamp::Now() - startTime;
uint32_t millis = static_cast<uint32_t>(elapsed.ToMilliseconds());
// convert error code to nsresult
// convert error code to nsresult.
nsresult status;
AddrInfo *ai = nullptr;
if (prai) {
ai = new AddrInfo(rec->host, prai);
if (ai) {
status = NS_OK;
Telemetry::Accumulate(!rec->addr_info_gencnt ?
@ -1032,44 +1033,44 @@ PLDHashOperator
CacheEntryEnumerator(PLDHashTable *table, PLDHashEntryHdr *entry,
uint32_t number, void *arg)
{
// We don't pay attention to address literals, only resolved domains.
// Also require a host.
nsHostRecord *rec = static_cast<nsHostDBEnt*>(entry)->rec;
if (!rec->addr_info || !rec->host) {
return PL_DHASH_NEXT;
}
nsHostDBEnt *ent = static_cast<nsHostDBEnt *> (entry);
nsTArray<DNSCacheEntries> *args =
static_cast<nsTArray<DNSCacheEntries> *> (arg);
nsHostRecord *rec = ent->rec;
// Without addr_info, there is no meaning of adding this entry
if (rec->addr_info) {
DNSCacheEntries info;
const char *hostname;
PRNetAddr addr;
if (rec->host)
hostname = rec->host;
else // No need to add this entry if no host name is there
return PL_DHASH_NEXT;
uint32_t now = NowInMinutes();
info.expiration = ((int64_t) rec->expiration - now) * 60;
DNSCacheEntries info;
info.hostname = rec->host;
info.family = rec->af;
info.expiration = ((int64_t)rec->expiration - NowInMinutes()) * 60;
if (info.expiration <= 0) {
// We only need valid DNS cache entries
return PL_DHASH_NEXT;
}
if (info.expiration <= 0)
return PL_DHASH_NEXT;
{
MutexAutoLock lock(rec->addr_info_lock);
info.family = rec->af;
info.hostname = hostname;
NetAddr *addr = nullptr;
NetAddrElement *addrElement = rec->addr_info->mAddresses.getFirst();
if (addrElement) {
addr = &addrElement->mAddress;
}
while (addr) {
char buf[kIPv6CStrBufSize];
if (NetAddrToString(addr, buf, sizeof(buf))) {
info.hostaddr.AppendElement(buf);
}
addrElement = addrElement->getNext();
if (addrElement) {
addr = &addrElement->mAddress;
{
MutexAutoLock lock(rec->addr_info_lock);
void *ptr = PR_EnumerateAddrInfo(nullptr, rec->addr_info, 0, &addr);
while (ptr) {
char buf[64];
if (PR_NetAddrToString(&addr, buf, sizeof(buf)) == PR_SUCCESS)
info.hostaddr.AppendElement(buf);
ptr = PR_EnumerateAddrInfo(ptr, rec->addr_info, 0, &addr);
}
}
}
nsTArray<DNSCacheEntries> *args = static_cast<nsTArray<DNSCacheEntries> *>(arg);
args->AppendElement(info);
args->AppendElement(info);
}
return PL_DHASH_NEXT;
}

View File

@ -17,7 +17,6 @@
#include "nsIDNSListener.h"
#include "nsString.h"
#include "nsTArray.h"
#include "mozilla/net/DNS.h"
#include "mozilla/net/DashboardTypes.h"
class nsHostResolver;
@ -70,8 +69,8 @@ public:
*/
Mutex addr_info_lock;
int addr_info_gencnt; /* generation count of |addr_info| */
mozilla::net::AddrInfo *addr_info;
mozilla::net::NetAddr *addr;
PRAddrInfo *addr_info;
PRNetAddr *addr;
bool negative; /* True if this record is a cache of a failed lookup.
Negative cache entries are valid just like any other
(though never for more than 60 seconds), but a use
@ -82,9 +81,9 @@ public:
bool HasResult() const { return addr_info || addr || negative; }
// hold addr_info_lock when calling the blacklist functions
bool Blacklisted(mozilla::net::NetAddr *query);
bool Blacklisted(PRNetAddr *query);
void ResetBlacklist();
void ReportUnusable(mozilla::net::NetAddr *addr);
void ReportUnusable(PRNetAddr *addr);
private:
friend class nsHostResolver;
@ -239,7 +238,7 @@ private:
nsresult Init();
nsresult IssueLookup(nsHostRecord *);
bool GetHostToLookup(nsHostRecord **m);
void OnLookupComplete(nsHostRecord *, nsresult, mozilla::net::AddrInfo *);
void OnLookupComplete(nsHostRecord *, nsresult, PRAddrInfo *);
void DeQueue(PRCList &aQ, nsHostRecord **aResult);
void ClearPendingQueue(PRCList *aPendingQueue);
nsresult ConditionallyCreateThread(nsHostRecord *rec);

View File

@ -4,10 +4,7 @@
#include "nsISupports.idl"
%{ C++
#include "mozilla/net/DNS.h"
%}
native NetAddr(mozilla::net::NetAddr);
native PRNetAddr(union PRNetAddr);
/**
* nsIDNSRecord
@ -17,7 +14,7 @@ native NetAddr(mozilla::net::NetAddr);
* like an enumerator, allowing the caller to easily step through the
* list of IP addresses.
*/
[scriptable, uuid(67E6CF03-12C7-4AF4-AE5F-AE362C7AF8FF)]
[scriptable, uuid(ead9e9d8-7eef-4dae-a7f0-a1edcfb20478)]
interface nsIDNSRecord : nsISupports
{
/**
@ -30,15 +27,15 @@ interface nsIDNSRecord : nsISupports
/**
* this function copies the value of the next IP address into the
* given NetAddr struct and increments the internal address iterator.
* given PRNetAddr struct and increments the internal address iterator.
*
* @param aPort
* A port number to initialize the NetAddr with.
* A port number to initialize the PRNetAddr with.
*
* @throws NS_ERROR_NOT_AVAILABLE if there is not another IP address in
* the record.
*/
[noscript] NetAddr getNextAddr(in uint16_t aPort);
[noscript] PRNetAddr getNextAddr(in uint16_t aPort);
/**
* this function returns the value of the next IP address as a

View File

@ -11,7 +11,6 @@
#include "ipc/IPCMessageUtils.h"
#include "nsStringGlue.h"
#include "prio.h"
#include "mozilla/net/DNS.h"
namespace IPC {
@ -86,24 +85,24 @@ struct ParamTraits<Permission>
};
template<>
struct ParamTraits<mozilla::net::NetAddr>
struct ParamTraits<PRNetAddr>
{
static void Write(Message* aMsg, const mozilla::net::NetAddr &aParam)
static void Write(Message* aMsg, const PRNetAddr &aParam)
{
WriteParam(aMsg, aParam.raw.family);
if (aParam.raw.family == AF_UNSPEC) {
if (aParam.raw.family == PR_AF_UNSPEC) {
aMsg->WriteBytes(aParam.raw.data, sizeof(aParam.raw.data));
} else if (aParam.raw.family == AF_INET) {
} else if (aParam.raw.family == PR_AF_INET) {
WriteParam(aMsg, aParam.inet.port);
WriteParam(aMsg, aParam.inet.ip);
} else if (aParam.raw.family == AF_INET6) {
WriteParam(aMsg, aParam.inet6.port);
WriteParam(aMsg, aParam.inet6.flowinfo);
WriteParam(aMsg, aParam.inet6.ip.u64[0]);
WriteParam(aMsg, aParam.inet6.ip.u64[1]);
WriteParam(aMsg, aParam.inet6.scope_id);
} else if (aParam.raw.family == PR_AF_INET6) {
WriteParam(aMsg, aParam.ipv6.port);
WriteParam(aMsg, aParam.ipv6.flowinfo);
WriteParam(aMsg, aParam.ipv6.ip.pr_s6_addr64[0]);
WriteParam(aMsg, aParam.ipv6.ip.pr_s6_addr64[1]);
WriteParam(aMsg, aParam.ipv6.scope_id);
#if defined(XP_UNIX) || defined(XP_OS2)
} else if (aParam.raw.family == AF_LOCAL) {
} else if (aParam.raw.family == PR_AF_LOCAL) {
// Train's already off the rails: let's get a stack trace at least...
NS_RUNTIMEABORT("Error: please post stack trace to "
"https://bugzilla.mozilla.org/show_bug.cgi?id=661158");
@ -115,26 +114,26 @@ struct ParamTraits<mozilla::net::NetAddr>
* we can do but let the deserializer fail when it gets this message */
}
static bool Read(const Message* aMsg, void** aIter, mozilla::net::NetAddr* aResult)
static bool Read(const Message* aMsg, void** aIter, PRNetAddr* aResult)
{
if (!ReadParam(aMsg, aIter, &aResult->raw.family))
return false;
if (aResult->raw.family == AF_UNSPEC) {
if (aResult->raw.family == PR_AF_UNSPEC) {
return aMsg->ReadBytes(aIter,
reinterpret_cast<const char**>(&aResult->raw.data),
sizeof(aResult->raw.data));
} else if (aResult->raw.family == AF_INET) {
} else if (aResult->raw.family == PR_AF_INET) {
return ReadParam(aMsg, aIter, &aResult->inet.port) &&
ReadParam(aMsg, aIter, &aResult->inet.ip);
} else if (aResult->raw.family == AF_INET6) {
return ReadParam(aMsg, aIter, &aResult->inet6.port) &&
ReadParam(aMsg, aIter, &aResult->inet6.flowinfo) &&
ReadParam(aMsg, aIter, &aResult->inet6.ip.u64[0]) &&
ReadParam(aMsg, aIter, &aResult->inet6.ip.u64[1]) &&
ReadParam(aMsg, aIter, &aResult->inet6.scope_id);
} else if (aResult->raw.family == PR_AF_INET6) {
return ReadParam(aMsg, aIter, &aResult->ipv6.port) &&
ReadParam(aMsg, aIter, &aResult->ipv6.flowinfo) &&
ReadParam(aMsg, aIter, &aResult->ipv6.ip.pr_s6_addr64[0]) &&
ReadParam(aMsg, aIter, &aResult->ipv6.ip.pr_s6_addr64[1]) &&
ReadParam(aMsg, aIter, &aResult->ipv6.scope_id);
#if defined(XP_UNIX) || defined(XP_OS2)
} else if (aResult->raw.family == AF_LOCAL) {
} else if (aResult->raw.family == PR_AF_LOCAL) {
return aMsg->ReadBytes(aIter,
reinterpret_cast<const char**>(&aResult->local.path),
sizeof(aResult->local.path));

View File

@ -42,8 +42,6 @@ extern PRLogModuleInfo* gFTPLog;
#define LOG(args) PR_LOG(gFTPLog, PR_LOG_DEBUG, args)
#define LOG_ALWAYS(args) PR_LOG(gFTPLog, PR_LOG_ALWAYS, args)
using namespace mozilla::net;
// remove FTP parameters (starting with ";") from the path
static void
removeParamsFromPath(nsCString& path)
@ -1294,9 +1292,7 @@ nsFtpState::S_pasv() {
if (!mAddressChecked) {
// Find socket address
mAddressChecked = true;
mServerAddress.raw.family = AF_INET;
mServerAddress.inet.ip = htonl(INADDR_ANY);
mServerAddress.inet.port = htons(0);
PR_InitializeNetAddr(PR_IpAddrAny, 0, &mServerAddress);
nsITransport *controlSocket = mControlConnection->Transport();
if (!controlSocket)
@ -1308,9 +1304,9 @@ nsFtpState::S_pasv() {
if (sTrans) {
nsresult rv = sTrans->GetPeerAddr(&mServerAddress);
if (NS_SUCCEEDED(rv)) {
if (!IsIPAddrAny(&mServerAddress))
mServerIsIPv6 = (mServerAddress.raw.family == AF_INET6) &&
!IsIPAddrV4Mapped(&mServerAddress);
if (!PR_IsNetAddrType(&mServerAddress, PR_IpAddrAny))
mServerIsIPv6 = mServerAddress.raw.family == PR_AF_INET6 &&
!PR_IsNetAddrType(&mServerAddress, PR_IpAddrV4Mapped);
else {
/*
* In case of SOCKS5 remote DNS resolution, we do
@ -1319,11 +1315,12 @@ nsFtpState::S_pasv() {
* socks server should also be IPv6, and this is the
* self address of the transport.
*/
NetAddr selfAddress;
PRNetAddr selfAddress;
rv = sTrans->GetSelfAddr(&selfAddress);
if (NS_SUCCEEDED(rv))
mServerIsIPv6 = (selfAddress.raw.family == AF_INET6) &&
!IsIPAddrV4Mapped(&selfAddress);
mServerIsIPv6 = selfAddress.raw.family == PR_AF_INET6
&& !PR_IsNetAddrType(&selfAddress,
PR_IpAddrV4Mapped);
}
}
}
@ -1449,9 +1446,9 @@ nsFtpState::R_pasv() {
nsCOMPtr<nsISocketTransport> strans;
nsAutoCString host;
if (!IsIPAddrAny(&mServerAddress)) {
char buf[kIPv6CStrBufSize];
NetAddrToString(&mServerAddress, buf, sizeof(buf));
if (!PR_IsNetAddrType(&mServerAddress, PR_IpAddrAny)) {
char buf[64];
PR_NetAddrToString(&mServerAddress, buf, sizeof(buf));
host.Assign(buf);
} else {
/*

View File

@ -30,7 +30,6 @@
#include "nsIPrompt.h"
#include "nsITransport.h"
#include "nsIProxyInfo.h"
#include "mozilla/net/DNS.h"
#include "nsFtpControlConnection.h"
@ -259,7 +258,7 @@ private:
static uint32_t mSessionStartTime;
mozilla::net::NetAddr mServerAddress;
PRNetAddr mServerAddress;
// ***** control read gvars
nsresult mControlStatus;

View File

@ -1274,8 +1274,8 @@ HttpBaseChannel::GetLocalAddress(nsACString& addr)
if (mSelfAddr.raw.family == PR_AF_UNSPEC)
return NS_ERROR_NOT_AVAILABLE;
addr.SetCapacity(kIPv6CStrBufSize);
NetAddrToString(&mSelfAddr, addr.BeginWriting(), kIPv6CStrBufSize);
addr.SetCapacity(64);
PR_NetAddrToString(&mSelfAddr, addr.BeginWriting(), 64);
addr.SetLength(strlen(addr.BeginReading()));
return NS_OK;
@ -1287,10 +1287,10 @@ HttpBaseChannel::GetLocalPort(int32_t* port)
NS_ENSURE_ARG_POINTER(port);
if (mSelfAddr.raw.family == PR_AF_INET) {
*port = (int32_t)ntohs(mSelfAddr.inet.port);
*port = (int32_t)PR_ntohs(mSelfAddr.inet.port);
}
else if (mSelfAddr.raw.family == PR_AF_INET6) {
*port = (int32_t)ntohs(mSelfAddr.inet6.port);
*port = (int32_t)PR_ntohs(mSelfAddr.ipv6.port);
}
else
return NS_ERROR_NOT_AVAILABLE;
@ -1304,8 +1304,8 @@ HttpBaseChannel::GetRemoteAddress(nsACString& addr)
if (mPeerAddr.raw.family == PR_AF_UNSPEC)
return NS_ERROR_NOT_AVAILABLE;
addr.SetCapacity(kIPv6CStrBufSize);
NetAddrToString(&mPeerAddr, addr.BeginWriting(), kIPv6CStrBufSize);
addr.SetCapacity(64);
PR_NetAddrToString(&mPeerAddr, addr.BeginWriting(), 64);
addr.SetLength(strlen(addr.BeginReading()));
return NS_OK;
@ -1317,10 +1317,10 @@ HttpBaseChannel::GetRemotePort(int32_t* port)
NS_ENSURE_ARG_POINTER(port);
if (mPeerAddr.raw.family == PR_AF_INET) {
*port = (int32_t)ntohs(mPeerAddr.inet.port);
*port = (int32_t)PR_ntohs(mPeerAddr.inet.port);
}
else if (mPeerAddr.raw.family == PR_AF_INET6) {
*port = (int32_t)ntohs(mPeerAddr.inet6.port);
*port = (int32_t)PR_ntohs(mPeerAddr.ipv6.port);
}
else
return NS_ERROR_NOT_AVAILABLE;

View File

@ -31,7 +31,6 @@
#include "mozilla/net/NeckoCommon.h"
#include "nsThreadUtils.h"
#include "PrivateBrowsingChannel.h"
#include "mozilla/net/DNS.h"
namespace mozilla {
namespace net {
@ -189,8 +188,8 @@ public:
nsHttpResponseHead * GetResponseHead() const { return mResponseHead; }
nsHttpRequestHead * GetRequestHead() { return &mRequestHead; }
const NetAddr& GetSelfAddr() { return mSelfAddr; }
const NetAddr& GetPeerAddr() { return mPeerAddr; }
const PRNetAddr& GetSelfAddr() { return mSelfAddr; }
const PRNetAddr& GetPeerAddr() { return mPeerAddr; }
public: /* Necko internal use only... */
@ -244,8 +243,8 @@ protected:
nsCString mContentCharsetHint;
nsCString mUserSetCookieHeader;
NetAddr mSelfAddr;
NetAddr mPeerAddr;
PRNetAddr mSelfAddr;
PRNetAddr mPeerAddr;
// HTTP Upgrade Data
nsCString mUpgradeProtocol;

View File

@ -18,7 +18,6 @@
#include "base/compiler_specific.h"
#include "mozilla/ipc/InputStreamUtils.h"
#include "mozilla/ipc/URIUtils.h"
#include "mozilla/net/DNS.h"
using namespace mozilla::dom;
using namespace mozilla::ipc;
@ -174,8 +173,8 @@ class StartRequestEvent : public ChannelEvent
const uint32_t& cacheExpirationTime,
const nsCString& cachedCharset,
const nsCString& securityInfoSerialization,
const NetAddr& selfAddr,
const NetAddr& peerAddr)
const PRNetAddr& selfAddr,
const PRNetAddr& peerAddr)
: mChild(child)
, mResponseHead(responseHead)
, mRequestHeaders(requestHeaders)
@ -206,8 +205,8 @@ class StartRequestEvent : public ChannelEvent
uint32_t mCacheExpirationTime;
nsCString mCachedCharset;
nsCString mSecurityInfoSerialization;
NetAddr mSelfAddr;
NetAddr mPeerAddr;
PRNetAddr mSelfAddr;
PRNetAddr mPeerAddr;
};
bool
@ -219,8 +218,8 @@ HttpChannelChild::RecvOnStartRequest(const nsHttpResponseHead& responseHead,
const uint32_t& cacheExpirationTime,
const nsCString& cachedCharset,
const nsCString& securityInfoSerialization,
const NetAddr& selfAddr,
const NetAddr& peerAddr)
const PRNetAddr& selfAddr,
const PRNetAddr& peerAddr)
{
if (mEventQ.ShouldEnqueue()) {
mEventQ.Enqueue(new StartRequestEvent(this, responseHead, useResponseHead,
@ -246,8 +245,8 @@ HttpChannelChild::OnStartRequest(const nsHttpResponseHead& responseHead,
const uint32_t& cacheExpirationTime,
const nsCString& cachedCharset,
const nsCString& securityInfoSerialization,
const NetAddr& selfAddr,
const NetAddr& peerAddr)
const PRNetAddr& selfAddr,
const PRNetAddr& peerAddr)
{
LOG(("HttpChannelChild::RecvOnStartRequest [this=%x]\n", this));

View File

@ -27,7 +27,6 @@
#include "nsIAssociatedContentSecurity.h"
#include "nsIChildChannel.h"
#include "nsIHttpChannelChild.h"
#include "mozilla/net/DNS.h"
namespace mozilla {
namespace net {
@ -98,8 +97,8 @@ protected:
const uint32_t& cacheExpirationTime,
const nsCString& cachedCharset,
const nsCString& securityInfoSerialization,
const mozilla::net::NetAddr& selfAddr,
const mozilla::net::NetAddr& peerAddr);
const PRNetAddr& selfAddr,
const PRNetAddr& peerAddr);
bool RecvOnTransportAndData(const nsresult& status,
const uint64_t& progress,
const uint64_t& progressMax,
@ -152,8 +151,8 @@ private:
const uint32_t& cacheExpirationTime,
const nsCString& cachedCharset,
const nsCString& securityInfoSerialization,
const mozilla::net::NetAddr& selfAddr,
const mozilla::net::NetAddr& peerAddr);
const PRNetAddr& selfAddr,
const PRNetAddr& peerAddr);
void OnTransportAndData(const nsresult& status,
const uint64_t progress,
const uint64_t& progressMax,

View File

@ -13,14 +13,13 @@ include protocol PBlob; //FIXME: bug #792908
include "mozilla/net/PHttpChannelParams.h";
include "mozilla/net/NeckoMessageUtils.h";
include "mozilla/net/DNS.h";
include "prio.h";
using RequestHeaderTuples;
using nsHttpHeaderArray;
using nsHttpResponseHead;
using nsHttpAtom;
using mozilla::net::NetAddr;
using PRNetAddr;
namespace mozilla {
namespace net {
@ -103,8 +102,8 @@ child:
uint32_t cacheExpirationTime,
nsCString cachedCharset,
nsCString securityInfoSerialization,
NetAddr selfAddr,
NetAddr peerAddr);
PRNetAddr selfAddr,
PRNetAddr peerAddr);
// Combines a single OnDataAvailable and its associated OnProgress &
// OnStatus calls into one IPDL message

View File

@ -11,7 +11,6 @@
#include "nsNetCID.h"
#include "nsCOMPtr.h"
#include "nsNetUtil.h"
#include "mozilla/net/DNS.h"
#include "nsIServiceManager.h"
@ -2839,11 +2838,11 @@ nsHttpConnectionMgr::nsHalfOpenSocket::OnTransportStatus(nsITransport *trans,
!mEnt->mConnInfo->UsingProxy() &&
mEnt->mCoalescingKey.IsEmpty()) {
NetAddr addr;
PRNetAddr addr;
nsresult rv = mSocketTransport->GetPeerAddr(&addr);
if (NS_SUCCEEDED(rv)) {
mEnt->mCoalescingKey.SetCapacity(kIPv6CStrBufSize + 26);
NetAddrToString(&addr, mEnt->mCoalescingKey.BeginWriting(), kIPv6CStrBufSize);
mEnt->mCoalescingKey.SetCapacity(72);
PR_NetAddrToString(&addr, mEnt->mCoalescingKey.BeginWriting(), 64);
mEnt->mCoalescingKey.SetLength(
strlen(mEnt->mCoalescingKey.BeginReading()));

View File

@ -6,15 +6,12 @@
#include "nsISupports.idl"
%{ C++
#include "mozilla/net/DNS.h"
%}
[ptr] native NetAddrPtr(mozilla::net::NetAddr);
[ptr] native PRNetAddrPtr(union PRNetAddr);
[scriptable, uuid(D5C0D1F9-22D7-47DC-BF91-D9AC6E1251A6)]
[scriptable, uuid(8f755c44-1dd2-11b2-a613-91117453fa95)]
interface nsISOCKSSocketInfo : nsISupports
{
[noscript] attribute NetAddrPtr destinationAddr;
[noscript] attribute NetAddrPtr externalProxyAddr;
[noscript] attribute NetAddrPtr internalProxyAddr;
[noscript] attribute PRNetAddrPtr destinationAddr;
[noscript] attribute PRNetAddrPtr externalProxyAddr;
[noscript] attribute PRNetAddrPtr internalProxyAddr;
};

View File

@ -19,12 +19,9 @@
#include "nsIDNSListener.h"
#include "nsICancelable.h"
#include "nsThreadUtils.h"
#include "mozilla/net/DNS.h"
using namespace mozilla::net;
static PRDescIdentity nsSOCKSIOLayerIdentity;
static PRIOMethods nsSOCKSIOLayerMethods;
static PRDescIdentity nsSOCKSIOLayerIdentity;
static PRIOMethods nsSOCKSIOLayerMethods;
static bool firstTime = true;
static bool ipv6Supported = true;
@ -87,7 +84,7 @@ private:
void HandshakeFinished(PRErrorCode err = 0);
PRStatus StartDNS(PRFileDesc *fd);
PRStatus ConnectToProxy(PRFileDesc *fd);
void FixupAddressFamily(PRFileDesc *fd, NetAddr *proxy);
void FixupAddressFamily(PRFileDesc *fd, PRNetAddr *proxy);
PRStatus ContinueConnectingToProxy(PRFileDesc *fd, int16_t oflags);
PRStatus WriteV4ConnectRequest();
PRStatus ReadV4ConnectResponse();
@ -101,15 +98,15 @@ private:
void WriteUint8(uint8_t d);
void WriteUint16(uint16_t d);
void WriteUint32(uint32_t d);
void WriteNetAddr(const NetAddr *addr);
void WriteNetPort(const NetAddr *addr);
void WriteNetAddr(const PRNetAddr *addr);
void WriteNetPort(const PRNetAddr *addr);
void WriteString(const nsACString &str);
uint8_t ReadUint8();
uint16_t ReadUint16();
uint32_t ReadUint32();
void ReadNetAddr(NetAddr *addr, uint16_t fam);
void ReadNetPort(NetAddr *addr);
void ReadNetAddr(PRNetAddr *addr, uint16_t fam);
void ReadNetPort(PRNetAddr *addr);
void WantRead(uint32_t sz);
PRStatus ReadFromSocket(PRFileDesc *fd);
@ -133,9 +130,9 @@ private:
int32_t mVersion; // SOCKS version 4 or 5
int32_t mDestinationFamily;
uint32_t mFlags;
NetAddr mInternalProxyAddr;
NetAddr mExternalProxyAddr;
NetAddr mDestinationAddr;
PRNetAddr mInternalProxyAddr;
PRNetAddr mExternalProxyAddr;
PRNetAddr mDestinationAddr;
PRIntervalTime mTimeout;
};
@ -147,23 +144,14 @@ nsSOCKSSocketInfo::nsSOCKSSocketInfo()
, mAmountToRead(0)
, mProxyPort(-1)
, mVersion(-1)
, mDestinationFamily(AF_INET)
, mDestinationFamily(PR_AF_INET)
, mFlags(0)
, mTimeout(PR_INTERVAL_NO_TIMEOUT)
{
mData = new uint8_t[BUFFER_SIZE];
mInternalProxyAddr.raw.family = AF_INET;
mInternalProxyAddr.inet.ip = htonl(INADDR_ANY);
mInternalProxyAddr.inet.port = htons(0);
mExternalProxyAddr.raw.family = AF_INET;
mExternalProxyAddr.inet.ip = htonl(INADDR_ANY);
mExternalProxyAddr.inet.port = htons(0);
mDestinationAddr.raw.family = AF_INET;
mDestinationAddr.inet.ip = htonl(INADDR_ANY);
mDestinationAddr.inet.port = htons(0);
PR_InitializeNetAddr(PR_IpAddrAny, 0, &mInternalProxyAddr);
PR_InitializeNetAddr(PR_IpAddrAny, 0, &mExternalProxyAddr);
PR_InitializeNetAddr(PR_IpAddrAny, 0, &mDestinationAddr);
}
void
@ -180,44 +168,44 @@ nsSOCKSSocketInfo::Init(int32_t version, int32_t family, const char *proxyHost,
NS_IMPL_THREADSAFE_ISUPPORTS2(nsSOCKSSocketInfo, nsISOCKSSocketInfo, nsIDNSListener)
NS_IMETHODIMP
nsSOCKSSocketInfo::GetExternalProxyAddr(NetAddr * *aExternalProxyAddr)
nsSOCKSSocketInfo::GetExternalProxyAddr(PRNetAddr * *aExternalProxyAddr)
{
memcpy(*aExternalProxyAddr, &mExternalProxyAddr, sizeof(NetAddr));
memcpy(*aExternalProxyAddr, &mExternalProxyAddr, sizeof(PRNetAddr));
return NS_OK;
}
NS_IMETHODIMP
nsSOCKSSocketInfo::SetExternalProxyAddr(NetAddr *aExternalProxyAddr)
nsSOCKSSocketInfo::SetExternalProxyAddr(PRNetAddr *aExternalProxyAddr)
{
memcpy(&mExternalProxyAddr, aExternalProxyAddr, sizeof(NetAddr));
memcpy(&mExternalProxyAddr, aExternalProxyAddr, sizeof(PRNetAddr));
return NS_OK;
}
NS_IMETHODIMP
nsSOCKSSocketInfo::GetDestinationAddr(NetAddr * *aDestinationAddr)
nsSOCKSSocketInfo::GetDestinationAddr(PRNetAddr * *aDestinationAddr)
{
memcpy(*aDestinationAddr, &mDestinationAddr, sizeof(NetAddr));
memcpy(*aDestinationAddr, &mDestinationAddr, sizeof(PRNetAddr));
return NS_OK;
}
NS_IMETHODIMP
nsSOCKSSocketInfo::SetDestinationAddr(NetAddr *aDestinationAddr)
nsSOCKSSocketInfo::SetDestinationAddr(PRNetAddr *aDestinationAddr)
{
memcpy(&mDestinationAddr, aDestinationAddr, sizeof(NetAddr));
memcpy(&mDestinationAddr, aDestinationAddr, sizeof(PRNetAddr));
return NS_OK;
}
NS_IMETHODIMP
nsSOCKSSocketInfo::GetInternalProxyAddr(NetAddr * *aInternalProxyAddr)
nsSOCKSSocketInfo::GetInternalProxyAddr(PRNetAddr * *aInternalProxyAddr)
{
memcpy(*aInternalProxyAddr, &mInternalProxyAddr, sizeof(NetAddr));
memcpy(*aInternalProxyAddr, &mInternalProxyAddr, sizeof(PRNetAddr));
return NS_OK;
}
NS_IMETHODIMP
nsSOCKSSocketInfo::SetInternalProxyAddr(NetAddr *aInternalProxyAddr)
nsSOCKSSocketInfo::SetInternalProxyAddr(PRNetAddr *aInternalProxyAddr)
{
memcpy(&mInternalProxyAddr, aInternalProxyAddr, sizeof(NetAddr));
memcpy(&mInternalProxyAddr, aInternalProxyAddr, sizeof(PRNetAddr));
return NS_OK;
}
@ -309,7 +297,7 @@ nsSOCKSSocketInfo::ConnectToProxy(PRFileDesc *fd)
// Try socks5 if the destination addrress is IPv6
if (mVersion == 4 &&
mDestinationAddr.raw.family == AF_INET6) {
PR_NetAddrFamily(&mDestinationAddr) == PR_AF_INET6) {
mVersion = 5;
}
@ -327,16 +315,14 @@ nsSOCKSSocketInfo::ConnectToProxy(PRFileDesc *fd)
}
#if defined(PR_LOGGING)
char buf[kIPv6CStrBufSize];
NetAddrToString(&mInternalProxyAddr, buf, sizeof(buf));
char buf[64];
PR_NetAddrToString(&mInternalProxyAddr, buf, sizeof(buf));
LOGDEBUG(("socks: trying proxy server, %s:%hu",
buf, ntohs(mInternalProxyAddr.inet.port)));
buf, PR_ntohs(PR_NetAddrInetPort(&mInternalProxyAddr))));
#endif
NetAddr proxy = mInternalProxyAddr;
PRNetAddr proxy = mInternalProxyAddr;
FixupAddressFamily(fd, &proxy);
PRNetAddr prProxy;
NetAddrToPRNetAddr(&proxy, &prProxy);
status = fd->lower->methods->connect(fd->lower, &prProxy, mTimeout);
status = fd->lower->methods->connect(fd->lower, &proxy, mTimeout);
if (status != PR_SUCCESS) {
PRErrorCode c = PR_GetError();
// If EINPROGRESS, return now and check back later after polling
@ -354,25 +340,25 @@ nsSOCKSSocketInfo::ConnectToProxy(PRFileDesc *fd)
}
void
nsSOCKSSocketInfo::FixupAddressFamily(PRFileDesc *fd, NetAddr *proxy)
nsSOCKSSocketInfo::FixupAddressFamily(PRFileDesc *fd, PRNetAddr *proxy)
{
int32_t proxyFamily = mInternalProxyAddr.raw.family;
int32_t proxyFamily = PR_NetAddrFamily(&mInternalProxyAddr);
// Do nothing if the address family is already matched
if (proxyFamily == mDestinationFamily) {
return;
}
// If the system does not support IPv6 and the proxy address is IPv6,
// We can do nothing here.
if (proxyFamily == AF_INET6 && !ipv6Supported) {
if (proxyFamily == PR_AF_INET6 && !ipv6Supported) {
return;
}
// If the system does not support IPv6 and the destination address is
// IPv6, convert IPv4 address to IPv4-mapped IPv6 address to satisfy
// the emulation layer
if (mDestinationFamily == AF_INET6 && !ipv6Supported) {
proxy->inet6.family = AF_INET6;
proxy->inet6.port = mInternalProxyAddr.inet.port;
uint8_t *proxyp = proxy->inet6.ip.u8;
if (mDestinationFamily == PR_AF_INET6 && !ipv6Supported) {
proxy->ipv6.family = PR_AF_INET6;
proxy->ipv6.port = mInternalProxyAddr.inet.port;
uint8_t *proxyp = proxy->ipv6.ip.pr_s6_addr;
memset(proxyp, 0, 10);
memset(proxyp + 10, 0xff, 2);
memcpy(proxyp + 12,(char *) &mInternalProxyAddr.inet.ip, 4);
@ -438,7 +424,7 @@ nsSOCKSSocketInfo::ContinueConnectingToProxy(PRFileDesc *fd, int16_t oflags)
PRStatus
nsSOCKSSocketInfo::WriteV4ConnectRequest()
{
NetAddr *addr = &mDestinationAddr;
PRNetAddr *addr = &mDestinationAddr;
int32_t proxy_resolve;
NS_ABORT_IF_FALSE(mState == SOCKS_CONNECTING_TO_PROXY,
@ -462,7 +448,7 @@ nsSOCKSSocketInfo::WriteV4ConnectRequest()
// four bytes set to 0 and the last byte set to something other
// than 0, is used to notify the proxy that this is a SOCKS 4a
// request. This request type works for Tor and perhaps others.
WriteUint32(htonl(0x00000001)); // Fake IP
WriteUint32(PR_htonl(0x00000001)); // Fake IP
WriteUint8(0x00); // Send an emtpy username
if (mDestinationHost.Length() > MAX_HOSTNAME_LEN) {
LOGERROR(("socks4: destination host name is too long!"));
@ -471,10 +457,10 @@ nsSOCKSSocketInfo::WriteV4ConnectRequest()
}
WriteString(mDestinationHost); // Hostname
WriteUint8(0x00);
} else if (addr->raw.family == AF_INET) {
} else if (PR_NetAddrFamily(addr) == PR_AF_INET) {
WriteNetAddr(addr); // Add the IPv4 address
WriteUint8(0x00); // Send an emtpy username
} else if (addr->raw.family == AF_INET6) {
} else if (PR_NetAddrFamily(addr) == PR_AF_INET6) {
LOGERROR(("socks: SOCKS 4 can't handle IPv6 addresses!"));
HandshakeFinished(PR_BAD_ADDRESS_ERROR);
return PR_FAILURE;
@ -558,7 +544,7 @@ PRStatus
nsSOCKSSocketInfo::WriteV5ConnectRequest()
{
// Send SOCKS 5 connect request
NetAddr *addr = &mDestinationAddr;
PRNetAddr *addr = &mDestinationAddr;
int32_t proxy_resolve;
proxy_resolve = mFlags & nsISocketProvider::PROXY_RESOLVES_HOST;
@ -585,10 +571,10 @@ nsSOCKSSocketInfo::WriteV5ConnectRequest()
WriteUint8(0x03); // addr type -- domainname
WriteUint8(mDestinationHost.Length()); // name length
WriteString(mDestinationHost);
} else if (addr->raw.family == AF_INET) {
} else if (PR_NetAddrFamily(addr) == PR_AF_INET) {
WriteUint8(0x01); // addr type -- IPv4
WriteNetAddr(addr);
} else if (addr->raw.family == AF_INET6) {
} else if (PR_NetAddrFamily(addr) == PR_AF_INET6) {
WriteUint8(0x04); // addr type -- IPv6
WriteNetAddr(addr);
} else {
@ -732,14 +718,14 @@ nsSOCKSSocketInfo::ReadV5ConnectResponseBottom()
// Read what the proxy says is our source address
switch (type) {
case 0x01: // ipv4
ReadNetAddr(&mExternalProxyAddr, AF_INET);
ReadNetAddr(&mExternalProxyAddr, PR_AF_INET);
break;
case 0x04: // ipv6
ReadNetAddr(&mExternalProxyAddr, AF_INET6);
ReadNetAddr(&mExternalProxyAddr, PR_AF_INET6);
break;
case 0x03: // fqdn (skip)
mReadOffset += len;
mExternalProxyAddr.raw.family = AF_INET;
mExternalProxyAddr.raw.family = PR_AF_INET;
break;
}
@ -879,17 +865,17 @@ nsSOCKSSocketInfo::WriteUint32(uint32_t v)
}
void
nsSOCKSSocketInfo::WriteNetAddr(const NetAddr *addr)
nsSOCKSSocketInfo::WriteNetAddr(const PRNetAddr *addr)
{
const char *ip = NULL;
uint32_t len = 0;
if (addr->raw.family == AF_INET) {
if (PR_NetAddrFamily(addr) == PR_AF_INET) {
ip = (const char*)&addr->inet.ip;
len = sizeof(addr->inet.ip);
} else if (addr->raw.family == AF_INET6) {
ip = (const char*)addr->inet6.ip.u8;
len = sizeof(addr->inet6.ip.u8);
} else if (PR_NetAddrFamily(addr) == PR_AF_INET6) {
ip = (const char*)addr->ipv6.ip.pr_s6_addr;
len = sizeof(addr->ipv6.ip.pr_s6_addr);
}
NS_ABORT_IF_FALSE(ip != NULL, "Unknown address");
@ -901,9 +887,9 @@ nsSOCKSSocketInfo::WriteNetAddr(const NetAddr *addr)
}
void
nsSOCKSSocketInfo::WriteNetPort(const NetAddr *addr)
nsSOCKSSocketInfo::WriteNetPort(const PRNetAddr *addr)
{
WriteUint16(addr->inet.port);
WriteUint16(PR_NetAddrInetPort(addr));
}
void
@ -949,29 +935,29 @@ nsSOCKSSocketInfo::ReadUint32()
}
void
nsSOCKSSocketInfo::ReadNetAddr(NetAddr *addr, uint16_t fam)
nsSOCKSSocketInfo::ReadNetAddr(PRNetAddr *addr, uint16_t fam)
{
uint32_t amt = 0;
const uint8_t *ip = mData + mReadOffset;
addr->raw.family = fam;
if (fam == AF_INET) {
if (fam == PR_AF_INET) {
amt = sizeof(addr->inet.ip);
NS_ABORT_IF_FALSE(mReadOffset + amt <= mDataLength,
"Not enough space to pop an ipv4 addr!");
memcpy(&addr->inet.ip, ip, amt);
} else if (fam == AF_INET6) {
amt = sizeof(addr->inet6.ip.u8);
} else if (fam == PR_AF_INET6) {
amt = sizeof(addr->ipv6.ip.pr_s6_addr);
NS_ABORT_IF_FALSE(mReadOffset + amt <= mDataLength,
"Not enough space to pop an ipv6 addr!");
memcpy(addr->inet6.ip.u8, ip, amt);
memcpy(addr->ipv6.ip.pr_s6_addr, ip, amt);
}
mReadOffset += amt;
}
void
nsSOCKSSocketInfo::ReadNetPort(NetAddr *addr)
nsSOCKSSocketInfo::ReadNetPort(PRNetAddr *addr)
{
addr->inet.port = ReadUint16();
}
@ -1074,24 +1060,22 @@ static PRStatus
nsSOCKSIOLayerConnect(PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime to)
{
PRStatus status;
NetAddr dst;
PRNetAddr dst;
nsSOCKSSocketInfo * info = (nsSOCKSSocketInfo*) fd->secret;
if (info == NULL) return PR_FAILURE;
if (addr->raw.family == PR_AF_INET6 &&
if (PR_NetAddrFamily(addr) == PR_AF_INET6 &&
PR_IsNetAddrType(addr, PR_IpAddrV4Mapped)) {
const uint8_t *srcp;
LOGDEBUG(("socks: converting ipv4-mapped ipv6 address to ipv4"));
// copied from _PR_ConvertToIpv4NetAddr()
dst.raw.family = AF_INET;
dst.inet.ip = htonl(INADDR_ANY);
dst.inet.port = htons(0);
PR_InitializeNetAddr(PR_IpAddrAny, 0, &dst);
srcp = addr->ipv6.ip.pr_s6_addr;
memcpy(&dst.inet.ip, srcp + 12, 4);
dst.inet.family = AF_INET;
dst.inet.family = PR_AF_INET;
dst.inet.port = addr->ipv6.port;
} else {
memcpy(&dst, addr, sizeof(dst));
@ -1179,12 +1163,8 @@ nsSOCKSIOLayerGetName(PRFileDesc *fd, PRNetAddr *addr)
nsSOCKSSocketInfo * info = (nsSOCKSSocketInfo*) fd->secret;
if (info != NULL && addr != NULL) {
NetAddr temp;
NetAddr *tempPtr = &temp;
if (info->GetExternalProxyAddr(&tempPtr) == NS_OK) {
NetAddrToPRNetAddr(tempPtr, addr);
if (info->GetExternalProxyAddr(&addr) == NS_OK)
return PR_SUCCESS;
}
}
return PR_FAILURE;
@ -1196,12 +1176,8 @@ nsSOCKSIOLayerGetPeerName(PRFileDesc *fd, PRNetAddr *addr)
nsSOCKSSocketInfo * info = (nsSOCKSSocketInfo*) fd->secret;
if (info != NULL && addr != NULL) {
NetAddr temp;
NetAddr *tempPtr = &temp;
if (info->GetDestinationAddr(&tempPtr) == NS_OK) {
NetAddrToPRNetAddr(tempPtr, addr);
if (info->GetDestinationAddr(&addr) == NS_OK)
return PR_SUCCESS;
}
}
return PR_FAILURE;
@ -1243,21 +1219,21 @@ nsSOCKSIOLayerAddToSocket(int32_t family,
PR_Close(tmpfd);
}
nsSOCKSIOLayerIdentity = PR_GetUniqueIdentity("SOCKS layer");
nsSOCKSIOLayerMethods = *PR_GetDefaultIOMethods();
nsSOCKSIOLayerIdentity = PR_GetUniqueIdentity("SOCKS layer");
nsSOCKSIOLayerMethods = *PR_GetDefaultIOMethods();
nsSOCKSIOLayerMethods.connect = nsSOCKSIOLayerConnect;
nsSOCKSIOLayerMethods.connectcontinue = nsSOCKSIOLayerConnectContinue;
nsSOCKSIOLayerMethods.poll = nsSOCKSIOLayerPoll;
nsSOCKSIOLayerMethods.bind = nsSOCKSIOLayerBind;
nsSOCKSIOLayerMethods.connect = nsSOCKSIOLayerConnect;
nsSOCKSIOLayerMethods.connectcontinue = nsSOCKSIOLayerConnectContinue;
nsSOCKSIOLayerMethods.poll = nsSOCKSIOLayerPoll;
nsSOCKSIOLayerMethods.bind = nsSOCKSIOLayerBind;
nsSOCKSIOLayerMethods.acceptread = nsSOCKSIOLayerAcceptRead;
nsSOCKSIOLayerMethods.getsockname = nsSOCKSIOLayerGetName;
nsSOCKSIOLayerMethods.getpeername = nsSOCKSIOLayerGetPeerName;
nsSOCKSIOLayerMethods.accept = nsSOCKSIOLayerAccept;
nsSOCKSIOLayerMethods.listen = nsSOCKSIOLayerListen;
nsSOCKSIOLayerMethods.close = nsSOCKSIOLayerClose;
nsSOCKSIOLayerMethods.accept = nsSOCKSIOLayerAccept;
nsSOCKSIOLayerMethods.listen = nsSOCKSIOLayerListen;
nsSOCKSIOLayerMethods.close = nsSOCKSIOLayerClose;
firstTime = false;
firstTime = false;
#if defined(PR_LOGGING)
gSOCKSLog = PR_NewLogModule("SOCKS");
@ -1267,8 +1243,8 @@ nsSOCKSIOLayerAddToSocket(int32_t family,
LOGDEBUG(("Entering nsSOCKSIOLayerAddToSocket()."));
PRFileDesc *layer;
PRStatus rv;
PRFileDesc * layer;
PRStatus rv;
layer = PR_CreateIOLayerStub(nsSOCKSIOLayerIdentity, &nsSOCKSIOLayerMethods);
if (! layer)