added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / ObjectStore / src / sync / impl / apple / system_configuration.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2017 Realm Inc.
4 //
5 // Licensed under the Apache License, Version 2.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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 #include "sync/impl/apple/system_configuration.hpp"
20
21 #if NETWORK_REACHABILITY_AVAILABLE
22
23 #include <asl.h>
24 #include "dlfcn.h"
25
26 using namespace realm;
27 using namespace realm::_impl;
28
29 SystemConfiguration::SystemConfiguration()
30 {
31     m_framework_handle = dlopen("/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration", RTLD_LAZY);
32
33     if (m_framework_handle) {
34         m_create_with_name = (create_with_name_t)dlsym(m_framework_handle, "SCNetworkReachabilityCreateWithName");
35         m_create_with_address = (create_with_address_t)dlsym(m_framework_handle, "SCNetworkReachabilityCreateWithAddress");
36         m_set_dispatch_queue = (set_dispatch_queue_t)dlsym(m_framework_handle, "SCNetworkReachabilitySetDispatchQueue");
37         m_set_callback = (set_callback_t)dlsym(m_framework_handle, "SCNetworkReachabilitySetCallback");
38         m_get_flags = (get_flags_t)dlsym(m_framework_handle, "SCNetworkReachabilityGetFlags");
39     } else {
40 #pragma clang diagnostic push
41 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
42         asl_log(nullptr, nullptr, ASL_LEVEL_WARNING, "network reachability is not available");
43 #pragma clang diagnostic pop
44     }
45 }
46
47 SystemConfiguration& SystemConfiguration::shared()
48 {
49     static SystemConfiguration system_configuration;
50
51     return system_configuration;
52 }
53
54 SCNetworkReachabilityRef SystemConfiguration::network_reachability_create_with_name(CFAllocatorRef allocator,
55                                                                                     const char *hostname)
56 {
57     if (m_create_with_name)
58         return m_create_with_name(allocator, hostname);
59
60     return nullptr;
61 }
62
63 SCNetworkReachabilityRef SystemConfiguration::network_reachability_create_with_address(CFAllocatorRef allocator,
64                                                                                        const sockaddr *address)
65 {
66     if (m_create_with_address)
67         return m_create_with_address(allocator, address);
68
69     return nullptr;
70 }
71
72 bool SystemConfiguration::network_reachability_set_dispatch_queue(SCNetworkReachabilityRef target, dispatch_queue_t queue)
73 {
74     if (m_set_dispatch_queue)
75         return m_set_dispatch_queue(target, queue);
76
77     return false;
78 }
79
80 bool SystemConfiguration::network_reachability_set_callback(SCNetworkReachabilityRef target,
81                                                             SCNetworkReachabilityCallBack callback,
82                                                             SCNetworkReachabilityContext *context)
83 {
84     if (m_set_callback)
85         return m_set_callback(target, callback, context);
86
87     return false;
88 }
89
90 bool SystemConfiguration::network_reachability_get_flags(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags *flags)
91 {
92     if (m_get_flags)
93         return m_get_flags(target, flags);
94
95     return false;
96 }
97
98 #endif // NETWORK_REACHABILITY_AVAILABLE