Apache HTTPD
sockopt.c
Go to the documentation of this file.
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_arch_misc.h" /* apr_os_level */
19#include "apr_network_io.h"
20#include "apr_general.h"
21#include "apr_strings.h"
22#include <string.h>
23
24/* IPV6_V6ONLY is missing from pre-Windows 2008 SDK as well as MinGW
25 * (at least up through 1.0.16).
26 * Runtime support is a separate issue.
27 */
28#ifndef IPV6_V6ONLY
29#define IPV6_V6ONLY 27
30#endif
31
33{
34 u_long zero = 0;
35
36 if (ioctlsocket(sd, FIONBIO, &zero) == SOCKET_ERROR) {
37 return apr_get_netos_error();
38 }
39 return APR_SUCCESS;
40}
41
43{
44 u_long one = 1;
45
46 if (ioctlsocket(sd, FIONBIO, &one) == SOCKET_ERROR) {
47 return apr_get_netos_error();
48 }
49 return APR_SUCCESS;
50}
51
52
54{
56
57 if (t == 0) {
58 /* Set the socket non-blocking if it was previously blocking */
59 if (sock->timeout != 0) {
61 return stat;
62 }
63 }
64 else if (t > 0) {
65 /* Set the socket to blocking if it was previously non-blocking */
68 return stat;
70 }
71 /* Reset socket timeouts if the new timeout differs from the old timeout */
72 if (sock->timeout != t)
73 {
74 /* Win32 timeouts are in msec, represented as int */
77 (char *) &sock->timeout_ms,
78 sizeof(sock->timeout_ms));
80 (char *) &sock->timeout_ms,
81 sizeof(sock->timeout_ms));
82 }
83 }
84 else if (t < 0) {
85 int zero = 0;
86 /* Set the socket to blocking with infinite timeouts */
88 return stat;
90 (char *) &zero, sizeof(zero));
92 (char *) &zero, sizeof(zero));
93 }
94 sock->timeout = t;
95 return APR_SUCCESS;
96}
97
98
101{
102 int one;
104
105 one = on ? 1 : 0;
106
107 switch (opt) {
108 case APR_SO_KEEPALIVE:
111 (void *)&one, sizeof(int)) == -1) {
112 return apr_get_netos_error();
113 }
115 }
116 break;
117 case APR_SO_DEBUG:
120 (void *)&one, sizeof(int)) == -1) {
121 return apr_get_netos_error();
122 }
124 }
125 break;
126 case APR_SO_SNDBUF:
128 (void *)&on, sizeof(int)) == -1) {
129 return apr_get_netos_error();
130 }
131 break;
132 case APR_SO_RCVBUF:
134 (void *)&on, sizeof(int)) == -1) {
135 return apr_get_netos_error();
136 }
137 break;
138 case APR_SO_BROADCAST:
141 (void *)&one, sizeof(int)) == -1) {
142 return apr_get_netos_error();
143 }
145 }
146 break;
147 case APR_SO_REUSEADDR:
150 (void *)&one, sizeof(int)) == -1) {
151 return apr_get_netos_error();
152 }
154 }
155 break;
156 case APR_SO_NONBLOCK:
158 if (on) {
160 return stat;
161 }
162 else {
164 return stat;
165 }
167 }
168 break;
169 case APR_SO_LINGER:
170 {
172 struct linger li;
173 li.l_onoff = on;
174 li.l_linger = APR_MAX_SECS_TO_LINGER;
176 (char *) &li, sizeof(struct linger)) == -1) {
177 return apr_get_netos_error();
178 }
180 }
181 break;
182 }
184#if defined(TCP_DEFER_ACCEPT)
186 int optlevel = IPPROTO_TCP;
188
190 (void *)&on, sizeof(int)) == -1) {
191 return apr_get_netos_error();
192 }
194 }
195#else
196 return APR_ENOTIMPL;
197#endif
198 case APR_TCP_NODELAY:
200 int optlevel = IPPROTO_TCP;
201 int optname = TCP_NODELAY;
202
203#if APR_HAVE_SCTP
204 if (sock->protocol == IPPROTO_SCTP) {
207 }
208#endif
210 (void *)&on, sizeof(int)) == -1) {
211 return apr_get_netos_error();
212 }
214 }
215 break;
216 case APR_IPV6_V6ONLY:
217#if APR_HAVE_IPV6
220 /* apr_set_option() called at socket creation */
221 if (on) {
222 return APR_SUCCESS;
223 }
224 else {
225 return APR_ENOTIMPL;
226 }
227 }
228 /* we don't know the initial setting of this option,
229 * so don't check sock->options since that optimization
230 * won't work
231 */
233 (void *)&on, sizeof(int)) == -1) {
234 return apr_get_netos_error();
235 }
237#else
238 return APR_ENOTIMPL;
239#endif
240 break;
241 default:
242 return APR_EINVAL;
243 break;
244 }
245 return APR_SUCCESS;
246}
247
248
250{
251 *t = sock->timeout;
252 return APR_SUCCESS;
253}
254
255
258{
259 switch (opt) {
261 *on = sock->disconnected;
262 break;
263 case APR_SO_KEEPALIVE:
264 case APR_SO_DEBUG:
265 case APR_SO_REUSEADDR:
266 case APR_SO_NONBLOCK:
267 case APR_SO_LINGER:
268 default:
270 break;
271 }
272 return APR_SUCCESS;
273}
274
275
277{
279
280 if (ioctlsocket(sock->socketdes, SIOCATMARK, (void*) &oobmark) < 0)
281 return apr_get_netos_error();
282
283 *atmark = (oobmark != 0);
284
285 return APR_SUCCESS;
286}
287
288
291{
292 if (gethostname(buf, len) == -1) {
293 buf[0] = '\0';
294 return apr_get_netos_error();
295 }
296 else if (!memchr(buf, '\0', len)) { /* buffer too small */
297 buf[0] = '\0';
298 return APR_ENAMETOOLONG;
299 }
300 return APR_SUCCESS;
301}
302
const char apr_size_t len
Definition ap_regex.h:187
#define setsockopt
APR Miscellaneous library routines.
APR Network library.
APR Strings library.
const unsigned char * buf
Definition util_md5.h:50
#define APR_ENAMETOOLONG
Definition apr_errno.h:655
#define APR_ENOTIMPL
Definition apr_errno.h:476
#define APR_EINVAL
Definition apr_errno.h:711
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
apr_size_t size
#define apr_get_netos_error()
Definition apr_errno.h:1222
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * memchr(const void *s, int c, size_t n)
apr_pool_t * cont
Definition apr_getopt.h:103
apr_int32_t opt
apr_socket_t * sock
apr_interval_time_t t
int * atmark
#define APR_MAX_SECS_TO_LINGER
apr_int32_t apr_int32_t on
#define APR_SO_KEEPALIVE
#define APR_SO_SNDBUF
#define APR_TCP_NODELAY
#define APR_SO_DEBUG
#define APR_SO_RCVBUF
#define APR_SO_DISCONNECTED
#define APR_SO_LINGER
#define APR_SO_BROADCAST
#define APR_SO_REUSEADDR
#define APR_TCP_DEFER_ACCEPT
#define APR_SO_NONBLOCK
#define APR_IPV6_V6ONLY
#define apr_time_as_msec(time)
Definition apr_time.h:72
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
apr_int32_t family
apr_interval_time_t timeout
apr_int32_t disconnected
apr_sockaddr_t * local_addr
#define apr_is_option_set(skt, option)
#define apr_set_option(skt, option, on)
apr_status_t apr_socket_opt_set(apr_socket_t *sock, apr_int32_t opt, apr_int32_t on)
Definition sockopt.c:113
apr_status_t apr_socket_opt_get(apr_socket_t *sock, apr_int32_t opt, apr_int32_t *on)
Definition sockopt.c:362
apr_status_t apr_socket_atmark(apr_socket_t *sock, int *atmark)
Definition sockopt.c:373
static apr_status_t soblock(int sd)
Definition sockopt.c:21
apr_status_t apr_gethostname(char *buf, apr_int32_t len, apr_pool_t *cont)
Definition sockopt.c:389
static apr_status_t sononblock(int sd)
Definition sockopt.c:48
apr_status_t apr_socket_timeout_set(apr_socket_t *sock, apr_interval_time_t t)
Definition sockopt.c:75
apr_status_t apr_socket_timeout_get(apr_socket_t *sock, apr_interval_time_t *t)
Definition sockopt.c:355
typedef int(WSAAPI *apr_winapi_fpt_WSAPoll)(IN OUT LPWSAPOLLFD fdArray
APR_DECLARE_DATA apr_oslevel_e apr_os_level
Definition misc.c:24
@ APR_WIN_VISTA
#define IPV6_V6ONLY
Definition sockopt.c:29