3 #include <netinet/in.h>
5 #include <sys/socket.h>
12 #ifndef SO_MAX_PACING_RATE
13 #define SO_MAX_PACING_RATE 47
18 Client::Client(int sock)
21 request.reserve(1024);
23 // Find the remote address, and convert it to ASCII.
25 socklen_t addr_len = sizeof(addr);
27 if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
28 log_perror("getpeername");
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");
43 if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == nullptr) {
44 log_perror("inet_ntop");
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()),
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())
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)");
79 if (serialized.has_header_or_short_response_old()) {
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();
88 connect_time.tv_sec = serialized.connect_time_sec();
89 connect_time.tv_nsec = serialized.connect_time_nsec();
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");
99 tls_data_to_send = tls_get_write_buffer(tls_context, &tls_data_left_to_send);
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;
106 tls_data_to_send += serialized.tls_output_bytes_already_consumed();
107 tls_data_left_to_send -= serialized.tls_output_bytes_already_consumed();
109 in_ktls_mode = serialized.in_ktls_mode();
112 tls_context = nullptr;
116 ClientProto Client::serialize(unordered_map<const string *, size_t> *short_response_pool) const
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);
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);
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);
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.
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",
160 string *serialized_context = serialized.mutable_tls_context();
161 serialized_context->resize(required_size);
163 int ret = tls_export_context(tls_context,
164 reinterpret_cast<unsigned char *>(&(*serialized_context)[0]),
165 serialized_context->size(),
167 assert(ret == required_size);
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);
178 assert(tls_data_to_send + tls_data_left_to_send == base_tls_data_to_send + base_tls_data_left_to_send);
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);
190 string escape_string(const string &str) {
192 for (size_t i = 0; i < str.size(); ++i) {
194 if (isprint(str[i]) && str[i] >= 32 && str[i] != '"' && str[i] != '\\') {
195 ret.push_back(str[i]);
197 snprintf(buf, sizeof(buf), "\\x%02x", (unsigned char)str[i]);
206 ClientStats Client::get_stats() const
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();