flip_config.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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#ifndef NET_TOOLS_FLIP_PROXY_CONFIG_H
6#define NET_TOOLS_FLIP_PROXY_CONFIG_H
7#pragma once
8
9#include <arpa/inet.h>  // in_addr_t
10
11#include "base/logging.h"
12#include "net/tools/flip_server/create_listener.h"
13
14#include <vector>
15#include <string>
16
17using std::string;
18using std::vector;
19
20enum FlipHandlerType {
21    FLIP_HANDLER_PROXY,
22    FLIP_HANDLER_SPDY_SERVER,
23    FLIP_HANDLER_HTTP_SERVER
24};
25
26class FlipAcceptor {
27public:
28   enum FlipHandlerType flip_handler_type_;
29   string listen_ip_;
30   string listen_port_;
31   string ssl_cert_filename_;
32   string ssl_key_filename_;
33   string http_server_ip_;
34   string http_server_port_;
35   string https_server_ip_;
36   string https_server_port_;
37   int spdy_only_;
38   int accept_backlog_size_;
39   bool disable_nagle_;
40   int accepts_per_wake_;
41   int listen_fd_;
42   void* memory_cache_;
43
44   FlipAcceptor(enum FlipHandlerType flip_handler_type,
45                string listen_ip,
46                string listen_port,
47                string ssl_cert_filename,
48                string ssl_key_filename,
49                string http_server_ip,
50                string http_server_port,
51                string https_server_ip,
52                string https_server_port,
53                int spdy_only,
54                int accept_backlog_size,
55                bool disable_nagle,
56                int accepts_per_wake,
57                bool reuseport,
58                bool wait_for_iface,
59                void *memory_cache) :
60       flip_handler_type_(flip_handler_type),
61       listen_ip_(listen_ip),
62       listen_port_(listen_port),
63       ssl_cert_filename_(ssl_cert_filename),
64       ssl_key_filename_(ssl_key_filename),
65       http_server_ip_(http_server_ip),
66       http_server_port_(http_server_port),
67       https_server_ip_(https_server_ip),
68       https_server_port_(https_server_port),
69       spdy_only_(spdy_only),
70       accept_backlog_size_(accept_backlog_size),
71       disable_nagle_(disable_nagle),
72       accepts_per_wake_(accepts_per_wake),
73       memory_cache_(memory_cache)
74   {
75     VLOG(1) << "Attempting to listen on " << listen_ip_.c_str() << ":"
76             << listen_port_.c_str();
77     if (!https_server_ip_.size())
78       https_server_ip_ = http_server_ip_;
79     if (!https_server_port_.size())
80       https_server_port_ = http_server_port_;
81
82     while (1) {
83       int ret = net::CreateListeningSocket(listen_ip_,
84                                            listen_port_,
85                                            true,
86                                            accept_backlog_size_,
87                                            true,
88                                            reuseport,
89                                            wait_for_iface,
90                                            disable_nagle_,
91                                            &listen_fd_);
92       if ( ret == 0 ) {
93         break;
94       } else if ( ret == -3 && wait_for_iface ) {
95         // Binding error EADDRNOTAVAIL was encounted. We need
96         // to wait for the interfaces to raised. try again.
97         usleep(200000);
98       } else {
99         LOG(ERROR) << "Unable to create listening socket for: ret = " << ret
100                    << ": " << listen_ip_.c_str() << ":"
101                    << listen_port_.c_str();
102         return;
103       }
104     }
105     net::SetNonBlocking(listen_fd_);
106     VLOG(1) << "Listening on socket: ";
107     if (flip_handler_type == FLIP_HANDLER_PROXY)
108       VLOG(1) << "\tType         : Proxy";
109     else if (FLIP_HANDLER_SPDY_SERVER)
110       VLOG(1) << "\tType         : SPDY Server";
111     else if (FLIP_HANDLER_HTTP_SERVER)
112       VLOG(1) << "\tType         : HTTP Server";
113     VLOG(1) << "\tIP           : " << listen_ip_;
114     VLOG(1) << "\tPort         : " << listen_port_;
115     VLOG(1) << "\tHTTP Server  : " << http_server_ip_ << ":"
116             << http_server_port_;
117     VLOG(1) << "\tHTTPS Server : " << https_server_ip_ << ":"
118             << https_server_port_;
119     VLOG(1) << "\tSSL          : "
120             << (ssl_cert_filename.size()?"true":"false");
121     VLOG(1) << "\tCertificate  : " << ssl_cert_filename;
122     VLOG(1) << "\tKey          : " << ssl_key_filename;
123     VLOG(1) << "\tSpdy Only    : " << (spdy_only?"true":"flase");
124   }
125   ~FlipAcceptor () {}
126};
127
128class FlipConfig {
129public:
130   std::vector <FlipAcceptor*> acceptors_;
131   double server_think_time_in_s_;
132   enum logging::LoggingDestination log_destination_;
133   string log_filename_;
134   bool forward_ip_header_enabled_;
135   string forward_ip_header_;
136   bool wait_for_iface_;
137   int ssl_session_expiry_;
138   bool ssl_disable_compression_;
139
140   FlipConfig() :
141       server_think_time_in_s_(0),
142       log_destination_(logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG),
143       forward_ip_header_enabled_(false),
144       wait_for_iface_(false),
145       ssl_session_expiry_(300),
146       ssl_disable_compression_(false)
147       {}
148
149   ~FlipConfig() {}
150
151   void AddAcceptor(enum FlipHandlerType flip_handler_type,
152                    string listen_ip,
153                    string listen_port,
154                    string ssl_cert_filename,
155                    string ssl_key_filename,
156                    string http_server_ip,
157                    string http_server_port,
158                    string https_server_ip,
159                    string https_server_port,
160                    int spdy_only,
161                    int accept_backlog_size,
162                    bool disable_nagle,
163                    int accepts_per_wake,
164                    bool reuseport,
165                    bool wait_for_iface,
166                    void *memory_cache) {
167       // TODO(mbelshe): create a struct FlipConfigArgs{} for the arguments.
168       acceptors_.push_back(new FlipAcceptor(flip_handler_type,
169                                             listen_ip,
170                                             listen_port,
171                                             ssl_cert_filename,
172                                             ssl_key_filename,
173                                             http_server_ip,
174                                             http_server_port,
175                                             https_server_ip,
176                                             https_server_port,
177                                             spdy_only,
178                                             accept_backlog_size,
179                                             disable_nagle,
180                                             accepts_per_wake,
181                                             reuseport,
182                                             wait_for_iface,
183                                             memory_cache));
184   }
185
186};
187
188#endif
189