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