long int sock_apr_write(net_sock_t *nsock, tbuffer_t *buf, size_t bpos, size_t len, Net_timeout_t tm) { int err; apr_size_t nbytes; tbuffer_var_t tbv; network_sock_t *sock = (network_sock_t *)nsock; if (sock == NULL) return(-1); //** If closed return if (sock->fd == NULL) return(-1); tbuffer_var_init(&tbv); tbv.nbytes = len; tbuffer_next(buf, bpos, &tbv); if (tbv.n_iov > IOV_MAX) tbv.n_iov = IOV_MAX; //** Make sure we don't have to many entries err = apr_socket_sendv(sock->fd, tbv.buffer, tbv.n_iov, &nbytes); log_printf(5, "apr_socket_sendv=%d nbytes=%d APR_SUCCESS=%d APR_TIMEUP=%d\n", err, nbytes, APR_SUCCESS, APR_TIMEUP); if (err == APR_SUCCESS) { if (nbytes == 0) nbytes = -1; //** Dead connection } else if (err == APR_TIMEUP) { //** Try again nbytes = 0; } else { //** Generic error nbytes = -1; } return(nbytes); }
static apr_status_t APR_THREAD_FUNC uxp_socket_sendv(apr_socket_t *sock, const struct iovec *vec, apr_int32_t nvec, apr_size_t *len) { tcn_uxp_conn_t *con = (tcn_uxp_conn_t *)sock; return apr_socket_sendv(con->sock, vec, nvec, len); }
static apr_status_t writev_it_all(apr_socket_t *s, struct iovec *vec, int nvec, apr_size_t len, apr_size_t *nbytes) { apr_size_t bytes_written = 0; apr_status_t rv; apr_size_t n = len; int i = 0; *nbytes = 0; /* XXX handle checking for non-blocking socket */ while (bytes_written != len) { rv = apr_socket_sendv(s, vec + i, nvec - i, &n); *nbytes += n; bytes_written += n; if (rv != APR_SUCCESS) return rv; /* If the write did not complete, adjust the iovecs and issue * apr_socket_sendv again */ if (bytes_written < len) { /* Skip over the vectors that have already been written */ apr_size_t cnt = vec[i].iov_len; while (n >= cnt && i + 1 < nvec) { i++; cnt += vec[i].iov_len; } if (n < cnt) { /* Handle partial write of vec i */ vec[i].iov_base = (char *) vec[i].iov_base + (vec[i].iov_len - (cnt - n)); vec[i].iov_len = cnt -n; } } n = len - bytes_written; } return APR_SUCCESS; }
/* Wrapper for apr_socket_sendv that handles updating the worker stats. */ static apr_status_t send_data(proxy_conn_rec *conn, struct iovec *vec, int nvec, apr_size_t *len) { apr_status_t rv = APR_SUCCESS; apr_size_t written = 0, to_write = 0; int i, offset; apr_socket_t *s = conn->sock; for (i = 0; i < nvec; i++) { to_write += vec[i].iov_len; } offset = 0; while (to_write) { apr_size_t n = 0; rv = apr_socket_sendv(s, vec + offset, nvec - offset, &n); if (rv != APR_SUCCESS) { break; } if (n > 0) { written += n; if (written >= to_write) break; /* short circuit out */ for (i = offset; i < nvec; ) { if (n >= vec[i].iov_len) { offset++; n -= vec[i++].iov_len; } else { vec[i].iov_len -= n; vec[i].iov_base = (char *) vec[i].iov_base + n; break; } } } } conn->worker->s->transferred += written; *len = written; return rv; }
apr_status_t jxr_send_message_add_header(jaxer_connection *ac, const char* buf, int msglen, enum BlockType bType) { apr_size_t len = 0; apr_size_t to_send = 0; apr_status_t rv = APR_SUCCESS; struct iovec vec[2]; Jaxer_Header jx_hdr; request_rec *r = ac->request; apr_socket_t* sock = ac->sock; jaxer_dir_conf *config = (jaxer_dir_conf *)ap_get_module_config(r->per_dir_config, &jaxer_module); jxr_init_header(bType, msglen, &jx_hdr); vec[0].iov_base = (unsigned char*)&jx_hdr; vec[0].iov_len = sizeof(Jaxer_Header); vec[1].iov_base = (char*) buf; vec[1].iov_len = msglen; to_send = msglen + sizeof(Jaxer_Header); compat_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "mod_jaxer: sending a message (type=%s len=%d sock=%d)", sBlockType[bType], msglen, ac->sock); if(g_jxr_network_trace!=0) { jxr_trace("SEND", vec[0].iov_base, vec[0].iov_len, config->reqPool); jxr_trace("SEND", buf, msglen, config->reqPool); } if((rv = apr_socket_sendv(sock, vec, 2, &len)) != APR_SUCCESS) { compat_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, "mod_jaxer: error in sending a messasge"); return rv; } if (len < to_send) { compat_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, "mod_jaxer: sending message error: send %d bytes of %d", len, to_send); return (!rv); } return rv; }
static apr_status_t writev_nonblocking(apr_socket_t *s, struct iovec *vec, apr_size_t nvec, apr_bucket_brigade *bb, apr_size_t *cumulative_bytes_written, conn_rec *c) { apr_status_t rv = APR_SUCCESS, arv; apr_size_t bytes_written = 0, bytes_to_write = 0; apr_size_t i, offset; apr_interval_time_t old_timeout; arv = apr_socket_timeout_get(s, &old_timeout); if (arv != APR_SUCCESS) { return arv; } arv = apr_socket_timeout_set(s, 0); if (arv != APR_SUCCESS) { return arv; } for (i = 0; i < nvec; i++) { bytes_to_write += vec[i].iov_len; } offset = 0; while (bytes_written < bytes_to_write) { apr_size_t n = 0; rv = apr_socket_sendv(s, vec + offset, nvec - offset, &n); if (n > 0) { bytes_written += n; for (i = offset; i < nvec; ) { apr_bucket *bucket = APR_BRIGADE_FIRST(bb); if (APR_BUCKET_IS_METADATA(bucket)) { apr_bucket_delete(bucket); } else if (n >= vec[i].iov_len) { apr_bucket_delete(bucket); offset++; n -= vec[i++].iov_len; } else { apr_bucket_split(bucket, n); apr_bucket_delete(bucket); vec[i].iov_len -= n; vec[i].iov_base = (char *) vec[i].iov_base + n; break; } } } if (rv != APR_SUCCESS) { break; } } if ((ap__logio_add_bytes_out != NULL) && (bytes_written > 0)) { ap__logio_add_bytes_out(c, bytes_written); } *cumulative_bytes_written += bytes_written; arv = apr_socket_timeout_set(s, old_timeout); if ((arv != APR_SUCCESS) && (rv == APR_SUCCESS)) { return arv; } else { return rv; } }
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; }
apr_status_t apr_sendv(apr_socket_t * sock, const struct iovec *vec, apr_int32_t nvec, apr_size_t *len) { return apr_socket_sendv(sock, vec, nvec, len); }
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 off = *offset; int rv, nbytes = 0, total_hdrbytes, i; apr_status_t arv; 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->netmask & APR_INCOMPLETE_WRITE) { sock->netmask &= ~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) && apr_is_option_set(sock->netmask, APR_SO_TIMEOUT)) { 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 (apr_is_option_set(sock->netmask, APR_SO_TIMEOUT)) { sock->netmask |= 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; }
/* * apr_status_t apr_socket_sendfile(apr_socket_t *, apr_file_t *, apr_hdtr_t *, * apr_off_t *, apr_size_t *, apr_int32_t flags) * Send a file from an open file descriptor to a socket, along with * optional headers and trailers * arg 1) The socket to which we're writing * arg 2) The open file from which to read * arg 3) A structure containing the headers and trailers to send * arg 4) Offset into the file where we should begin writing * arg 5) Number of bytes to send out of the file * arg 6) APR flags that are mapped to OS specific flags */ APR_DECLARE(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) { apr_status_t status = APR_SUCCESS; apr_status_t rv; apr_off_t curoff = *offset; DWORD dwFlags = 0; apr_size_t nbytes; TRANSMIT_FILE_BUFFERS tfb, *ptfb = NULL; int ptr = 0; apr_size_t bytes_to_send; /* Bytes to send out of the file (not including headers) */ int disconnected = 0; int sendv_trailers = 0; char hdtrbuf[4096]; if (apr_os_level < APR_WIN_NT) { return APR_ENOTIMPL; } /* Use len to keep track of number of total bytes sent (including headers) */ bytes_to_send = *len; *len = 0; /* Handle the goofy case of sending headers/trailers and a zero byte file */ if (!bytes_to_send && hdtr) { if (hdtr->numheaders) { rv = apr_socket_sendv(sock, hdtr->headers, hdtr->numheaders, &nbytes); if (rv != APR_SUCCESS) return rv; *len += nbytes; } if (hdtr->numtrailers) { rv = apr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers, &nbytes); if (rv != APR_SUCCESS) return rv; *len += nbytes; } return APR_SUCCESS; } memset(&tfb, '\0', sizeof (tfb)); /* Collapse the headers into a single buffer */ if (hdtr && hdtr->numheaders) { apr_size_t head_length = tfb.HeadLength; ptfb = &tfb; nbytes = 0; rv = collapse_iovec((char **)&ptfb->Head, &head_length, hdtr->headers, hdtr->numheaders, hdtrbuf, sizeof(hdtrbuf)); tfb.HeadLength = (DWORD)head_length; /* If not enough buffer, punt to sendv */ if (rv == APR_INCOMPLETE) { rv = apr_socket_sendv(sock, hdtr->headers, hdtr->numheaders, &nbytes); if (rv != APR_SUCCESS) return rv; *len += nbytes; ptfb = NULL; } } /* Initialize the overlapped structure used on TransmitFile */ if (!sock->overlapped) { sock->overlapped = apr_pcalloc(sock->pool, sizeof(OVERLAPPED)); sock->overlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); } while (bytes_to_send) { DWORD xmitbytes; if (bytes_to_send > MAX_SEGMENT_SIZE) { xmitbytes = MAX_SEGMENT_SIZE; } else { /* Last call to TransmitFile() */ xmitbytes = (DWORD)bytes_to_send; /* Collapse the trailers into a single buffer */ if (hdtr && hdtr->numtrailers) { apr_size_t tail_length = tfb.TailLength; ptfb = &tfb; rv = collapse_iovec((char**) &ptfb->Tail, &tail_length, hdtr->trailers, hdtr->numtrailers, hdtrbuf + ptfb->HeadLength, sizeof(hdtrbuf) - ptfb->HeadLength); tfb.TailLength = (DWORD)tail_length; if (rv == APR_INCOMPLETE) { /* If not enough buffer, punt to sendv, later */ sendv_trailers = 1; } } /* Disconnect the socket after last send */ if ((flags & APR_SENDFILE_DISCONNECT_SOCKET) && !sendv_trailers) { dwFlags |= TF_REUSE_SOCKET; dwFlags |= TF_DISCONNECT; disconnected = 1; } } sock->overlapped->Offset = (DWORD)(curoff); #if APR_HAS_LARGE_FILES sock->overlapped->OffsetHigh = (DWORD)(curoff >> 32); #endif /* XXX BoundsChecker claims dwFlags must not be zero. */ rv = TransmitFile(sock->socketdes, /* socket */ file->filehand, /* open file descriptor of the file to be sent */ xmitbytes, /* number of bytes to send. 0=send all */ 0, /* Number of bytes per send. 0=use default */ sock->overlapped, /* OVERLAPPED structure */ ptfb, /* header and trailer buffers */ dwFlags); /* flags to control various aspects of TransmitFile */ if (!rv) { status = apr_get_netos_error(); if ((status == APR_FROM_OS_ERROR(ERROR_IO_PENDING)) || (status == APR_FROM_OS_ERROR(WSA_IO_PENDING))) { rv = WaitForSingleObject(sock->overlapped->hEvent, (DWORD)(sock->timeout >= 0 ? sock->timeout_ms : INFINITE)); if (rv == WAIT_OBJECT_0) { status = APR_SUCCESS; if (!disconnected) { if (!WSAGetOverlappedResult(sock->socketdes, sock->overlapped, &xmitbytes, FALSE, &dwFlags)) { status = apr_get_netos_error(); } /* Ugly code alert: WSAGetOverlappedResult returns * a count of all bytes sent. This loop only * tracks bytes sent out of the file. */ else if (ptfb) { xmitbytes -= (ptfb->HeadLength + ptfb->TailLength); } } } else if (rv == WAIT_TIMEOUT) { status = APR_FROM_OS_ERROR(WAIT_TIMEOUT); } else if (rv == WAIT_ABANDONED) { /* Hummm... WAIT_ABANDONDED is not an error code. It is * a return specific to the Win32 WAIT functions that * indicates that a thread exited while holding a * mutex. Should consider triggering an assert * to detect the condition... */ status = APR_FROM_OS_ERROR(WAIT_TIMEOUT); } else status = apr_get_os_error(); } } if (status != APR_SUCCESS) break; bytes_to_send -= xmitbytes; curoff += xmitbytes; *len += xmitbytes; /* Adjust len for any headers/trailers sent */ if (ptfb) { *len += (ptfb->HeadLength + ptfb->TailLength); memset(&tfb, '\0', sizeof (tfb)); ptfb = NULL; } } if (status == APR_SUCCESS) { if (sendv_trailers) { rv = apr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers, &nbytes); if (rv != APR_SUCCESS) return rv; *len += nbytes; } /* Mark the socket as disconnected, but do not close it. * Note: The application must have stored the socket prior to making * the call to apr_socket_sendfile in order to either reuse it * or close it. */ if (disconnected) { sock->disconnected = 1; sock->socketdes = INVALID_SOCKET; } } return status; }