Fix timeout behavior with persistent connections.
[cubemap] / client.cpp
1 #include <stdio.h>
2 #include <arpa/inet.h>
3 #include <netinet/in.h>
4 #include <stdint.h>
5 #include <sys/socket.h>
6
7 #include "client.h"
8 #include "log.h"
9 #include "state.pb.h"
10 #include "stream.h"
11
12 #ifndef SO_MAX_PACING_RATE
13 #define SO_MAX_PACING_RATE 47
14 #endif
15
16 using namespace std;
17
18 Client::Client(int sock)
19         : sock(sock)
20 {
21         request.reserve(1024);
22
23         // Find the remote address, and convert it to ASCII.
24         sockaddr_in6 addr;
25         socklen_t addr_len = sizeof(addr);
26
27         if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
28                 log_perror("getpeername");
29                 remote_addr = "";
30                 return;
31         }
32
33         char buf[INET6_ADDRSTRLEN];
34         if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) {
35                 // IPv4 address, really.
36                 if (inet_ntop(AF_INET, &addr.sin6_addr.s6_addr32[3], buf, sizeof(buf)) == nullptr) {
37                         log_perror("inet_ntop");
38                         remote_addr = "";
39                 } else {
40                         remote_addr = buf;
41                 }
42         } else {
43                 if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == nullptr) {
44                         log_perror("inet_ntop");
45                         remote_addr = "";
46                 } else {
47                         remote_addr = buf;
48                 }
49         }
50 }
51         
52 Client::Client(const ClientProto &serialized, const vector<shared_ptr<const string>> &short_responses, Stream *stream)
53         : sock(serialized.sock()),
54           remote_addr(serialized.remote_addr()),
55           referer(serialized.referer()),
56           user_agent(serialized.user_agent()),
57           state(State(serialized.state())),
58           request(serialized.request()),
59           url(serialized.url()),
60           stream(stream),
61           header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent()),
62           stream_pos(serialized.stream_pos()),
63           stream_pos_end(serialized.stream_pos_end()),
64           bytes_sent(serialized.bytes_sent()),
65           bytes_lost(serialized.bytes_lost()),
66           num_loss_events(serialized.num_loss_events())
67 {
68         if (stream != nullptr) {
69                 if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &stream->pacing_rate, sizeof(stream->pacing_rate)) == -1) {
70                         if (stream->pacing_rate != ~0U) {
71                                 log_perror("setsockopt(SO_MAX_PACING_RATE)");
72                         }
73                 }
74         }
75
76         if (serialized.has_header_or_short_response_old()) {
77                 // Pre-1.4.0.
78                 header_or_short_response_holder = serialized.header_or_short_response_old();
79                 header_or_short_response = &header_or_short_response_holder;
80         } else if (serialized.has_header_or_short_response_index()) {
81                 assert(size_t(serialized.header_or_short_response_index()) < short_responses.size());
82                 header_or_short_response_ref = short_responses[serialized.header_or_short_response_index()];
83                 header_or_short_response = header_or_short_response_ref.get();
84         }
85         connect_time.tv_sec = serialized.connect_time_sec();
86         connect_time.tv_nsec = serialized.connect_time_nsec();
87
88         in_ktls_mode = false;
89         if (serialized.has_tls_context()) {
90                 tls_context = tls_import_context(
91                         reinterpret_cast<const unsigned char *>(serialized.tls_context().data()),
92                         serialized.tls_context().size());
93                 if (tls_context == nullptr) {
94                         log(WARNING, "tls_import_context() failed, TLS client might not survive across restart");
95                 } else {
96                         tls_data_to_send = tls_get_write_buffer(tls_context, &tls_data_left_to_send);
97
98                         assert(serialized.tls_output_bytes_already_consumed() <= tls_data_left_to_send);
99                         if (serialized.tls_output_bytes_already_consumed() >= tls_data_left_to_send) {
100                                 tls_buffer_clear(tls_context);
101                                 tls_data_to_send = nullptr;
102                         } else {
103                                 tls_data_to_send += serialized.tls_output_bytes_already_consumed();
104                                 tls_data_left_to_send -= serialized.tls_output_bytes_already_consumed();
105                         }
106                         in_ktls_mode = serialized.in_ktls_mode();
107                 }
108         } else {
109                 tls_context = nullptr;
110         }
111 }
112
113 ClientProto Client::serialize(unordered_map<const string *, size_t> *short_response_pool) const
114 {
115         ClientProto serialized;
116         serialized.set_sock(sock);
117         serialized.set_remote_addr(remote_addr);
118         serialized.set_referer(referer);
119         serialized.set_user_agent(user_agent);
120         serialized.set_connect_time_sec(connect_time.tv_sec);
121         serialized.set_connect_time_nsec(connect_time.tv_nsec);
122         serialized.set_state(state);
123         serialized.set_request(request);
124         serialized.set_url(url);
125
126         if (header_or_short_response != nullptr) {
127                 // See if this string is already in the pool (deduplicated by the pointer); if not, insert it.
128                 auto iterator_and_inserted = short_response_pool->emplace(
129                         header_or_short_response, short_response_pool->size());
130                 serialized.set_header_or_short_response_index(iterator_and_inserted.first->second);
131         }
132
133         serialized.set_header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent());
134         serialized.set_stream_pos(stream_pos);
135         serialized.set_stream_pos_end(stream_pos_end);
136         serialized.set_bytes_sent(bytes_sent);
137         serialized.set_bytes_lost(bytes_lost);
138         serialized.set_num_loss_events(num_loss_events);
139
140         if (tls_context != nullptr) {
141                 bool small_version = false;
142                 int required_size = tls_export_context(tls_context, nullptr, 0, small_version);
143                 if (required_size <= 0) {
144                         // Can happen if we're in the middle of the key exchange, unfortunately.
145                         // We'll get an error fairly fast, and this client hasn't started playing
146                         // anything yet, so just log the error and continue.
147                         //
148                         // In theory, we could still rescue it if we had sent _zero_ bytes,
149                         // by doing an entirely new TLS context, but it's an edge case
150                         // that's not really worth it.
151                         log(WARNING, "tls_export_context() failed (returned %d), TLS client might not survive across restart",
152                                 required_size);
153                 } else {
154                         string *serialized_context = serialized.mutable_tls_context();
155                         serialized_context->resize(required_size);
156
157                         int ret = tls_export_context(tls_context,
158                                 reinterpret_cast<unsigned char *>(&(*serialized_context)[0]),
159                                 serialized_context->size(),
160                                 small_version);
161                         assert(ret == required_size);
162
163                         // tls_export_context() has exported the contents of the write buffer, but it doesn't
164                         // know how much of that we've consumed, so we need to figure that out by ourselves.
165                         // In a sense, it's unlikely that this will ever be relevant, though, since TLSe can't
166                         // currently serialize in-progress key exchanges.
167                         unsigned base_tls_data_left_to_send;
168                         const unsigned char *base_tls_data_to_send = tls_get_write_buffer(tls_context, &base_tls_data_left_to_send);
169                         if (base_tls_data_to_send == nullptr) {
170                                 assert(tls_data_to_send == nullptr);
171                         } else {
172                                 assert(tls_data_to_send + tls_data_left_to_send == base_tls_data_to_send + base_tls_data_left_to_send);
173                         }
174                         serialized.set_tls_output_bytes_already_consumed(tls_data_to_send - base_tls_data_to_send);
175                         serialized.set_in_ktls_mode(in_ktls_mode);
176                 }
177         }
178
179         return serialized;
180 }
181
182 namespace {
183
184 string escape_string(const string &str) {
185         string ret;
186         for (size_t i = 0; i < str.size(); ++i) {
187                 char buf[16];
188                 if (isprint(str[i]) && str[i] >= 32 && str[i] != '"' && str[i] != '\\') {
189                         ret.push_back(str[i]);
190                 } else {
191                         snprintf(buf, sizeof(buf), "\\x%02x", (unsigned char)str[i]);
192                         ret += buf;
193                 }
194         }
195         return ret;
196 }
197
198 } // namespace
199         
200 ClientStats Client::get_stats() const
201 {
202         ClientStats stats;
203         if (url.empty()) {
204                 stats.url = "-";
205         } else {
206                 stats.url = url;
207         }
208         stats.sock = sock;
209         stats.remote_addr = remote_addr;
210         stats.referer = escape_string(referer);
211         stats.user_agent = escape_string(user_agent);
212         stats.connect_time = connect_time;
213         stats.bytes_sent = bytes_sent;
214         stats.bytes_lost = bytes_lost;
215         stats.num_loss_events = num_loss_events;
216         return stats;
217 }