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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_arch_networkio.h"
18 #include "apr_strings.h"
21 static apr_status_t soblock(int sd)
23 /* BeOS uses setsockopt at present for non blocking... */
27 fd_flags = fcntl(sd, F_GETFL, 0);
28 #if defined(O_NONBLOCK)
29 fd_flags &= ~O_NONBLOCK;
30 #elif defined(O_NDELAY)
31 fd_flags &= ~O_NDELAY;
32 #elif defined(FNDELAY)
35 #error Please teach APR how to make sockets blocking on your platform.
37 if (fcntl(sd, F_SETFL, fd_flags) == -1) {
42 if (setsockopt(sd, SOL_SOCKET, SO_NONBLOCK, &on, sizeof(int)) < 0)
48 static apr_status_t sononblock(int sd)
53 fd_flags = fcntl(sd, F_GETFL, 0);
54 #if defined(O_NONBLOCK)
55 fd_flags |= O_NONBLOCK;
56 #elif defined(O_NDELAY)
58 #elif defined(FNDELAY)
61 #error Please teach APR how to make sockets non-blocking on your platform.
63 if (fcntl(sd, F_SETFL, fd_flags) == -1) {
68 if (setsockopt(sd, SOL_SOCKET, SO_NONBLOCK, &on, sizeof(int)) < 0)
75 apr_status_t apr_socket_timeout_set(apr_socket_t *sock, apr_interval_time_t t)
79 /* If our timeout is positive or zero and our last timeout was
80 * negative, then we need to ensure that we are non-blocking.
81 * Conversely, if our timeout is negative and we had a positive
82 * or zero timeout, we must make sure our socket is blocking.
83 * We want to avoid calling fcntl more than necessary on the socket,
85 if (t >= 0 && sock->timeout < 0) {
86 if (apr_is_option_set(sock->netmask, APR_SO_NONBLOCK) != 1) {
87 if ((stat = sononblock(sock->socketdes)) != APR_SUCCESS) {
90 apr_set_option(&sock->netmask, APR_SO_NONBLOCK, 1);
93 else if (t < 0 && sock->timeout >= 0) {
94 if (apr_is_option_set(sock->netmask, APR_SO_NONBLOCK) != 0) {
95 if ((stat = soblock(sock->socketdes)) != APR_SUCCESS) {
98 apr_set_option(&sock->netmask, APR_SO_NONBLOCK, 0);
101 /* must disable the incomplete read support if we disable
105 sock->netmask &= ~APR_INCOMPLETE_READ;
108 apr_set_option(&sock->netmask, APR_SO_TIMEOUT, t > 0);
113 apr_status_t apr_socket_opt_set(apr_socket_t *sock,
114 apr_int32_t opt, apr_int32_t on)
124 case APR_SO_KEEPALIVE:
126 if (on != apr_is_option_set(sock->netmask, APR_SO_KEEPALIVE)) {
127 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_KEEPALIVE, (void *)&one, sizeof(int)) == -1) {
130 apr_set_option(&sock->netmask,APR_SO_KEEPALIVE, on);
137 if (on != apr_is_option_set(sock->netmask, APR_SO_DEBUG)) {
138 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_DEBUG, (void *)&one, sizeof(int)) == -1) {
141 apr_set_option(&sock->netmask, APR_SO_DEBUG, on);
144 case APR_SO_REUSEADDR:
145 if (on != apr_is_option_set(sock->netmask, APR_SO_REUSEADDR)) {
146 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(int)) == -1) {
149 apr_set_option(&sock->netmask, APR_SO_REUSEADDR, on);
154 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_SNDBUF, (void *)&on, sizeof(int)) == -1) {
163 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_RCVBUF, (void *)&on, sizeof(int)) == -1) {
170 case APR_SO_NONBLOCK:
171 if (apr_is_option_set(sock->netmask, APR_SO_NONBLOCK) != on) {
173 if ((rv = sononblock(sock->socketdes)) != APR_SUCCESS)
177 if ((rv = soblock(sock->socketdes)) != APR_SUCCESS)
180 apr_set_option(&sock->netmask, APR_SO_NONBLOCK, on);
185 if (apr_is_option_set(sock->netmask, APR_SO_LINGER) != on) {
188 li.l_linger = APR_MAX_SECS_TO_LINGER;
189 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(struct linger)) == -1) {
192 apr_set_option(&sock->netmask, APR_SO_LINGER, on);
199 /* XXX: To be deprecated */
200 return apr_socket_timeout_set(sock, on);
202 case APR_TCP_NODELAY:
203 #if defined(TCP_NODELAY)
204 if (apr_is_option_set(sock->netmask, APR_TCP_NODELAY) != on) {
205 int optlevel = IPPROTO_TCP;
206 int optname = TCP_NODELAY;
209 if (sock->protocol == IPPROTO_SCTP) {
210 optlevel = IPPROTO_SCTP;
211 optname = SCTP_NODELAY;
214 if (setsockopt(sock->socketdes, optlevel, optname, (void *)&on, sizeof(int)) == -1) {
217 apr_set_option(&sock->netmask, APR_TCP_NODELAY, on);
220 /* BeOS pre-BONE has TCP_NODELAY set by default.
221 * As it can't be turned off we might as well check if they're asking
222 * for it to be turned on!
233 #if APR_TCP_NOPUSH_FLAG
234 if (apr_is_option_set(sock->netmask, APR_TCP_NOPUSH) != on) {
235 int optlevel = IPPROTO_TCP;
236 int optname = TCP_NODELAY;
239 if (sock->protocol == IPPROTO_SCTP) {
240 optlevel = IPPROTO_SCTP;
241 optname = SCTP_NODELAY;
244 /* OK we're going to change some settings here... */
245 /* TCP_NODELAY is mutually exclusive, so do we have it set? */
246 if (apr_is_option_set(sock->netmask, APR_TCP_NODELAY) == 1 && on) {
247 /* If we want to set NOPUSH then if we have the TCP_NODELAY
248 * flag set we need to switch it off...
251 if (setsockopt(sock->socketdes, optlevel, optname,
252 (void*)&tmpflag, sizeof(int)) == -1) {
255 apr_set_option(&sock->netmask, APR_RESET_NODELAY, 1);
256 apr_set_option(&sock->netmask, APR_TCP_NODELAY, 0);
258 apr_set_option(&sock->netmask, APR_RESET_NODELAY, 0);
260 /* OK, now we can just set the TCP_NOPUSH flag accordingly...*/
261 if (setsockopt(sock->socketdes, IPPROTO_TCP, APR_TCP_NOPUSH_FLAG,
262 (void*)&on, sizeof(int)) == -1) {
265 apr_set_option(&sock->netmask, APR_TCP_NOPUSH, on);
266 if (!on && apr_is_option_set(sock->netmask, APR_RESET_NODELAY)) {
268 if (setsockopt(sock->socketdes, optlevel, optname,
269 (void*)&tmpflag, sizeof(int)) == -1) {
272 apr_set_option(&sock->netmask, APR_RESET_NODELAY,0);
273 apr_set_option(&sock->netmask, APR_TCP_NODELAY, 1);
280 case APR_INCOMPLETE_READ:
281 apr_set_option(&sock->netmask, APR_INCOMPLETE_READ, on);
283 case APR_IPV6_V6ONLY:
284 #if APR_HAVE_IPV6 && defined(IPV6_V6ONLY)
285 /* we don't know the initial setting of this option,
286 * so don't check sock->netmask since that optimization
289 if (setsockopt(sock->socketdes, IPPROTO_IPV6, IPV6_V6ONLY,
290 (void *)&on, sizeof(int)) == -1) {
293 apr_set_option(&sock->netmask, APR_IPV6_V6ONLY, on);
306 apr_status_t apr_socket_timeout_get(apr_socket_t *sock, apr_interval_time_t *t)
313 apr_status_t apr_socket_opt_get(apr_socket_t *sock,
314 apr_int32_t opt, apr_int32_t *on)
318 /* XXX: To be deprecated */
319 *on = (apr_int32_t)sock->timeout;
322 *on = apr_is_option_set(sock->netmask, opt);
328 apr_status_t apr_socket_atmark(apr_socket_t *sock, int *atmark)
330 /* In 1.0 we rely on compile failure to assure all platforms grabbed
331 * the correct header file support for SIOCATMARK, but we don't want
332 * to fail the build of 0.9. Keep things good for the released branch.
337 if (ioctl(sock->socketdes, SIOCATMARK, (void*) &oobmark) < 0)
338 return apr_get_netos_error();
340 *atmark = (oobmark != 0);
350 apr_status_t apr_setsocketopt(apr_socket_t *sock,
351 apr_int32_t opt, apr_int32_t on)
353 return apr_socket_opt_set(sock, opt, on);
356 apr_status_t apr_getsocketopt(apr_socket_t *sock,
357 apr_int32_t opt, apr_int32_t *on)
359 return apr_socket_opt_get(sock, opt, on);
363 apr_status_t apr_gethostname(char *buf, apr_int32_t len, apr_pool_t *cont)
365 if (gethostname(buf, len) == -1) {
369 else if (!memchr(buf, '\0', len)) { /* buffer too small */
370 /* note... most platforms just truncate in this condition
371 * linux+glibc return an error
374 return APR_ENAMETOOLONG;
379 #if APR_HAS_SO_ACCEPTFILTER
380 apr_status_t apr_socket_accept_filter(apr_socket_t *sock, char *name,
383 struct accept_filter_arg af;
384 strncpy(af.af_name, name, 16);
385 strncpy(af.af_arg, args, 256 - 16);
387 if ((setsockopt(sock->socketdes, SOL_SOCKET, SO_ACCEPTFILTER,
388 &af, sizeof(af))) < 0) {