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_network_io.h"
19#include "apr_general.h"
20#include "apr_lib.h"
21#include "apr_strings.h"
22#include <errno.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <netinet/tcp.h>
26#include <netinet/in.h>
27#include <unistd.h>
28#include <netdb.h>
29#include <sys/so_ioctl.h>
30
31
38
39
42{
43 int one;
44 struct linger li;
45
46 if (on)
47 one = 1;
48 else
49 one = 0;
50
51 if (opt & APR_SO_KEEPALIVE) {
52 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_KEEPALIVE, (void *)&one, sizeof(int)) == -1) {
53 return APR_OS2_STATUS(sock_errno());
54 }
55 }
56 if (opt & APR_SO_DEBUG) {
57 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_DEBUG, (void *)&one, sizeof(int)) == -1) {
58 return APR_OS2_STATUS(sock_errno());
59 }
60 }
61 if (opt & APR_SO_BROADCAST) {
62 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_BROADCAST, (void *)&one, sizeof(int)) == -1) {
64 }
65 }
66 if (opt & APR_SO_REUSEADDR) {
67 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(int)) == -1) {
68 return APR_OS2_STATUS(sock_errno());
69 }
70 }
71 if (opt & APR_SO_SNDBUF) {
72 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_SNDBUF, (void *)&on, sizeof(int)) == -1) {
73 return APR_OS2_STATUS(sock_errno());
74 }
75 }
76 if (opt & APR_SO_NONBLOCK) {
77 if (ioctl(sock->socketdes, FIONBIO, (caddr_t)&one, sizeof(one)) == -1) {
78 return APR_OS2_STATUS(sock_errno());
79 } else {
80 sock->nonblock = one;
81 }
82 }
83 if (opt & APR_SO_LINGER) {
84 li.l_onoff = on;
85 li.l_linger = APR_MAX_SECS_TO_LINGER;
86 if (setsockopt(sock->socketdes, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(struct linger)) == -1) {
87 return APR_OS2_STATUS(sock_errno());
88 }
89 }
90 if (opt & APR_TCP_NODELAY) {
91 if (setsockopt(sock->socketdes, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(int)) == -1) {
92 return APR_OS2_STATUS(sock_errno());
93 }
94 }
95 return APR_SUCCESS;
96}
97
98
101{
102 *t = sock->timeout;
103 return APR_SUCCESS;
104}
105
106
109{
110 switch(opt) {
111 default:
112 return APR_EINVAL;
113 }
114 return APR_SUCCESS;
115}
116
117
119{
120 int oobmark;
121
122 if (ioctl(sock->socketdes, SIOCATMARK, (void*)&oobmark, sizeof(oobmark)) < 0) {
123 return APR_OS2_STATUS(sock_errno());
124 }
125
126 *atmark = (oobmark != 0);
127
128 return APR_SUCCESS;
129}
130
131
134{
135 if (gethostname(buf, len) == -1) {
136 buf[0] = '\0';
137 return APR_OS2_STATUS(sock_errno());
138 }
139 else if (!memchr(buf, '\0', len)) { /* buffer too small */
140 buf[0] = '\0';
141 return APR_ENAMETOOLONG;
142 }
143 return APR_SUCCESS;
144}
const char apr_size_t len
Definition ap_regex.h:187
#define ioctl
#define setsockopt
#define sock_errno
APR Miscellaneous library routines.
APR general purpose 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_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_FROM_OS_ERROR(e)
Definition apr_errno.h:1214
#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_LINGER
#define APR_SO_BROADCAST
#define APR_SO_REUSEADDR
#define APR_SO_NONBLOCK
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
apr_interval_time_t timeout
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
apr_status_t apr_gethostname(char *buf, apr_int32_t len, apr_pool_t *cont)
Definition sockopt.c:389
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