OW_Socket.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_Socket.hpp"
00038 #include "OW_UnnamedPipe.hpp"
00039 #include "OW_Assertion.hpp"
00040 #include "OW_MutexLock.hpp"
00041 #include "OW_SSLException.hpp"
00042 #include "OW_Exception.hpp"
00043 #include "OW_IOException.hpp"
00044 #include "OW_ExceptionIds.hpp"
00045 #include "OW_SocketImpl.hpp"
00046 #include "OW_SSLSocketImpl.hpp"
00047 
00048 
00049 namespace OW_NAMESPACE
00050 {
00051 
00052 OW_DEFINE_EXCEPTION_WITH_ID(Socket);
00053 OW_DEFINE_EXCEPTION_WITH_ID(SocketTimeout);
00054 
00055 Socket::ShutDownMechanism_t Socket::s_shutDownMechanism = 0;
00056 
00058 Socket::Socket()
00059    : m_impl(new SocketImpl)
00060 {
00061 }
00063 Socket::Socket(const SSLClientCtxRef& sslCtx)
00064 {
00065    if (sslCtx)
00066    {
00067 #ifndef OW_NO_SSL
00068       m_impl = SocketBaseImplRef(new SSLSocketImpl(sslCtx));
00069 #else
00070       OW_THROW(SSLException, "Not built with SSL");
00071 #endif // #ifndef OW_NO_SSL
00072    }
00073    else
00074    {
00075       m_impl = SocketBaseImplRef(new SocketImpl);
00076    }
00077 }
00078 
00080 Socket::Socket(SocketFlags::ESSLFlag isSSL)
00081 {
00082    if (isSSL == SocketFlags::E_SSL)
00083    {
00084 #ifndef OW_NO_SSL
00085       m_impl = SocketBaseImplRef(new SSLSocketImpl);
00086 #else
00087       OW_THROW(SSLException, "Not built with SSL");
00088 #endif // #ifndef OW_NO_SSL
00089    }
00090    else
00091    {
00092       m_impl = SocketBaseImplRef(new SocketImpl);
00093    }
00094 }
00096 Socket::Socket(SocketHandle_t fd,
00097    SocketAddress::AddressType addrType, SocketFlags::ESSLFlag isSSL)
00098 {
00099    if (isSSL == SocketFlags::E_SSL)
00100    {
00101 #ifndef OW_NO_SSL
00102       m_impl = SocketBaseImplRef(new SSLSocketImpl(fd, addrType));
00103 #else
00104       OW_THROW(SSLException, "Not built with SSL");
00105 #endif // #ifndef OW_NO_SSL
00106    }
00107    else
00108    {
00109       m_impl = SocketBaseImplRef(new SocketImpl(fd, addrType));
00110    }
00111 }
00113 // Used by ServerSocket2::accept()
00114 Socket::Socket(SocketHandle_t fd,
00115    SocketAddress::AddressType addrType, const SSLServerCtxRef& sslCtx)
00116 {
00117    if (sslCtx)
00118    {
00119 #ifndef OW_NO_SSL
00120       m_impl = SocketBaseImplRef(new SSLSocketImpl(fd, addrType, sslCtx));
00121 #else
00122       OW_THROW(SSLException, "Not built with SSL");
00123 #endif // #ifndef OW_NO_SSL
00124    }
00125    else
00126    {
00127       m_impl = SocketBaseImplRef(new SocketImpl(fd, addrType));
00128    }
00129 }
00131 Socket::Socket(const SocketAddress& addr, SocketFlags::ESSLFlag isSSL)
00132 {
00133    if (isSSL == SocketFlags::E_SSL)
00134    {
00135 #ifndef OW_NO_SSL
00136       m_impl = SocketBaseImplRef(new SSLSocketImpl(addr));
00137 #else
00138       OW_THROW(SSLException, "Not built with SSL");
00139 #endif // #ifndef OW_NO_SSL
00140    }
00141    else
00142    {
00143       m_impl = SocketBaseImplRef(new SocketImpl(addr));
00144    }
00145 }
00146 static bool b_gotShutDown = false;
00147 static Mutex shutdownMutex;
00149 // STATIC
00150 void
00151 Socket::shutdownAllSockets()
00152 {
00153    MutexLock mlock(shutdownMutex);
00154 
00155    OW_ASSERT(s_shutDownMechanism != 0);
00156    if (!s_shutDownMechanism)
00157    {
00158       return;
00159    }
00160 
00161    b_gotShutDown = true;
00162 #if defined(OW_WIN32)
00163    ::SetEvent(s_shutDownMechanism);
00164 #else
00165    if (s_shutDownMechanism->writeString("die!") == -1)
00166    {
00167       OW_THROW_ERRNO_MSG(IOException, "Failed writing to socket shutdown pipe");
00168    }
00169 #endif
00170 }
00172 // STATIC
00173 void
00174 Socket::createShutDownMechanism()
00175 {
00176    MutexLock mlock(shutdownMutex);
00177    if (!s_shutDownMechanism)
00178    {
00179 #if defined(OW_WIN32)
00180       s_shutDownMechanism = ::CreateEvent(0, TRUE, FALSE, 0);
00181       OW_ASSERT(s_shutDownMechanism != 0);
00182 #else
00183       s_shutDownMechanism = UnnamedPipe::createUnnamedPipe();
00184       s_shutDownMechanism->setBlocking(UnnamedPipe::E_NONBLOCKING);
00185 #endif
00186       b_gotShutDown = false;
00187    }
00188 }
00190 // STATIC
00191 void
00192 Socket::deleteShutDownMechanism()
00193 {
00194    MutexLock mlock(shutdownMutex);
00195    if (s_shutDownMechanism)
00196    {
00197 #if defined(OW_WIN32)
00198       ::CloseHandle(s_shutDownMechanism);
00199 #endif
00200       s_shutDownMechanism = 0;
00201    }
00202 }
00204 // STATIC
00205 bool
00206 Socket::gotShutDown()
00207 {
00208    MutexLock mlock(shutdownMutex);
00209    return b_gotShutDown;
00210 }
00211 #ifndef OW_NO_SSL
00212 SSL*
00213 Socket::getSSL() const
00214 {
00215    IntrusiveReference<SSLSocketImpl> sslsock = m_impl.cast_to<SSLSocketImpl>(); 
00216    if (!sslsock)
00217    {
00218       return 0; 
00219    }
00220    return sslsock->getSSL(); 
00221 }
00222 
00224 bool
00225 Socket::peerCertVerified() const
00226 {
00227     IntrusiveReference<SSLSocketImpl> sslsock = m_impl.cast_to<SSLSocketImpl>(); 
00228     if (!sslsock)
00229     {
00230             return false; 
00231     }
00232     return sslsock->peerCertVerified(); 
00233 }
00234 #endif
00235 
00236 } // end namespace OW_NAMESPACE
00237 

Generated on Thu Feb 9 08:48:11 2006 for openwbem by  doxygen 1.4.6