1/*
2 * IXSocketFactory.cpp
3 * Author: Benjamin Sergeant
4 * Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
5 */
6
7#include "IXSocketFactory.h"
8
9#include "IXUniquePtr.h"
10#ifdef IXWEBSOCKET_USE_TLS
11
12#ifdef IXWEBSOCKET_USE_MBED_TLS
13#include "IXSocketMbedTLS.h"
14#elif defined(IXWEBSOCKET_USE_OPEN_SSL)
15#include "IXSocketOpenSSL.h"
16#elif __APPLE__
17#include "IXSocketAppleSSL.h"
18#endif
19
20#else
21
22#include "IXSocket.h"
23
24#endif
25
26namespace ix
27{
28 std::unique_ptr<Socket> createSocket(bool tls,
29 int fd,
30 std::string& errorMsg,
31 const SocketTLSOptions& tlsOptions)
32 {
33 (void) tlsOptions;
34 errorMsg.clear();
35 std::unique_ptr<Socket> socket;
36
37 if (!tls)
38 {
39 socket = ix::make_unique<Socket>(fd);
40 }
41 else
42 {
43#ifdef IXWEBSOCKET_USE_TLS
44#if defined(IXWEBSOCKET_USE_MBED_TLS)
45 socket = ix::make_unique<SocketMbedTLS>(tlsOptions, fd);
46#elif defined(IXWEBSOCKET_USE_OPEN_SSL)
47 socket = ix::make_unique<SocketOpenSSL>(tlsOptions, fd);
48#elif defined(__APPLE__)
49 socket = ix::make_unique<SocketAppleSSL>(tlsOptions, fd);
50#endif
51#else
52 errorMsg = "TLS support is not enabled on this platform.";
53 return nullptr;
54#endif
55 }
56
57 if (!socket->init(errorMsg))
58 {
59 socket.reset();
60 }
61
62 return socket;
63 }
64} // namespace ix
65