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 new timeout is non-negative and our old timeout was
80 * negative, then we need to ensure that we are non-blocking.
81 * Conversely, if our new timeout is negative and we had
82 * non-negative timeout, we must make sure our socket is blocking.
83 * We want to avoid calling fcntl more than necessary on the
86 if (t >= 0 && sock->timeout < 0) {
87 if (apr_is_option_set(sock, APR_SO_NONBLOCK) != 1) {
88 if ((stat = sononblock(sock->socketdes)) != APR_SUCCESS) {
91 apr_set_option(sock, APR_SO_NONBLOCK, 1);
94 else if (t < 0 && sock->timeout >= 0) {
95 if (apr_is_option_set(sock, APR_SO_NONBLOCK) != 0) {
96 if ((stat = soblock(sock->socketdes)) != APR_SUCCESS) {
99 apr_set_option(sock, APR_SO_NONBLOCK, 0);
102 /* must disable the incomplete read support if we disable
106 sock->options &= ~APR_INCOMPLETE_READ;
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, APR_SO_KEEPALIVE)) {
127 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_KEEPALIVE, (void *)&one, sizeof(int)) == -1) {
130 apr_set_option(sock, APR_SO_KEEPALIVE, on);
137 if (on != apr_is_option_set(sock, APR_SO_DEBUG)) {
138 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_DEBUG, (void *)&one, sizeof(int)) == -1) {
141 apr_set_option(sock, APR_SO_DEBUG, on);
144 case APR_SO_BROADCAST:
146 if (on != apr_is_option_set(sock, APR_SO_BROADCAST)) {
147 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_BROADCAST, (void *)&one, sizeof(int)) == -1) {
150 apr_set_option(sock, APR_SO_BROADCAST, on);
156 case APR_SO_REUSEADDR:
157 if (on != apr_is_option_set(sock, APR_SO_REUSEADDR)) {
158 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(int)) == -1) {
161 apr_set_option(sock, APR_SO_REUSEADDR, on);
166 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_SNDBUF, (void *)&on, sizeof(int)) == -1) {
175 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_RCVBUF, (void *)&on, sizeof(int)) == -1) {
182 case APR_SO_NONBLOCK:
183 if (apr_is_option_set(sock, APR_SO_NONBLOCK) != on) {
185 if ((rv = sononblock(sock->socketdes)) != APR_SUCCESS)
189 if ((rv = soblock(sock->socketdes)) != APR_SUCCESS)
192 apr_set_option(sock, APR_SO_NONBLOCK, on);
197 if (apr_is_option_set(sock, APR_SO_LINGER) != on) {
200 li.l_linger = APR_MAX_SECS_TO_LINGER;
201 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(struct linger)) == -1) {
204 apr_set_option(sock, APR_SO_LINGER, on);
210 case APR_TCP_DEFER_ACCEPT:
211 #if defined(TCP_DEFER_ACCEPT)
212 if (apr_is_option_set(sock, APR_TCP_DEFER_ACCEPT) != on) {
213 int optlevel = IPPROTO_TCP;
214 int optname = TCP_DEFER_ACCEPT;
216 if (setsockopt(sock->socketdes, optlevel, optname,
217 (void *)&on, sizeof(int)) == -1) {
220 apr_set_option(sock, APR_TCP_DEFER_ACCEPT, on);
226 case APR_TCP_NODELAY:
227 #if defined(TCP_NODELAY)
228 if (apr_is_option_set(sock, APR_TCP_NODELAY) != on) {
229 int optlevel = IPPROTO_TCP;
230 int optname = TCP_NODELAY;
233 if (sock->protocol == IPPROTO_SCTP) {
234 optlevel = IPPROTO_SCTP;
235 optname = SCTP_NODELAY;
238 if (setsockopt(sock->socketdes, optlevel, optname, (void *)&on, sizeof(int)) == -1) {
241 apr_set_option(sock, APR_TCP_NODELAY, on);
244 /* BeOS pre-BONE has TCP_NODELAY set by default.
245 * As it can't be turned off we might as well check if they're asking
246 * for it to be turned on!
257 #if APR_TCP_NOPUSH_FLAG
258 /* TCP_NODELAY and TCP_CORK are mutually exclusive on Linux
259 * kernels < 2.6; on newer kernels they can be used together
260 * and TCP_CORK takes preference, which is the desired
261 * behaviour. On older kernels, TCP_NODELAY must be toggled
262 * to "off" whilst TCP_CORK is in effect. */
263 if (apr_is_option_set(sock, APR_TCP_NOPUSH) != on) {
264 #ifndef HAVE_TCP_NODELAY_WITH_CORK
265 int optlevel = IPPROTO_TCP;
266 int optname = TCP_NODELAY;
269 if (sock->protocol == IPPROTO_SCTP) {
270 optlevel = IPPROTO_SCTP;
271 optname = SCTP_NODELAY;
274 /* OK we're going to change some settings here... */
275 if (apr_is_option_set(sock, APR_TCP_NODELAY) == 1 && on) {
276 /* Now toggle TCP_NODELAY to off, if TCP_CORK is being
279 if (setsockopt(sock->socketdes, optlevel, optname,
280 (void*)&tmpflag, sizeof(int)) == -1) {
283 apr_set_option(sock, APR_RESET_NODELAY, 1);
284 apr_set_option(sock, APR_TCP_NODELAY, 0);
286 apr_set_option(sock, APR_RESET_NODELAY, 0);
288 #endif /* HAVE_TCP_NODELAY_WITH_CORK */
290 /* OK, now we can just set the TCP_NOPUSH flag accordingly...*/
291 if (setsockopt(sock->socketdes, IPPROTO_TCP, APR_TCP_NOPUSH_FLAG,
292 (void*)&on, sizeof(int)) == -1) {
295 apr_set_option(sock, APR_TCP_NOPUSH, on);
296 #ifndef HAVE_TCP_NODELAY_WITH_CORK
297 if (!on && apr_is_option_set(sock, APR_RESET_NODELAY)) {
298 /* Now, if TCP_CORK was just turned off, turn
299 * TCP_NODELAY back on again if it was earlier toggled
302 if (setsockopt(sock->socketdes, optlevel, optname,
303 (void*)&tmpflag, sizeof(int)) == -1) {
306 apr_set_option(sock, APR_RESET_NODELAY,0);
307 apr_set_option(sock, APR_TCP_NODELAY, 1);
309 #endif /* HAVE_TCP_NODELAY_WITH_CORK */
315 case APR_INCOMPLETE_READ:
316 apr_set_option(sock, APR_INCOMPLETE_READ, on);
318 case APR_IPV6_V6ONLY:
319 #if APR_HAVE_IPV6 && defined(IPV6_V6ONLY)
320 /* we don't know the initial setting of this option,
321 * so don't check sock->options since that optimization
324 if (setsockopt(sock->socketdes, IPPROTO_IPV6, IPV6_V6ONLY,
325 (void *)&on, sizeof(int)) == -1) {
328 apr_set_option(sock, APR_IPV6_V6ONLY, on);
333 case APR_SO_FREEBIND:
334 #if defined(IP_FREEBIND)
335 if (setsockopt(sock->socketdes, SOL_IP, IP_FREEBIND,
336 (void *)&one, sizeof(int)) == -1) {
339 apr_set_option(sock, APR_SO_FREEBIND, on);
340 #elif 0 /* defined(IP_BINDANY) ... */
341 /* TODO: insert FreeBSD support here, note family specific
342 * options, IP_BINDANY vs IPV6_BINDANY */
355 apr_status_t apr_socket_timeout_get(apr_socket_t *sock, apr_interval_time_t *t)
362 apr_status_t apr_socket_opt_get(apr_socket_t *sock,
363 apr_int32_t opt, apr_int32_t *on)
367 *on = apr_is_option_set(sock, opt);
373 apr_status_t apr_socket_atmark(apr_socket_t *sock, int *atmark)
378 if (ioctl(sock->socketdes, SIOCATMARK, (void*) &oobmark) < 0)
379 return apr_get_netos_error();
381 *atmark = (oobmark != 0);
389 apr_status_t apr_gethostname(char *buf, apr_int32_t len, apr_pool_t *cont)
392 if (gethostname(buf, len) == 0) {
394 if (gethostname(buf, len) != 0) {
399 else if (!memchr(buf, '\0', len)) { /* buffer too small */
400 /* note... most platforms just truncate in this condition
401 * linux+glibc return an error
404 return APR_ENAMETOOLONG;
409 #if APR_HAS_SO_ACCEPTFILTER
410 apr_status_t apr_socket_accept_filter(apr_socket_t *sock, char *nonconst_name,
413 /* these should have been const; act like they are */
414 const char *name = nonconst_name;
415 const char *args = nonconst_args;
417 struct accept_filter_arg af;
418 socklen_t optlen = sizeof(af);
420 /* FreeBSD returns an error if the filter is already set; ignore
421 * this call if we previously set it to the same value.
423 if ((getsockopt(sock->socketdes, SOL_SOCKET, SO_ACCEPTFILTER,
424 &af, &optlen)) == 0) {
425 if (!strcmp(name, af.af_name) && !strcmp(args, af.af_arg)) {
430 /* Uhh, at least in FreeBSD 9 the fields are declared as arrays of
431 * these lengths; did sizeof not work in some ancient release?
433 * FreeBSD kernel sets the last byte to a '\0'.
435 apr_cpystrn(af.af_name, name, 16);
436 apr_cpystrn(af.af_arg, args, 256 - 16);
438 if ((setsockopt(sock->socketdes, SOL_SOCKET, SO_ACCEPTFILTER,
439 &af, sizeof(af))) < 0) {
446 APR_PERMS_SET_IMPLEMENT(socket)
448 #if APR_HAVE_SOCKADDR_UN
449 apr_status_t rv = APR_SUCCESS;
450 apr_socket_t *socket = (apr_socket_t *)thesocket;
452 if (socket->local_addr->family == APR_UNIX) {
453 if (!(perms & APR_FPROT_GSETID))
455 if (fchown(socket->socketdes, uid, gid) < 0) {