Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 1 | //===-- Socket.cpp ----------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Host/Socket.h" |
| 11 | |
| 12 | #include "lldb/Core/Log.h" |
| 13 | #include "lldb/Core/RegularExpression.h" |
| 14 | #include "lldb/Host/Config.h" |
| 15 | #include "lldb/Host/Host.h" |
| 16 | #include "lldb/Host/SocketAddress.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 17 | #include "lldb/Host/StringConvert.h" |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 18 | #include "lldb/Host/TimeValue.h" |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 19 | #include "lldb/Host/common/TCPSocket.h" |
| 20 | #include "lldb/Host/common/UDPSocket.h" |
| 21 | |
| 22 | #ifndef LLDB_DISABLE_POSIX |
| 23 | #include "lldb/Host/posix/DomainSocket.h" |
| 24 | |
| 25 | #include <arpa/inet.h> |
| 26 | #include <netdb.h> |
| 27 | #include <netinet/in.h> |
| 28 | #include <netinet/tcp.h> |
| 29 | #include <sys/socket.h> |
| 30 | #include <sys/un.h> |
| 31 | #endif |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 32 | |
Shawn Best | 8da0bf3 | 2014-11-08 01:41:49 +0000 | [diff] [blame] | 33 | #ifdef __ANDROID_NDK__ |
| 34 | #include <linux/tcp.h> |
| 35 | #include <bits/error_constants.h> |
| 36 | #include <asm-generic/errno-base.h> |
| 37 | #include <errno.h> |
| 38 | #include <arpa/inet.h> |
Chaoren Lin | e271658 | 2015-07-15 19:22:12 +0000 | [diff] [blame] | 39 | #if defined(ANDROID_ARM_BUILD_STATIC) |
| 40 | #include <unistd.h> |
| 41 | #include <sys/syscall.h> |
Tamas Berghammer | 2875318 | 2015-07-16 12:35:04 +0000 | [diff] [blame] | 42 | #include <fcntl.h> |
Chaoren Lin | e271658 | 2015-07-15 19:22:12 +0000 | [diff] [blame] | 43 | #endif // ANDROID_ARM_BUILD_STATIC |
| 44 | #endif // __ANDROID_NDK__ |
Shawn Best | 8da0bf3 | 2014-11-08 01:41:49 +0000 | [diff] [blame] | 45 | |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 46 | using namespace lldb; |
| 47 | using namespace lldb_private; |
| 48 | |
| 49 | #if defined(_WIN32) |
| 50 | typedef const char * set_socket_option_arg_type; |
| 51 | typedef char * get_socket_option_arg_type; |
| 52 | const NativeSocket Socket::kInvalidSocketValue = INVALID_SOCKET; |
| 53 | #else // #if defined(_WIN32) |
| 54 | typedef const void * set_socket_option_arg_type; |
| 55 | typedef void * get_socket_option_arg_type; |
| 56 | const NativeSocket Socket::kInvalidSocketValue = -1; |
| 57 | #endif // #if defined(_WIN32) |
| 58 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 59 | namespace { |
| 60 | |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 61 | bool IsInterrupted() |
| 62 | { |
| 63 | #if defined(_WIN32) |
| 64 | return ::WSAGetLastError() == WSAEINTR; |
| 65 | #else |
| 66 | return errno == EINTR; |
| 67 | #endif |
| 68 | } |
| 69 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 72 | Socket::Socket(NativeSocket socket, SocketProtocol protocol, bool should_close) |
| 73 | : IOObject(eFDTypeSocket, should_close) |
| 74 | , m_protocol(protocol) |
| 75 | , m_socket(socket) |
| 76 | { |
| 77 | |
| 78 | } |
| 79 | |
| 80 | Socket::~Socket() |
| 81 | { |
| 82 | Close(); |
| 83 | } |
| 84 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 85 | Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 86 | { |
Jason Molenda | 67e5ba3 | 2015-07-24 22:42:03 +0000 | [diff] [blame] | 87 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION)); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 88 | if (log) |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 89 | log->Printf ("Socket::%s (host/port = %s)", __FUNCTION__, host_and_port.data()); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 90 | |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 91 | Error error; |
| 92 | std::unique_ptr<TCPSocket> connect_socket(new TCPSocket(child_processes_inherit, error)); |
| 93 | if (error.Fail()) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 94 | return error; |
| 95 | |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 96 | error = connect_socket->Connect(host_and_port); |
| 97 | if (error.Success()) |
| 98 | socket = connect_socket.release(); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 99 | |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 100 | return error; |
| 101 | } |
| 102 | |
Greg Clayton | fceca9b | 2015-07-24 16:55:00 +0000 | [diff] [blame] | 103 | Error |
| 104 | Socket::TcpListen (llvm::StringRef host_and_port, |
| 105 | bool child_processes_inherit, |
| 106 | Socket *&socket, |
| 107 | Predicate<uint16_t>* predicate, |
| 108 | int backlog) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 109 | { |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 110 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); |
| 111 | if (log) |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 112 | log->Printf ("Socket::%s (%s)", __FUNCTION__, host_and_port.data()); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 113 | |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 114 | Error error; |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 115 | std::string host_str; |
| 116 | std::string port_str; |
| 117 | int32_t port = INT32_MIN; |
| 118 | if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error)) |
| 119 | return error; |
| 120 | |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 121 | std::unique_ptr<TCPSocket> listen_socket(new TCPSocket(child_processes_inherit, error)); |
| 122 | if (error.Fail()) |
| 123 | return error; |
Greg Clayton | fceca9b | 2015-07-24 16:55:00 +0000 | [diff] [blame] | 124 | |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 125 | error = listen_socket->Listen(host_and_port, backlog); |
| 126 | if (error.Success()) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 127 | { |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 128 | // We were asked to listen on port zero which means we |
| 129 | // must now read the actual port that was given to us |
| 130 | // as port zero is a special code for "find an open port |
| 131 | // for me". |
| 132 | if (port == 0) |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 133 | port = listen_socket->GetLocalPortNumber(); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 134 | |
| 135 | // Set the port predicate since when doing a listen://<host>:<port> |
| 136 | // it often needs to accept the incoming connection which is a blocking |
| 137 | // system call. Allowing access to the bound port using a predicate allows |
| 138 | // us to wait for the port predicate to be set to a non-zero value from |
| 139 | // another thread in an efficient manor. |
| 140 | if (predicate) |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 141 | predicate->SetValue (port, eBroadcastAlways); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 142 | socket = listen_socket.release(); |
| 143 | } |
| 144 | |
| 145 | return error; |
| 146 | } |
| 147 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 148 | Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 149 | { |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 150 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); |
| 151 | if (log) |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 152 | log->Printf ("Socket::%s (host/port = %s)", __FUNCTION__, host_and_port.data()); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 153 | |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 154 | return UDPSocket::Connect(host_and_port, child_processes_inherit, send_socket, recv_socket); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 157 | Error Socket::UnixDomainConnect(llvm::StringRef name, bool child_processes_inherit, Socket *&socket) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 158 | { |
| 159 | Error error; |
| 160 | #ifndef LLDB_DISABLE_POSIX |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 161 | std::unique_ptr<DomainSocket> connect_socket(new DomainSocket(child_processes_inherit, error)); |
| 162 | if (error.Fail()) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 163 | return error; |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 164 | |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 165 | error = connect_socket->Connect(name); |
| 166 | if (error.Success()) |
| 167 | socket = connect_socket.release(); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 168 | #else |
| 169 | error.SetErrorString("Unix domain sockets are not supported on this platform."); |
| 170 | #endif |
| 171 | return error; |
| 172 | } |
| 173 | |
Oleksiy Vyalov | 477e42a | 2014-11-14 16:25:18 +0000 | [diff] [blame] | 174 | Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 175 | { |
| 176 | Error error; |
| 177 | #ifndef LLDB_DISABLE_POSIX |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 178 | std::unique_ptr<DomainSocket> listen_socket(new DomainSocket(child_processes_inherit, error)); |
| 179 | if (error.Fail()) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 180 | return error; |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 181 | |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 182 | error = listen_socket->Listen(name, 5); |
| 183 | if (error.Fail()) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 184 | return error; |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 185 | |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 186 | error = listen_socket->Accept(name, child_processes_inherit, socket); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 187 | #else |
| 188 | error.SetErrorString("Unix domain sockets are not supported on this platform."); |
| 189 | #endif |
| 190 | return error; |
| 191 | } |
| 192 | |
| 193 | bool |
| 194 | Socket::DecodeHostAndPort(llvm::StringRef host_and_port, |
| 195 | std::string &host_str, |
| 196 | std::string &port_str, |
| 197 | int32_t& port, |
| 198 | Error *error_ptr) |
| 199 | { |
| 200 | static RegularExpression g_regex ("([^:]+):([0-9]+)"); |
| 201 | RegularExpression::Match regex_match(2); |
| 202 | if (g_regex.Execute (host_and_port.data(), ®ex_match)) |
| 203 | { |
| 204 | if (regex_match.GetMatchAtIndex (host_and_port.data(), 1, host_str) && |
| 205 | regex_match.GetMatchAtIndex (host_and_port.data(), 2, port_str)) |
| 206 | { |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 207 | bool ok = false; |
| 208 | port = StringConvert::ToUInt32 (port_str.c_str(), UINT32_MAX, 10, &ok); |
| 209 | if (ok && port < UINT16_MAX) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 210 | { |
| 211 | if (error_ptr) |
| 212 | error_ptr->Clear(); |
| 213 | return true; |
| 214 | } |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 215 | // port is too large |
| 216 | if (error_ptr) |
| 217 | error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port.data()); |
| 218 | return false; |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | // If this was unsuccessful, then check if it's simply a signed 32-bit integer, representing |
| 223 | // a port with an empty host. |
| 224 | host_str.clear(); |
| 225 | port_str.clear(); |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 226 | bool ok = false; |
| 227 | port = StringConvert::ToUInt32 (host_and_port.data(), UINT32_MAX, 10, &ok); |
| 228 | if (ok && port < UINT16_MAX) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 229 | { |
| 230 | port_str = host_and_port; |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 231 | if (error_ptr) |
| 232 | error_ptr->Clear(); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
| 236 | if (error_ptr) |
| 237 | error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port.data()); |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | IOObject::WaitableHandle Socket::GetWaitableHandle() |
| 242 | { |
| 243 | // TODO: On Windows, use WSAEventSelect |
| 244 | return m_socket; |
| 245 | } |
| 246 | |
| 247 | Error Socket::Read (void *buf, size_t &num_bytes) |
| 248 | { |
| 249 | Error error; |
| 250 | int bytes_received = 0; |
| 251 | do |
| 252 | { |
| 253 | bytes_received = ::recv (m_socket, static_cast<char *>(buf), num_bytes, 0); |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 254 | } while (bytes_received < 0 && IsInterrupted ()); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 255 | |
| 256 | if (bytes_received < 0) |
| 257 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 258 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 259 | num_bytes = 0; |
| 260 | } |
| 261 | else |
| 262 | num_bytes = bytes_received; |
| 263 | |
Jason Molenda | 67e5ba3 | 2015-07-24 22:42:03 +0000 | [diff] [blame] | 264 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION)); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 265 | if (log) |
| 266 | { |
| 267 | log->Printf ("%p Socket::Read() (socket = %" PRIu64 ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)", |
| 268 | static_cast<void*>(this), |
| 269 | static_cast<uint64_t>(m_socket), |
| 270 | buf, |
| 271 | static_cast<uint64_t>(num_bytes), |
| 272 | static_cast<int64_t>(bytes_received), |
| 273 | error.AsCString()); |
| 274 | } |
| 275 | |
| 276 | return error; |
| 277 | } |
| 278 | |
| 279 | Error Socket::Write (const void *buf, size_t &num_bytes) |
| 280 | { |
| 281 | Error error; |
| 282 | int bytes_sent = 0; |
| 283 | do |
| 284 | { |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 285 | bytes_sent = Send(buf, num_bytes); |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 286 | } while (bytes_sent < 0 && IsInterrupted ()); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 287 | |
| 288 | if (bytes_sent < 0) |
| 289 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 290 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 291 | num_bytes = 0; |
| 292 | } |
| 293 | else |
| 294 | num_bytes = bytes_sent; |
| 295 | |
Jason Molenda | 67e5ba3 | 2015-07-24 22:42:03 +0000 | [diff] [blame] | 296 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION)); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 297 | if (log) |
| 298 | { |
| 299 | log->Printf ("%p Socket::Write() (socket = %" PRIu64 ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)", |
| 300 | static_cast<void*>(this), |
| 301 | static_cast<uint64_t>(m_socket), |
| 302 | buf, |
| 303 | static_cast<uint64_t>(num_bytes), |
| 304 | static_cast<int64_t>(bytes_sent), |
| 305 | error.AsCString()); |
| 306 | } |
| 307 | |
| 308 | return error; |
| 309 | } |
| 310 | |
| 311 | Error Socket::PreDisconnect() |
| 312 | { |
| 313 | Error error; |
| 314 | return error; |
| 315 | } |
| 316 | |
| 317 | Error Socket::Close() |
| 318 | { |
| 319 | Error error; |
| 320 | if (!IsValid() || !m_should_close_fd) |
| 321 | return error; |
| 322 | |
| 323 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); |
| 324 | if (log) |
| 325 | log->Printf ("%p Socket::Close (fd = %i)", static_cast<void*>(this), m_socket); |
| 326 | |
| 327 | #if defined(_WIN32) |
| 328 | bool success = !!closesocket(m_socket); |
| 329 | #else |
| 330 | bool success = !!::close (m_socket); |
| 331 | #endif |
| 332 | // A reference to a FD was passed in, set it to an invalid value |
| 333 | m_socket = kInvalidSocketValue; |
| 334 | if (!success) |
| 335 | { |
Oleksiy Vyalov | 4e1588c | 2015-04-10 02:31:37 +0000 | [diff] [blame] | 336 | SetLastError (error); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | return error; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | int Socket::GetOption(int level, int option_name, int &option_value) |
| 344 | { |
| 345 | get_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value); |
| 346 | socklen_t option_value_size = sizeof(int); |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 347 | return ::getsockopt(m_socket, level, option_name, option_value_p, &option_value_size); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | int Socket::SetOption(int level, int option_name, int option_value) |
| 351 | { |
| 352 | set_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value); |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 353 | return ::setsockopt(m_socket, level, option_name, option_value_p, sizeof(option_value)); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 356 | size_t Socket::Send(const void *buf, const size_t num_bytes) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 357 | { |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 358 | return ::send (m_socket, static_cast<const char *>(buf), num_bytes, 0); |
| 359 | } |
| 360 | |
| 361 | void Socket::SetLastError(Error &error) |
| 362 | { |
| 363 | #if defined(_WIN32) |
| 364 | error.SetError(::WSAGetLastError(), lldb::eErrorTypeWin32); |
| 365 | #else |
| 366 | error.SetErrorToErrno(); |
| 367 | #endif |
| 368 | } |
| 369 | |
| 370 | NativeSocket |
| 371 | Socket::CreateSocket(const int domain, |
| 372 | const int type, |
| 373 | const int protocol, |
| 374 | bool child_processes_inherit, |
| 375 | Error& error) |
| 376 | { |
| 377 | error.Clear(); |
| 378 | auto socketType = type; |
| 379 | #ifdef SOCK_CLOEXEC |
| 380 | if (!child_processes_inherit) |
| 381 | socketType |= SOCK_CLOEXEC; |
| 382 | #endif |
| 383 | auto sock = ::socket (domain, socketType, protocol); |
| 384 | if (sock == kInvalidSocketValue) |
| 385 | SetLastError(error); |
| 386 | |
| 387 | return sock; |
| 388 | } |
| 389 | |
| 390 | NativeSocket |
| 391 | Socket::AcceptSocket(NativeSocket sockfd, |
| 392 | struct sockaddr *addr, |
| 393 | socklen_t *addrlen, |
| 394 | bool child_processes_inherit, |
| 395 | Error& error) |
| 396 | { |
| 397 | error.Clear(); |
| 398 | #if defined(ANDROID_ARM_BUILD_STATIC) |
| 399 | // Temporary workaround for statically linking Android lldb-server with the |
| 400 | // latest API. |
| 401 | int fd = syscall(__NR_accept, sockfd, addr, addrlen); |
| 402 | if (fd >= 0 && !child_processes_inherit) |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 403 | { |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 404 | int flags = ::fcntl(fd, F_GETFD); |
| 405 | if (flags != -1 && ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1) |
| 406 | return fd; |
| 407 | SetLastError(error); |
| 408 | close(fd); |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 409 | } |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 410 | return fd; |
| 411 | #elif defined(SOCK_CLOEXEC) |
| 412 | int flags = 0; |
| 413 | if (!child_processes_inherit) { |
| 414 | flags |= SOCK_CLOEXEC; |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 415 | } |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame^] | 416 | #if defined(__NetBSD__) |
| 417 | NativeSocket fd = ::paccept (sockfd, addr, addrlen, nullptr, flags); |
| 418 | #else |
| 419 | NativeSocket fd = ::accept4 (sockfd, addr, addrlen, flags); |
| 420 | #endif |
| 421 | #else |
| 422 | NativeSocket fd = ::accept (sockfd, addr, addrlen); |
| 423 | #endif |
| 424 | if (fd == kInvalidSocketValue) |
| 425 | SetLastError(error); |
| 426 | return fd; |
Vince Harron | 014bb7d | 2015-01-16 00:47:08 +0000 | [diff] [blame] | 427 | } |