summaryrefslogtreecommitdiffstats
path: root/network_io/os2
diff options
context:
space:
mode:
Diffstat (limited to 'network_io/os2')
-rw-r--r--network_io/os2/inet_ntop.c1
-rw-r--r--network_io/os2/inet_pton.c1
-rw-r--r--network_io/os2/os2calls.c132
-rw-r--r--network_io/os2/sendrecv.c155
-rw-r--r--network_io/os2/sendrecv_udp.c104
-rw-r--r--network_io/os2/sockaddr.c1
-rw-r--r--network_io/os2/socket_util.c1
-rw-r--r--network_io/os2/sockets.c317
-rw-r--r--network_io/os2/sockopt.c144
9 files changed, 856 insertions, 0 deletions
diff --git a/network_io/os2/inet_ntop.c b/network_io/os2/inet_ntop.c
new file mode 100644
index 0000000..f1f79d4
--- /dev/null
+++ b/network_io/os2/inet_ntop.c
@@ -0,0 +1 @@
+#include "../unix/inet_ntop.c"
diff --git a/network_io/os2/inet_pton.c b/network_io/os2/inet_pton.c
new file mode 100644
index 0000000..dbd3ac4
--- /dev/null
+++ b/network_io/os2/inet_pton.c
@@ -0,0 +1 @@
+#include "../unix/inet_pton.c"
diff --git a/network_io/os2/os2calls.c b/network_io/os2/os2calls.c
new file mode 100644
index 0000000..6bf1fcd
--- /dev/null
+++ b/network_io/os2/os2calls.c
@@ -0,0 +1,132 @@
+/* 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_portable.h"
+#include "apr_general.h"
+#include "apr_lib.h"
+
+static int os2_socket_init(int, int ,int);
+
+int (*apr_os2_socket)(int, int, int) = os2_socket_init;
+int (*apr_os2_select)(int *, int, int, int, long) = NULL;
+int (*apr_os2_sock_errno)() = NULL;
+int (*apr_os2_accept)(int, struct sockaddr *, int *) = NULL;
+int (*apr_os2_bind)(int, struct sockaddr *, int) = NULL;
+int (*apr_os2_connect)(int, struct sockaddr *, int) = NULL;
+int (*apr_os2_getpeername)(int, struct sockaddr *, int *) = NULL;
+int (*apr_os2_getsockname)(int, struct sockaddr *, int *) = NULL;
+int (*apr_os2_getsockopt)(int, int, int, char *, int *) = NULL;
+int (*apr_os2_ioctl)(int, int, caddr_t, int) = NULL;
+int (*apr_os2_listen)(int, int) = NULL;
+int (*apr_os2_recv)(int, char *, int, int) = NULL;
+int (*apr_os2_send)(int, const char *, int, int) = NULL;
+int (*apr_os2_setsockopt)(int, int, int, char *, int) = NULL;
+int (*apr_os2_shutdown)(int, int) = NULL;
+int (*apr_os2_soclose)(int) = NULL;
+int (*apr_os2_writev)(int, struct iovec *, int) = NULL;
+int (*apr_os2_sendto)(int, const char *, int, int, const struct sockaddr *, int);
+int (*apr_os2_recvfrom)(int, char *, int, int, struct sockaddr *, int *);
+
+static HMODULE hSO32DLL;
+
+static int os2_fn_link()
+{
+ DosEnterCritSec(); /* Stop two threads doing this at the same time */
+
+ if (apr_os2_socket == os2_socket_init) {
+ ULONG rc;
+ char errorstr[200];
+
+ rc = DosLoadModule(errorstr, sizeof(errorstr), "SO32DLL", &hSO32DLL);
+
+ if (rc)
+ return APR_OS2_STATUS(rc);
+
+ rc = DosQueryProcAddr(hSO32DLL, 0, "SOCKET", &apr_os2_socket);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "SELECT", &apr_os2_select);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "SOCK_ERRNO", &apr_os2_sock_errno);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "ACCEPT", &apr_os2_accept);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "BIND", &apr_os2_bind);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "CONNECT", &apr_os2_connect);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "GETPEERNAME", &apr_os2_getpeername);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "GETSOCKNAME", &apr_os2_getsockname);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "GETSOCKOPT", &apr_os2_getsockopt);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "IOCTL", &apr_os2_ioctl);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "LISTEN", &apr_os2_listen);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "RECV", &apr_os2_recv);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "SEND", &apr_os2_send);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "SETSOCKOPT", &apr_os2_setsockopt);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "SHUTDOWN", &apr_os2_shutdown);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "SOCLOSE", &apr_os2_soclose);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "WRITEV", &apr_os2_writev);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "SENDTO", &apr_os2_sendto);
+
+ if (!rc)
+ rc = DosQueryProcAddr(hSO32DLL, 0, "RECVFROM", &apr_os2_recvfrom);
+
+ if (rc)
+ return APR_OS2_STATUS(rc);
+ }
+
+ DosExitCritSec();
+ return APR_SUCCESS;
+}
+
+
+
+static int os2_socket_init(int domain, int type, int protocol)
+{
+ int rc = os2_fn_link();
+ if (rc == APR_SUCCESS)
+ return apr_os2_socket(domain, type, protocol);
+ return rc;
+}
diff --git a/network_io/os2/sendrecv.c b/network_io/os2/sendrecv.c
new file mode 100644
index 0000000..839ff3f
--- /dev/null
+++ b/network_io/os2/sendrecv.c
@@ -0,0 +1,155 @@
+/* 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 <sys/time.h>
+
+APR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf,
+ apr_size_t *len)
+{
+ apr_ssize_t rv;
+ int fds, err = 0;
+
+ if (*len > 65536) {
+ *len = 65536;
+ }
+
+ do {
+ if (!sock->nonblock || err == SOCEWOULDBLOCK) {
+ fds = sock->socketdes;
+ rv = select(&fds, 0, 1, 0, sock->timeout >= 0 ? sock->timeout/1000 : -1);
+
+ if (rv != 1) {
+ *len = 0;
+ err = sock_errno();
+
+ if (rv == 0)
+ return APR_TIMEUP;
+
+ if (err == SOCEINTR)
+ continue;
+
+ return APR_OS2_STATUS(err);
+ }
+ }
+
+ rv = send(sock->socketdes, buf, (*len), 0);
+ err = rv < 0 ? sock_errno() : 0;
+ } while (err == SOCEINTR || err == SOCEWOULDBLOCK);
+
+ if (err) {
+ *len = 0;
+ return APR_OS2_STATUS(err);
+ }
+
+ (*len) = rv;
+ 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;
+ int fds, err = 0;
+
+ do {
+ if (!sock->nonblock || (err == SOCEWOULDBLOCK && sock->timeout != 0)) {
+ fds = sock->socketdes;
+ rv = select(&fds, 1, 0, 0, sock->timeout >= 0 ? sock->timeout/1000 : -1);
+
+ if (rv != 1) {
+ *len = 0;
+ err = sock_errno();
+
+ if (rv == 0)
+ return APR_TIMEUP;
+
+ if (err == SOCEINTR)
+ continue;
+
+ return APR_OS2_STATUS(err);
+ }
+ }
+
+ rv = recv(sock->socketdes, buf, (*len), 0);
+ err = rv < 0 ? sock_errno() : 0;
+ } while (err == SOCEINTR || (err == SOCEWOULDBLOCK && sock->timeout != 0));
+
+ if (err) {
+ *len = 0;
+ return APR_OS2_STATUS(err);
+ }
+
+ (*len) = rv;
+ return rv == 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 *len)
+{
+ apr_status_t rv;
+ struct iovec *tmpvec;
+ int fds, err = 0;
+ int nv_tosend, total = 0;
+
+ /* Make sure writev() only gets fed 64k at a time */
+ for ( nv_tosend = 0; nv_tosend < nvec && total + vec[nv_tosend].iov_len < 65536; nv_tosend++ ) {
+ total += vec[nv_tosend].iov_len;
+ }
+
+ tmpvec = alloca(sizeof(struct iovec) * nv_tosend);
+ memcpy(tmpvec, vec, sizeof(struct iovec) * nv_tosend);
+
+ do {
+ if (!sock->nonblock || err == SOCEWOULDBLOCK) {
+ fds = sock->socketdes;
+ rv = select(&fds, 0, 1, 0, sock->timeout >= 0 ? sock->timeout/1000 : -1);
+
+ if (rv != 1) {
+ *len = 0;
+ err = sock_errno();
+
+ if (rv == 0)
+ return APR_TIMEUP;
+
+ if (err == SOCEINTR)
+ continue;
+
+ return APR_OS2_STATUS(err);
+ }
+ }
+
+ rv = writev(sock->socketdes, tmpvec, nv_tosend);
+ err = rv < 0 ? sock_errno() : 0;
+ } while (err == SOCEINTR || err == SOCEWOULDBLOCK);
+
+ if (err) {
+ *len = 0;
+ return APR_OS2_STATUS(err);
+ }
+
+ *len = rv;
+ return APR_SUCCESS;
+}
diff --git a/network_io/os2/sendrecv_udp.c b/network_io/os2/sendrecv_udp.c
new file mode 100644
index 0000000..c0dcd85
--- /dev/null
+++ b/network_io/os2/sendrecv_udp.c
@@ -0,0 +1,104 @@
+/* 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_support.h"
+#include "apr_lib.h"
+#include <sys/time.h>
+
+
+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;
+ int serrno;
+
+ do {
+ rv = sendto(sock->socketdes, buf, (*len), flags,
+ (struct sockaddr*)&where->sa,
+ where->salen);
+ } while (rv == -1 && (serrno = sock_errno()) == EINTR);
+
+ if (rv == -1 && serrno == SOCEWOULDBLOCK && 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 && (serrno = sock_errno()) == SOCEINTR);
+ }
+ }
+
+ if (rv == -1) {
+ *len = 0;
+ return APR_FROM_OS_ERROR(serrno);
+ }
+
+ *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;
+ int serrno;
+
+ do {
+ rv = recvfrom(sock->socketdes, buf, (*len), flags,
+ (struct sockaddr*)&from->sa, &from->salen);
+ } while (rv == -1 && (serrno = sock_errno()) == EINTR);
+
+ if (rv == -1 && serrno == SOCEWOULDBLOCK && 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 && (serrno = sock_errno()) == EINTR);
+ }
+ }
+
+ if (rv == -1) {
+ (*len) = 0;
+ return APR_FROM_OS_ERROR(serrno);
+ }
+
+ (*len) = rv;
+
+ if (rv == 0 && sock->type == SOCK_STREAM)
+ return APR_EOF;
+
+ return APR_SUCCESS;
+}
diff --git a/network_io/os2/sockaddr.c b/network_io/os2/sockaddr.c
new file mode 100644
index 0000000..2afe4b7
--- /dev/null
+++ b/network_io/os2/sockaddr.c
@@ -0,0 +1 @@
+#include "../unix/sockaddr.c"
diff --git a/network_io/os2/socket_util.c b/network_io/os2/socket_util.c
new file mode 100644
index 0000000..cdc1cea
--- /dev/null
+++ b/network_io/os2/socket_util.c
@@ -0,0 +1 @@
+#include "../unix/socket_util.c"
diff --git a/network_io/os2/sockets.c b/network_io/os2/sockets.c
new file mode 100644
index 0000000..bb951e4
--- /dev/null
+++ b/network_io/os2/sockets.c
@@ -0,0 +1,317 @@
+/* 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_arch_inherit.h"
+#include "apr_network_io.h"
+#include "apr_general.h"
+#include "apr_portable.h"
+#include "apr_lib.h"
+#include "apr_strings.h"
+#include <errno.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include "apr_arch_os2calls.h"
+
+static apr_status_t socket_cleanup(void *sock)
+{
+ apr_socket_t *thesocket = sock;
+
+ if (thesocket->socketdes < 0) {
+ return APR_EINVALSOCK;
+ }
+
+ if (soclose(thesocket->socketdes) == 0) {
+ thesocket->socketdes = -1;
+ return APR_SUCCESS;
+ }
+ else {
+ return APR_OS2_STATUS(sock_errno());
+ }
+}
+
+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)->pool = p;
+ (*new)->local_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
+ sizeof(apr_sockaddr_t));
+ (*new)->local_addr->pool = p;
+
+ (*new)->remote_addr = (apr_sockaddr_t *)apr_pcalloc((*new)->pool,
+ sizeof(apr_sockaddr_t));
+ (*new)->remote_addr->pool = p;
+ (*new)->remote_addr_unknown = 1;
+
+ /* Create a pollset with room for one descriptor. */
+ /* ### check return codes */
+ (void) apr_pollset_create(&(*new)->pollset, 1, p, 0);
+}
+
+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(apr_socket_t **new, int family, int type,
+ int protocol, apr_pool_t *cont)
+{
+ int downgrade = (family == AF_UNSPEC);
+ apr_pollfd_t pfd;
+
+ if (family == AF_UNSPEC) {
+#if APR_HAVE_IPV6
+ family = AF_INET6;
+#else
+ family = AF_INET;
+#endif
+ }
+
+ alloc_socket(new, cont);
+
+ (*new)->socketdes = socket(family, type, protocol);
+#if APR_HAVE_IPV6
+ if ((*new)->socketdes < 0 && downgrade) {
+ family = AF_INET;
+ (*new)->socketdes = socket(family, type, protocol);
+ }
+#endif
+
+ if ((*new)->socketdes < 0) {
+ return APR_OS2_STATUS(sock_errno());
+ }
+ set_socket_vars(*new, family, type, protocol);
+
+ (*new)->timeout = -1;
+ (*new)->nonblock = FALSE;
+ apr_pool_cleanup_register((*new)->pool, (void *)(*new),
+ socket_cleanup, apr_pool_cleanup_null);
+
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_socket_shutdown(apr_socket_t *thesocket,
+ apr_shutdown_how_e how)
+{
+ if (shutdown(thesocket->socketdes, how) == 0) {
+ return APR_SUCCESS;
+ }
+ else {
+ return APR_OS2_STATUS(sock_errno());
+ }
+}
+
+APR_DECLARE(apr_status_t) apr_socket_close(apr_socket_t *thesocket)
+{
+ apr_pool_cleanup_kill(thesocket->pool, 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_OS2_STATUS(sock_errno());
+ else {
+ sock->local_addr = sa;
+ /* XXX IPv6 - this assumes sin_port and sin6_port at same offset */
+ if (sock->local_addr->sa.sin.sin_port == 0) { /* no need for ntohs() when comparing w/ 0 */
+ sock->local_port_unknown = 1; /* kernel got us an 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) == -1)
+ return APR_OS2_STATUS(sock_errno());
+ else
+ return APR_SUCCESS;
+}
+
+APR_DECLARE(apr_status_t) apr_socket_accept(apr_socket_t **new,
+ apr_socket_t *sock,
+ apr_pool_t *connection_context)
+{
+ alloc_socket(new, connection_context);
+ set_socket_vars(*new, sock->local_addr->sa.sin.sin_family, SOCK_STREAM, sock->protocol);
+
+ (*new)->timeout = -1;
+ (*new)->nonblock = FALSE;
+
+ (*new)->socketdes = accept(sock->socketdes,
+ (struct sockaddr *)&(*new)->remote_addr->sa,
+ &(*new)->remote_addr->salen);
+
+ if ((*new)->socketdes < 0) {
+ return APR_OS2_STATUS(sock_errno());
+ }
+
+ *(*new)->local_addr = *sock->local_addr;
+ (*new)->local_addr->pool = connection_context;
+ (*new)->remote_addr->port = ntohs((*new)->remote_addr->sa.sin.sin_port);
+
+ /* 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;
+ }
+
+ apr_pool_cleanup_register((*new)->pool, (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)
+{
+ if ((connect(sock->socketdes, (struct sockaddr *)&sa->sa.sin,
+ sa->salen) < 0) &&
+ (sock_errno() != SOCEINPROGRESS)) {
+ return APR_OS2_STATUS(sock_errno());
+ }
+ else {
+ int namelen = sizeof(sock->local_addr->sa.sin);
+ getsockname(sock->socketdes, (struct sockaddr *)&sock->local_addr->sa.sin,
+ &namelen);
+ sock->remote_addr = sa;
+ return APR_SUCCESS;
+ }
+}
+
+APR_DECLARE(apr_status_t) apr_socket_type_get(apr_socket_t *sock, int *type)
+{
+ *type = sock->type;
+ 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->pool, sizeof(sock_userdata_t));
+
+ new->key = apr_pstrdup(sock->pool, key);
+ new->data = data;
+ new->next = sock->userdata;
+ sock->userdata = new;
+
+ if (cleanup) {
+ apr_pool_cleanup_register(sock->pool, 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);
+ set_socket_vars(*apr_sock, os_sock_info->family, os_sock_info->type, os_sock_info->protocol);
+ (*apr_sock)->timeout = -1;
+ (*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);
+ /* 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);
+ /* 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)->pool, (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 (cont == NULL) {
+ return APR_ENOPOOL;
+ }
+ if ((*sock) == NULL) {
+ alloc_socket(sock, cont);
+ set_socket_vars(*sock, AF_INET, SOCK_STREAM, 0);
+ (*sock)->timeout = -1;
+ }
+
+ (*sock)->local_port_unknown = (*sock)->local_interface_unknown = 1;
+ (*sock)->remote_addr_unknown = 1;
+ (*sock)->socketdes = *thesock;
+ return APR_SUCCESS;
+}
+
+APR_POOL_IMPLEMENT_ACCESSOR(socket);
+
+APR_IMPLEMENT_INHERIT_SET(socket, inherit, pool, socket_cleanup)
+
+APR_IMPLEMENT_INHERIT_UNSET(socket, inherit, pool, socket_cleanup)
+
diff --git a/network_io/os2/sockopt.c b/network_io/os2/sockopt.c
new file mode 100644
index 0000000..094cd24
--- /dev/null
+++ b/network_io/os2/sockopt.c
@@ -0,0 +1,144 @@
+/* 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_strings.h"
+#include <errno.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+#include <netinet/in.h>
+#include <unistd.h>
+#include <netdb.h>
+#include <sys/so_ioctl.h>
+
+
+APR_DECLARE(apr_status_t) apr_socket_timeout_set(apr_socket_t *sock,
+ apr_interval_time_t t)
+{
+ 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;
+ struct linger li;
+
+ if (on)
+ one = 1;
+ else
+ one = 0;
+
+ if (opt & APR_SO_KEEPALIVE) {
+ if (setsockopt(sock->socketdes, SOL_SOCKET, SO_KEEPALIVE, (void *)&one, sizeof(int)) == -1) {
+ return APR_OS2_STATUS(sock_errno());
+ }
+ }
+ if (opt & APR_SO_DEBUG) {
+ if (setsockopt(sock->socketdes, SOL_SOCKET, SO_DEBUG, (void *)&one, sizeof(int)) == -1) {
+ return APR_OS2_STATUS(sock_errno());
+ }
+ }
+ if (opt & APR_SO_BROADCAST) {
+ if (setsockopt(sock->socketdes, SOL_SOCKET, SO_BROADCAST, (void *)&one, sizeof(int)) == -1) {
+ return APR_FROM_OS_ERROR(sock_errno());
+ }
+ }
+ if (opt & APR_SO_REUSEADDR) {
+ if (setsockopt(sock->socketdes, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(int)) == -1) {
+ return APR_OS2_STATUS(sock_errno());
+ }
+ }
+ if (opt & APR_SO_SNDBUF) {
+ if (setsockopt(sock->socketdes, SOL_SOCKET, SO_SNDBUF, (void *)&on, sizeof(int)) == -1) {
+ return APR_OS2_STATUS(sock_errno());
+ }
+ }
+ if (opt & APR_SO_NONBLOCK) {
+ if (ioctl(sock->socketdes, FIONBIO, (caddr_t)&one, sizeof(one)) == -1) {
+ return APR_OS2_STATUS(sock_errno());
+ } else {
+ sock->nonblock = one;
+ }
+ }
+ if (opt & APR_SO_LINGER) {
+ 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_OS2_STATUS(sock_errno());
+ }
+ }
+ if (opt & APR_TCP_NODELAY) {
+ if (setsockopt(sock->socketdes, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(int)) == -1) {
+ return APR_OS2_STATUS(sock_errno());
+ }
+ }
+ 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) {
+ default:
+ return APR_EINVAL;
+ }
+ return APR_SUCCESS;
+}
+
+
+APR_DECLARE(apr_status_t) apr_socket_atmark(apr_socket_t *sock, int *atmark)
+{
+ int oobmark;
+
+ if (ioctl(sock->socketdes, SIOCATMARK, (void*)&oobmark, sizeof(oobmark)) < 0) {
+ return APR_OS2_STATUS(sock_errno());
+ }
+
+ *atmark = (oobmark != 0);
+
+ return APR_SUCCESS;
+}
+
+
+APR_DECLARE(apr_status_t) apr_gethostname(char *buf, apr_int32_t len,
+ apr_pool_t *cont)
+{
+ if (gethostname(buf, len) == -1) {
+ buf[0] = '\0';
+ return APR_OS2_STATUS(sock_errno());
+ }
+ else if (!memchr(buf, '\0', len)) { /* buffer too small */
+ buf[0] = '\0';
+ return APR_ENAMETOOLONG;
+ }
+ return APR_SUCCESS;
+}