Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 347 lines (283 sloc) 8.663 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2010 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

#include "common-internal.h"
#include "bind.h"
#include "util.h"

ret_t
cherokee_bind_new (cherokee_bind_t **listener)
{
CHEROKEE_NEW_STRUCT(n,bind);

INIT_LIST_HEAD (&n->listed);

cherokee_buffer_init (&n->ip);
cherokee_socket_init (&n->socket);
n->port = 0;
n->id = 0;

cherokee_buffer_init (&n->server_string);
cherokee_buffer_init (&n->server_string_ext);
cherokee_buffer_init (&n->server_string_w_port);
cherokee_buffer_init (&n->server_address);
cherokee_buffer_init (&n->server_port);

n->accept_continuous = 0;
n->accept_continuous_max = 0;
n->accept_recalculate = 0;

*listener = n;
return ret_ok;
}


ret_t
cherokee_bind_free (cherokee_bind_t *listener)
{
cherokee_socket_close (&listener->socket);
cherokee_socket_mrproper (&listener->socket);

cherokee_buffer_mrproper (&listener->ip);

cherokee_buffer_mrproper (&listener->server_string);
cherokee_buffer_mrproper (&listener->server_string_ext);
cherokee_buffer_mrproper (&listener->server_string_w_port);
cherokee_buffer_mrproper (&listener->server_address);
cherokee_buffer_mrproper (&listener->server_port);

free (listener);
return ret_ok;
}


static ret_t
build_strings (cherokee_bind_t *listener,
cherokee_server_token_t token)
{
ret_t ret;
char server_ip[CHE_INET_ADDRSTRLEN+1];

/* server_string_
*/
cherokee_buffer_clean (&listener->server_string_w_port);
ret = cherokee_version_add_w_port (&listener->server_string_w_port,
token, listener->port);
if (ret != ret_ok)
return ret;

cherokee_buffer_clean (&listener->server_string_ext);
ret = cherokee_version_add (&listener->server_string_ext, token);
if (ret != ret_ok)
return ret;

cherokee_buffer_clean (&listener->server_string);
ret = cherokee_version_add_simple (&listener->server_string, token);
if (ret != ret_ok)
return ret;

/* server_address
*/
cherokee_socket_ntop (&listener->socket, server_ip, sizeof(server_ip)-1);
cherokee_buffer_add (&listener->server_address, server_ip, strlen(server_ip));

/* server_port
*/
cherokee_buffer_add_va (&listener->server_port, "%d", listener->port);
return ret_ok;
}


ret_t
cherokee_bind_configure (cherokee_bind_t *listener,
cherokee_config_node_t *conf)
{
ret_t ret;
cherokee_boolean_t tls;
cherokee_buffer_t *buf;

listener->id = atoi(conf->key.buf);

ret = cherokee_config_node_read (conf, "interface", &buf);
if (ret == ret_ok) {
cherokee_buffer_mrproper (&listener->ip);
cherokee_buffer_add_buffer (&listener->ip, buf);
}

ret = cherokee_config_node_read_int (conf, "port", &listener->port);
if (ret != ret_ok) {
LOG_CRITICAL_S (CHEROKEE_ERROR_BIND_PORT_NEEDED);
return ret_error;
}

ret = cherokee_config_node_read_bool (conf, "tls", &tls);
if (ret == ret_ok) {
listener->socket.is_tls = (tls) ? TLS : non_TLS;
}

return ret_ok;
}


ret_t
cherokee_bind_set_default (cherokee_bind_t *listener)
{
listener->port = 80;
return ret_ok;
}


static ret_t
set_socket_opts (int socket)
{
ret_t ret;
int re;
int on;
        struct linger ling = {0, 0};
#ifdef SO_ACCEPTFILTER
        struct accept_filter_arg afa;
#endif

/* Set 'close-on-exec'
*/
ret = cherokee_fd_set_closexec (socket);
if (ret != ret_ok)
return ret;

/* To re-bind without wait to TIME_WAIT. It prevents 2MSL
* delay on accept.
*/
on = 1;
re = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (re != 0) return ret_error;

/* TCP_MAXSEG:
* The maximum size of a TCP segment is based on the network MTU for des-
* tinations on local networks or on a default MTU of 576 bytes for desti-
* nations on nonlocal networks. The default behavior can be altered by
* setting the TCP_MAXSEG option to an integer value from 1 to 65,535.
* However, TCP will not use a maximum segment size smaller than 32 or
* larger than the local network MTU. Setting the TCP_MAXSEG option to a
* value of zero results in default behavior. The TCP_MAXSEG option can
* only be set prior to calling listen or connect on the socket. For pas-
* sive connections, the TCP_MAXSEG option value is inherited from the
* listening socket. This option takes an int value, with a range of 0 to
* 65535.
*/
#ifdef TCP_MAXSEG
on = 64000;
setsockopt (socket, SOL_SOCKET, TCP_MAXSEG, &on, sizeof(on));

/* Do no check the returned value */
#endif

/* SO_LINGER:
* Don't want to block on calls to close.
*
* kernels that map pages for IO end up failing if the pipe is full
* at exit and we take away the final buffer. this is really a kernel
* bug but it's harmless on systems that are not broken, so...
*
* http://www.apache.org/docs/misc/fin_wait_2.html
*/
setsockopt (socket, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));

/* TCP_DEFER_ACCEPT:
* Allows a listener to be awakened only when data arrives on the socket.
* Takes an integer value (seconds), this can bound the maximum number of
* attempts TCP will make to complete the connection. This option should
* not be used in code intended to be portable.
*
* Give clients 5s to send first data packet
*/
#ifdef TCP_DEFER_ACCEPT
on = 5;
setsockopt (socket, SOL_TCP, TCP_DEFER_ACCEPT, &on, sizeof(on));
#endif

/* SO_ACCEPTFILTER:
* FreeBSD accept filter for HTTP:
*
* http://www.freebsd.org/cgi/man.cgi?query=accf_http
*/
#ifdef SO_ACCEPTFILTER
memset (&afa, 0, sizeof(afa));
strcpy (afa.af_name, "httpready");

setsockopt (socket, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
#endif

return ret_ok;
}


static ret_t
init_socket (cherokee_bind_t *listener, int family)
{
ret_t ret;

/* Create the socket, and set its properties
*/
ret = cherokee_socket_set_client (&listener->socket, family);
if (ret != ret_ok)
return ret;

ret = set_socket_opts (SOCKET_FD(&listener->socket));
if (ret != ret_ok)
return ret;

/* Bind the socket
*/
ret = cherokee_socket_bind (&listener->socket, listener->port, &listener->ip);
if (ret != ret_ok)
return ret;

return ret_ok;
}


ret_t
cherokee_bind_init_port (cherokee_bind_t *listener,
cuint_t listen_queue,
cherokee_boolean_t ipv6,
cherokee_server_token_t token)
{
ret_t ret;

/* Init the port
*/
#ifdef HAVE_IPV6
if (ipv6) {
ret = init_socket (listener, AF_INET6);
} else
#endif
{
ret = ret_not_found;
}

if (ret != ret_ok) {
ret = init_socket (listener, AF_INET);

if (ret != ret_ok) {
LOG_CRITICAL (CHEROKEE_ERROR_BIND_COULDNT_BIND_PORT,
listener->port, getuid(), getgid());
return ret_error;
}
}

/* Set no-delay mode:
* If no clients are waiting, accept() will return -1 immediately
*/
ret = cherokee_fd_set_nodelay (listener->socket.socket, true);
if (ret != ret_ok)
return ret;

/* Listen
*/
ret = cherokee_socket_listen (&listener->socket, listen_queue);
if (ret != ret_ok) {
cherokee_socket_close (&listener->socket);
return ret_error;
}

/* Build the strings
*/
ret = build_strings (listener, token);
if (ret != ret_ok)
return ret;

return ret_ok;
}


ret_t
cherokee_bind_accept_more (cherokee_bind_t *listener,
ret_t prev_ret)
{
/* Failed to accept
*/
if (prev_ret != ret_ok) {
listener->accept_continuous = 0;

if (listener->accept_recalculate)
listener->accept_recalculate -= 1;
else
listener->accept_recalculate = 10;

return ret_deny;
}

/* Did accept a connection
*/
listener->accept_continuous++;

if (listener->accept_recalculate <= 0) {
listener->accept_continuous_max = listener->accept_continuous;
return ret_ok;
}

if (listener->accept_continuous < listener->accept_continuous_max) {
return ret_ok;
}

listener->accept_continuous = 0;
return ret_deny;
}
Something went wrong with that request. Please try again.