Back to index

lightning-sunbird  0.9+nobinonly
nsSOCKSSocketProvider.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Justin Bradford <jab@atdot.org>
00025  *   Darin Fisher <darin@meer.net>
00026  *   Malcolm Smith <malsmith@cs.rmit.edu.au>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #include "nsIServiceManager.h"
00043 #include "nsSOCKSSocketProvider.h"
00044 #include "nsSOCKSIOLayer.h"
00045 #include "nsCOMPtr.h"
00046 #include "nsNetError.h"
00047 
00049 
00050 NS_IMPL_THREADSAFE_ISUPPORTS1(nsSOCKSSocketProvider, nsISocketProvider)
00051 
00052 NS_METHOD
00053 nsSOCKSSocketProvider::CreateV4(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00054 {
00055     nsresult rv;
00056     nsCOMPtr<nsISocketProvider> inst =
00057             new nsSOCKSSocketProvider(NS_SOCKS_VERSION_4);
00058     if (!inst)
00059         rv = NS_ERROR_OUT_OF_MEMORY;
00060     else
00061         rv = inst->QueryInterface(aIID, aResult); 
00062     return rv;
00063 }
00064 
00065 NS_METHOD
00066 nsSOCKSSocketProvider::CreateV5(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00067 {
00068     nsresult rv;
00069     nsCOMPtr<nsISocketProvider> inst =
00070             new nsSOCKSSocketProvider(NS_SOCKS_VERSION_5);
00071     if (!inst)
00072         rv = NS_ERROR_OUT_OF_MEMORY;
00073     else
00074         rv = inst->QueryInterface(aIID, aResult); 
00075     return rv;
00076 }
00077 
00078 NS_IMETHODIMP
00079 nsSOCKSSocketProvider::NewSocket(PRInt32 family,
00080                                  const char *host, 
00081                                  PRInt32 port,
00082                                  const char *proxyHost,
00083                                  PRInt32 proxyPort,
00084                                  PRUint32 flags,
00085                                  PRFileDesc **result, 
00086                                  nsISupports **socksInfo)
00087 {
00088     PRFileDesc *sock;
00089     
00090     sock = PR_OpenTCPSocket(family);
00091     if (!sock)
00092         return NS_ERROR_OUT_OF_MEMORY;
00093 
00094     nsresult rv = nsSOCKSIOLayerAddToSocket(family,
00095                                             host, 
00096                                             port,
00097                                             proxyHost,
00098                                             proxyPort,
00099                                             mVersion,
00100                                             flags,
00101                                             sock, 
00102                                             socksInfo);
00103     if (NS_SUCCEEDED(rv)) {
00104         *result = sock;
00105         return NS_OK;
00106     }
00107 
00108     return NS_ERROR_SOCKET_CREATE_FAILED;
00109 }
00110 
00111 NS_IMETHODIMP
00112 nsSOCKSSocketProvider::AddToSocket(PRInt32 family,
00113                                    const char *host,
00114                                    PRInt32 port,
00115                                    const char *proxyHost,
00116                                    PRInt32 proxyPort,
00117                                    PRUint32 flags,
00118                                    PRFileDesc *sock, 
00119                                    nsISupports **socksInfo)
00120 {
00121     nsresult rv = nsSOCKSIOLayerAddToSocket(family,
00122                                             host, 
00123                                             port,
00124                                             proxyHost,
00125                                             proxyPort,
00126                                             mVersion,
00127                                             flags,
00128                                             sock, 
00129                                             socksInfo);
00130     
00131     if (NS_FAILED(rv))
00132         rv = NS_ERROR_SOCKET_CREATE_FAILED;
00133     return rv;
00134 }