| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #include <config.h> |
|---|
| 22 | |
|---|
| 23 | #include <xapian/error.h> |
|---|
| 24 | |
|---|
| 25 | #include "safeerrno.h" |
|---|
| 26 | #include "safefcntl.h" |
|---|
| 27 | #include "safeunistd.h" |
|---|
| 28 | |
|---|
| 29 | #include <string> |
|---|
| 30 | |
|---|
| 31 | #include "omassert.h" |
|---|
| 32 | #include "omdebug.h" |
|---|
| 33 | #include "omtime.h" |
|---|
| 34 | #include "remoteconnection.h" |
|---|
| 35 | #include "serialise.h" |
|---|
| 36 | |
|---|
| 37 | #ifndef __WIN32__ |
|---|
| 38 | # include "safesysselect.h" |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | using namespace std; |
|---|
| 42 | |
|---|
| 43 | #ifdef __WIN32__ |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | # if defined _MSC_VER && _MSC_VER >= 1400 && defined __STDC_SECURE_LIB__ |
|---|
| 48 | # include <stdlib.h> // For _set_invalid_parameter_handler(), etc. |
|---|
| 49 | # include <crtdbg.h> // For _CrtSetReportMode, etc. |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | static void dummy_handler(const wchar_t*, |
|---|
| 53 | const wchar_t*, |
|---|
| 54 | const wchar_t*, |
|---|
| 55 | unsigned int, |
|---|
| 56 | uintptr_t) |
|---|
| 57 | { |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | class MSVCIgnoreInvalidParameter { |
|---|
| 66 | _invalid_parameter_handler old_handler; |
|---|
| 67 | int old_report_mode; |
|---|
| 68 | |
|---|
| 69 | public: |
|---|
| 70 | MSVCIgnoreInvalidParameter() { |
|---|
| 71 | |
|---|
| 72 | old_handler = _set_invalid_parameter_handler(dummy_handler); |
|---|
| 73 | |
|---|
| 74 | old_report_mode = _CrtSetReportMode(_CRT_ASSERT, 0); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | ~MSVCIgnoreInvalidParameter() { |
|---|
| 78 | |
|---|
| 79 | _set_invalid_parameter_handler(old_handler); |
|---|
| 80 | _CrtSetReportMode(_CRT_ASSERT, old_report_mode); |
|---|
| 81 | } |
|---|
| 82 | }; |
|---|
| 83 | # else |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | struct MSVCIgnoreInvalidParameter { |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | MSVCIgnoreInvalidParameter() { } |
|---|
| 92 | }; |
|---|
| 93 | # endif |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | static HANDLE fd_to_handle(int fd) { |
|---|
| 97 | MSVCIgnoreInvalidParameter invalid_handle_value_is_ok; |
|---|
| 98 | HANDLE handle = (HANDLE)_get_osfhandle(fd); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | return (handle != INVALID_HANDLE_VALUE ? handle : (HANDLE)fd); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | static void close_fd_or_socket(int fd) { |
|---|
| 106 | MSVCIgnoreInvalidParameter invalid_fd_value_is_ok; |
|---|
| 107 | if (close(fd) == -1 && errno == EBADF) { |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | closesocket(fd); |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | #else |
|---|
| 114 | |
|---|
| 115 | inline void close_fd_or_socket(int fd) { close(fd); } |
|---|
| 116 | #endif |
|---|
| 117 | |
|---|
| 118 | RemoteConnection::RemoteConnection(int fdin_, int fdout_, |
|---|
| 119 | const string & context_) |
|---|
| 120 | : fdin(fdin_), fdout(fdout_), context(context_) |
|---|
| 121 | { |
|---|
| 122 | #ifdef __WIN32__ |
|---|
| 123 | memset(&overlapped, 0, sizeof(overlapped)); |
|---|
| 124 | overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); |
|---|
| 125 | if (!overlapped.hEvent) |
|---|
| 126 | throw Xapian::NetworkError("Failed to setup OVERLAPPED", |
|---|
| 127 | context, -(int)GetLastError()); |
|---|
| 128 | |
|---|
| 129 | #endif |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | RemoteConnection::~RemoteConnection() |
|---|
| 133 | { |
|---|
| 134 | #ifdef __WIN32__ |
|---|
| 135 | if (overlapped.hEvent) |
|---|
| 136 | CloseHandle(overlapped.hEvent); |
|---|
| 137 | #endif |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void |
|---|
| 141 | RemoteConnection::read_at_least(size_t min_len, const OmTime & end_time) |
|---|
| 142 | { |
|---|
| 143 | DEBUGCALL(REMOTE, string, "RemoteConnection::read_at_least", |
|---|
| 144 | min_len << ", " << end_time); |
|---|
| 145 | |
|---|
| 146 | if (buffer.length() >= min_len) return; |
|---|
| 147 | |
|---|
| 148 | #ifdef __WIN32__ |
|---|
| 149 | HANDLE hin = fd_to_handle(fdin); |
|---|
| 150 | do { |
|---|
| 151 | char buf[4096]; |
|---|
| 152 | DWORD received; |
|---|
| 153 | BOOL ok = ReadFile(hin, buf, sizeof(buf), &received, &overlapped); |
|---|
| 154 | if (!ok) { |
|---|
| 155 | int errcode = GetLastError(); |
|---|
| 156 | if (errcode != ERROR_IO_PENDING) |
|---|
| 157 | throw Xapian::NetworkError("read failed", context, -errcode); |
|---|
| 158 | |
|---|
| 159 | DWORD waitrc; |
|---|
| 160 | waitrc = WaitForSingleObject(overlapped.hEvent, calc_read_wait_msecs(end_time)); |
|---|
| 161 | if (waitrc != WAIT_OBJECT_0) { |
|---|
| 162 | DEBUGLINE(REMOTE, "read: timeout has expired"); |
|---|
| 163 | throw Xapian::NetworkTimeoutError("Timeout expired while trying to read", context); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | if (!GetOverlappedResult(hin, &overlapped, &received, FALSE)) |
|---|
| 167 | throw Xapian::NetworkError("Failed to get overlapped result", |
|---|
| 168 | context, -(int)GetLastError()); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | if (received == 0) |
|---|
| 172 | throw Xapian::NetworkError("Received EOF", context); |
|---|
| 173 | |
|---|
| 174 | buffer.append(buf, received); |
|---|
| 175 | } while (buffer.length() < min_len); |
|---|
| 176 | #else |
|---|
| 177 | |
|---|
| 178 | if (fcntl(fdin, F_SETFL, end_time.is_set() ? O_NONBLOCK : 0) < 0) { |
|---|
| 179 | throw Xapian::NetworkError("Failed to set fdin non-blocking-ness", |
|---|
| 180 | context, errno); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | while (true) { |
|---|
| 184 | char buf[4096]; |
|---|
| 185 | ssize_t received = read(fdin, buf, sizeof(buf)); |
|---|
| 186 | |
|---|
| 187 | if (received > 0) { |
|---|
| 188 | buffer.append(buf, received); |
|---|
| 189 | if (buffer.length() >= min_len) return; |
|---|
| 190 | continue; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | if (received == 0) |
|---|
| 194 | throw Xapian::NetworkError("Received EOF", context); |
|---|
| 195 | |
|---|
| 196 | DEBUGLINE(REMOTE, "read gave errno = " << strerror(errno)); |
|---|
| 197 | if (errno == EINTR) continue; |
|---|
| 198 | |
|---|
| 199 | if (errno != EAGAIN) |
|---|
| 200 | throw Xapian::NetworkError("read failed", context, errno); |
|---|
| 201 | |
|---|
| 202 | Assert(end_time.is_set()); |
|---|
| 203 | while (true) { |
|---|
| 204 | |
|---|
| 205 | OmTime time_diff = end_time - OmTime::now(); |
|---|
| 206 | |
|---|
| 207 | if (time_diff.sec < 0) { |
|---|
| 208 | DEBUGLINE(REMOTE, "read: timeout has expired"); |
|---|
| 209 | throw Xapian::NetworkTimeoutError("Timeout expired while trying to read", context); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | struct timeval tv; |
|---|
| 213 | tv.tv_sec = time_diff.sec; |
|---|
| 214 | tv.tv_usec = time_diff.usec; |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | fd_set fdset; |
|---|
| 218 | FD_ZERO(&fdset); |
|---|
| 219 | FD_SET(fdin, &fdset); |
|---|
| 220 | |
|---|
| 221 | int select_result = select(fdin + 1, &fdset, 0, &fdset, &tv); |
|---|
| 222 | if (select_result > 0) break; |
|---|
| 223 | |
|---|
| 224 | if (select_result == 0) |
|---|
| 225 | throw Xapian::NetworkTimeoutError("Timeout expired while trying to read", context); |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | if (errno != EINTR) |
|---|
| 229 | throw Xapian::NetworkError("select failed during read", context, errno); |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | #endif |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | bool |
|---|
| 236 | RemoteConnection::ready_to_read() const |
|---|
| 237 | { |
|---|
| 238 | DEBUGCALL(REMOTE, bool, "RemoteConnection::ready_to_read", ""); |
|---|
| 239 | |
|---|
| 240 | if (!buffer.empty()) RETURN(true); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | fd_set fdset; |
|---|
| 244 | FD_ZERO(&fdset); |
|---|
| 245 | FD_SET(fdin, &fdset); |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | struct timeval tv; |
|---|
| 251 | tv.tv_sec = 0; |
|---|
| 252 | tv.tv_usec = 100000; |
|---|
| 253 | RETURN(select(fdin + 1, &fdset, 0, &fdset, &tv) > 0); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | void |
|---|
| 257 | RemoteConnection::send_message(char type, const string &message, const OmTime & end_time) |
|---|
| 258 | { |
|---|
| 259 | DEBUGCALL(REMOTE, void, "RemoteConnection::send_message", |
|---|
| 260 | type << ", " << message << ", " << end_time); |
|---|
| 261 | |
|---|
| 262 | string header; |
|---|
| 263 | header += type; |
|---|
| 264 | header += encode_length(message.size()); |
|---|
| 265 | |
|---|
| 266 | #ifdef __WIN32__ |
|---|
| 267 | HANDLE hout = fd_to_handle(fdout); |
|---|
| 268 | const string * str = &header; |
|---|
| 269 | |
|---|
| 270 | size_t count = 0; |
|---|
| 271 | while (true) { |
|---|
| 272 | DWORD n; |
|---|
| 273 | BOOL ok = WriteFile(hout, str->data() + count, str->size() - count, &n, &overlapped); |
|---|
| 274 | if (!ok) { |
|---|
| 275 | int errcode = GetLastError(); |
|---|
| 276 | if (errcode != ERROR_IO_PENDING) |
|---|
| 277 | throw Xapian::NetworkError("write failed", context, -errcode); |
|---|
| 278 | |
|---|
| 279 | DWORD waitrc; |
|---|
| 280 | waitrc = WaitForSingleObject(overlapped.hEvent, calc_read_wait_msecs(end_time)); |
|---|
| 281 | if (waitrc != WAIT_OBJECT_0) { |
|---|
| 282 | DEBUGLINE(REMOTE, "write: timeout has expired"); |
|---|
| 283 | throw Xapian::NetworkTimeoutError("Timeout expired while trying to write", context); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | if (!GetOverlappedResult(hout, &overlapped, &n, FALSE)) |
|---|
| 287 | throw Xapian::NetworkError("Failed to get overlapped result", |
|---|
| 288 | context, -(int)GetLastError()); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | count += n; |
|---|
| 292 | if (count == str->size()) { |
|---|
| 293 | if (str == &message || message.empty()) return; |
|---|
| 294 | str = &message; |
|---|
| 295 | count = 0; |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | #else |
|---|
| 299 | |
|---|
| 300 | if (fcntl(fdout, F_SETFL, end_time.is_set() ? O_NONBLOCK : 0) < 0) { |
|---|
| 301 | throw Xapian::NetworkError("Failed to set fdout non-blocking-ness", |
|---|
| 302 | context, errno); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | const string * str = &header; |
|---|
| 306 | |
|---|
| 307 | fd_set fdset; |
|---|
| 308 | size_t count = 0; |
|---|
| 309 | while (true) { |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | ssize_t n = write(fdout, str->data() + count, str->size() - count); |
|---|
| 313 | |
|---|
| 314 | if (n >= 0) { |
|---|
| 315 | count += n; |
|---|
| 316 | if (count == str->size()) { |
|---|
| 317 | if (str == &message || message.empty()) return; |
|---|
| 318 | str = &message; |
|---|
| 319 | count = 0; |
|---|
| 320 | } |
|---|
| 321 | continue; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | DEBUGLINE(REMOTE, "write gave errno = " << strerror(errno)); |
|---|
| 325 | if (errno == EINTR) continue; |
|---|
| 326 | |
|---|
| 327 | if (errno != EAGAIN) |
|---|
| 328 | throw Xapian::NetworkError("write failed", context, errno); |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | FD_ZERO(&fdset); |
|---|
| 332 | FD_SET(fdout, &fdset); |
|---|
| 333 | |
|---|
| 334 | OmTime time_diff(end_time - OmTime::now()); |
|---|
| 335 | if (time_diff.sec < 0) { |
|---|
| 336 | DEBUGLINE(REMOTE, "write: timeout has expired"); |
|---|
| 337 | throw Xapian::NetworkTimeoutError("Timeout expired while trying to write", context); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | struct timeval tv; |
|---|
| 341 | tv.tv_sec = time_diff.sec; |
|---|
| 342 | tv.tv_usec = time_diff.usec; |
|---|
| 343 | |
|---|
| 344 | int select_result = select(fdout + 1, 0, &fdset, &fdset, &tv); |
|---|
| 345 | |
|---|
| 346 | if (select_result < 0) { |
|---|
| 347 | if (errno == EINTR) { |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | continue; |
|---|
| 352 | } |
|---|
| 353 | throw Xapian::NetworkError("select failed during write", context, errno); |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | if (select_result == 0) |
|---|
| 357 | throw Xapian::NetworkTimeoutError("Timeout expired while trying to write", context); |
|---|
| 358 | } |
|---|
| 359 | #endif |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | char |
|---|
| 363 | RemoteConnection::get_message(string &result, const OmTime & end_time) |
|---|
| 364 | { |
|---|
| 365 | DEBUGCALL(REMOTE, char, "RemoteConnection::get_message", |
|---|
| 366 | "[result], " << end_time); |
|---|
| 367 | |
|---|
| 368 | read_at_least(2, end_time); |
|---|
| 369 | size_t len = static_cast<unsigned char>(buffer[1]); |
|---|
| 370 | read_at_least(len + 2, end_time); |
|---|
| 371 | if (len != 0xff) { |
|---|
| 372 | result.assign(buffer.data() + 2, len); |
|---|
| 373 | char type = buffer[0]; |
|---|
| 374 | buffer.erase(0, len + 2); |
|---|
| 375 | RETURN(type); |
|---|
| 376 | } |
|---|
| 377 | len = 0; |
|---|
| 378 | string::const_iterator i = buffer.begin() + 2; |
|---|
| 379 | unsigned char ch; |
|---|
| 380 | int shift = 0; |
|---|
| 381 | do { |
|---|
| 382 | if (i == buffer.end() || shift > 28) { |
|---|
| 383 | |
|---|
| 384 | throw Xapian::NetworkError("Insane message length specified!"); |
|---|
| 385 | } |
|---|
| 386 | ch = *i++; |
|---|
| 387 | len |= size_t(ch & 0x7f) << shift; |
|---|
| 388 | shift += 7; |
|---|
| 389 | } while ((ch & 0x80) == 0); |
|---|
| 390 | len += 255; |
|---|
| 391 | size_t header_len = (i - buffer.begin()); |
|---|
| 392 | read_at_least(header_len + len, end_time); |
|---|
| 393 | result.assign(buffer.data() + header_len, len); |
|---|
| 394 | char type = buffer[0]; |
|---|
| 395 | buffer.erase(0, header_len + len); |
|---|
| 396 | RETURN(type); |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | void |
|---|
| 400 | RemoteConnection::do_close(bool wait) |
|---|
| 401 | { |
|---|
| 402 | DEBUGCALL(REMOTE, void, "RemoteConnection::do_close", wait); |
|---|
| 403 | |
|---|
| 404 | if (fdout == -1) return; |
|---|
| 405 | |
|---|
| 406 | if (wait) { |
|---|
| 407 | try { |
|---|
| 408 | send_message(MSG_SHUTDOWN, string(), OmTime()); |
|---|
| 409 | } catch (...) { |
|---|
| 410 | } |
|---|
| 411 | #ifdef __WIN32__ |
|---|
| 412 | HANDLE hin = fd_to_handle(fdin); |
|---|
| 413 | char dummy; |
|---|
| 414 | DWORD received; |
|---|
| 415 | BOOL ok = ReadFile(hin, &dummy, 1, &received, &overlapped); |
|---|
| 416 | if (!ok && GetLastError() == ERROR_IO_PENDING) { |
|---|
| 417 | |
|---|
| 418 | (void)WaitForSingleObject(overlapped.hEvent, INFINITE); |
|---|
| 419 | } |
|---|
| 420 | #else |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | fd_set fdset; |
|---|
| 424 | FD_ZERO(&fdset); |
|---|
| 425 | FD_SET(fdin, &fdset); |
|---|
| 426 | int res; |
|---|
| 427 | do { |
|---|
| 428 | res = select(fdin + 1, &fdset, 0, &fdset, NULL); |
|---|
| 429 | } while (res < 0 && errno == EINTR); |
|---|
| 430 | #endif |
|---|
| 431 | } |
|---|
| 432 | close_fd_or_socket(fdin); |
|---|
| 433 | if (fdin != fdout) close_fd_or_socket(fdout); |
|---|
| 434 | fdout = -1; |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | #ifdef __WIN32__ |
|---|
| 438 | DWORD |
|---|
| 439 | RemoteConnection::calc_read_wait_msecs(const OmTime & end_time) |
|---|
| 440 | { |
|---|
| 441 | if (!end_time.is_set()) |
|---|
| 442 | return INFINITE; |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | OmTime now(OmTime::now()); |
|---|
| 446 | |
|---|
| 447 | DWORD msecs; |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | if (now > end_time) { |
|---|
| 451 | throw Xapian::NetworkTimeoutError("Timeout expired before starting read", context); |
|---|
| 452 | } |
|---|
| 453 | OmTime time_diff = end_time - now; |
|---|
| 454 | msecs = time_diff.sec * 1000 + time_diff.usec / 1000; |
|---|
| 455 | return msecs; |
|---|
| 456 | } |
|---|
| 457 | #endif |
|---|