From e8ec7aa8e38a93f5b034ac74cebce5de23710317 Mon Sep 17 00:00:00 2001 From: hongbotian Date: Mon, 30 Nov 2015 01:45:08 -0500 Subject: upload http JIRA: BOTTLENECK-10 Change-Id: I7598427ff904df438ce77c2819ee48ac75ffa8da Signed-off-by: hongbotian --- .../srclib/apr/network_io/win32/sendrecv.c | 460 ++++++++++++++++++ .../srclib/apr/network_io/win32/sockets.c | 532 +++++++++++++++++++++ .../srclib/apr/network_io/win32/sockopt.c | 280 +++++++++++ 3 files changed, 1272 insertions(+) create mode 100644 rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sendrecv.c create mode 100644 rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sockets.c create mode 100644 rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sockopt.c (limited to 'rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32') diff --git a/rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sendrecv.c b/rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sendrecv.c new file mode 100644 index 00000000..99470c82 --- /dev/null +++ b/rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sendrecv.c @@ -0,0 +1,460 @@ +/* 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_errno.h" +#include "apr_general.h" +#include "apr_network_io.h" +#include "apr_lib.h" +#include "apr_arch_file_io.h" +#if APR_HAVE_TIME_H +#include +#endif + +/* MAX_SEGMENT_SIZE is the maximum amount of data that will be sent to a client + * in one call of TransmitFile. This number must be small enough to give the + * slowest client time to receive the data before the socket timeout triggers. + * The same problem can exist with apr_socket_send(). In that case, we rely on + * the application to adjust socket timeouts and max send segment + * sizes appropriately. + * For example, Apache will in most cases call apr_socket_send() with less + * than 8193 bytes. + */ +#define MAX_SEGMENT_SIZE 65536 +#define WSABUF_ON_STACK 50 + +APR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf, + apr_size_t *len) +{ + apr_ssize_t rv; + WSABUF wsaData; + int lasterror; + DWORD dwBytes = 0; + + wsaData.len = *len; + wsaData.buf = (char*) buf; + +#ifndef _WIN32_WCE + rv = WSASend(sock->socketdes, &wsaData, 1, &dwBytes, 0, NULL, NULL); +#else + rv = send(sock->socketdes, wsaData.buf, wsaData.len, 0); + dwBytes = rv; +#endif + if (rv == SOCKET_ERROR) { + lasterror = apr_get_netos_error(); + *len = 0; + return lasterror; + } + + *len = dwBytes; + + return APR_SUCCESS; +} + + +APR_DECLARE(apr_status_t) apr_socket_recv(apr_socket_t *sock, char *buf, + apr_size_t *len) +{ + apr_ssize_t rv; + WSABUF wsaData; + int lasterror; + DWORD dwBytes = 0; + DWORD flags = 0; + + wsaData.len = *len; + wsaData.buf = (char*) buf; + +#ifndef _WIN32_WCE + rv = WSARecv(sock->socketdes, &wsaData, 1, &dwBytes, &flags, NULL, NULL); +#else + rv = recv(sock->socketdes, wsaData.buf, wsaData.len, 0); + dwBytes = rv; +#endif + if (rv == SOCKET_ERROR) { + lasterror = apr_get_netos_error(); + *len = 0; + return lasterror; + } + + *len = dwBytes; + return dwBytes == 0 ? APR_EOF : APR_SUCCESS; +} + + +APR_DECLARE(apr_status_t) apr_socket_sendv(apr_socket_t *sock, + const struct iovec *vec, + apr_int32_t nvec, apr_size_t *nbytes) +{ + apr_status_t rc = APR_SUCCESS; + apr_ssize_t rv; + int i; + DWORD dwBytes = 0; + WSABUF *pWsaBuf = (nvec <= WSABUF_ON_STACK) ? _alloca(sizeof(WSABUF) * (nvec)) + : malloc(sizeof(WSABUF) * (nvec)); + + if (!pWsaBuf) + return APR_ENOMEM; + + for (i = 0; i < nvec; i++) { + pWsaBuf[i].buf = vec[i].iov_base; + pWsaBuf[i].len = vec[i].iov_len; + } +#ifndef _WIN32_WCE + rv = WSASend(sock->socketdes, pWsaBuf, nvec, &dwBytes, 0, NULL, NULL); + if (rv == SOCKET_ERROR) { + rc = apr_get_netos_error(); + } +#else + for (i = 0; i < nvec; i++) { + rv = send(sock->socketdes, pWsaBuf[i].buf, pWsaBuf[i].len, 0); + if (rv == SOCKET_ERROR) { + rc = apr_get_netos_error(); + break; + } + dwBytes += rv; + } +#endif + if (nvec > WSABUF_ON_STACK) + free(pWsaBuf); + + *nbytes = dwBytes; + return rc; +} + + +APR_DECLARE(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; + + rv = sendto(sock->socketdes, buf, (*len), flags, + (const struct sockaddr*)&where->sa, + where->salen); + if (rv == SOCKET_ERROR) { + *len = 0; + return apr_get_netos_error(); + } + + *len = rv; + return APR_SUCCESS; +} + + +APR_DECLARE(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; + + rv = recvfrom(sock->socketdes, buf, (*len), flags, + (struct sockaddr*)&from->sa, &from->salen); + if (rv == SOCKET_ERROR) { + (*len) = 0; + return apr_get_netos_error(); + } + (*len) = rv; + if (rv == 0 && sock->type == SOCK_STREAM) + return APR_EOF; + + return APR_SUCCESS; +} + + +static apr_status_t collapse_iovec(char **off, apr_size_t *len, + struct iovec *iovec, int numvec, + char *buf, apr_size_t buflen) +{ + if (numvec == 1) { + *off = iovec[0].iov_base; + *len = iovec[0].iov_len; + } + else { + int i; + for (i = 0; i < numvec; i++) { + *len += iovec[i].iov_len; + } + + if (*len > buflen) { + *len = 0; + return APR_INCOMPLETE; + } + + *off = buf; + + for (i = 0; i < numvec; i++) { + memcpy(buf, iovec[i].iov_base, iovec[i].iov_len); + buf += iovec[i].iov_len; + } + } + return APR_SUCCESS; +} + + +#if APR_HAS_SENDFILE +/* + * 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_ssize_t rv; + apr_off_t curoff = *offset; + DWORD dwFlags = 0; + DWORD nbytes; + TRANSMIT_FILE_BUFFERS tfb, *ptfb = NULL; + int ptr = 0; + int 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) { + ptfb = &tfb; + nbytes = 0; + rv = collapse_iovec((char **)&ptfb->Head, &ptfb->HeadLength, + hdtr->headers, hdtr->numheaders, + hdtrbuf, sizeof(hdtrbuf)); + /* If not enough buffer, punt to sendv */ + if (rv == APR_INCOMPLETE) { + rv = apr_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->cntxt, sizeof(OVERLAPPED)); + sock->overlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); + } + while (bytes_to_send) { + if (bytes_to_send > MAX_SEGMENT_SIZE) { + nbytes = MAX_SEGMENT_SIZE; + } + else { + /* Last call to TransmitFile() */ + nbytes = bytes_to_send; + /* Collapse the trailers into a single buffer */ + if (hdtr && hdtr->numtrailers) { + ptfb = &tfb; + rv = collapse_iovec((char**) &ptfb->Tail, &ptfb->TailLength, + hdtr->trailers, hdtr->numtrailers, + hdtrbuf + ptfb->HeadLength, + sizeof(hdtrbuf) - ptfb->HeadLength); + 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 */ + nbytes, /* 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, + &nbytes, + 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) { + nbytes -= (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 -= nbytes; + curoff += nbytes; + *len += nbytes; + /* 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_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; +} + +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_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) +{ + return apr_socket_sendfile(sock, file, hdtr, offset, len, flags); +} + +#endif + +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_send(apr_socket_t *sock, const char *buf, + apr_size_t *len) +{ + return apr_socket_send(sock, buf, len); +} + +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_sendv(apr_socket_t *sock, + const struct iovec *vec, + apr_int32_t nvec, apr_size_t *nbytes) +{ + return apr_socket_sendv(sock, vec, nvec, nbytes); +} + +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_sendto(apr_socket_t *sock, apr_sockaddr_t *where, + apr_int32_t flags, const char *buf, + apr_size_t *len) +{ + return apr_socket_sendto(sock, where, flags, buf, len); +} + +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_recvfrom(apr_sockaddr_t *from, + apr_socket_t *sock, + apr_int32_t flags, + char *buf, apr_size_t *len) +{ + return apr_socket_recvfrom(from, sock, flags, buf, len); +} + +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_recv(apr_socket_t *sock, char *buf, + apr_size_t *len) +{ + return apr_socket_recv(sock, buf, len); +} diff --git a/rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sockets.c b/rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sockets.c new file mode 100644 index 00000000..fc2bcb73 --- /dev/null +++ b/rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sockets.c @@ -0,0 +1,532 @@ +/* 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_network_io.h" +#include "apr_general.h" +#include "apr_lib.h" +#include "apr_portable.h" +#include "apr_strings.h" +#include +#include "apr_arch_inherit.h" +#include "apr_arch_misc.h" + +static char generic_inaddr_any[16] = {0}; /* big enough for IPv4 or IPv6 */ + +static apr_status_t socket_cleanup(void *sock) +{ + apr_socket_t *thesocket = sock; + + if (thesocket->socketdes != INVALID_SOCKET) { + if (closesocket(thesocket->socketdes) == SOCKET_ERROR) { + return apr_get_netos_error(); + } + thesocket->socketdes = INVALID_SOCKET; + } +#if APR_HAS_SENDFILE + if (thesocket->overlapped) { + CloseHandle(thesocket->overlapped->hEvent); + thesocket->overlapped = NULL; + } +#endif + return APR_SUCCESS; +} + +static void set_socket_vars(apr_socket_t *sock, int family, int type, int protocol) +{ + sock->type = type; + sock->protocol = protocol; + apr_sockaddr_vars_set(sock->local_addr, family, 0); + apr_sockaddr_vars_set(sock->remote_addr, family, 0); +} +static void alloc_socket(apr_socket_t **new, apr_pool_t *p) +{ + *new = (apr_socket_t *)apr_pcalloc(p, sizeof(apr_socket_t)); + (*new)->cntxt = p; + (*new)->local_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->cntxt, + sizeof(apr_sockaddr_t)); + (*new)->local_addr->pool = p; + (*new)->remote_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->cntxt, + sizeof(apr_sockaddr_t)); + (*new)->remote_addr->pool = p; +} + +APR_DECLARE(apr_status_t) apr_socket_protocol_get(apr_socket_t *sock, + int *protocol) +{ + *protocol = sock->protocol; + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_socket_create_ex(apr_socket_t **new, int family, + int type, int protocol, + apr_pool_t *cont) +{ + int downgrade = (family == AF_UNSPEC); + + if (family == AF_UNSPEC) { +#if APR_HAVE_IPV6 + family = AF_INET6; +#else + family = AF_INET; +#endif + } + + alloc_socket(new, cont); + + /* For right now, we are not using socket groups. We may later. + * No flags to use when creating a socket, so use 0 for that parameter as well. + */ + (*new)->socketdes = socket(family, type, protocol); +#if APR_HAVE_IPV6 + if ((*new)->socketdes == INVALID_SOCKET && downgrade) { + family = AF_INET; + (*new)->socketdes = socket(family, type, protocol); + } +#endif + + if ((*new)->socketdes == INVALID_SOCKET) { + return apr_get_netos_error(); + } + +#ifdef WIN32 + /* Socket handles are never truly inheritable, there are too many + * bugs associated. WSADuplicateSocket will copy them, but for our + * purposes, always transform the socket() created as a non-inherited + * handle + */ +#if APR_HAS_UNICODE_FS && !defined(_WIN32_WCE) + IF_WIN_OS_IS_UNICODE { + /* A different approach. Many users report errors such as + * (32538)An operation was attempted on something that is not + * a socket. : Parent: WSADuplicateSocket failed... + * + * This appears that the duplicated handle is no longer recognized + * as a socket handle. SetHandleInformation should overcome that + * problem by not altering the handle identifier. But this won't + * work on 9x - it's unsupported. + */ + SetHandleInformation((HANDLE) (*new)->socketdes, + HANDLE_FLAG_INHERIT, 0); + } +#endif +#if APR_HAS_ANSI_FS || defined(_WIN32_WCE) + ELSE_WIN_OS_IS_ANSI { + HANDLE hProcess = GetCurrentProcess(); + HANDLE dup; + if (DuplicateHandle(hProcess, (HANDLE) (*new)->socketdes, hProcess, + &dup, 0, FALSE, DUPLICATE_SAME_ACCESS)) { + closesocket((*new)->socketdes); + (*new)->socketdes = (SOCKET) dup; + } + } +#endif + +#endif /* def WIN32 */ + + set_socket_vars(*new, family, type, protocol); + + (*new)->timeout = -1; + (*new)->disconnected = 0; + + apr_pool_cleanup_register((*new)->cntxt, (void *)(*new), + socket_cleanup, apr_pool_cleanup_null); + + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_socket_create(apr_socket_t **new, int family, + int type, apr_pool_t *cont) +{ + return apr_socket_create_ex(new, family, type, 0, cont); +} + +APR_DECLARE(apr_status_t) apr_socket_shutdown(apr_socket_t *thesocket, + apr_shutdown_how_e how) +{ + int winhow = 0; + +#ifdef SD_RECEIVE + switch (how) { + case APR_SHUTDOWN_READ: { + winhow = SD_RECEIVE; + break; + } + case APR_SHUTDOWN_WRITE: { + winhow = SD_SEND; + break; + } + case APR_SHUTDOWN_READWRITE: { + winhow = SD_BOTH; + break; + } + default: + return APR_BADARG; + } +#endif + if (shutdown(thesocket->socketdes, winhow) == 0) { + return APR_SUCCESS; + } + else { + return apr_get_netos_error(); + } +} + +APR_DECLARE(apr_status_t) apr_socket_close(apr_socket_t *thesocket) +{ + apr_pool_cleanup_kill(thesocket->cntxt, thesocket, socket_cleanup); + return socket_cleanup(thesocket); +} + +APR_DECLARE(apr_status_t) apr_socket_bind(apr_socket_t *sock, + apr_sockaddr_t *sa) +{ + if (bind(sock->socketdes, + (struct sockaddr *)&sa->sa, + sa->salen) == -1) { + return apr_get_netos_error(); + } + else { + sock->local_addr = sa; + if (sock->local_addr->sa.sin.sin_port == 0) { + sock->local_port_unknown = 1; /* ephemeral port */ + } + return APR_SUCCESS; + } +} + +APR_DECLARE(apr_status_t) apr_socket_listen(apr_socket_t *sock, + apr_int32_t backlog) +{ + if (listen(sock->socketdes, backlog) == SOCKET_ERROR) + return apr_get_netos_error(); + else + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_socket_accept(apr_socket_t **new, + apr_socket_t *sock, apr_pool_t *p) +{ + SOCKET s; + struct sockaddr sa; + int salen = sizeof(sock->remote_addr->sa); + + /* Don't allocate the memory until after we call accept. This allows + us to work with nonblocking sockets. */ + s = accept(sock->socketdes, (struct sockaddr *)&sa, &salen); + if (s == INVALID_SOCKET) { + return apr_get_netos_error(); + } + + alloc_socket(new, p); + set_socket_vars(*new, sock->local_addr->sa.sin.sin_family, SOCK_STREAM, + sock->protocol); + + (*new)->timeout = -1; + (*new)->disconnected = 0; + + (*new)->socketdes = s; + /* XXX next line looks bogus w.r.t. AF_INET6 support */ + (*new)->remote_addr->salen = sizeof((*new)->remote_addr->sa); + memcpy (&(*new)->remote_addr->sa, &sa, salen); + *(*new)->local_addr = *sock->local_addr; + + /* The above assignment just overwrote the pool entry. Setting the local_addr + pool for the accepted socket back to what it should be. Otherwise all + allocations for this socket will come from a server pool that is not + freed until the process goes down.*/ + (*new)->local_addr->pool = p; + + /* fix up any pointers which are no longer valid */ + if (sock->local_addr->sa.sin.sin_family == AF_INET) { + (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin.sin_addr; + } +#if APR_HAVE_IPV6 + else if (sock->local_addr->sa.sin.sin_family == AF_INET6) { + (*new)->local_addr->ipaddr_ptr = &(*new)->local_addr->sa.sin6.sin6_addr; + } +#endif + (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port); + if (sock->local_port_unknown) { + /* not likely for a listening socket, but theoretically possible :) */ + (*new)->local_port_unknown = 1; + } + +#if APR_TCP_NODELAY_INHERITED + if (apr_is_option_set(sock->netmask, APR_TCP_NODELAY) == 1) { + apr_set_option(&(*new)->netmask, APR_TCP_NODELAY, 1); + } +#endif /* TCP_NODELAY_INHERITED */ +#if APR_O_NONBLOCK_INHERITED + if (apr_is_option_set(sock->netmask, APR_SO_NONBLOCK) == 1) { + apr_set_option(&(*new)->netmask, APR_SO_NONBLOCK, 1); + } +#endif /* APR_O_NONBLOCK_INHERITED */ + + if (sock->local_interface_unknown || + !memcmp(sock->local_addr->ipaddr_ptr, + generic_inaddr_any, + sock->local_addr->ipaddr_len)) { + /* If the interface address inside the listening socket's local_addr wasn't + * up-to-date, we don't know local interface of the connected socket either. + * + * If the listening socket was not bound to a specific interface, we + * don't know the local_addr of the connected socket. + */ + (*new)->local_interface_unknown = 1; + } + + apr_pool_cleanup_register((*new)->cntxt, (void *)(*new), + socket_cleanup, apr_pool_cleanup_null); + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_socket_connect(apr_socket_t *sock, + apr_sockaddr_t *sa) +{ + apr_status_t rv; + + if ((sock->socketdes == INVALID_SOCKET) || (!sock->local_addr)) { + return APR_ENOTSOCK; + } + + if (connect(sock->socketdes, (const struct sockaddr *)&sa->sa.sin, + sa->salen) == SOCKET_ERROR) { + int rc; + struct timeval tv, *tvptr; + fd_set wfdset, efdset; + + rv = apr_get_netos_error(); + if (rv != APR_FROM_OS_ERROR(WSAEWOULDBLOCK)) { + return rv; + } + + if (sock->timeout == 0) { + /* Tell the app that the connect is in progress... + * Gotta play some games here. connect on Unix will return + * EINPROGRESS under the same circumstances that Windows + * returns WSAEWOULDBLOCK. Do some adhoc canonicalization... + */ + return APR_FROM_OS_ERROR(WSAEINPROGRESS); + } + + /* wait for the connect to complete or timeout */ + FD_ZERO(&wfdset); + FD_SET(sock->socketdes, &wfdset); + FD_ZERO(&efdset); + FD_SET(sock->socketdes, &efdset); + + if (sock->timeout < 0) { + tvptr = NULL; + } + else { + /* casts for winsock/timeval definition */ + tv.tv_sec = (long)apr_time_sec(sock->timeout); + tv.tv_usec = (int)apr_time_usec(sock->timeout); + tvptr = &tv; + } + rc = select(FD_SETSIZE+1, NULL, &wfdset, &efdset, tvptr); + if (rc == SOCKET_ERROR) { + return apr_get_netos_error(); + } + else if (!rc) { + return APR_FROM_OS_ERROR(WSAETIMEDOUT); + } + /* Evaluate the efdset */ + if (FD_ISSET(sock->socketdes, &efdset)) { + /* The connect failed. */ + int rclen = sizeof(rc); + if (getsockopt(sock->socketdes, SOL_SOCKET, SO_ERROR, (char*) &rc, &rclen)) { + return apr_get_netos_error(); + } + return APR_FROM_OS_ERROR(rc); + } + } + /* connect was OK .. amazing */ + sock->remote_addr = sa; + if (sock->local_addr->sa.sin.sin_port == 0) { + sock->local_port_unknown = 1; + } + if (!memcmp(sock->local_addr->ipaddr_ptr, + generic_inaddr_any, + sock->local_addr->ipaddr_len)) { + /* not bound to specific local interface; connect() had to assign + * one for the socket + */ + sock->local_interface_unknown = 1; + } + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_socket_data_get(void **data, const char *key, + apr_socket_t *sock) +{ + sock_userdata_t *cur = sock->userdata; + + *data = NULL; + + while (cur) { + if (!strcmp(cur->key, key)) { + *data = cur->data; + break; + } + cur = cur->next; + } + + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_socket_data_set(apr_socket_t *sock, void *data, + const char *key, + apr_status_t (*cleanup)(void *)) +{ + sock_userdata_t *new = apr_palloc(sock->cntxt, sizeof(sock_userdata_t)); + + new->key = apr_pstrdup(sock->cntxt, key); + new->data = data; + new->next = sock->userdata; + sock->userdata = new; + + if (cleanup) { + apr_pool_cleanup_register(sock->cntxt, data, cleanup, cleanup); + } + + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_os_sock_get(apr_os_sock_t *thesock, + apr_socket_t *sock) +{ + *thesock = sock->socketdes; + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_os_sock_make(apr_socket_t **apr_sock, + apr_os_sock_info_t *os_sock_info, + apr_pool_t *cont) +{ + alloc_socket(apr_sock, cont); +#ifdef APR_ENABLE_FOR_1_0 /* no protocol field yet */ + set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, os_sock_info->protocol); +#else + set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, 0); +#endif + (*apr_sock)->timeout = -1; + (*apr_sock)->disconnected = 0; + (*apr_sock)->socketdes = *os_sock_info->os_sock; + if (os_sock_info->local) { + memcpy(&(*apr_sock)->local_addr->sa.sin, + os_sock_info->local, + (*apr_sock)->local_addr->salen); + (*apr_sock)->local_addr->pool = cont; + /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */ + (*apr_sock)->local_addr->port = ntohs((*apr_sock)->local_addr->sa.sin.sin_port); + } + else { + (*apr_sock)->local_port_unknown = (*apr_sock)->local_interface_unknown = 1; + } + if (os_sock_info->remote) { + memcpy(&(*apr_sock)->remote_addr->sa.sin, + os_sock_info->remote, + (*apr_sock)->remote_addr->salen); + (*apr_sock)->remote_addr->pool = cont; + /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */ + (*apr_sock)->remote_addr->port = ntohs((*apr_sock)->remote_addr->sa.sin.sin_port); + } + else { + (*apr_sock)->remote_addr_unknown = 1; + } + + apr_pool_cleanup_register((*apr_sock)->cntxt, (void *)(*apr_sock), + socket_cleanup, apr_pool_cleanup_null); + + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_os_sock_put(apr_socket_t **sock, + apr_os_sock_t *thesock, + apr_pool_t *cont) +{ + if ((*sock) == NULL) { + alloc_socket(sock, cont); + /* XXX figure out the actual socket type here */ + /* *or* just decide that apr_os_sock_put() has to be told the family and type */ + set_socket_vars(*sock, AF_INET, SOCK_STREAM, 0); + (*sock)->timeout = -1; + (*sock)->disconnected = 0; + } + (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1; + (*sock)->remote_addr_unknown = 1; + (*sock)->socketdes = *thesock; + return APR_SUCCESS; +} + + +/* Sockets cannot be inherited through the standard sockets + * inheritence. WSADuplicateSocket must be used. + * This is not trivial to implement. + */ + +APR_DECLARE(apr_status_t) apr_socket_inherit_set(apr_socket_t *socket) +{ + return APR_ENOTIMPL; +} +/* Deprecated */ +APR_DECLARE(void) apr_socket_set_inherit(apr_socket_t *socket) +{ + apr_socket_inherit_set(socket); +} + +APR_DECLARE(apr_status_t) apr_socket_inherit_unset(apr_socket_t *socket) +{ + return APR_ENOTIMPL; +} +/* Deprecated */ +APR_DECLARE(void) apr_socket_unset_inherit(apr_socket_t *socket) +{ + apr_socket_inherit_unset(socket); +} +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_shutdown(apr_socket_t *thesocket, + apr_shutdown_how_e how) +{ + return apr_socket_shutdown(thesocket, how); +} + +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_bind(apr_socket_t *sock, apr_sockaddr_t *sa) +{ + return apr_socket_bind(sock, sa); +} + +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_listen(apr_socket_t *sock, apr_int32_t backlog) +{ + return apr_socket_listen(sock, backlog); +} + +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_accept(apr_socket_t **new, apr_socket_t *sock, + apr_pool_t *p) +{ + return apr_socket_accept(new, sock, p); +} + +/* Deprecated */ +APR_DECLARE(apr_status_t) apr_connect(apr_socket_t *sock, apr_sockaddr_t *sa) +{ + return apr_socket_connect(sock, sa); +} diff --git a/rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sockopt.c b/rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sockopt.c new file mode 100644 index 00000000..ab5dafd1 --- /dev/null +++ b/rubbos/app/httpd-2.0.64/srclib/apr/network_io/win32/sockopt.c @@ -0,0 +1,280 @@ +/* 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_network_io.h" +#include "apr_general.h" +#include "apr_strings.h" +#include + +apr_status_t soblock(SOCKET sd) +{ + u_long zero = 0; + + if (ioctlsocket(sd, FIONBIO, &zero) == SOCKET_ERROR) { + return apr_get_netos_error(); + } + return APR_SUCCESS; +} + +apr_status_t sononblock(SOCKET sd) +{ + u_long one = 1; + + if (ioctlsocket(sd, FIONBIO, &one) == SOCKET_ERROR) { + return apr_get_netos_error(); + } + return APR_SUCCESS; +} + + +APR_DECLARE(apr_status_t) apr_socket_timeout_set(apr_socket_t *sock, apr_interval_time_t t) +{ + apr_status_t stat; + + if (t == 0) { + /* Set the socket non-blocking if it was previously blocking */ + if (sock->timeout != 0) { + if ((stat = sononblock(sock->socketdes)) != APR_SUCCESS) + return stat; + } + } + else if (t > 0) { + /* Set the socket to blocking if it was previously non-blocking */ + if (sock->timeout == 0) { + if ((stat = soblock(sock->socketdes)) != APR_SUCCESS) + return stat; + } + /* Reset socket timeouts if the new timeout differs from the old timeout */ + if (sock->timeout != t) + { + /* Win32 timeouts are in msec, represented as int */ + sock->timeout_ms = (int)apr_time_as_msec(t); + setsockopt(sock->socketdes, SOL_SOCKET, SO_RCVTIMEO, + (char *) &sock->timeout_ms, + sizeof(sock->timeout_ms)); + setsockopt(sock->socketdes, SOL_SOCKET, SO_SNDTIMEO, + (char *) &sock->timeout_ms, + sizeof(sock->timeout_ms)); + } + } + else if (t < 0) { + int zero = 0; + /* Set the socket to blocking with infinite timeouts */ + if ((stat = soblock(sock->socketdes)) != APR_SUCCESS) + return stat; + setsockopt(sock->socketdes, SOL_SOCKET, SO_RCVTIMEO, + (char *) &zero, sizeof(zero)); + setsockopt(sock->socketdes, SOL_SOCKET, SO_SNDTIMEO, + (char *) &zero, sizeof(zero)); + } + sock->timeout = t; + return APR_SUCCESS; +} + + +APR_DECLARE(apr_status_t) apr_socket_opt_set(apr_socket_t *sock, + apr_int32_t opt, apr_int32_t on) +{ + int one; + apr_status_t stat; + + one = on ? 1 : 0; + + switch (opt) { + case APR_SO_TIMEOUT: + { + /* XXX: To be deprecated */ + return apr_socket_timeout_set(sock, on); + } + case APR_SO_KEEPALIVE: + if (on != apr_is_option_set(sock->netmask, APR_SO_KEEPALIVE)) { + if (setsockopt(sock->socketdes, SOL_SOCKET, SO_KEEPALIVE, + (void *)&one, sizeof(int)) == -1) { + return apr_get_netos_error(); + } + apr_set_option(&sock->netmask,APR_SO_KEEPALIVE, on); + } + break; + case APR_SO_DEBUG: + if (on != apr_is_option_set(sock->netmask, APR_SO_DEBUG)) { + if (setsockopt(sock->socketdes, SOL_SOCKET, SO_DEBUG, + (void *)&one, sizeof(int)) == -1) { + return apr_get_netos_error(); + } + apr_set_option(&sock->netmask, APR_SO_DEBUG, on); + } + break; + case APR_SO_SNDBUF: + if (setsockopt(sock->socketdes, SOL_SOCKET, SO_SNDBUF, + (void *)&on, sizeof(int)) == -1) { + return apr_get_netos_error(); + } + break; + case APR_SO_RCVBUF: + if (setsockopt(sock->socketdes, SOL_SOCKET, SO_RCVBUF, + (void *)&on, sizeof(int)) == -1) { + return apr_get_netos_error(); + } + break; + case APR_SO_REUSEADDR: + if (on != apr_is_option_set(sock->netmask, APR_SO_REUSEADDR)) { + if (setsockopt(sock->socketdes, SOL_SOCKET, SO_REUSEADDR, + (void *)&one, sizeof(int)) == -1) { + return apr_get_netos_error(); + } + apr_set_option(&sock->netmask, APR_SO_REUSEADDR, on); + } + break; + case APR_SO_NONBLOCK: + if (apr_is_option_set(sock->netmask, APR_SO_NONBLOCK) != on) { + if (on) { + if ((stat = sononblock(sock->socketdes)) != APR_SUCCESS) + return stat; + } + else { + if ((stat = soblock(sock->socketdes)) != APR_SUCCESS) + return stat; + } + apr_set_option(&sock->netmask, APR_SO_NONBLOCK, on); + } + break; + case APR_SO_LINGER: + { + if (apr_is_option_set(sock->netmask, APR_SO_LINGER) != on) { + struct linger li; + li.l_onoff = on; + li.l_linger = APR_MAX_SECS_TO_LINGER; + if (setsockopt(sock->socketdes, SOL_SOCKET, SO_LINGER, + (char *) &li, sizeof(struct linger)) == -1) { + return apr_get_netos_error(); + } + apr_set_option(&sock->netmask, APR_SO_LINGER, on); + } + break; + } + case APR_TCP_NODELAY: + if (apr_is_option_set(sock->netmask, APR_TCP_NODELAY) != on) { + int optlevel = IPPROTO_TCP; + int optname = TCP_NODELAY; + +#if APR_HAVE_SCTP + if (sock->protocol == IPPROTO_SCTP) { + optlevel = IPPROTO_SCTP; + optname = SCTP_NODELAY; + } +#endif + if (setsockopt(sock->socketdes, optlevel, optname, + (void *)&on, sizeof(int)) == -1) { + return apr_get_netos_error(); + } + apr_set_option(&sock->netmask, APR_TCP_NODELAY, on); + } + break; + case APR_IPV6_V6ONLY: +#if APR_HAVE_IPV6 && defined(IPV6_V6ONLY) + /* we don't know the initial setting of this option, + * so don't check sock->netmask since that optimization + * won't work + */ + if (setsockopt(sock->socketdes, IPPROTO_IPV6, IPV6_V6ONLY, + (void *)&on, sizeof(int)) == -1) { + return apr_get_netos_error(); + } + apr_set_option(&sock->netmask, APR_IPV6_V6ONLY, on); +#else + return APR_ENOTIMPL; +#endif + break; + default: + return APR_EINVAL; + break; + } + return APR_SUCCESS; +} + + +APR_DECLARE(apr_status_t) apr_socket_timeout_get(apr_socket_t *sock, apr_interval_time_t *t) +{ + *t = sock->timeout; + return APR_SUCCESS; +} + + +APR_DECLARE(apr_status_t) apr_socket_opt_get(apr_socket_t *sock, + apr_int32_t opt, apr_int32_t *on) +{ + switch (opt) { + case APR_SO_TIMEOUT: + /* XXX: to be deprecated */ + *on = (apr_int32_t)sock->timeout; + break; + case APR_SO_DISCONNECTED: + *on = sock->disconnected; + break; + case APR_SO_KEEPALIVE: + case APR_SO_DEBUG: + case APR_SO_REUSEADDR: + case APR_SO_NONBLOCK: + case APR_SO_LINGER: + default: + *on = apr_is_option_set(sock->netmask, opt); + } + return APR_SUCCESS; +} + + +/* deprecated */ +APR_DECLARE(apr_status_t) apr_setsocketopt(apr_socket_t *sock, + apr_int32_t opt, apr_int32_t on) +{ + return apr_socket_opt_set(sock, opt, on); +} + +APR_DECLARE(apr_status_t) apr_getsocketopt(apr_socket_t *sock, + apr_int32_t opt, apr_int32_t *on) +{ + return apr_socket_opt_get(sock, opt, on); +} + + +APR_DECLARE(apr_status_t) apr_socket_atmark(apr_socket_t *sock, int *atmark) +{ + u_long oobmark; + + if (ioctlsocket(sock->socketdes, SIOCATMARK, (void*) &oobmark) < 0) + return apr_get_netos_error(); + + *atmark = (oobmark != 0); + + return APR_SUCCESS; +} + + +APR_DECLARE(apr_status_t) apr_gethostname(char *buf, int len, + apr_pool_t *cont) +{ + if (gethostname(buf, len) == -1) { + buf[0] = '\0'; + return apr_get_netos_error(); + } + else if (!memchr(buf, '\0', len)) { /* buffer too small */ + buf[0] = '\0'; + return APR_ENAMETOOLONG; + } + return APR_SUCCESS; +} + -- cgit 1.2.3-korg