OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #if defined(_MSC_VER) && _MSC_VER < 1300 | 11 #if defined(_MSC_VER) && _MSC_VER < 1300 |
12 #pragma warning(disable:4786) | 12 #pragma warning(disable:4786) |
13 #endif | 13 #endif |
14 | 14 |
15 #include <assert.h> | 15 #include <assert.h> |
16 | 16 |
17 #ifdef MEMORY_SANITIZER | 17 #ifdef MEMORY_SANITIZER |
18 #include <sanitizer/msan_interface.h> | 18 #include <sanitizer/msan_interface.h> |
19 #endif | 19 #endif |
20 | 20 |
21 #if defined(WEBRTC_POSIX) | 21 #if defined(WEBRTC_POSIX) |
22 #include <dlfcn.h> | |
22 #include <string.h> | 23 #include <string.h> |
23 #include <errno.h> | 24 #include <errno.h> |
24 #include <fcntl.h> | 25 #include <fcntl.h> |
25 #include <sys/time.h> | 26 #include <sys/time.h> |
26 #include <sys/select.h> | 27 #include <sys/select.h> |
27 #include <unistd.h> | 28 #include <unistd.h> |
28 #include <signal.h> | 29 #include <signal.h> |
29 #endif | 30 #endif |
30 | 31 |
31 #if defined(WEBRTC_WIN) | 32 #if defined(WEBRTC_WIN) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 68, // Official minimum | 90 68, // Official minimum |
90 0, // End of list marker | 91 0, // End of list marker |
91 }; | 92 }; |
92 | 93 |
93 static const int IP_HEADER_SIZE = 20u; | 94 static const int IP_HEADER_SIZE = 20u; |
94 static const int IPV6_HEADER_SIZE = 40u; | 95 static const int IPV6_HEADER_SIZE = 40u; |
95 static const int ICMP_HEADER_SIZE = 8u; | 96 static const int ICMP_HEADER_SIZE = 8u; |
96 static const int ICMP_PING_TIMEOUT_MILLIS = 10000u; | 97 static const int ICMP_PING_TIMEOUT_MILLIS = 10000u; |
97 #endif | 98 #endif |
98 | 99 |
100 static const int ERR_NOT_IMPLEMENTED = -11; | |
101 | |
102 #if defined(ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD) | |
103 static const int ERR_NETWORK_CHANGED = -21; | |
104 static const int ERR_FAILED = -1; | |
105 static const uint32_t INVALID_NETWORK_HANDLE = 0xFFFFFFFF; | |
pthatcher1
2016/01/06 21:58:46
Where do these values come from?
honghaiz3
2016/01/12 20:36:42
From the Chromium code. I changed them to an enum
| |
106 #endif | |
107 | |
99 PhysicalSocket::PhysicalSocket(PhysicalSocketServer* ss, SOCKET s) | 108 PhysicalSocket::PhysicalSocket(PhysicalSocketServer* ss, SOCKET s) |
100 : ss_(ss), s_(s), enabled_events_(0), error_(0), | 109 : ss_(ss), s_(s), enabled_events_(0), error_(0), |
101 state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED), | 110 state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED), |
102 resolver_(nullptr) { | 111 resolver_(nullptr) { |
103 #if defined(WEBRTC_WIN) | 112 #if defined(WEBRTC_WIN) |
104 // EnsureWinsockInit() ensures that winsock is initialized. The default | 113 // EnsureWinsockInit() ensures that winsock is initialized. The default |
105 // version of this function doesn't do anything because winsock is | 114 // version of this function doesn't do anything because winsock is |
106 // initialized by constructor of a static object. If neccessary libjingle | 115 // initialized by constructor of a static object. If neccessary libjingle |
107 // users can link it with a different version of this function by replacing | 116 // users can link it with a different version of this function by replacing |
108 // win32socketinit.cc. See win32socketinit.cc for more details. | 117 // win32socketinit.cc. See win32socketinit.cc for more details. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 UpdateLastError(); | 179 UpdateLastError(); |
171 #if !defined(NDEBUG) | 180 #if !defined(NDEBUG) |
172 if (0 == err) { | 181 if (0 == err) { |
173 dbg_addr_ = "Bound @ "; | 182 dbg_addr_ = "Bound @ "; |
174 dbg_addr_.append(GetLocalAddress().ToString()); | 183 dbg_addr_.append(GetLocalAddress().ToString()); |
175 } | 184 } |
176 #endif | 185 #endif |
177 return err; | 186 return err; |
178 } | 187 } |
179 | 188 |
189 int PhysicalSocket::BindToNetwork(NetworkHandle network) { | |
190 #if defined(ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD) | |
191 LOG(LS_INFO) << "Bind socket " << s_ << " to network handle " << network; | |
192 // Android prior to Lollipop didn't have support for binding sockets to | |
193 // networks. However, it should not have reached here in that case | |
194 // because the default network handle is invalid. | |
195 ASSERT(network != INVALID_NETWORK_HANDLE); | |
196 | |
197 // NOTE: This does rely on Android implementation details, but | |
198 // these details are unlikely to change. | |
199 typedef int (*SetNetworkForSocket)(unsigned netId, int socketFd); | |
200 static SetNetworkForSocket setNetworkForSocket; | |
201 // This is racy, but all racers should come out with the same answer so it | |
202 // shouldn't matter. | |
203 if (setNetworkForSocket == nullptr) { | |
204 // Android's netd client library should always be loaded in our address | |
205 // space as it shims libc functions like connect(). | |
206 const std::string net_library_path = "libnetd_client.so"; | |
207 void* lib = dlopen(net_library_path.c_str(), RTLD_LAZY); | |
208 if (lib == nullptr) { | |
209 LOG(LS_ERROR) << "Library " << net_library_path << " not found!"; | |
210 return ERR_NOT_IMPLEMENTED; | |
211 } | |
212 setNetworkForSocket = reinterpret_cast<SetNetworkForSocket>( | |
213 dlsym(lib, "setNetworkForSocket")); | |
214 } | |
215 if (setNetworkForSocket == nullptr) { | |
216 LOG(LS_ERROR) << "Symbol setNetworkForSocket not found "; | |
217 return ERR_NOT_IMPLEMENTED; | |
218 } | |
219 int rv = setNetworkForSocket(network, s_); | |
220 // If |network| has since disconnected, |rv| will be ENONET. Surface this as | |
221 // ERR_NETWORK_CHANGED, rather than MapSystemError(ENONET) which gives back | |
222 // the less descriptive ERR_FAILED. | |
223 if (rv == ENONET) | |
224 return ERR_NETWORK_CHANGED; | |
225 return ERR_FAILED; | |
pthatcher1
2016/01/06 21:58:46
For readability, can you move this into its own An
honghaiz3
2016/01/12 20:36:42
Done.
| |
226 #else | |
227 ASSERT(false); | |
228 return ERR_NOT_IMPLEMENTED; | |
229 #endif | |
230 } | |
231 | |
180 int PhysicalSocket::Connect(const SocketAddress& addr) { | 232 int PhysicalSocket::Connect(const SocketAddress& addr) { |
181 // TODO(pthatcher): Implicit creation is required to reconnect... | 233 // TODO(pthatcher): Implicit creation is required to reconnect... |
182 // ...but should we make it more explicit? | 234 // ...but should we make it more explicit? |
183 if (state_ != CS_CLOSED) { | 235 if (state_ != CS_CLOSED) { |
184 SetError(EALREADY); | 236 SetError(EALREADY); |
185 return SOCKET_ERROR; | 237 return SOCKET_ERROR; |
186 } | 238 } |
187 if (addr.IsUnresolvedIP()) { | 239 if (addr.IsUnresolvedIP()) { |
188 LOG(LS_VERBOSE) << "Resolving addr in PhysicalSocket::Connect"; | 240 LOG(LS_VERBOSE) << "Resolving addr in PhysicalSocket::Connect"; |
189 resolver_ = new AsyncResolver(); | 241 resolver_ = new AsyncResolver(); |
(...skipping 930 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1120 return false; | 1172 return false; |
1121 | 1173 |
1122 state_ = CS_CLOSED; | 1174 state_ = CS_CLOSED; |
1123 signal_close_ = false; | 1175 signal_close_ = false; |
1124 SignalCloseEvent(this, signal_err_); | 1176 SignalCloseEvent(this, signal_err_); |
1125 return true; | 1177 return true; |
1126 } | 1178 } |
1127 | 1179 |
1128 int SocketDispatcher::next_id_ = 0; | 1180 int SocketDispatcher::next_id_ = 0; |
1129 | 1181 |
1130 #endif // WEBRTC_WIN | 1182 #endif // WEBRTC_WIN |
1131 | 1183 |
1132 // Sets the value of a boolean value to false when signaled. | 1184 // Sets the value of a boolean value to false when signaled. |
1133 class Signaler : public EventDispatcher { | 1185 class Signaler : public EventDispatcher { |
1134 public: | 1186 public: |
1135 Signaler(PhysicalSocketServer* ss, bool* pf) | 1187 Signaler(PhysicalSocketServer* ss, bool* pf) |
1136 : EventDispatcher(ss), pf_(pf) { | 1188 : EventDispatcher(ss), pf_(pf) { |
1137 } | 1189 } |
1138 ~Signaler() override { } | 1190 ~Signaler() override { } |
1139 | 1191 |
1140 void OnEvent(uint32_t ff, int err) override { | 1192 void OnEvent(uint32_t ff, int err) override { |
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1614 break; | 1666 break; |
1615 cmsElapsed = TimeSince(msStart); | 1667 cmsElapsed = TimeSince(msStart); |
1616 if ((cmsWait != kForever) && (cmsElapsed >= cmsWait)) { | 1668 if ((cmsWait != kForever) && (cmsElapsed >= cmsWait)) { |
1617 break; | 1669 break; |
1618 } | 1670 } |
1619 } | 1671 } |
1620 | 1672 |
1621 // Done | 1673 // Done |
1622 return true; | 1674 return true; |
1623 } | 1675 } |
1624 #endif // WEBRTC_WIN | 1676 #endif // WEBRTC_WIN |
1625 | 1677 |
1626 } // namespace rtc | 1678 } // namespace rtc |
OLD | NEW |