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 9 namespace net { 10 11 FlipAcceptor::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() << ":" 70 << listen_port_.c_str(); 71 return; 72 } 73 } 74 75 SetNonBlocking(listen_fd_); 76 VLOG(1) << "Listening on socket: "; 77 if (flip_handler_type == FLIP_HANDLER_PROXY) 78 VLOG(1) << "\tType : Proxy"; 79 else if (FLIP_HANDLER_SPDY_SERVER) 80 VLOG(1) << "\tType : SPDY Server"; 81 else if (FLIP_HANDLER_HTTP_SERVER) 82 VLOG(1) << "\tType : HTTP Server"; 83 VLOG(1) << "\tIP : " << listen_ip_; 84 VLOG(1) << "\tPort : " << listen_port_; 85 VLOG(1) << "\tHTTP Server : " << http_server_ip_ << ":" 86 << http_server_port_; 87 VLOG(1) << "\tHTTPS Server : " << https_server_ip_ << ":" 88 << https_server_port_; 89 VLOG(1) << "\tSSL : " 90 << (ssl_cert_filename.size()?"true":"false"); 91 VLOG(1) << "\tCertificate : " << ssl_cert_filename; 92 VLOG(1) << "\tKey : " << ssl_key_filename; 93 VLOG(1) << "\tSpdy Only : " << (spdy_only?"true":"flase"); 94 } 95 96 FlipAcceptor::~FlipAcceptor() {} 97 98 FlipConfig::FlipConfig() 99 : server_think_time_in_s_(0), 100 log_destination_(logging::LOG_TO_SYSTEM_DEBUG_LOG), 101 wait_for_iface_(false) { 102 } 103 104 FlipConfig::~FlipConfig() {} 105 106 void FlipConfig::AddAcceptor(enum FlipHandlerType flip_handler_type, 107 std::string listen_ip, 108 std::string listen_port, 109 std::string ssl_cert_filename, 110 std::string ssl_key_filename, 111 std::string http_server_ip, 112 std::string http_server_port, 113 std::string https_server_ip, 114 std::string https_server_port, 115 int spdy_only, 116 int accept_backlog_size, 117 bool disable_nagle, 118 int accepts_per_wake, 119 bool reuseport, 120 bool wait_for_iface, 121 void *memory_cache) { 122 // TODO(mbelshe): create a struct FlipConfigArgs{} for the arguments. 123 acceptors_.push_back(new FlipAcceptor(flip_handler_type, 124 listen_ip, 125 listen_port, 126 ssl_cert_filename, 127 ssl_key_filename, 128 http_server_ip, 129 http_server_port, 130 https_server_ip, 131 https_server_port, 132 spdy_only, 133 accept_backlog_size, 134 disable_nagle, 135 accepts_per_wake, 136 reuseport, 137 wait_for_iface, 138 memory_cache)); 139 } 140 141 } // namespace 142