2012-01-26 17:32:05 +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 <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <set>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <fstream>
|
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
#include "spdylay_ssl.h"
|
2012-01-26 17:32:05 +01:00
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
namespace spdylay {
|
|
|
|
|
|
|
|
bool ssl_debug = false;
|
|
|
|
|
|
|
|
Spdylay::Spdylay(int fd, SSL *ssl, const spdylay_session_callbacks *callbacks)
|
|
|
|
: fd_(fd), ssl_(ssl), want_write_(false)
|
|
|
|
{
|
|
|
|
spdylay_session_client_new(&session_, callbacks, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Spdylay::~Spdylay()
|
|
|
|
{
|
|
|
|
spdylay_session_del(session_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Spdylay::recv()
|
|
|
|
{
|
|
|
|
return spdylay_session_recv(session_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Spdylay::send()
|
|
|
|
{
|
|
|
|
return spdylay_session_send(session_);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Spdylay::send_data(const uint8_t *data, size_t len, int flags)
|
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
r = SSL_write(ssl_, data, len);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Spdylay::recv_data(uint8_t *data, size_t len, int flags)
|
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
want_write_ = false;
|
|
|
|
r = SSL_read(ssl_, data, len);
|
|
|
|
if(r < 0) {
|
|
|
|
if(SSL_get_error(ssl_, r) == SSL_ERROR_WANT_WRITE) {
|
|
|
|
want_write_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Spdylay::want_read()
|
|
|
|
{
|
|
|
|
return spdylay_session_want_read(session_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Spdylay::want_write()
|
|
|
|
{
|
|
|
|
return spdylay_session_want_write(session_) || want_write_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Spdylay::fd() const
|
|
|
|
{
|
|
|
|
return fd_;
|
|
|
|
}
|
|
|
|
|
2012-02-01 13:47:25 +01:00
|
|
|
int Spdylay::submit_request(const std::string& hostport,
|
2012-02-03 17:37:21 +01:00
|
|
|
const std::string& path, uint8_t pri,
|
|
|
|
void *stream_user_data)
|
2012-01-29 16:34:10 +01:00
|
|
|
{
|
|
|
|
const char *nv[] = {
|
2012-02-01 13:47:25 +01:00
|
|
|
"host", hostport.c_str(),
|
2012-01-29 16:34:10 +01:00
|
|
|
"method", "GET",
|
|
|
|
"scheme", "https",
|
|
|
|
"url", path.c_str(),
|
|
|
|
"user-agent", "spdylay/0.0.0",
|
|
|
|
"version", "HTTP/1.1",
|
|
|
|
NULL
|
|
|
|
};
|
2012-02-03 17:37:21 +01:00
|
|
|
return spdylay_submit_request(session_, pri, nv, NULL, stream_user_data);
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Spdylay::would_block(int r)
|
|
|
|
{
|
|
|
|
int e = SSL_get_error(ssl_, r);
|
|
|
|
return e == SSL_ERROR_WANT_WRITE || e == SSL_ERROR_WANT_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
int connect_to(const std::string& host, uint16_t port)
|
2012-01-26 17:32:05 +01:00
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
int fd = -1;
|
|
|
|
int r;
|
2012-01-29 16:34:10 +01:00
|
|
|
char service[10];
|
|
|
|
snprintf(service, sizeof(service), "%u", port);
|
2012-01-26 17:32:05 +01:00
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
struct addrinfo *res;
|
2012-01-29 16:34:10 +01:00
|
|
|
r = getaddrinfo(host.c_str(), service, &hints, &res);
|
2012-01-26 17:32:05 +01:00
|
|
|
if(r != 0) {
|
|
|
|
std::cerr << "getaddrinfo: " << gai_strerror(r) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for(struct addrinfo *rp = res; rp; rp = rp->ai_next) {
|
|
|
|
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
if(fd == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while((r = connect(fd, rp->ai_addr, rp->ai_addrlen)) == -1 &&
|
|
|
|
errno == EINTR);
|
|
|
|
if(r == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
freeaddrinfo(res);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2012-02-07 17:50:58 +01:00
|
|
|
int make_listen_socket(uint16_t port)
|
|
|
|
{
|
|
|
|
addrinfo hints;
|
|
|
|
int fd = -1;
|
|
|
|
int r;
|
|
|
|
char service[10];
|
|
|
|
snprintf(service, sizeof(service), "%u", port);
|
|
|
|
memset(&hints, 0, sizeof(addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
|
|
|
|
addrinfo *res, *rp;
|
|
|
|
r = getaddrinfo(0, service, &hints, &res);
|
|
|
|
if(r != 0) {
|
|
|
|
std::cerr << "getaddrinfo: " << gai_strerror(r) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for(rp = res; rp; rp = rp->ai_next) {
|
|
|
|
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
if(fd == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int val = 1;
|
|
|
|
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(bind(fd, rp->ai_addr, rp->ai_addrlen) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
freeaddrinfo(res);
|
|
|
|
if(rp == 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if(listen(fd, 16) == -1) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-26 17:32:05 +01:00
|
|
|
int make_non_block(int fd)
|
|
|
|
{
|
|
|
|
int flags, r;
|
|
|
|
while((flags = fcntl(fd, F_GETFL, 0)) == -1 && errno == EINTR);
|
|
|
|
if(flags == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while((r = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) == -1 && errno == EINTR);
|
|
|
|
if(r == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-07 17:50:58 +01:00
|
|
|
int set_tcp_nodelay(int fd)
|
|
|
|
{
|
|
|
|
int val = 1;
|
|
|
|
return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, (socklen_t)sizeof(val));
|
|
|
|
}
|
|
|
|
|
2012-01-26 17:32:05 +01:00
|
|
|
ssize_t send_callback(spdylay_session *session,
|
|
|
|
const uint8_t *data, size_t len, int flags,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2012-01-29 16:34:10 +01:00
|
|
|
Spdylay *sc = (Spdylay*)user_data;
|
2012-01-26 17:32:05 +01:00
|
|
|
ssize_t r = sc->send_data(data, len, flags);
|
|
|
|
if(r < 0) {
|
|
|
|
if(sc->would_block(r)) {
|
|
|
|
r = SPDYLAY_ERR_WOULDBLOCK;
|
|
|
|
} else {
|
|
|
|
r = SPDYLAY_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t recv_callback(spdylay_session *session,
|
|
|
|
uint8_t *data, size_t len, int flags, void *user_data)
|
|
|
|
{
|
2012-01-29 16:34:10 +01:00
|
|
|
Spdylay *sc = (Spdylay*)user_data;
|
2012-01-26 17:32:05 +01:00
|
|
|
ssize_t r = sc->recv_data(data, len, flags);
|
|
|
|
if(r < 0) {
|
|
|
|
if(sc->would_block(r)) {
|
|
|
|
r = SPDYLAY_ERR_WOULDBLOCK;
|
|
|
|
} else {
|
|
|
|
r = SPDYLAY_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
} else if(r == 0) {
|
|
|
|
r = SPDYLAY_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-01-30 16:46:46 +01:00
|
|
|
namespace {
|
|
|
|
const char *ctrl_names[] = {
|
2012-01-27 20:30:23 +01:00
|
|
|
"SYN_STREAM",
|
|
|
|
"SYN_REPLY",
|
|
|
|
"RST_STREAM",
|
|
|
|
"SETTINGS",
|
|
|
|
"NOOP",
|
|
|
|
"PING",
|
|
|
|
"GOAWAY",
|
|
|
|
"HEADERS"
|
|
|
|
};
|
2012-01-30 16:46:46 +01:00
|
|
|
} // namespace
|
2012-01-27 20:30:23 +01:00
|
|
|
|
2012-02-01 16:37:48 +01:00
|
|
|
namespace {
|
|
|
|
void print_frame_attr_indent()
|
|
|
|
{
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
void print_nv(char **nv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; nv[i]; i += 2) {
|
2012-02-01 16:37:48 +01:00
|
|
|
print_frame_attr_indent();
|
|
|
|
printf("%s: %s\n", nv[i], nv[i+1]);
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-30 16:46:46 +01:00
|
|
|
void print_timer()
|
|
|
|
{
|
2012-01-31 14:04:51 +01:00
|
|
|
timeval tv;
|
|
|
|
get_timer(&tv);
|
|
|
|
printf("[%3ld.%03ld]", tv.tv_sec, tv.tv_usec/1000);
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
|
|
|
|
2012-02-01 16:37:48 +01:00
|
|
|
namespace {
|
|
|
|
void print_ctrl_hd(const spdylay_ctrl_hd& hd)
|
|
|
|
{
|
|
|
|
printf("<version=%u, flags=%u, length=%d>\n",
|
|
|
|
hd.version, hd.flags, hd.length);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-02-07 17:50:58 +01:00
|
|
|
namespace {
|
2012-01-30 16:46:46 +01:00
|
|
|
void print_frame(spdylay_frame_type type, spdylay_frame *frame)
|
2012-01-27 19:54:53 +01:00
|
|
|
{
|
2012-01-30 16:46:46 +01:00
|
|
|
printf("%s frame ", ctrl_names[type-1]);
|
2012-02-01 16:37:48 +01:00
|
|
|
print_ctrl_hd(frame->syn_stream.hd);
|
2012-01-27 19:54:53 +01:00
|
|
|
switch(type) {
|
2012-01-30 16:46:46 +01:00
|
|
|
case SPDYLAY_SYN_STREAM:
|
2012-02-01 16:37:48 +01:00
|
|
|
print_frame_attr_indent();
|
|
|
|
printf("(stream_id=%d, assoc_stream_id=%d, pri=%u)\n",
|
2012-01-30 16:46:46 +01:00
|
|
|
frame->syn_stream.stream_id, frame->syn_stream.assoc_stream_id,
|
2012-02-01 16:37:48 +01:00
|
|
|
frame->syn_stream.pri);
|
2012-01-30 16:46:46 +01:00
|
|
|
print_nv(frame->syn_stream.nv);
|
|
|
|
break;
|
2012-01-27 19:54:53 +01:00
|
|
|
case SPDYLAY_SYN_REPLY:
|
2012-02-01 16:37:48 +01:00
|
|
|
print_frame_attr_indent();
|
|
|
|
printf("(stream_id=%d)\n", frame->syn_reply.stream_id);
|
2012-01-27 19:54:53 +01:00
|
|
|
print_nv(frame->syn_reply.nv);
|
|
|
|
break;
|
2012-01-31 17:16:35 +01:00
|
|
|
case SPDYLAY_RST_STREAM:
|
2012-02-01 16:37:48 +01:00
|
|
|
print_frame_attr_indent();
|
|
|
|
printf("(stream_id=%d, status_code=%u)\n",
|
|
|
|
frame->rst_stream.stream_id, frame->rst_stream.status_code);
|
2012-01-31 17:16:35 +01:00
|
|
|
break;
|
|
|
|
case SPDYLAY_SETTINGS:
|
2012-02-01 16:37:48 +01:00
|
|
|
print_frame_attr_indent();
|
|
|
|
printf("(niv=%lu)\n", static_cast<unsigned long>(frame->settings.niv));
|
2012-01-31 17:16:35 +01:00
|
|
|
for(size_t i = 0; i < frame->settings.niv; ++i) {
|
2012-02-01 16:37:48 +01:00
|
|
|
print_frame_attr_indent();
|
|
|
|
printf("[%d(%u):%u]\n",
|
2012-01-31 17:16:35 +01:00
|
|
|
frame->settings.iv[i].settings_id,
|
|
|
|
frame->settings.iv[i].flags, frame->settings.iv[i].value);
|
|
|
|
}
|
|
|
|
break;
|
2012-01-29 16:34:10 +01:00
|
|
|
case SPDYLAY_PING:
|
2012-02-01 16:37:48 +01:00
|
|
|
print_frame_attr_indent();
|
2012-01-29 16:34:10 +01:00
|
|
|
printf("(unique_id=%d)\n", frame->ping.unique_id);
|
|
|
|
break;
|
2012-01-30 16:46:46 +01:00
|
|
|
case SPDYLAY_GOAWAY:
|
2012-02-01 16:37:48 +01:00
|
|
|
print_frame_attr_indent();
|
2012-01-30 16:46:46 +01:00
|
|
|
printf("(last_good_stream_id=%d)\n", frame->goaway.last_good_stream_id);
|
|
|
|
break;
|
2012-01-31 17:16:35 +01:00
|
|
|
case SPDYLAY_HEADERS:
|
2012-02-01 16:37:48 +01:00
|
|
|
print_frame_attr_indent();
|
|
|
|
printf("(stream_id=%d)\n", frame->headers.stream_id);
|
2012-01-31 17:16:35 +01:00
|
|
|
print_nv(frame->headers.nv);
|
|
|
|
break;
|
2012-01-27 19:54:53 +01:00
|
|
|
default:
|
2012-01-29 16:34:10 +01:00
|
|
|
printf("\n");
|
2012-01-27 19:54:53 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
2012-02-07 17:50:58 +01:00
|
|
|
} // namespace
|
2012-01-30 16:46:46 +01:00
|
|
|
|
|
|
|
void on_ctrl_recv_callback
|
|
|
|
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
print_timer();
|
|
|
|
printf(" recv ");
|
|
|
|
print_frame(type, frame);
|
2012-01-28 11:44:42 +01:00
|
|
|
fflush(stdout);
|
2012-01-27 19:54:53 +01:00
|
|
|
}
|
|
|
|
|
2012-02-07 17:50:58 +01:00
|
|
|
void on_ctrl_send_callback
|
|
|
|
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
print_timer();
|
|
|
|
printf(" send ");
|
|
|
|
print_frame(type, frame);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void print_data_frame(uint8_t flags, int32_t stream_id, int32_t length)
|
|
|
|
{
|
|
|
|
printf("DATA frame (stream_id=%d, flags=%d, length=%d)\n",
|
|
|
|
stream_id, flags, length);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-01-27 19:54:53 +01:00
|
|
|
void on_data_recv_callback
|
2012-01-27 20:30:23 +01:00
|
|
|
(spdylay_session *session, uint8_t flags, int32_t stream_id, int32_t length,
|
2012-01-27 19:54:53 +01:00
|
|
|
void *user_data)
|
|
|
|
{
|
2012-01-30 16:46:46 +01:00
|
|
|
print_timer();
|
2012-02-07 17:50:58 +01:00
|
|
|
printf(" recv ");
|
|
|
|
print_data_frame(flags, stream_id, length);
|
2012-01-28 11:44:42 +01:00
|
|
|
fflush(stdout);
|
2012-01-27 19:54:53 +01:00
|
|
|
}
|
|
|
|
|
2012-02-07 17:50:58 +01:00
|
|
|
void on_data_send_callback
|
|
|
|
(spdylay_session *session, uint8_t flags, int32_t stream_id, int32_t length,
|
2012-01-27 20:30:23 +01:00
|
|
|
void *user_data)
|
|
|
|
{
|
2012-01-30 16:46:46 +01:00
|
|
|
print_timer();
|
|
|
|
printf(" send ");
|
2012-02-07 17:50:58 +01:00
|
|
|
print_data_frame(flags, stream_id, length);
|
2012-01-28 11:44:42 +01:00
|
|
|
fflush(stdout);
|
2012-01-27 20:30:23 +01:00
|
|
|
}
|
|
|
|
|
2012-01-31 14:04:51 +01:00
|
|
|
void ctl_poll(pollfd *pollfd, Spdylay *sc)
|
2012-01-26 17:32:05 +01:00
|
|
|
{
|
2012-01-31 14:04:51 +01:00
|
|
|
pollfd->events = 0;
|
2012-01-29 16:34:10 +01:00
|
|
|
if(sc->want_read()) {
|
2012-01-31 14:04:51 +01:00
|
|
|
pollfd->events |= POLLIN;
|
2012-01-26 17:32:05 +01:00
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
if(sc->want_write()) {
|
2012-01-31 14:04:51 +01:00
|
|
|
pollfd->events |= POLLOUT;
|
2012-01-26 17:32:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int select_next_proto_cb(SSL* ssl,
|
|
|
|
unsigned char **out, unsigned char *outlen,
|
|
|
|
const unsigned char *in, unsigned int inlen,
|
|
|
|
void *arg)
|
|
|
|
{
|
2012-01-29 16:34:10 +01:00
|
|
|
if(ssl_debug) {
|
2012-01-30 16:46:46 +01:00
|
|
|
print_timer();
|
|
|
|
std::cout << " NPN select next protocol: the remote server offers:"
|
|
|
|
<< std::endl;
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
2012-01-26 17:32:05 +01:00
|
|
|
for(unsigned int i = 0; i < inlen; i += in[i]+1) {
|
2012-01-29 16:34:10 +01:00
|
|
|
if(ssl_debug) {
|
2012-02-01 16:37:48 +01:00
|
|
|
std::cout << " * ";
|
2012-01-29 16:34:10 +01:00
|
|
|
std::cout.write(reinterpret_cast<const char*>(&in[i+1]), in[i]);
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2012-02-03 15:53:43 +01:00
|
|
|
}
|
|
|
|
if(spdylay_select_next_protocol(out, outlen, in, inlen) == -1) {
|
2012-02-05 13:48:20 +01:00
|
|
|
std::cerr << "Server did not advertise spdy/2 protocol." << std::endl;
|
2012-02-03 01:31:11 +01:00
|
|
|
abort();
|
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
if(ssl_debug) {
|
2012-02-03 15:53:43 +01:00
|
|
|
std::cout << " NPN selected the protocol: "
|
2012-02-03 01:31:11 +01:00
|
|
|
<< std::string((const char*)*out, (size_t)*outlen) << std::endl;
|
2012-01-26 17:32:05 +01:00
|
|
|
}
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
void setup_ssl_ctx(SSL_CTX *ssl_ctx)
|
2012-01-26 17:32:05 +01:00
|
|
|
{
|
|
|
|
/* Disable SSLv2 and enable all workarounds for buggy servers */
|
|
|
|
SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL|SSL_OP_NO_SSLv2);
|
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
|
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
|
|
|
|
SSL_CTX_set_next_proto_select_cb(ssl_ctx, select_next_proto_cb, 0);
|
2012-01-29 16:34:10 +01:00
|
|
|
}
|
2012-01-26 17:32:05 +01:00
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
int ssl_handshake(SSL *ssl, int fd)
|
|
|
|
{
|
2012-01-26 17:32:05 +01:00
|
|
|
if(SSL_set_fd(ssl, fd) == 0) {
|
|
|
|
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
int r = SSL_connect(ssl);
|
2012-01-26 17:32:05 +01:00
|
|
|
if(r <= 0) {
|
|
|
|
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-29 16:34:10 +01:00
|
|
|
|
2012-01-30 16:46:46 +01:00
|
|
|
namespace {
|
2012-01-31 14:04:51 +01:00
|
|
|
timeval base_tv;
|
2012-01-30 16:46:46 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void reset_timer()
|
|
|
|
{
|
2012-01-31 14:04:51 +01:00
|
|
|
gettimeofday(&base_tv, 0);
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
|
|
|
|
2012-01-31 14:04:51 +01:00
|
|
|
void get_timer(timeval* tv)
|
2012-01-30 16:46:46 +01:00
|
|
|
{
|
2012-01-31 14:04:51 +01:00
|
|
|
gettimeofday(tv, 0);
|
|
|
|
tv->tv_usec -= base_tv.tv_usec;
|
|
|
|
tv->tv_sec -= base_tv.tv_sec;
|
|
|
|
if(tv->tv_usec < 0) {
|
|
|
|
tv->tv_usec += 1000000;
|
|
|
|
--tv->tv_sec;
|
2012-01-30 16:46:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 16:34:10 +01:00
|
|
|
} // namespace spdylay
|