📄 sendrecv.c
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 "apr_arch_networkio.h"#include "apr_support.h"#if APR_HAS_SENDFILE/* This file is needed to allow us access to the apr_file_t internals. */#include "apr_arch_file_io.h"#endif /* APR_HAS_SENDFILE *//* osreldate.h is only needed on FreeBSD for sendfile detection */#if defined(__FreeBSD__)#include <osreldate.h>#endifapr_status_t apr_socket_send(apr_socket_t *sock, const char *buf, apr_size_t *len){ apr_ssize_t rv; if (sock->options & APR_INCOMPLETE_WRITE) { sock->options &= ~APR_INCOMPLETE_WRITE; goto do_select; } do { rv = write(sock->socketdes, buf, (*len)); } while (rv == -1 && errno == EINTR); while (rv == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) && (sock->timeout > 0)) { apr_status_t arv;do_select: arv = apr_wait_for_io_or_timeout(NULL, sock, 0); if (arv != APR_SUCCESS) { *len = 0; return arv; } else { do { rv = write(sock->socketdes, buf, (*len)); } while (rv == -1 && errno == EINTR); } } if (rv == -1) { *len = 0; return errno; } if ((sock->timeout > 0) && (rv < *len)) { sock->options |= APR_INCOMPLETE_WRITE; } (*len) = rv; return APR_SUCCESS;}apr_status_t apr_socket_recv(apr_socket_t *sock, char *buf, apr_size_t *len){ apr_ssize_t rv; apr_status_t arv; if (sock->options & APR_INCOMPLETE_READ) { sock->options &= ~APR_INCOMPLETE_READ; goto do_select; } do { rv = read(sock->socketdes, buf, (*len)); } while (rv == -1 && errno == EINTR); while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK) && (sock->timeout > 0)) {do_select: arv = apr_wait_for_io_or_timeout(NULL, sock, 1); if (arv != APR_SUCCESS) { *len = 0; return arv; } else { do { rv = read(sock->socketdes, buf, (*len)); } while (rv == -1 && errno == EINTR); } } if (rv == -1) { (*len) = 0; return errno; } if ((sock->timeout > 0) && (rv < *len)) { sock->options |= APR_INCOMPLETE_READ; } (*len) = rv; if (rv == 0) { return APR_EOF; } return APR_SUCCESS;}apr_status_t apr_socket_sendto(apr_socket_t *sock, apr_sockaddr_t *where, apr_int32_t flags, const char *buf, apr_size_t *len){ apr_ssize_t rv; do { rv = sendto(sock->socketdes, buf, (*len), flags, (const struct sockaddr*)&where->sa, where->salen); } while (rv == -1 && errno == EINTR); while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK) && (sock->timeout > 0)) { apr_status_t arv = apr_wait_for_io_or_timeout(NULL, sock, 0); if (arv != APR_SUCCESS) { *len = 0; return arv; } else { do { rv = sendto(sock->socketdes, buf, (*len), flags, (const struct sockaddr*)&where->sa, where->salen); } while (rv == -1 && errno == EINTR); } } if (rv == -1) { *len = 0; return errno; } *len = rv; return APR_SUCCESS;}apr_status_t apr_socket_recvfrom(apr_sockaddr_t *from, apr_socket_t *sock, apr_int32_t flags, char *buf, apr_size_t *len){ apr_ssize_t rv; do { rv = recvfrom(sock->socketdes, buf, (*len), flags, (struct sockaddr*)&from->sa, &from->salen); } while (rv == -1 && errno == EINTR); while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK) && (sock->timeout > 0)) { apr_status_t arv = apr_wait_for_io_or_timeout(NULL, sock, 1); if (arv != APR_SUCCESS) { *len = 0; return arv; } else { do { rv = recvfrom(sock->socketdes, buf, (*len), flags, (struct sockaddr*)&from->sa, &from->salen); } while (rv == -1 && errno == EINTR); } } if (rv == -1) { (*len) = 0; return errno; } (*len) = rv; if (rv == 0 && sock->type == SOCK_STREAM) { return APR_EOF; } return APR_SUCCESS;}apr_status_t apr_socket_sendv(apr_socket_t * sock, const struct iovec *vec, apr_int32_t nvec, apr_size_t *len){#ifdef HAVE_WRITEV apr_ssize_t rv; apr_size_t requested_len = 0; apr_int32_t i; for (i = 0; i < nvec; i++) { requested_len += vec[i].iov_len; } if (sock->options & APR_INCOMPLETE_WRITE) { sock->options &= ~APR_INCOMPLETE_WRITE; goto do_select; } do { rv = writev(sock->socketdes, vec, nvec); } while (rv == -1 && errno == EINTR); while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK) && (sock->timeout > 0)) { apr_status_t arv;do_select: arv = apr_wait_for_io_or_timeout(NULL, sock, 0); if (arv != APR_SUCCESS) { *len = 0; return arv; } else { do { rv = writev(sock->socketdes, vec, nvec); } while (rv == -1 && errno == EINTR); } } if (rv == -1) { *len = 0; return errno; } if ((sock->timeout > 0) && (rv < requested_len)) { sock->options |= APR_INCOMPLETE_WRITE; } (*len) = rv; return APR_SUCCESS;#else *len = vec[0].iov_len; return apr_socket_send(sock, vec[0].iov_base, len);#endif}#if APR_HAS_SENDFILE/* TODO: Verify that all platforms handle the fd the same way, * i.e. that they don't move the file pointer. *//* TODO: what should flags be? int_32? *//* Define a structure to pass in when we have a NULL header value */static apr_hdtr_t no_hdtr;#if defined(__linux__) && defined(HAVE_WRITEV)apr_status_t apr_socket_sendfile(apr_socket_t *sock, apr_file_t *file, apr_hdtr_t *hdtr, apr_off_t *offset, apr_size_t *len, apr_int32_t flags){ int rv, nbytes = 0, total_hdrbytes, i; apr_status_t arv;#if APR_HAS_LARGE_FILES && defined(HAVE_SENDFILE64) apr_off_t off = *offset;#define sendfile sendfile64#elif APR_HAS_LARGE_FILES && SIZEOF_OFF_T == 4 /* 64-bit apr_off_t but no sendfile64(): fail if trying to send * past the 2Gb limit. */ off_t off; if ((apr_int64_t)*offset + *len > INT_MAX) { return EINVAL; } off = *offset;#else off_t off = *offset; /* Multiple reports have shown sendfile failing with EINVAL if * passed a >=2Gb count value on some 64-bit kernels. It won't * noticably hurt performance to limit each call to <2Gb at a * time, so avoid that issue here: */ if (sizeof(off_t) == 8 && *len > INT_MAX) { *len = INT_MAX; }#endif if (!hdtr) { hdtr = &no_hdtr; } /* Ignore flags for now. */ flags = 0; if (hdtr->numheaders > 0) { apr_size_t hdrbytes; /* cork before writing headers */ rv = apr_socket_opt_set(sock, APR_TCP_NOPUSH, 1); if (rv != APR_SUCCESS) { return rv; } /* Now write the headers */ arv = apr_socket_sendv(sock, hdtr->headers, hdtr->numheaders, &hdrbytes); if (arv != APR_SUCCESS) { *len = 0; return errno; } nbytes += hdrbytes; /* If this was a partial write and we aren't doing timeouts, * return now with the partial byte count; this is a non-blocking * socket. */ total_hdrbytes = 0; for (i = 0; i < hdtr->numheaders; i++) { total_hdrbytes += hdtr->headers[i].iov_len; } if (hdrbytes < total_hdrbytes) { *len = hdrbytes; return apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0); } } if (sock->options & APR_INCOMPLETE_WRITE) { sock->options &= ~APR_INCOMPLETE_WRITE; goto do_select; } do { rv = sendfile(sock->socketdes, /* socket */ file->filedes, /* open file descriptor of the file to be sent */ &off, /* where in the file to start */ *len); /* number of bytes to send */ } while (rv == -1 && errno == EINTR); while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK) && (sock->timeout > 0)) {do_select: arv = apr_wait_for_io_or_timeout(NULL, sock, 0); if (arv != APR_SUCCESS) { *len = 0; return arv; } else { do { rv = sendfile(sock->socketdes, /* socket */ file->filedes, /* open file descriptor of the file to be sent */ &off, /* where in the file to start */ *len); /* number of bytes to send */ } while (rv == -1 && errno == EINTR); } } if (rv == -1) { *len = nbytes; rv = errno; apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0); return rv; } nbytes += rv; if (rv < *len) { *len = nbytes; arv = apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0); if (rv > 0) { /* If this was a partial write, return now with the * partial byte count; this is a non-blocking socket. */ if (sock->timeout > 0) { sock->options |= APR_INCOMPLETE_WRITE; } return arv; } else { /* If the file got smaller mid-request, eventually the offset * becomes equal to the new file size and the kernel returns 0. * Make this an error so the caller knows to log something and * exit. */ return APR_EOF; } } /* Now write the footers */ if (hdtr->numtrailers > 0) { apr_size_t trbytes; arv = apr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers, &trbytes); nbytes += trbytes; if (arv != APR_SUCCESS) { *len = nbytes; rv = errno; apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0); return rv; } } apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0); (*len) = nbytes; return rv < 0 ? errno : APR_SUCCESS;}#elif defined(__FreeBSD__) || defined(__DragonFly__)/* Release 3.1 or greater */apr_status_t apr_socket_sendfile(apr_socket_t * sock, apr_file_t * file, apr_hdtr_t * hdtr, apr_off_t * offset, apr_size_t * len, apr_int32_t flags){ off_t nbytes = 0; int rv, i; struct sf_hdtr headerstruct; apr_size_t bytes_to_send = *len; /* Ignore flags for now. */ flags = 0; if (!hdtr) { hdtr = &no_hdtr; }#if defined(__FreeBSD_version) && __FreeBSD_version < 460001 else if (hdtr->numheaders) { /* On early versions of FreeBSD sendfile, the number of bytes to send * must include the length of the headers. Don't look at the man page * for this :( Instead, look at the the logic in * src/sys/kern/uipc_syscalls::sendfile(). * * This was fixed in the middle of 4.6-STABLE */ for (i = 0; i < hdtr->numheaders; i++) { bytes_to_send += hdtr->headers[i].iov_len; } }#endif headerstruct.headers = hdtr->headers; headerstruct.hdr_cnt = hdtr->numheaders; headerstruct.trailers = hdtr->trailers; headerstruct.trl_cnt = hdtr->numtrailers; /* FreeBSD can send the headers/footers as part of the system call */ do { if (sock->options & APR_INCOMPLETE_WRITE) { apr_status_t arv; sock->options &= ~APR_INCOMPLETE_WRITE; arv = apr_wait_for_io_or_timeout(NULL, sock, 0); if (arv != APR_SUCCESS) { *len = 0; return arv; } } if (bytes_to_send) { /* We won't dare call sendfile() if we don't have * header or file bytes to send because bytes_to_send == 0 * means send the whole file. */ rv = sendfile(file->filedes, /* file to be sent */ sock->socketdes, /* socket */ *offset, /* where in the file to start */ bytes_to_send, /* number of bytes to send */ &headerstruct, /* Headers/footers */ &nbytes, /* number of bytes written */ flags); /* undefined, set to 0 */ if (rv == -1) { if (errno == EAGAIN) { if (sock->timeout > 0) { sock->options |= APR_INCOMPLETE_WRITE; } /* FreeBSD's sendfile can return -1/EAGAIN even if it * sent bytes. Sanitize the result so we get normal EAGAIN * semantics w.r.t. bytes sent. */ if (nbytes) { /* normal exit for a big file & non-blocking io */ (*len) = nbytes; return APR_SUCCESS; } } } else { /* rv == 0 (or the kernel is broken) */ if (nbytes == 0) { /* Most likely the file got smaller after the stat. * Return an error so the caller can do the Right Thing. */ (*len) = nbytes;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -