2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FNetSock_SecureSocketManagedNetConnectionEventListenerImpl.cpp
20 * @brief This is the implementation file for _SecureSocketManagedNetConnectionEventListener class.
23 * This file contains the implementation of _SecureSocketManagedNetConnectionEventListener class.
28 #include <FBaseString.h>
29 #include <FBaseSysLog.h>
30 #include <FNetNetConnectionInfo.h>
31 #include <FBase_StringConverter.h>
32 #include "FNet_NetConnectionInfoImpl.h"
33 #include "FNetSock_SecureSocketEventArg.h"
34 #include "FNetSock_SecureSocketManagedNetConnectionEventListenerImpl.h"
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Runtime;
39 namespace Tizen { namespace Net { namespace Sockets
42 static const long _VERIFY_DEPTH = 9; // certificate verification depth 9
44 _SecureSocketManagedNetConnectionEventListener::_SecureSocketManagedNetConnectionEventListener(void)
47 , __pNetConnectionInfo(null)
48 , __socketFd(INVALID_HANDLE)
51 , __pSecureSocketEvent(null)
53 , __pSecureSocketImpl(null)
56 memset(&__remoteAddr, 0, sizeof(__remoteAddr));
57 __remoteAddr.sin_family = AF_INET;
58 __remoteAddr.sin_addr.s_addr = htonl(INADDR_ANY);
59 __remoteAddr.sin_port = htons(0);
62 _SecureSocketManagedNetConnectionEventListener::~_SecureSocketManagedNetConnectionEventListener(void)
64 __pSecureSocketEvent = null;
67 __socketFd = INVALID_HANDLE;
68 __pNetConnectionInfo = null;
69 __pSecureSocketImpl = null;
81 _SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionBearerChanged(ManagedNetConnection& managedNetConnection)
83 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener - OnManagedNetConnectionBearerChanged - Enter");
85 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener - OnManagedNetConnectionBearerChanged - End");
89 _SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionStarted(ManagedNetConnection& managedNetConnection)
91 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener - OnManagedNetConnectionStarted - Enter");
96 const _NetConnectionInfoImpl* pNCInfoImpl = null;
98 char* pDeviceName = null;
101 result r = E_SUCCESS;
103 __pNetConnectionInfo = managedNetConnection.GetNetConnectionInfo();
104 if (__pNetConnectionInfo == null)
106 SysLogException(NID_NET_SOCK, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] NetConnectionInfo is null. Failed to ManagedNetConnection GetNetConnectionInfo.");
110 pNCInfoImpl = _NetConnectionInfoImpl::GetInstance(*__pNetConnectionInfo);
112 deviceName = pNCInfoImpl->GetDeviceName();
114 if (deviceName.IsEmpty())
116 SysLog(NID_NET_SOCK, "DeviceName is empty string. - Emulator Case");
120 SysLog(NID_NET_SOCK, "DeviceName is not empty string.[Device name : %ls] - Target Case", deviceName.GetPointer());
122 pDeviceName = _StringConverter::CopyToCharArrayN(deviceName);
123 SysTryCatch(NID_NET_SOCK, pDeviceName != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
125 err = setsockopt(__socketFd, SOL_SOCKET, SO_BINDTODEVICE, pDeviceName, strlen(pDeviceName) + 1);
126 delete[] pDeviceName;
129 ConvertErrorToResult(errno);
131 SysLogException(NID_NET_SOCK, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] Failed to bind device name.");
139 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionStarted - Construct");
143 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionStarted - Connect");
144 err = connect(__socketFd, (struct sockaddr*) &__remoteAddr, sizeof(__remoteAddr));
148 r = ConvertErrorToResult(errno);
149 SysTryCatch(NID_NET_SOCK,r == E_WOULD_BLOCK, , r, "[%s] Failed to connect the socket.", GetErrorMessage(r));
153 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, "[%s] Failed to sslconnect the socket.",GetErrorMessage(r));
157 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionStarted - Listen");
158 err = listen(__socketFd, __backLog);
164 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener - OnManagedNetConnectionStarted - End");
171 _SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionStopped(ManagedNetConnection& managedNetConnection, NetConnectionStoppedReason reason)
173 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener - OnManagedNetConnectionStopped - Enter");
180 _SecureSocketEventArg* pSecureSocketEventArg = null;
185 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionStopped - Construct");
189 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionStopped - Connect");
192 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionStopped - Listen");
199 pSecureSocketEventArg = new (std::nothrow) _SecureSocketEventArg(__pSecureSocketImpl->__socketFd, NET_SOCKET_EVENT_CLOSE);
200 pSecureSocketEventArg->SetError(E_NETWORK_UNAVAILABLE);
201 __pSecureSocketEvent->Fire(*pSecureSocketEventArg);
203 if (__pSecureSocketImpl->__pGlibSocketInfo != null)
205 delete __pSecureSocketImpl->__pGlibSocketInfo;
206 __pSecureSocketImpl->__pGlibSocketInfo = null;
209 if (__pSecureSocketImpl->__socketFd > INVALID_HANDLE)
211 sockFd = __pSecureSocketImpl->__socketFd;
212 close(__pSecureSocketImpl->__socketFd);
213 __pSecureSocketImpl->__socketFd = INVALID_HANDLE;
214 SysLog(NID_NET_HTTP, "### SocketFd(%d) was closed..", sockFd);
217 SysLog(NID_NET_SOCK, "_SocketManagedNetConnectionEventListener - OnManagedNetConnectionStopped - End");
221 _SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionSuspended(ManagedNetConnection& managedNetConnection)
223 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener - OnManagedNetConnectionSuspended - Enter");
225 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener - OnManagedNetConnectionSuspended - End");
229 _SecureSocketManagedNetConnectionEventListener::OnManagedNetConnectionResumed(ManagedNetConnection& managedNetConnection)
231 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener - OnManagedNetConnectionResumed - Enter");
233 SysLog(NID_NET_SOCK, "_SecureSocketManagedNetConnectionEventListener - OnManagedNetConnectionResumed - End");
237 _SecureSocketManagedNetConnectionEventListener::SetConstructParams(_SecureSocketEvent* pSocketEventSet, HSocket socketFdSet, _SocketMethodFlag flagSet, _SecureSocketImpl* pSecuresSocketImplSet)
239 __pSecureSocketEvent = pSocketEventSet;
240 __socketFd = socketFdSet;
242 __pSecureSocketImpl = pSecuresSocketImplSet;
246 _SecureSocketManagedNetConnectionEventListener::SetConnectParams(HSocket socketFdSet, sockaddr_in remoteAddrSet, _SocketMethodFlag flagSet,
247 _SecureSocketImpl* pSecureSocketImplSet)
249 __socketFd = socketFdSet;
250 __remoteAddr = remoteAddrSet;
252 __pSecureSocketImpl = pSecureSocketImplSet;
256 _SecureSocketManagedNetConnectionEventListener::SetListenParams(HSocket __socketFdSet, int backLogSet, _SocketMethodFlag flagSet)
258 __socketFd = __socketFdSet;
259 __backLog = backLogSet;
264 _SecureSocketManagedNetConnectionEventListener::SetStartedFlag(bool isStartSet)
266 __isStarted = isStartSet;
270 _SecureSocketManagedNetConnectionEventListener::GetStartedFlag()
276 _SecureSocketManagedNetConnectionEventListener::SslConnect()
278 result r = E_SUCCESS;
283 __pSecureSocketImpl->__pSsl = SSL_new(__pSecureSocketImpl->__pSslCtx);
284 SysTryCatch(NID_NET_SOCK, __pSecureSocketImpl->__pSsl != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to create the SSL Object.");
286 SSL_set_fd(__pSecureSocketImpl->__pSsl, __socketFd);
288 SSL_set_options(__pSecureSocketImpl->__pSsl, SSL_OP_ALL);
290 if (__pSecureSocketImpl->__isNonblock == true)
292 SSL_CTX_set_client_cert_cb(__pSecureSocketImpl->__pSslCtx, (int (*)(SSL*, X509**, EVP_PKEY**)) _SecureSocketImpl::OnClientCertCallback);
294 SSL_set_mode(__pSecureSocketImpl->__pSsl, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
296 sslIndex = SSL_get_ex_new_index(0, (char*) "_SecureSocketImpl index", null, null, null);
298 SSL_set_ex_data(__pSecureSocketImpl->__pSsl, sslIndex, __pSecureSocketImpl);
300 __pSslKey = new Integer((int) __pSecureSocketImpl->__pSsl);
302 pthread_mutex_lock(&_SecureSocketImpl::__mapMutex);
303 _Singleton::GetSslMap()->Add(*(__pSslKey), *(new Integer((int) sslIndex)));
304 pthread_mutex_unlock(&_SecureSocketImpl::__mapMutex);
306 SSL_set_verify(__pSecureSocketImpl->__pSsl, SSL_VERIFY_PEER, (int (*)(int, X509_STORE_CTX*)) _SecureSocketImpl::OnVerifyPeerCallback);
307 // verify certification depth is set to VERIFY_DEPTH.
308 // Allows level 0: peer certificates.
309 // level 1: CA certificates.
310 // level 2: higher level CA certificates.
312 __pSecureSocketImpl->__sslDepth = _VERIFY_DEPTH;
313 SSL_set_verify_depth(__pSecureSocketImpl->__pSsl, __pSecureSocketImpl->__sslDepth);
316 __pThread = new (std::nothrow) Thread();
317 SysTryCatch(NID_NET_SOCK, __pThread != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
319 r = __pThread->Construct(*__pSecureSocketImpl);
320 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, GetErrorMessage(r));
323 r = __pThread->Start();
324 SysTryCatch(NID_NET_SOCK, r == E_SUCCESS, , r, GetErrorMessage(r));
328 if (__pSecureSocketImpl->__sslVerify == SECURE_SOCKET_VERIFY_NONE)
330 SSL_set_verify(__pSecureSocketImpl->__pSsl, SSL_VERIFY_PEER, null);
333 sslError = SSL_connect(__pSecureSocketImpl->__pSsl);
334 SysTryReturnResult(NID_NET_SOCK, sslError >= 1, E_SYSTEM, "Failed to perform the handshake.");
349 } } } // Tizen::Net::Sockets