1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/tools/flip_server/flip_config.h"
6
7namespace net {
8
9FlipAcceptor::FlipAcceptor(enum FlipHandlerType flip_handler_type,
10                           std::string listen_ip,
11                           std::string listen_port,
12                           std::string ssl_cert_filename,
13                           std::string ssl_key_filename,
14                           std::string http_server_ip,
15                           std::string http_server_port,
16                           std::string https_server_ip,
17                           std::string https_server_port,
18                           int spdy_only,
19                           int accept_backlog_size,
20                           bool disable_nagle,
21                           int accepts_per_wake,
22                           bool reuseport,
23                           bool wait_for_iface,
24                           void *memory_cache)
25    : flip_handler_type_(flip_handler_type),
26      listen_ip_(listen_ip),
27      listen_port_(listen_port),
28      ssl_cert_filename_(ssl_cert_filename),
29      ssl_key_filename_(ssl_key_filename),
30      http_server_ip_(http_server_ip),
31      http_server_port_(http_server_port),
32      https_server_ip_(https_server_ip),
33      https_server_port_(https_server_port),
34      spdy_only_(spdy_only),
35      accept_backlog_size_(accept_backlog_size),
36      disable_nagle_(disable_nagle),
37      accepts_per_wake_(accepts_per_wake),
38      memory_cache_(memory_cache),
39      ssl_session_expiry_(300),  // TODO(mbelshe):  Hook these up!
40      ssl_disable_compression_(false),
41      idle_socket_timeout_s_(300) {
42  VLOG(1) << "Attempting to listen on " << listen_ip_.c_str() << ":"
43          << listen_port_.c_str();
44  if (!https_server_ip_.size())
45    https_server_ip_ = http_server_ip_;
46  if (!https_server_port_.size())
47    https_server_port_ = http_server_port_;
48
49  while (1) {
50    int ret = CreateListeningSocket(listen_ip_,
51                                    listen_port_,
52                                    true,
53                                    accept_backlog_size_,
54                                    true,
55                                    reuseport,
56                                    wait_for_iface,
57                                    disable_nagle_,
58                                    &listen_fd_);
59    if ( ret == 0 ) {
60      break;
61    } else if ( ret == -3 && wait_for_iface ) {
62      // Binding error EADDRNOTAVAIL was encounted. We need
63      // to wait for the interfaces to raised. try again.
64      usleep(200000);
65    } else {
66      LOG(ERROR) << "Unable to create listening socket for: ret = " << ret
67                 << ": " << listen_ip_.c_str() << ":"
68                 << listen_port_.c_str();
69      return;
70    }
71  }
72
73  SetNonBlocking(listen_fd_);
74  VLOG(1) << "Listening on socket: ";
75  if (flip_handler_type == FLIP_HANDLER_PROXY)
76    VLOG(1) << "\tType         : Proxy";
77  else if (FLIP_HANDLER_SPDY_SERVER)
78    VLOG(1) << "\tType         : SPDY Server";
79  else if (FLIP_HANDLER_HTTP_SERVER)
80    VLOG(1) << "\tType         : HTTP Server";
81  VLOG(1) << "\tIP           : " << listen_ip_;
82  VLOG(1) << "\tPort         : " << listen_port_;
83  VLOG(1) << "\tHTTP Server  : " << http_server_ip_ << ":"
84          << http_server_port_;
85  VLOG(1) << "\tHTTPS Server : " << https_server_ip_ << ":"
86          << https_server_port_;
87  VLOG(1) << "\tSSL          : "
88          << (ssl_cert_filename.size()?"true":"false");
89  VLOG(1) << "\tCertificate  : " << ssl_cert_filename;
90  VLOG(1) << "\tKey          : " << ssl_key_filename;
91  VLOG(1) << "\tSpdy Only    : " << (spdy_only?"true":"flase");
92}
93
94FlipAcceptor::~FlipAcceptor() {}
95
96FlipConfig::FlipConfig()
97    : server_think_time_in_s_(0),
98      log_destination_(logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG),
99      wait_for_iface_(false) {
100}
101
102FlipConfig::~FlipConfig() {}
103
104void FlipConfig::AddAcceptor(enum FlipHandlerType flip_handler_type,
105                             std::string listen_ip,
106                             std::string listen_port,
107                             std::string ssl_cert_filename,
108                             std::string ssl_key_filename,
109                             std::string http_server_ip,
110                             std::string http_server_port,
111                             std::string https_server_ip,
112                             std::string https_server_port,
113                             int spdy_only,
114                             int accept_backlog_size,
115                             bool disable_nagle,
116                             int accepts_per_wake,
117                             bool reuseport,
118                             bool wait_for_iface,
119                             void *memory_cache) {
120  // TODO(mbelshe): create a struct FlipConfigArgs{} for the arguments.
121  acceptors_.push_back(new FlipAcceptor(flip_handler_type,
122                                        listen_ip,
123                                        listen_port,
124                                        ssl_cert_filename,
125                                        ssl_key_filename,
126                                        http_server_ip,
127                                        http_server_port,
128                                        https_server_ip,
129                                        https_server_port,
130                                        spdy_only,
131                                        accept_backlog_size,
132                                        disable_nagle,
133                                        accepts_per_wake,
134                                        reuseport,
135                                        wait_for_iface,
136                                        memory_cache));
137}
138
139}  // namespace
140