1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#include "apr_arch_networkio.h"
18
#include "apr_errno.h"
19
#include "apr_general.h"
20
#include "apr_network_io.h"
22
#include "apr_arch_file_io.h"
27
/* MAX_SEGMENT_SIZE is the maximum amount of data that will be sent to a client
28
* in one call of TransmitFile. This number must be small enough to give the
29
* slowest client time to receive the data before the socket timeout triggers.
30
* The same problem can exist with apr_socket_send(). In that case, we rely on
31
* the application to adjust socket timeouts and max send segment
32
* sizes appropriately.
33
* For example, Apache will in most cases call apr_socket_send() with less
36
#define MAX_SEGMENT_SIZE 65536
37
#define WSABUF_ON_STACK 50
39
APR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf,
47
wsaData.len = (u_long)*len;
48
wsaData.buf = (char*) buf;
51
rv = WSASend(sock->socketdes, &wsaData, 1, &dwBytes, 0, NULL, NULL);
53
rv = send(sock->socketdes, wsaData.buf, wsaData.len, 0);
56
if (rv == SOCKET_ERROR) {
57
lasterror = apr_get_netos_error();
68
APR_DECLARE(apr_status_t) apr_socket_recv(apr_socket_t *sock, char *buf,
77
wsaData.len = (u_long)*len;
78
wsaData.buf = (char*) buf;
81
rv = WSARecv(sock->socketdes, &wsaData, 1, &dwBytes, &flags, NULL, NULL);
83
rv = recv(sock->socketdes, wsaData.buf, wsaData.len, 0);
86
if (rv == SOCKET_ERROR) {
87
lasterror = apr_get_netos_error();
93
return dwBytes == 0 ? APR_EOF : APR_SUCCESS;
97
APR_DECLARE(apr_status_t) apr_socket_sendv(apr_socket_t *sock,
98
const struct iovec *vec,
99
apr_int32_t in_vec, apr_size_t *nbytes)
101
apr_status_t rc = APR_SUCCESS;
104
apr_int32_t nvec = 0;
109
for (i = 0; i < in_vec; i++) {
110
cur_len = vec[i].iov_len;
112
while (cur_len > APR_DWORD_MAX) {
114
cur_len -= APR_DWORD_MAX;
118
pWsaBuf = (nvec <= WSABUF_ON_STACK) ? _alloca(sizeof(WSABUF) * (nvec))
119
: malloc(sizeof(WSABUF) * (nvec));
123
for (i = 0; i < in_vec; i++) {
124
char * base = vec[i].iov_base;
125
cur_len = vec[i].iov_len;
128
if (cur_len > APR_DWORD_MAX) {
129
pWsaBuf[j].buf = base;
130
pWsaBuf[j].len = APR_DWORD_MAX;
131
cur_len -= APR_DWORD_MAX;
132
base += APR_DWORD_MAX;
135
pWsaBuf[j].buf = base;
136
pWsaBuf[j].len = (DWORD)cur_len;
141
} while (cur_len > 0);
144
rv = WSASend(sock->socketdes, pWsaBuf, nvec, &dwBytes, 0, NULL, NULL);
145
if (rv == SOCKET_ERROR) {
146
rc = apr_get_netos_error();
149
for (i = 0; i < nvec; i++) {
150
rv = send(sock->socketdes, pWsaBuf[i].buf, pWsaBuf[i].len, 0);
151
if (rv == SOCKET_ERROR) {
152
rc = apr_get_netos_error();
158
if (nvec > WSABUF_ON_STACK)
166
APR_DECLARE(apr_status_t) apr_socket_sendto(apr_socket_t *sock,
167
apr_sockaddr_t *where,
168
apr_int32_t flags, const char *buf,
173
rv = sendto(sock->socketdes, buf, (int)*len, flags,
174
(const struct sockaddr*)&where->sa,
176
if (rv == SOCKET_ERROR) {
178
return apr_get_netos_error();
186
APR_DECLARE(apr_status_t) apr_socket_recvfrom(apr_sockaddr_t *from,
189
char *buf, apr_size_t *len)
193
rv = recvfrom(sock->socketdes, buf, (int)*len, flags,
194
(struct sockaddr*)&from->sa, &from->salen);
195
if (rv == SOCKET_ERROR) {
197
return apr_get_netos_error();
200
if (rv == 0 && sock->type == SOCK_STREAM)
207
static apr_status_t collapse_iovec(char **off, apr_size_t *len,
208
struct iovec *iovec, int numvec,
209
char *buf, apr_size_t buflen)
212
*off = iovec[0].iov_base;
213
*len = iovec[0].iov_len;
217
for (i = 0; i < numvec; i++) {
218
*len += iovec[i].iov_len;
223
return APR_INCOMPLETE;
228
for (i = 0; i < numvec; i++) {
229
memcpy(buf, iovec[i].iov_base, iovec[i].iov_len);
230
buf += iovec[i].iov_len;
239
* apr_status_t apr_socket_sendfile(apr_socket_t *, apr_file_t *, apr_hdtr_t *,
240
* apr_off_t *, apr_size_t *, apr_int32_t flags)
241
* Send a file from an open file descriptor to a socket, along with
242
* optional headers and trailers
243
* arg 1) The socket to which we're writing
244
* arg 2) The open file from which to read
245
* arg 3) A structure containing the headers and trailers to send
246
* arg 4) Offset into the file where we should begin writing
247
* arg 5) Number of bytes to send out of the file
248
* arg 6) APR flags that are mapped to OS specific flags
250
APR_DECLARE(apr_status_t) apr_socket_sendfile(apr_socket_t *sock,
257
apr_status_t status = APR_SUCCESS;
259
apr_off_t curoff = *offset;
262
TRANSMIT_FILE_BUFFERS tfb, *ptfb = NULL;
264
apr_size_t bytes_to_send; /* Bytes to send out of the file (not including headers) */
265
int disconnected = 0;
266
int sendv_trailers = 0;
269
if (apr_os_level < APR_WIN_NT) {
273
/* Use len to keep track of number of total bytes sent (including headers) */
274
bytes_to_send = *len;
277
/* Handle the goofy case of sending headers/trailers and a zero byte file */
278
if (!bytes_to_send && hdtr) {
279
if (hdtr->numheaders) {
280
rv = apr_socket_sendv(sock, hdtr->headers, hdtr->numheaders,
282
if (rv != APR_SUCCESS)
286
if (hdtr->numtrailers) {
287
rv = apr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers,
289
if (rv != APR_SUCCESS)
296
memset(&tfb, '\0', sizeof (tfb));
298
/* Collapse the headers into a single buffer */
299
if (hdtr && hdtr->numheaders) {
300
apr_size_t head_length = tfb.HeadLength;
303
rv = collapse_iovec((char **)&ptfb->Head, &head_length,
304
hdtr->headers, hdtr->numheaders,
305
hdtrbuf, sizeof(hdtrbuf));
307
tfb.HeadLength = (DWORD)head_length;
309
/* If not enough buffer, punt to sendv */
310
if (rv == APR_INCOMPLETE) {
311
rv = apr_socket_sendv(sock, hdtr->headers, hdtr->numheaders, &nbytes);
312
if (rv != APR_SUCCESS)
319
/* Initialize the overlapped structure used on TransmitFile
321
if (!sock->overlapped) {
322
sock->overlapped = apr_pcalloc(sock->pool, sizeof(OVERLAPPED));
323
sock->overlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
325
while (bytes_to_send) {
328
if (bytes_to_send > MAX_SEGMENT_SIZE) {
329
xmitbytes = MAX_SEGMENT_SIZE;
332
/* Last call to TransmitFile() */
333
xmitbytes = (DWORD)bytes_to_send;
334
/* Collapse the trailers into a single buffer */
335
if (hdtr && hdtr->numtrailers) {
336
apr_size_t tail_length = tfb.TailLength;
338
rv = collapse_iovec((char**) &ptfb->Tail, &tail_length,
339
hdtr->trailers, hdtr->numtrailers,
340
hdtrbuf + ptfb->HeadLength,
341
sizeof(hdtrbuf) - ptfb->HeadLength);
343
tfb.TailLength = (DWORD)tail_length;
345
if (rv == APR_INCOMPLETE) {
346
/* If not enough buffer, punt to sendv, later */
350
/* Disconnect the socket after last send */
351
if ((flags & APR_SENDFILE_DISCONNECT_SOCKET)
352
&& !sendv_trailers) {
353
dwFlags |= TF_REUSE_SOCKET;
354
dwFlags |= TF_DISCONNECT;
359
sock->overlapped->Offset = (DWORD)(curoff);
360
#if APR_HAS_LARGE_FILES
361
sock->overlapped->OffsetHigh = (DWORD)(curoff >> 32);
363
/* XXX BoundsChecker claims dwFlags must not be zero. */
364
rv = TransmitFile(sock->socketdes, /* socket */
365
file->filehand, /* open file descriptor of the file to be sent */
366
xmitbytes, /* number of bytes to send. 0=send all */
367
0, /* Number of bytes per send. 0=use default */
368
sock->overlapped, /* OVERLAPPED structure */
369
ptfb, /* header and trailer buffers */
370
dwFlags); /* flags to control various aspects of TransmitFile */
372
status = apr_get_netos_error();
373
if ((status == APR_FROM_OS_ERROR(ERROR_IO_PENDING)) ||
374
(status == APR_FROM_OS_ERROR(WSA_IO_PENDING)))
376
rv = WaitForSingleObject(sock->overlapped->hEvent,
377
(DWORD)(sock->timeout >= 0
378
? sock->timeout_ms : INFINITE));
379
if (rv == WAIT_OBJECT_0) {
380
status = APR_SUCCESS;
382
if (!WSAGetOverlappedResult(sock->socketdes,
387
status = apr_get_netos_error();
389
/* Ugly code alert: WSAGetOverlappedResult returns
390
* a count of all bytes sent. This loop only
391
* tracks bytes sent out of the file.
394
xmitbytes -= (ptfb->HeadLength + ptfb->TailLength);
398
else if (rv == WAIT_TIMEOUT) {
399
status = APR_FROM_OS_ERROR(WAIT_TIMEOUT);
401
else if (rv == WAIT_ABANDONED) {
402
/* Hummm... WAIT_ABANDONDED is not an error code. It is
403
* a return specific to the Win32 WAIT functions that
404
* indicates that a thread exited while holding a
405
* mutex. Should consider triggering an assert
406
* to detect the condition...
408
status = APR_FROM_OS_ERROR(WAIT_TIMEOUT);
411
status = apr_get_os_error();
414
if (status != APR_SUCCESS)
417
bytes_to_send -= xmitbytes;
420
/* Adjust len for any headers/trailers sent */
422
*len += (ptfb->HeadLength + ptfb->TailLength);
423
memset(&tfb, '\0', sizeof (tfb));
428
if (status == APR_SUCCESS) {
429
if (sendv_trailers) {
430
rv = apr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers, &nbytes);
431
if (rv != APR_SUCCESS)
437
/* Mark the socket as disconnected, but do not close it.
438
* Note: The application must have stored the socket prior to making
439
* the call to apr_socket_sendfile in order to either reuse it
443
sock->disconnected = 1;
444
sock->socketdes = INVALID_SOCKET;