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