Log all finished accesses to an access log.
[cubemap] / client.cpp
1 #include <arpa/inet.h>
2 #include <netinet/in.h>
3 #include <stdio.h>
4 #include <sys/socket.h>
5
6 #include "client.h"
7 #include "log.h"
8 #include "markpool.h"
9 #include "state.pb.h"
10 #include "stream.h"
11
12 using namespace std;
13
14 Client::Client(int sock)
15         : sock(sock),
16           fwmark(0),
17           connect_time(time(NULL)),
18           state(Client::READING_REQUEST),
19           stream(NULL),
20           header_or_error_bytes_sent(0),
21           stream_pos(0),
22           bytes_sent(0),
23           bytes_lost(0),
24           num_loss_events(0)
25 {
26         request.reserve(1024);
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         } else {
36                 char buf[INET6_ADDRSTRLEN];
37                 if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) {
38                         log_perror("inet_ntop");
39                         remote_addr = "";
40                 } else {
41                         remote_addr = buf;
42                 }
43         }
44 }
45         
46 Client::Client(const ClientProto &serialized, Stream *stream)
47         : sock(serialized.sock()),
48           remote_addr(serialized.remote_addr()),
49           connect_time(serialized.connect_time()),
50           state(State(serialized.state())),
51           request(serialized.request()),
52           stream_id(serialized.stream_id()),
53           stream(stream),
54           header_or_error(serialized.header_or_error()),
55           header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()),
56           stream_pos(serialized.stream_pos()),
57           bytes_sent(serialized.bytes_sent()),
58           bytes_lost(serialized.bytes_lost()),
59           num_loss_events(serialized.num_loss_events())
60 {
61         if (stream != NULL && stream->mark_pool != NULL) {
62                 fwmark = stream->mark_pool->get_mark();
63         } else {
64                 fwmark = 0;  // No mark.
65         }
66         if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {
67                 if (fwmark != 0) {
68                         log_perror("setsockopt(SO_MARK)");
69                 }
70                 fwmark = 0;
71         }
72 }
73
74 ClientProto Client::serialize() const
75 {
76         ClientProto serialized;
77         serialized.set_sock(sock);
78         serialized.set_remote_addr(remote_addr);
79         serialized.set_connect_time(connect_time);
80         serialized.set_state(state);
81         serialized.set_request(request);
82         serialized.set_stream_id(stream_id);
83         serialized.set_header_or_error(header_or_error);
84         serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent());
85         serialized.set_stream_pos(stream_pos);
86         serialized.set_bytes_sent(bytes_sent);
87         serialized.set_bytes_lost(bytes_lost);
88         serialized.set_num_loss_events(num_loss_events);
89         return serialized;
90 }
91         
92 ClientStats Client::get_stats() const
93 {
94         ClientStats stats;
95         if (stream_id.empty()) {
96                 stats.stream_id = "-";
97         } else {
98                 stats.stream_id = stream_id;
99         }
100         stats.sock = sock;
101         stats.fwmark = fwmark;
102         stats.remote_addr = remote_addr;
103         stats.connect_time = connect_time;
104         stats.bytes_sent = bytes_sent;
105         stats.bytes_lost = bytes_lost;
106         stats.num_loss_events = num_loss_events;
107         return stats;
108 }