2 #include <netinet/in.h>
4 #include <sys/socket.h>
14 Client::Client(int sock)
17 connect_time(time(NULL)),
18 state(Client::READING_REQUEST),
20 header_or_error_bytes_sent(0),
26 request.reserve(1024);
28 // Find the remote address, and convert it to ASCII.
30 socklen_t addr_len = sizeof(addr);
32 if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
33 log_perror("getpeername");
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)) == NULL) {
42 log_perror("inet_ntop");
48 if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) {
49 log_perror("inet_ntop");
57 Client::Client(const ClientProto &serialized, Stream *stream)
58 : sock(serialized.sock()),
59 remote_addr(serialized.remote_addr()),
60 connect_time(serialized.connect_time()),
61 state(State(serialized.state())),
62 request(serialized.request()),
63 url(serialized.url()),
65 header_or_error(serialized.header_or_error()),
66 header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()),
67 stream_pos(serialized.stream_pos()),
68 bytes_sent(serialized.bytes_sent()),
69 bytes_lost(serialized.bytes_lost()),
70 num_loss_events(serialized.num_loss_events())
72 if (stream != NULL && stream->mark_pool != NULL) {
73 fwmark = stream->mark_pool->get_mark();
75 fwmark = 0; // No mark.
77 if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {
79 log_perror("setsockopt(SO_MARK)");
85 ClientProto Client::serialize() const
87 ClientProto serialized;
88 serialized.set_sock(sock);
89 serialized.set_remote_addr(remote_addr);
90 serialized.set_connect_time(connect_time);
91 serialized.set_state(state);
92 serialized.set_request(request);
93 serialized.set_url(url);
94 serialized.set_header_or_error(header_or_error);
95 serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent());
96 serialized.set_stream_pos(stream_pos);
97 serialized.set_bytes_sent(bytes_sent);
98 serialized.set_bytes_lost(bytes_lost);
99 serialized.set_num_loss_events(num_loss_events);
103 ClientStats Client::get_stats() const
112 stats.fwmark = fwmark;
113 stats.remote_addr = remote_addr;
114 stats.connect_time = connect_time;
115 stats.bytes_sent = bytes_sent;
116 stats.bytes_lost = bytes_lost;
117 stats.num_loss_events = num_loss_events;