4 #include <netinet/in.h>
6 #include <sys/socket.h>
14 #ifndef SO_MAX_PACING_RATE
15 #define SO_MAX_PACING_RATE 47
20 Client::Client(int sock)
23 request.reserve(1024);
25 // Find the remote address, and convert it to ASCII.
27 socklen_t addr_len = sizeof(addr);
29 if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
30 log_perror("getpeername");
35 char buf[INET6_ADDRSTRLEN];
36 if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) {
37 // IPv4 address, really.
38 if (inet_ntop(AF_INET, &addr.sin6_addr.s6_addr32[3], buf, sizeof(buf)) == nullptr) {
39 log_perror("inet_ntop");
45 if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == nullptr) {
46 log_perror("inet_ntop");
54 Client::Client(const ClientProto &serialized, const vector<shared_ptr<const string>> &short_responses, Stream *stream)
55 : sock(serialized.sock()),
56 remote_addr(serialized.remote_addr()),
57 referer(serialized.referer()),
58 user_agent(serialized.user_agent()),
59 x_playback_session_id(serialized.x_playback_session_id()),
60 state(State(serialized.state())),
61 request(serialized.request()),
62 url(serialized.url()),
64 close_after_response(serialized.close_after_response()),
65 http_11(serialized.http_11()),
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())
73 // Set back the close-on-exec flag for the socket.
74 // (This can't leak into a child, since we haven't been started yet.)
75 fcntl(sock, F_SETFD, FD_CLOEXEC);
77 if (stream != nullptr) {
78 if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &stream->pacing_rate, sizeof(stream->pacing_rate)) == -1) {
79 if (stream->pacing_rate != ~0U) {
80 log_perror("setsockopt(SO_MAX_PACING_RATE)");
85 if (serialized.has_header_or_short_response_old()) {
87 header_or_short_response_holder = serialized.header_or_short_response_old();
88 header_or_short_response = &header_or_short_response_holder;
89 } else if (serialized.has_header_or_short_response_index()) {
90 assert(size_t(serialized.header_or_short_response_index()) < short_responses.size());
91 header_or_short_response_ref = short_responses[serialized.header_or_short_response_index()];
92 header_or_short_response = header_or_short_response_ref.get();
94 connect_time.tv_sec = serialized.connect_time_sec();
95 connect_time.tv_nsec = serialized.connect_time_nsec();
98 if (serialized.has_tls_context()) {
99 tls_context = tls_import_context(
100 reinterpret_cast<const unsigned char *>(serialized.tls_context().data()),
101 serialized.tls_context().size());
102 if (tls_context == nullptr) {
103 log(WARNING, "tls_import_context() failed, TLS client might not survive across restart");
105 tls_data_to_send = tls_get_write_buffer(tls_context, &tls_data_left_to_send);
107 assert(serialized.tls_output_bytes_already_consumed() <= tls_data_left_to_send);
108 if (serialized.tls_output_bytes_already_consumed() >= tls_data_left_to_send) {
109 tls_buffer_clear(tls_context);
110 tls_data_to_send = nullptr;
112 tls_data_to_send += serialized.tls_output_bytes_already_consumed();
113 tls_data_left_to_send -= serialized.tls_output_bytes_already_consumed();
115 in_ktls_mode = serialized.in_ktls_mode();
118 tls_context = nullptr;
122 ClientProto Client::serialize(unordered_map<const string *, size_t> *short_response_pool) const
124 // Unset the close-on-exec flag for the socket.
125 // (This can't leak into a child, since there's only one thread left.)
126 fcntl(sock, F_SETFD, 0);
128 ClientProto serialized;
129 serialized.set_sock(sock);
130 serialized.set_remote_addr(remote_addr);
131 serialized.set_referer(referer);
132 serialized.set_user_agent(user_agent);
133 serialized.set_x_playback_session_id(x_playback_session_id);
134 serialized.set_connect_time_sec(connect_time.tv_sec);
135 serialized.set_connect_time_nsec(connect_time.tv_nsec);
136 serialized.set_state(state);
137 serialized.set_request(request);
138 serialized.set_url(url);
140 if (header_or_short_response != nullptr) {
141 // See if this string is already in the pool (deduplicated by the pointer); if not, insert it.
142 auto iterator_and_inserted = short_response_pool->emplace(
143 header_or_short_response, short_response_pool->size());
144 serialized.set_header_or_short_response_index(iterator_and_inserted.first->second);
147 serialized.set_header_or_short_response_bytes_sent(header_or_short_response_bytes_sent);
148 serialized.set_stream_pos(stream_pos);
149 serialized.set_stream_pos_end(stream_pos_end);
150 serialized.set_bytes_sent(bytes_sent);
151 serialized.set_bytes_lost(bytes_lost);
152 serialized.set_num_loss_events(num_loss_events);
153 serialized.set_http_11(http_11);
154 serialized.set_close_after_response(close_after_response);
156 if (tls_context != nullptr) {
157 bool small_version = false;
158 int required_size = tls_export_context(tls_context, nullptr, 0, small_version);
159 if (required_size <= 0) {
160 // Can happen if we're in the middle of the key exchange, unfortunately.
161 // We'll get an error fairly fast, and this client hasn't started playing
162 // anything yet, so just log the error and continue.
164 // In theory, we could still rescue it if we had sent _zero_ bytes,
165 // by doing an entirely new TLS context, but it's an edge case
166 // that's not really worth it.
167 log(WARNING, "tls_export_context() failed (returned %d), TLS client might not survive across restart",
170 string *serialized_context = serialized.mutable_tls_context();
171 serialized_context->resize(required_size);
173 int ret = tls_export_context(tls_context,
174 reinterpret_cast<unsigned char *>(&(*serialized_context)[0]),
175 serialized_context->size(),
177 assert(ret == required_size);
179 // tls_export_context() has exported the contents of the write buffer, but it doesn't
180 // know how much of that we've consumed, so we need to figure that out by ourselves.
181 // In a sense, it's unlikely that this will ever be relevant, though, since TLSe can't
182 // currently serialize in-progress key exchanges.
183 unsigned base_tls_data_left_to_send;
184 const unsigned char *base_tls_data_to_send = tls_get_write_buffer(tls_context, &base_tls_data_left_to_send);
185 if (base_tls_data_to_send == nullptr) {
186 assert(tls_data_to_send == nullptr);
188 assert(tls_data_to_send + tls_data_left_to_send == base_tls_data_to_send + base_tls_data_left_to_send);
190 serialized.set_tls_output_bytes_already_consumed(tls_data_to_send - base_tls_data_to_send);
191 serialized.set_in_ktls_mode(in_ktls_mode);
200 string escape_string(const string &str) {
202 for (size_t i = 0; i < str.size(); ++i) {
204 if (isprint(str[i]) && str[i] >= 32 && str[i] != '"' && str[i] != '\\') {
205 ret.push_back(str[i]);
207 snprintf(buf, sizeof(buf), "\\x%02x", (unsigned char)str[i]);
216 ClientStats Client::get_stats() const
225 stats.remote_addr = remote_addr;
226 stats.referer = escape_string(referer);
227 stats.user_agent = escape_string(user_agent);
228 stats.connect_time = connect_time;
229 stats.bytes_sent = bytes_sent;
230 stats.bytes_lost = bytes_lost;
231 stats.num_loss_events = num_loss_events;
232 stats.hls_zombie_key = get_hls_zombie_key();