Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / netwerk / socket / nsSOCKSSocketProvider.cpp
blobfc18e8e788b23d29bc4dbde8f719d7d779611dcb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsNamedPipeIOLayer.h"
8 #include "nsSOCKSSocketProvider.h"
9 #include "nsSOCKSIOLayer.h"
10 #include "nsCOMPtr.h"
11 #include "nsError.h"
13 using mozilla::OriginAttributes;
14 using namespace mozilla::net;
16 //////////////////////////////////////////////////////////////////////////
18 NS_IMPL_ISUPPORTS(nsSOCKSSocketProvider, nsISocketProvider)
20 // Per-platform implemenation of OpenTCPSocket helper function
21 // Different platforms have special cases to handle
23 #if defined(XP_WIN)
24 // The proxy host on Windows may be a named pipe uri, in which
25 // case a named-pipe (rather than a socket) should be returned
26 static PRFileDesc* OpenTCPSocket(int32_t family, nsIProxyInfo* proxy) {
27 PRFileDesc* sock = nullptr;
29 nsAutoCString proxyHost;
30 proxy->GetHost(proxyHost);
31 if (IsNamedPipePath(proxyHost)) {
32 sock = CreateNamedPipeLayer();
33 } else {
34 sock = PR_OpenTCPSocket(family);
37 return sock;
39 #elif defined(XP_UNIX)
40 // The proxy host on UNIX systems may point to a local file uri
41 // in which case we should create an AF_LOCAL (UNIX Domain) socket
42 // instead of the requested AF_INET or AF_INET6 socket.
44 // Normally,this socket would get thrown out and recreated later on
45 // with the proper family, but we want to do it early here so that
46 // we can enforce seccomp policy to blacklist socket(AF_INET) calls
47 // to prevent the content sandbox from creating network requests
48 static PRFileDesc* OpenTCPSocket(int32_t family, nsIProxyInfo* proxy) {
49 nsAutoCString proxyHost;
50 proxy->GetHost(proxyHost);
51 if (StringBeginsWith(proxyHost, "file://"_ns)) {
52 family = AF_LOCAL;
55 return PR_OpenTCPSocket(family);
57 #else
58 // Default, pass-through to PR_OpenTCPSocket
59 static PRFileDesc* OpenTCPSocket(int32_t family, nsIProxyInfo*) {
60 return PR_OpenTCPSocket(family);
62 #endif
64 NS_IMETHODIMP
65 nsSOCKSSocketProvider::NewSocket(int32_t family, const char* host, int32_t port,
66 nsIProxyInfo* proxy,
67 const OriginAttributes& originAttributes,
68 uint32_t flags, uint32_t tlsFlags,
69 PRFileDesc** result,
70 nsITLSSocketControl** tlsSocketControl) {
71 PRFileDesc* sock = OpenTCPSocket(family, proxy);
72 if (!sock) {
73 return NS_ERROR_OUT_OF_MEMORY;
76 nsresult rv = nsSOCKSIOLayerAddToSocket(family, host, port, proxy, mVersion,
77 flags, tlsFlags, sock);
78 if (NS_SUCCEEDED(rv)) {
79 *result = sock;
80 return NS_OK;
83 return NS_ERROR_SOCKET_CREATE_FAILED;
86 NS_IMETHODIMP
87 nsSOCKSSocketProvider::AddToSocket(int32_t family, const char* host,
88 int32_t port, nsIProxyInfo* proxy,
89 const OriginAttributes& originAttributes,
90 uint32_t flags, uint32_t tlsFlags,
91 PRFileDesc* sock,
92 nsITLSSocketControl** tlsSocketControl) {
93 nsresult rv = nsSOCKSIOLayerAddToSocket(family, host, port, proxy, mVersion,
94 flags, tlsFlags, sock);
96 if (NS_FAILED(rv)) rv = NS_ERROR_SOCKET_CREATE_FAILED;
97 return rv;