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