blocxx

Socket_needs_SSL.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00039 #include "blocxx/BLOCXX_config.h"
00040 #include "blocxx/Socket.hpp"
00041 #include "blocxx/SocketImpl.hpp"
00042 #include "blocxx/SSLException.hpp"
00043 #include "blocxx/SSLSocketImpl.hpp"
00044 
00045 // This code was pulled out of BLOCXX_Socket.cpp so that programs that use
00046 // Socket but don't need SSL don't have to pull in the SSL libraries.
00047 
00048 namespace BLOCXX_NAMESPACE
00049 {
00050 
00052 Socket::Socket(const SSLClientCtxRef& sslCtx)
00053 {
00054    if (sslCtx)
00055    {
00056 #ifndef BLOCXX_NO_SSL
00057       m_impl = SocketBaseImplRef(new SSLSocketImpl(sslCtx));
00058 #else
00059       BLOCXX_THROW(SSLException, "Not built with SSL");
00060 #endif // #ifndef BLOCXX_NO_SSL
00061    }
00062    else
00063    {
00064       m_impl = SocketBaseImplRef(new SocketImpl);
00065    }
00066 }
00067 
00069 Socket::Socket(SocketFlags::ESSLFlag isSSL)
00070 {
00071    if (isSSL == SocketFlags::E_SSL)
00072    {
00073 #ifndef BLOCXX_NO_SSL
00074       m_impl = SocketBaseImplRef(new SSLSocketImpl);
00075 #else
00076       BLOCXX_THROW(SSLException, "Not built with SSL");
00077 #endif // #ifndef BLOCXX_NO_SSL
00078    }
00079    else
00080    {
00081       m_impl = SocketBaseImplRef(new SocketImpl);
00082    }
00083 }
00085 Socket::Socket(SocketHandle_t fd,
00086    SocketAddress::AddressType addrType, SocketFlags::ESSLFlag isSSL)
00087 {
00088    if (isSSL == SocketFlags::E_SSL)
00089    {
00090 #ifndef BLOCXX_NO_SSL
00091       m_impl = SocketBaseImplRef(new SSLSocketImpl(fd, addrType));
00092 #else
00093       BLOCXX_THROW(SSLException, "Not built with SSL");
00094 #endif // #ifndef BLOCXX_NO_SSL
00095    }
00096    else
00097    {
00098       m_impl = SocketBaseImplRef(new SocketImpl(fd, addrType));
00099    }
00100 }
00102 // Used by ServerSocket2::accept()
00103 Socket::Socket(SocketHandle_t fd,
00104    SocketAddress::AddressType addrType, const SSLServerCtxRef& sslCtx)
00105 {
00106    if (sslCtx)
00107    {
00108 #ifndef BLOCXX_NO_SSL
00109       m_impl = SocketBaseImplRef(new SSLSocketImpl(fd, addrType, sslCtx));
00110 #else
00111       BLOCXX_THROW(SSLException, "Not built with SSL");
00112 #endif // #ifndef BLOCXX_NO_SSL
00113    }
00114    else
00115    {
00116       m_impl = SocketBaseImplRef(new SocketImpl(fd, addrType));
00117    }
00118 }
00120 Socket::Socket(const SocketAddress& addr, SocketFlags::ESSLFlag isSSL)
00121 {
00122    if (isSSL == SocketFlags::E_SSL)
00123    {
00124 #ifndef BLOCXX_NO_SSL
00125       m_impl = SocketBaseImplRef(new SSLSocketImpl(addr));
00126 #else
00127       BLOCXX_THROW(SSLException, "Not built with SSL");
00128 #endif // #ifndef BLOCXX_NO_SSL
00129    }
00130    else
00131    {
00132       m_impl = SocketBaseImplRef(new SocketImpl(addr));
00133    }
00134 }
00135 
00136 #ifndef BLOCXX_NO_SSL
00137 SSL*
00138 Socket::getSSL() const
00139 {
00140    IntrusiveReference<SSLSocketImpl> sslsock = m_impl.cast_to<SSLSocketImpl>(); 
00141    if (!sslsock)
00142    {
00143       return 0; 
00144    }
00145    return sslsock->getSSL(); 
00146 }
00147 
00149 bool
00150 Socket::peerCertVerified() const
00151 {
00152     IntrusiveReference<SSLSocketImpl> sslsock = m_impl.cast_to<SSLSocketImpl>(); 
00153     if (!sslsock)
00154     {
00155             return false; 
00156     }
00157     return sslsock->peerCertVerified(); 
00158 }
00159 #endif
00160 
00161 } // namespace BLOCXX_NAMESPACE