1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
/**
* Copyright 2009-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#ifndef WIN32
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include "types.h"
#ifndef WIN32
#include <poll.h>
/* Wait on socket availability with a timeout
*
* Returns:
* 0 on success
* -1 on failure, but not critical enough to throw an exception
* 1.. on failure, and throw an exception. The return value is the error code */
int mongo_io_wait_with_timeout(int sock, int timeout, char **error_message)
{
/* Socket timeout behavior varies based on the following:
* - Negative => no timeout (i.e. block indefinitely)
* - Zero => not specified (use default)
* - Positive => used specified timeout
*
* Note: MONGO_CONNECTION_DEFAULT_CONNECT_TIMEOUT is used as the default
* because it is consistent with default_socket_value (used by streams). We
* don't actually reference the INI option here, since mcon has no PHP
* dependencies. */
if (timeout < 0) {
timeout = -1;
}
if (timeout == 0) {
timeout = MONGO_CONNECTION_DEFAULT_CONNECT_TIMEOUT;
}
while (1) {
struct pollfd fds[1];
int status;
fds[0].fd = sock;
fds[0].events = POLLIN | POLLHUP | POLLERR;
status = poll(fds, 1, timeout);
if (status == -1) {
/* on EINTR, retry - this resets the timeout however to its full length */
if (errno == EINTR) {
continue;
}
*error_message = strdup(strerror(errno));
return 13;
}
if (status == 0) {
*error_message = malloc(256);
snprintf(
*error_message, 256,
"cursor timed out (timeout: %d, status: %d)",
timeout, status
);
return 80;
}
if (status > 0 && !(fds[0].revents & POLLIN)) {
*error_message = strdup("Exceptional condition on socket");
return 17;
}
break;
}
return 0;
}
#else
/* Win32 doesn't have a working poll(), so we use the old select() branch. */
int mongo_io_wait_with_timeout(int sock, int timeout, char **error_message)
{
/* Socket timeout behavior varies based on the following:
* - Negative => no timeout (i.e. block indefinitely)
* - Zero => not specified (use default)
* - Positive => used specified timeout
*
* Note: MONGO_CONNECTION_DEFAULT_CONNECT_TIMEOUT is used as the default
* because it is consistent with default_socket_value (used by streams). We
* don't actually reference the INI option here, since mcon has no PHP
* dependencies. */
if (timeout == 0) {
timeout = MONGO_CONNECTION_DEFAULT_CONNECT_TIMEOUT;
}
while (1) {
int status;
struct timeval tval;
fd_set readfds, exceptfds;
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
FD_ZERO(&exceptfds);
FD_SET(sock, &exceptfds);
if (timeout >= 0) {
tval.tv_sec = timeout / 1000;
tval.tv_usec = (timeout % 1000) * 1000;
}
status = select(sock+1, &readfds, NULL, &exceptfds, timeout >= 0 ? &tval : NULL);
if (status == -1) {
/* on EINTR, retry - this resets the timeout however to its full length */
if (errno == EINTR) {
continue;
}
*error_message = strdup(strerror(errno));
return 13;
}
if (FD_ISSET(sock, &exceptfds)) {
*error_message = strdup("Exceptional condition on socket");
return 17;
}
if (status == 0 && !FD_ISSET(sock, &readfds)) {
*error_message = malloc(256);
snprintf(
*error_message, 256,
"cursor timed out (timeout: %d, time left: %ld:%ld, status: %d)",
timeout, (long) tval.tv_sec, (long) tval.tv_usec, status
);
return 80;
}
/* if our descriptor is ready break out */
if (FD_ISSET(sock, &readfds)) {
break;
}
}
return 0;
}
#endif
/* Low-level send function.
*
* Goes through the buffer sending 4K byte batches.
* On failure, sets errmsg to errno string and returns -1.
* On success, returns number of bytes sent.
* Does not attempt to reconnect nor throw any exceptions.
*
* On failure, the calling function is responsible for disconnecting */
int mongo_io_send(mongo_connection *con, mongo_server_options *options, void *data, int size, char **error_message)
{
int sent = 0, status = 1;
while (sent < size && status > 0) {
int len = 4096 < (size - sent) ? 4096 : size - sent;
status = send((int) (long)con->socket, (const char*)data + sent, len, 0);
if (status == -1) {
*error_message = strdup(strerror(errno));
return -1;
}
sent += status;
}
return sent;
}
/* Low-level receive functions.
*
* On failure, sets errmsg to errno string and returns -1.
* On success, returns number of bytes read.
* Does not attempt to reconnect nor throw any exceptions.
*
* On failure, the calling function is responsible for disconnecting */
int mongo_io_recv_header(mongo_connection *con, mongo_server_options *options, int timeout, void *data, int size, char **error_message)
{
int status = mongo_io_wait_with_timeout((int) (long)con->socket, timeout ? timeout : options->socketTimeoutMS, error_message);
if (status != 0) {
/* We don't care which failure it was, it just failed and the error_message has been set */
*error_message = strdup("Timed out waiting for header data");
return -80;
}
status = recv((int) (long) con->socket, data, size, 0);
if (status == -1) {
*error_message = strdup(strerror(errno));
#ifndef WIN32
if (errno == ECONNRESET) {
return -32;
} else {
return -31;
}
#else
return -31;
#endif
} else if (status == 0) {
*error_message = strdup("Remote server has closed the connection");
return -32;
}
return status;
}
int mongo_io_recv_data(mongo_connection *con, mongo_server_options *options, int timeout, void *data, int size, char **error_message)
{
int num = 1, received = 0;
/* this can return FAILED if there is just no more data from db */
while (received < size && num > 0) {
int len = 4096 < (size - received) ? 4096 : size - received;
if (mongo_io_wait_with_timeout((int) (long) con->socket, timeout ? timeout : options->socketTimeoutMS, error_message) != 0) {
/* We don't care which failure it was, it just failed */
return -31;
}
// windows gives a WSAEFAULT if you try to get more bytes
num = recv((int) (long)con->socket, (char*)data, len, 0);
if (num < 0) {
return -31;
}
data = (char*)data + num;
received += num;
}
return received;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/
|