Skip to content

Commit

Permalink
Bug 1288308 - Part 0: add Named Pipe type on Windows platform; r=bagder
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: It2l5BJuHiS
  • Loading branch information
Liang-Heng Chen committed Nov 3, 2016
1 parent 780e21d commit f928a59
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 31 deletions.
1 change: 1 addition & 0 deletions netwerk/base/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ LOCAL_INCLUDES += [
'/docshell/base',
'/dom/base',
'/netwerk/protocol/http',
'/netwerk/socket',
'/security/pkix/include'
]

Expand Down
15 changes: 3 additions & 12 deletions netwerk/base/nsProtocolProxyService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsThreadUtils.h"
#include "nsSOCKSIOLayer.h"
#include "nsString.h"
#include "nsNetUtil.h"
#include "nsNetCID.h"
Expand Down Expand Up @@ -387,16 +388,6 @@ proxy_GetBoolPref(nsIPrefBranch *aPrefBranch,
aResult = temp;
}

static inline bool
IsHostDomainSocket(const nsACString& aHost)
{
#ifdef XP_UNIX
return Substring(aHost, 0, 5) == "file:";
#else
return false;
#endif // XP_UNIX
}

//----------------------------------------------------------------------------

static const int32_t PROXYCONFIG_DIRECT4X = 3;
Expand Down Expand Up @@ -1862,7 +1853,7 @@ nsProtocolProxyService::Resolve_Internal(nsIChannel *channel,

if ((flags & RESOLVE_PREFER_SOCKS_PROXY) &&
!mSOCKSProxyTarget.IsEmpty() &&
(IsHostDomainSocket(mSOCKSProxyTarget) || mSOCKSProxyPort > 0)) {
(IsHostLocalTarget(mSOCKSProxyTarget) || mSOCKSProxyPort > 0)) {
host = &mSOCKSProxyTarget;
if (mSOCKSProxyVersion == 4)
type = kProxyType_SOCKS4;
Expand Down Expand Up @@ -1900,7 +1891,7 @@ nsProtocolProxyService::Resolve_Internal(nsIChannel *channel,
port = mFTPProxyPort;
}
else if (!mSOCKSProxyTarget.IsEmpty() &&
(IsHostDomainSocket(mSOCKSProxyTarget) || mSOCKSProxyPort > 0)) {
(IsHostLocalTarget(mSOCKSProxyTarget) || mSOCKSProxyPort > 0)) {
host = &mSOCKSProxyTarget;
if (mSOCKSProxyVersion == 4)
type = kProxyType_SOCKS4;
Expand Down
3 changes: 2 additions & 1 deletion netwerk/base/nsProtocolProxyService.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ class nsProtocolProxyService final : public nsIProtocolProxyService2
nsCString mHTTPSProxyHost;
int32_t mHTTPSProxyPort;

// mSOCKSProxyTarget could be a host or a domain socket path.
// mSOCKSProxyTarget could be a host, a domain socket path,
// or a named-pipe name.
nsCString mSOCKSProxyTarget;
int32_t mSOCKSProxyPort;
int32_t mSOCKSProxyVersion;
Expand Down
5 changes: 5 additions & 0 deletions netwerk/dns/DNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ void NetAddrToPRNetAddr(const NetAddr *addr, PRNetAddr *prAddr)
prAddr->local.family = PR_AF_LOCAL;
memcpy(prAddr->local.path, addr->local.path, sizeof(addr->local.path));
}
#elif defined(XP_WIN)
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
}

Expand Down
9 changes: 7 additions & 2 deletions netwerk/dns/DNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include "winsock2.h"
#endif

#ifndef AF_LOCAL
#define AF_LOCAL 1 // used for named pipe
#endif

#define IPv6ADDR_IS_LOOPBACK(a) \
(((a)->u32[0] == 0) && \
((a)->u32[1] == 0) && \
Expand Down Expand Up @@ -103,8 +107,9 @@ union NetAddr {
IPv6Addr ip; /* the actual 128 bits of address */
uint32_t scope_id; /* set of interfaces for a scope */
} inet6;
#if defined(XP_UNIX)
struct { /* Unix domain socket address */
#if defined(XP_UNIX) || defined(XP_WIN)
struct { /* Unix domain socket or
Windows Named Pipes address */
uint16_t family; /* address family (AF_UNIX) */
char path[104]; /* null-terminated pathname */
} local;
Expand Down
49 changes: 34 additions & 15 deletions netwerk/socket/nsSOCKSIOLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,15 @@ class nsSOCKSSocketInfo : public nsISOCKSSocketInfo
PRStatus ReadFromSocket(PRFileDesc *fd);
PRStatus WriteToSocket(PRFileDesc *fd);

bool IsHostDomainSocket()
bool IsLocalProxy()
{
#ifdef XP_UNIX
nsAutoCString proxyHost;
mProxy->GetHost(proxyHost);
return Substring(proxyHost, 0, 5) == "file:";
#else
return false;
#endif // XP_UNIX
return IsHostLocalTarget(proxyHost);
}

nsresult SetDomainSocketPath(const nsACString& aDomainSocketPath,
NetAddr* aProxyAddr)
nsresult SetLocalProxyPath(const nsACString& aLocalProxyPath,
NetAddr* aProxyAddr)
{
#ifdef XP_UNIX
nsresult rv;
Expand All @@ -145,7 +141,7 @@ class nsSOCKSSocketInfo : public nsISOCKSSocketInfo
}

nsCOMPtr<nsIFile> socketFile;
rv = fileHandler->GetFileFromURLSpec(aDomainSocketPath,
rv = fileHandler->GetFileFromURLSpec(aLocalProxyPath,
getter_AddRefs(socketFile));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
Expand All @@ -164,10 +160,21 @@ class nsSOCKSSocketInfo : public nsISOCKSSocketInfo
aProxyAddr->raw.family = AF_UNIX;
strcpy(aProxyAddr->local.path, path.get());

return NS_OK;
#elif defined(XP_WIN)
MOZ_ASSERT(aProxyAddr);

if (sizeof(aProxyAddr->local.path) <= aLocalProxyPath.Length()) {
NS_WARNING("pipe path too long.");
return NS_ERROR_FAILURE;
}

aProxyAddr->raw.family = AF_LOCAL;
strcpy(aProxyAddr->local.path, PromiseFlatCString(aLocalProxyPath).get());
return NS_OK;
#else
mozilla::Unused << aLocalProxyPath;
mozilla::Unused << aProxyAddr;
mozilla::Unused << aDomainSocketPath;
return NS_ERROR_NOT_IMPLEMENTED;
#endif
}
Expand Down Expand Up @@ -489,12 +496,12 @@ nsSOCKSSocketInfo::ConnectToProxy(PRFileDesc *fd)

int32_t addresses = 0;
do {
if (IsHostDomainSocket()) {
rv = SetDomainSocketPath(proxyHost, &mInternalProxyAddr);
if (IsLocalProxy()) {
rv = SetLocalProxyPath(proxyHost, &mInternalProxyAddr);
if (NS_FAILED(rv)) {
LOGERROR(("socks: unable to connect to SOCKS proxy, %s",
proxyHost.get()));
return PR_FAILURE;
return PR_FAILURE;
}
} else {
if (addresses++) {
Expand Down Expand Up @@ -529,7 +536,7 @@ nsSOCKSSocketInfo::ConnectToProxy(PRFileDesc *fd)
if (c == PR_WOULD_BLOCK_ERROR || c == PR_IN_PROGRESS_ERROR) {
mState = SOCKS_CONNECTING_TO_PROXY;
return status;
} else if (IsHostDomainSocket()) {
} else if (IsLocalProxy()) {
LOGERROR(("socks: connect to domain socket failed (%d)", c));
PR_SetError(PR_CONNECT_REFUSED_ERROR, 0);
mState = SOCKS_FAILED;
Expand Down Expand Up @@ -1049,7 +1056,7 @@ nsSOCKSSocketInfo::DoHandshake(PRFileDesc *fd, int16_t oflags)

switch (mState) {
case SOCKS_INITIAL:
if (IsHostDomainSocket()) {
if (IsLocalProxy()) {
mState = SOCKS_DNS_COMPLETE;
mLookupStatus = NS_OK;
return ConnectToProxy(fd);
Expand Down Expand Up @@ -1535,3 +1542,15 @@ nsSOCKSIOLayerAddToSocket(int32_t family,
NS_ADDREF(*info);
return NS_OK;
}

bool
IsHostLocalTarget(const nsACString& aHost)
{
#if defined(XP_UNIX)
return StringBeginsWith(aHost, NS_LITERAL_CSTRING("file:"));
#elif defined(XP_WIN)
return StringBeginsWith(aHost, NS_LITERAL_CSTRING("\\\\.\\pipe\\"));
#else
return false;
#endif // XP_UNIX
}
2 changes: 2 additions & 0 deletions netwerk/socket/nsSOCKSIOLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ nsresult nsSOCKSIOLayerAddToSocket(int32_t family,
PRFileDesc *fd,
nsISupports **info);

bool IsHostLocalTarget(const nsACString& aHost);

#endif /* nsSOCKSIOLayer_h__ */
2 changes: 1 addition & 1 deletion nsprpub/pr/include/prio.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ union PRNetAddr {
PRIPv6Addr ip; /* the actual 128 bits of address */
PRUint32 scope_id; /* set of interfaces for a scope */
} ipv6;
#if defined(XP_UNIX) || defined(XP_OS2)
#if defined(XP_UNIX) || defined(XP_OS2) || defined(XP_WIN)
struct { /* Unix domain socket address */
PRUint16 family; /* address family (AF_UNIX) */
#ifdef XP_OS2
Expand Down

0 comments on commit f928a59

Please sign in to comment.