Keep information about HLS downloads around for some time afterwards.
[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           x_playback_session_id(serialized.x_playback_session_id()),
58           state(State(serialized.state())),
59           request(serialized.request()),
60           url(serialized.url()),
61           stream(stream),
62           header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent()),
63           stream_pos(serialized.stream_pos()),
64           stream_pos_end(serialized.stream_pos_end()),
65           bytes_sent(serialized.bytes_sent()),
66           bytes_lost(serialized.bytes_lost()),
67           num_loss_events(serialized.num_loss_events())
68 {
69         if (stream != nullptr) {
70                 if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &stream->pacing_rate, sizeof(stream->pacing_rate)) == -1) {
71                         if (stream->pacing_rate != ~0U) {
72                                 log_perror("setsockopt(SO_MAX_PACING_RATE)");
73                         }
74                 }
75         }
76
77         if (serialized.has_header_or_short_response_old()) {
78                 // Pre-1.4.0.
79                 header_or_short_response_holder = serialized.header_or_short_response_old();
80                 header_or_short_response = &header_or_short_response_holder;
81         } else if (serialized.has_header_or_short_response_index()) {
82                 assert(size_t(serialized.header_or_short_response_index()) < short_responses.size());
83                 header_or_short_response_ref = short_responses[serialized.header_or_short_response_index()];
84                 header_or_short_response = header_or_short_response_ref.get();
85         }
86         connect_time.tv_sec = serialized.connect_time_sec();
87         connect_time.tv_nsec = serialized.connect_time_nsec();
88
89         in_ktls_mode = false;
90         if (serialized.has_tls_context()) {
91                 tls_context = tls_import_context(
92                         reinterpret_cast<const unsigned char *>(serialized.tls_context().data()),
93                         serialized.tls_context().size());
94                 if (tls_context == nullptr) {
95                         log(WARNING, "tls_import_context() failed, TLS client might not survive across restart");
96                 } else {
97                         tls_data_to_send = tls_get_write_buffer(tls_context, &tls_data_left_to_send);
98
99                         assert(serialized.tls_output_bytes_already_consumed() <= tls_data_left_to_send);
100                         if (serialized.tls_output_bytes_already_consumed() >= tls_data_left_to_send) {
101                                 tls_buffer_clear(tls_context);
102                                 tls_data_to_send = nullptr;
103                         } else {
104                                 tls_data_to_send += serialized.tls_output_bytes_already_consumed();
105                                 tls_data_left_to_send -= serialized.tls_output_bytes_already_consumed();
106                         }
107                         in_ktls_mode = serialized.in_ktls_mode();
108                 }
109         } else {
110                 tls_context = nullptr;
111         }
112 }
113
114 ClientProto Client::serialize(unordered_map<const string *, size_t> *short_response_pool) const
115 {
116         ClientProto serialized;
117         serialized.set_sock(sock);
118         serialized.set_remote_addr(remote_addr);
119         serialized.set_referer(referer);
120         serialized.set_user_agent(user_agent);
121         serialized.set_x_playback_session_id(x_playback_session_id);
122         serialized.set_connect_time_sec(connect_time.tv_sec);
123         serialized.set_connect_time_nsec(connect_time.tv_nsec);
124         serialized.set_state(state);
125         serialized.set_request(request);
126         serialized.set_url(url);
127
128         if (header_or_short_response != nullptr) {
129                 // See if this string is already in the pool (deduplicated by the pointer); if not, insert it.
130                 auto iterator_and_inserted = short_response_pool->emplace(
131                         header_or_short_response, short_response_pool->size());
132                 serialized.set_header_or_short_response_index(iterator_and_inserted.first->second);
133         }
134
135         serialized.set_header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent());
136         serialized.set_stream_pos(stream_pos);
137         serialized.set_stream_pos_end(stream_pos_end);
138         serialized.set_bytes_sent(bytes_sent);
139         serialized.set_bytes_lost(bytes_lost);
140         serialized.set_num_loss_events(num_loss_events);
141
142         if (tls_context != nullptr) {
143                 bool small_version = false;
144                 int required_size = tls_export_context(tls_context, nullptr, 0, small_version);
145                 if (required_size <= 0) {
146                         // Can happen if we're in the middle of the key exchange, unfortunately.
147                         // We'll get an error fairly fast, and this client hasn't started playing
148                         // anything yet, so just log the error and continue.
149                         //
150                         // In theory, we could still rescue it if we had sent _zero_ bytes,
151                         // by doing an entirely new TLS context, but it's an edge case
152                         // that's not really worth it.
153                         log(WARNING, "tls_export_context() failed (returned %d), TLS client might not survive across restart",
154                                 required_size);
155                 } else {
156                         string *serialized_context = serialized.mutable_tls_context();
157                         serialized_context->resize(required_size);
158
159                         int ret = tls_export_context(tls_context,
160                                 reinterpret_cast<unsigned char *>(&(*serialized_context)[0]),
161                                 serialized_context->size(),
162                                 small_version);
163                         assert(ret == required_size);
164
165                         // tls_export_context() has exported the contents of the write buffer, but it doesn't
166                         // know how much of that we've consumed, so we need to figure that out by ourselves.
167                         // In a sense, it's unlikely that this will ever be relevant, though, since TLSe can't
168                         // currently serialize in-progress key exchanges.
169                         unsigned base_tls_data_left_to_send;
170                         const unsigned char *base_tls_data_to_send = tls_get_write_buffer(tls_context, &base_tls_data_left_to_send);
171                         if (base_tls_data_to_send == nullptr) {
172                                 assert(tls_data_to_send == nullptr);
173                         } else {
174                                 assert(tls_data_to_send + tls_data_left_to_send == base_tls_data_to_send + base_tls_data_left_to_send);
175                         }
176                         serialized.set_tls_output_bytes_already_consumed(tls_data_to_send - base_tls_data_to_send);
177                         serialized.set_in_ktls_mode(in_ktls_mode);
178                 }
179         }
180
181         return serialized;
182 }
183
184 namespace {
185
186 string escape_string(const string &str) {
187         string ret;
188         for (size_t i = 0; i < str.size(); ++i) {
189                 char buf[16];
190                 if (isprint(str[i]) && str[i] >= 32 && str[i] != '"' && str[i] != '\\') {
191                         ret.push_back(str[i]);
192                 } else {
193                         snprintf(buf, sizeof(buf), "\\x%02x", (unsigned char)str[i]);
194                         ret += buf;
195                 }
196         }
197         return ret;
198 }
199
200 } // namespace
201         
202 ClientStats Client::get_stats() const
203 {
204         ClientStats stats;
205         if (url.empty()) {
206                 stats.url = "-";
207         } else {
208                 stats.url = url;
209         }
210         stats.sock = sock;
211         stats.remote_addr = remote_addr;
212         stats.referer = escape_string(referer);
213         stats.user_agent = escape_string(user_agent);
214         stats.connect_time = connect_time;
215         stats.bytes_sent = bytes_sent;
216         stats.bytes_lost = bytes_lost;
217         stats.num_loss_events = num_loss_events;
218         stats.hls_zombie_key = get_hls_zombie_key();
219         return stats;
220 }