1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "apr_arch_networkio.h"
18#include "apr_network_io.h"
19#include "apr_portable.h"
20#include "apr_general.h"
21#include "apr_lib.h"
22
23static int os2_socket_init(int, int ,int);
24
25int (*apr_os2_socket)(int, int, int) = os2_socket_init;
26int (*apr_os2_select)(int *, int, int, int, long) = NULL;
27int (*apr_os2_sock_errno)() = NULL;
28int (*apr_os2_accept)(int, struct sockaddr *, int *) = NULL;
29int (*apr_os2_bind)(int, struct sockaddr *, int) = NULL;
30int (*apr_os2_connect)(int, struct sockaddr *, int) = NULL;
31int (*apr_os2_getpeername)(int, struct sockaddr *, int *) = NULL;
32int (*apr_os2_getsockname)(int, struct sockaddr *, int *) = NULL;
33int (*apr_os2_getsockopt)(int, int, int, char *, int *) = NULL;
34int (*apr_os2_ioctl)(int, int, caddr_t, int) = NULL;
35int (*apr_os2_listen)(int, int) = NULL;
36int (*apr_os2_recv)(int, char *, int, int) = NULL;
37int (*apr_os2_send)(int, const char *, int, int) = NULL;
38int (*apr_os2_setsockopt)(int, int, int, char *, int) = NULL;
39int (*apr_os2_shutdown)(int, int) = NULL;
40int (*apr_os2_soclose)(int) = NULL;
41int (*apr_os2_writev)(int, struct iovec *, int) = NULL;
42int (*apr_os2_sendto)(int, const char *, int, int, const struct sockaddr *, int);
43int (*apr_os2_recvfrom)(int, char *, int, int, struct sockaddr *, int *);
44
45static HMODULE hSO32DLL;
46
47static int os2_fn_link()
48{
49    DosEnterCritSec(); /* Stop two threads doing this at the same time */
50
51    if (apr_os2_socket == os2_socket_init) {
52        ULONG rc;
53        char errorstr[200];
54
55        rc = DosLoadModule(errorstr, sizeof(errorstr), "SO32DLL", &hSO32DLL);
56
57        if (rc)
58            return APR_OS2_STATUS(rc);
59
60        rc = DosQueryProcAddr(hSO32DLL, 0, "SOCKET", &apr_os2_socket);
61
62        if (!rc)
63            rc = DosQueryProcAddr(hSO32DLL, 0, "SELECT", &apr_os2_select);
64
65        if (!rc)
66            rc = DosQueryProcAddr(hSO32DLL, 0, "SOCK_ERRNO", &apr_os2_sock_errno);
67
68        if (!rc)
69            rc = DosQueryProcAddr(hSO32DLL, 0, "ACCEPT", &apr_os2_accept);
70
71        if (!rc)
72            rc = DosQueryProcAddr(hSO32DLL, 0, "BIND", &apr_os2_bind);
73
74        if (!rc)
75            rc = DosQueryProcAddr(hSO32DLL, 0, "CONNECT", &apr_os2_connect);
76
77        if (!rc)
78            rc = DosQueryProcAddr(hSO32DLL, 0, "GETPEERNAME", &apr_os2_getpeername);
79
80        if (!rc)
81            rc = DosQueryProcAddr(hSO32DLL, 0, "GETSOCKNAME", &apr_os2_getsockname);
82
83        if (!rc)
84            rc = DosQueryProcAddr(hSO32DLL, 0, "GETSOCKOPT", &apr_os2_getsockopt);
85
86        if (!rc)
87            rc = DosQueryProcAddr(hSO32DLL, 0, "IOCTL", &apr_os2_ioctl);
88
89        if (!rc)
90            rc = DosQueryProcAddr(hSO32DLL, 0, "LISTEN", &apr_os2_listen);
91
92        if (!rc)
93            rc = DosQueryProcAddr(hSO32DLL, 0, "RECV", &apr_os2_recv);
94
95        if (!rc)
96            rc = DosQueryProcAddr(hSO32DLL, 0, "SEND", &apr_os2_send);
97
98        if (!rc)
99            rc = DosQueryProcAddr(hSO32DLL, 0, "SETSOCKOPT", &apr_os2_setsockopt);
100
101        if (!rc)
102            rc = DosQueryProcAddr(hSO32DLL, 0, "SHUTDOWN", &apr_os2_shutdown);
103
104        if (!rc)
105            rc = DosQueryProcAddr(hSO32DLL, 0, "SOCLOSE", &apr_os2_soclose);
106
107        if (!rc)
108            rc = DosQueryProcAddr(hSO32DLL, 0, "WRITEV", &apr_os2_writev);
109
110        if (!rc)
111            rc = DosQueryProcAddr(hSO32DLL, 0, "SENDTO", &apr_os2_sendto);
112
113        if (!rc)
114            rc = DosQueryProcAddr(hSO32DLL, 0, "RECVFROM", &apr_os2_recvfrom);
115
116        if (rc)
117            return APR_OS2_STATUS(rc);
118    }
119
120    DosExitCritSec();
121    return APR_SUCCESS;
122}
123
124
125
126static int os2_socket_init(int domain, int type, int protocol)
127{
128    int rc = os2_fn_link();
129    if (rc == APR_SUCCESS)
130        return apr_os2_socket(domain, type, protocol);
131    return rc;
132}
133