00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
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
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
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
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
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 }
00237