nsSOCKSSocketProvider.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  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/. */
  6. #include "nsIServiceManager.h"
  7. #include "nsNamedPipeIOLayer.h"
  8. #include "nsSOCKSSocketProvider.h"
  9. #include "nsSOCKSIOLayer.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsError.h"
  12. using mozilla::NeckoOriginAttributes;
  13. using namespace mozilla::net;
  14. //////////////////////////////////////////////////////////////////////////
  15. NS_IMPL_ISUPPORTS(nsSOCKSSocketProvider, nsISocketProvider)
  16. nsresult
  17. nsSOCKSSocketProvider::CreateV4(nsISupports *aOuter, REFNSIID aIID, void **aResult)
  18. {
  19. nsresult rv;
  20. nsCOMPtr<nsISocketProvider> inst =
  21. new nsSOCKSSocketProvider(NS_SOCKS_VERSION_4);
  22. if (!inst)
  23. rv = NS_ERROR_OUT_OF_MEMORY;
  24. else
  25. rv = inst->QueryInterface(aIID, aResult);
  26. return rv;
  27. }
  28. nsresult
  29. nsSOCKSSocketProvider::CreateV5(nsISupports *aOuter, REFNSIID aIID, void **aResult)
  30. {
  31. nsresult rv;
  32. nsCOMPtr<nsISocketProvider> inst =
  33. new nsSOCKSSocketProvider(NS_SOCKS_VERSION_5);
  34. if (!inst)
  35. rv = NS_ERROR_OUT_OF_MEMORY;
  36. else
  37. rv = inst->QueryInterface(aIID, aResult);
  38. return rv;
  39. }
  40. NS_IMETHODIMP
  41. nsSOCKSSocketProvider::NewSocket(int32_t family,
  42. const char *host,
  43. int32_t port,
  44. nsIProxyInfo *proxy,
  45. const NeckoOriginAttributes &originAttributes,
  46. uint32_t flags,
  47. PRFileDesc **result,
  48. nsISupports **socksInfo)
  49. {
  50. PRFileDesc *sock;
  51. #if defined(XP_WIN)
  52. nsAutoCString proxyHost;
  53. proxy->GetHost(proxyHost);
  54. if (IsNamedPipePath(proxyHost)) {
  55. sock = CreateNamedPipeLayer();
  56. } else
  57. #endif
  58. {
  59. sock = PR_OpenTCPSocket(family);
  60. if (!sock) {
  61. return NS_ERROR_OUT_OF_MEMORY;
  62. }
  63. }
  64. nsresult rv = nsSOCKSIOLayerAddToSocket(family,
  65. host,
  66. port,
  67. proxy,
  68. mVersion,
  69. flags,
  70. sock,
  71. socksInfo);
  72. if (NS_SUCCEEDED(rv)) {
  73. *result = sock;
  74. return NS_OK;
  75. }
  76. return NS_ERROR_SOCKET_CREATE_FAILED;
  77. }
  78. NS_IMETHODIMP
  79. nsSOCKSSocketProvider::AddToSocket(int32_t family,
  80. const char *host,
  81. int32_t port,
  82. nsIProxyInfo *proxy,
  83. const NeckoOriginAttributes &originAttributes,
  84. uint32_t flags,
  85. PRFileDesc *sock,
  86. nsISupports **socksInfo)
  87. {
  88. nsresult rv = nsSOCKSIOLayerAddToSocket(family,
  89. host,
  90. port,
  91. proxy,
  92. mVersion,
  93. flags,
  94. sock,
  95. socksInfo);
  96. if (NS_FAILED(rv))
  97. rv = NS_ERROR_SOCKET_CREATE_FAILED;
  98. return rv;
  99. }