2012-02-07 17:50:58 +01:00
|
|
|
/*
|
|
|
|
* Spdylay - SPDY Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2012-02-25 17:31:45 +01:00
|
|
|
#include <cassert>
|
2012-02-07 17:50:58 +01:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <spdylay/spdylay.h>
|
|
|
|
|
|
|
|
#include "spdylay_ssl.h"
|
2012-02-14 13:49:22 +01:00
|
|
|
#include "SpdyServer.h"
|
2012-02-07 17:50:58 +01:00
|
|
|
|
|
|
|
namespace spdylay {
|
|
|
|
|
|
|
|
extern bool ssl_debug;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void print_usage(std::ostream& out)
|
|
|
|
{
|
2012-11-25 13:58:44 +01:00
|
|
|
out << "Usage: spdyd [-23DVhv] [-d <PATH>] [--no-tls] <PORT> [<PRIVATE_KEY> <CERT>]"
|
2012-05-12 14:13:56 +02:00
|
|
|
<< std::endl;
|
2012-02-07 17:50:58 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void print_help(std::ostream& out)
|
|
|
|
{
|
|
|
|
print_usage(out);
|
|
|
|
out << "\n"
|
|
|
|
<< "OPTIONS:\n"
|
2012-02-07 18:57:19 +01:00
|
|
|
<< " -D, --daemon Run in a background. If -D is used, the\n"
|
|
|
|
<< " current working directory is changed to '/'.\n"
|
|
|
|
<< " Therefore if this option is used, -d option\n"
|
|
|
|
<< " must be specified.\n"
|
2012-04-22 16:04:55 +02:00
|
|
|
<< " -V, --verify-client\n"
|
|
|
|
<< " The server sends a client certificate\n"
|
|
|
|
<< " request. If the client did not return a\n"
|
|
|
|
<< " certificate, the handshake is terminated.\n"
|
|
|
|
<< " Currently, this option just requests a\n"
|
|
|
|
<< " client certificate and does not verify it.\n"
|
2012-05-12 14:13:56 +02:00
|
|
|
<< " -d, --htdocs=<PATH>\n"
|
|
|
|
<< " Specify document root. If this option is\n"
|
|
|
|
<< " not specified, the document root is the\n"
|
|
|
|
<< " current working directory.\n"
|
2012-02-07 17:50:58 +01:00
|
|
|
<< " -v, --verbose Print debug information such as reception/\n"
|
|
|
|
<< " transmission of frames and name/value pairs.\n"
|
2012-11-25 13:58:44 +01:00
|
|
|
<< " -2, --spdy2 Only use SPDY/2.\n"
|
2012-02-25 17:31:45 +01:00
|
|
|
<< " -3, --spdy3 Only use SPDY/3.\n"
|
2012-11-25 13:58:44 +01:00
|
|
|
<< " --no-tls Disable SSL/TLS. Use -2 or -3 to specify\n"
|
|
|
|
<< " SPDY protocol version to use.\n"
|
2012-02-07 18:57:19 +01:00
|
|
|
<< " -h, --help Print this help.\n"
|
2012-02-07 17:50:58 +01:00
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-02-14 13:49:22 +01:00
|
|
|
Config config;
|
2012-02-07 17:50:58 +01:00
|
|
|
while(1) {
|
2012-11-25 13:58:44 +01:00
|
|
|
int flag;
|
2012-02-07 17:50:58 +01:00
|
|
|
static option long_options[] = {
|
2012-02-07 18:57:19 +01:00
|
|
|
{"daemon", no_argument, 0, 'D' },
|
2012-02-07 17:50:58 +01:00
|
|
|
{"htdocs", required_argument, 0, 'd' },
|
|
|
|
{"help", no_argument, 0, 'h' },
|
2012-02-07 18:57:19 +01:00
|
|
|
{"verbose", no_argument, 0, 'v' },
|
2012-11-25 13:58:44 +01:00
|
|
|
{"spdy2", no_argument, 0, '2' },
|
2012-02-25 17:31:45 +01:00
|
|
|
{"spdy3", no_argument, 0, '3' },
|
2012-04-22 16:04:55 +02:00
|
|
|
{"verify-client", no_argument, 0, 'V' },
|
2012-11-25 13:58:44 +01:00
|
|
|
{"no-tls", no_argument, &flag, 1 },
|
2012-02-07 17:50:58 +01:00
|
|
|
{0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
int option_index = 0;
|
2012-11-25 13:58:44 +01:00
|
|
|
int c = getopt_long(argc, argv, "DVd:hv23", long_options, &option_index);
|
2012-02-07 17:50:58 +01:00
|
|
|
if(c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch(c) {
|
2012-02-07 18:57:19 +01:00
|
|
|
case 'D':
|
|
|
|
config.daemon = true;
|
|
|
|
break;
|
2012-04-22 16:04:55 +02:00
|
|
|
case 'V':
|
|
|
|
config.verify_client = true;
|
|
|
|
break;
|
2012-02-07 17:50:58 +01:00
|
|
|
case 'd':
|
|
|
|
config.htdocs = optarg;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
print_help(std::cout);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'v':
|
|
|
|
config.verbose = true;
|
|
|
|
break;
|
2012-11-25 13:58:44 +01:00
|
|
|
case '2':
|
|
|
|
config.version = SPDYLAY_PROTO_SPDY2;
|
|
|
|
break;
|
2012-02-25 17:31:45 +01:00
|
|
|
case '3':
|
2012-11-25 13:58:44 +01:00
|
|
|
config.version = SPDYLAY_PROTO_SPDY3;
|
2012-02-25 17:31:45 +01:00
|
|
|
break;
|
2012-02-07 17:50:58 +01:00
|
|
|
case '?':
|
|
|
|
exit(EXIT_FAILURE);
|
2012-11-25 13:58:44 +01:00
|
|
|
case 0:
|
|
|
|
switch(flag) {
|
|
|
|
case 1:
|
|
|
|
// no-tls option
|
|
|
|
config.no_tls = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2012-02-07 17:50:58 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-25 13:58:44 +01:00
|
|
|
if(argc-optind < (config.no_tls ? 1 : 3)) {
|
2012-02-07 17:50:58 +01:00
|
|
|
print_usage(std::cerr);
|
|
|
|
std::cerr << "Too few arguments" << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-11-25 13:58:44 +01:00
|
|
|
|
|
|
|
config.port = strtol(argv[optind++], 0, 10);
|
|
|
|
|
|
|
|
if(config.no_tls) {
|
|
|
|
if(config.version == 0) {
|
|
|
|
std::cerr << "Specify SPDY protocol version using either -2 or -3."
|
|
|
|
<< std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config.private_key_file = argv[optind++];
|
|
|
|
config.cert_file = argv[optind++];
|
|
|
|
}
|
|
|
|
|
2012-02-07 18:57:19 +01:00
|
|
|
if(config.daemon) {
|
|
|
|
if(config.htdocs.empty()) {
|
|
|
|
print_usage(std::cerr);
|
|
|
|
std::cerr << "-d option must be specified when -D is used." << std::endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if(daemon(0, 0) == -1) {
|
|
|
|
perror("daemon");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(config.htdocs.empty()) {
|
|
|
|
config.htdocs = "./";
|
|
|
|
}
|
2012-02-07 17:50:58 +01:00
|
|
|
struct sigaction act;
|
|
|
|
memset(&act, 0, sizeof(struct sigaction));
|
|
|
|
act.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGPIPE, &act, 0);
|
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
|
|
|
reset_timer();
|
2012-02-14 13:49:22 +01:00
|
|
|
config.on_request_recv_callback = htdocs_on_request_recv_callback;
|
2012-02-07 17:50:58 +01:00
|
|
|
ssl_debug = config.verbose;
|
2012-02-14 13:49:22 +01:00
|
|
|
|
|
|
|
SpdyServer server(&config);
|
|
|
|
if(server.listen() == 0) {
|
|
|
|
server.run();
|
|
|
|
}
|
2012-02-07 17:50:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace spdylay
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return spdylay::main(argc, argv);
|
|
|
|
}
|