X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=client.cpp;h=f6361f34ab47e62dc30ec1ebd6844ae73462d21a;hp=f2d60a62653ea13abad4ff9366e8a2e5fa55ca97;hb=74cd48ffef90d7d0752e37a4515e4ecfb68f7c9d;hpb=807a8ae8f6aca56e3e809aa19f4034a323f1b987 diff --git a/client.cpp b/client.cpp index f2d60a6..f6361f3 100644 --- a/client.cpp +++ b/client.cpp @@ -1,9 +1,10 @@ #include #include -#include +#include #include #include "client.h" +#include "log.h" #include "markpool.h" #include "state.pb.h" #include "stream.h" @@ -17,7 +18,10 @@ Client::Client(int sock) state(Client::READING_REQUEST), stream(NULL), header_or_error_bytes_sent(0), - stream_pos(0) + stream_pos(0), + bytes_sent(0), + bytes_lost(0), + num_loss_events(0) { request.reserve(1024); @@ -26,12 +30,23 @@ Client::Client(int sock) socklen_t addr_len = sizeof(addr); if (getpeername(sock, reinterpret_cast(&addr), &addr_len) == -1) { - perror("getpeername"); + log_perror("getpeername"); remote_addr = ""; + return; + } + + char buf[INET6_ADDRSTRLEN]; + if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) { + // IPv4 address, really. + if (inet_ntop(AF_INET, &addr.sin6_addr.s6_addr32[3], buf, sizeof(buf)) == NULL) { + log_perror("inet_ntop"); + remote_addr = ""; + } else { + remote_addr = buf; + } } else { - char buf[INET6_ADDRSTRLEN]; if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) { - perror("inet_ntop"); + log_perror("inet_ntop"); remote_addr = ""; } else { remote_addr = buf; @@ -45,11 +60,14 @@ Client::Client(const ClientProto &serialized, Stream *stream) connect_time(serialized.connect_time()), state(State(serialized.state())), request(serialized.request()), - stream_id(serialized.stream_id()), + url(serialized.url()), stream(stream), header_or_error(serialized.header_or_error()), header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()), - stream_pos(serialized.stream_pos()) + stream_pos(serialized.stream_pos()), + bytes_sent(serialized.bytes_sent()), + bytes_lost(serialized.bytes_lost()), + num_loss_events(serialized.num_loss_events()) { if (stream != NULL && stream->mark_pool != NULL) { fwmark = stream->mark_pool->get_mark(); @@ -58,8 +76,9 @@ Client::Client(const ClientProto &serialized, Stream *stream) } if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) { if (fwmark != 0) { - perror("setsockopt(SO_MARK)"); + log_perror("setsockopt(SO_MARK)"); } + fwmark = 0; } } @@ -71,19 +90,30 @@ ClientProto Client::serialize() const serialized.set_connect_time(connect_time); serialized.set_state(state); serialized.set_request(request); - serialized.set_stream_id(stream_id); + serialized.set_url(url); serialized.set_header_or_error(header_or_error); serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()); serialized.set_stream_pos(stream_pos); + serialized.set_bytes_sent(bytes_sent); + serialized.set_bytes_lost(bytes_lost); + serialized.set_num_loss_events(num_loss_events); return serialized; } ClientStats Client::get_stats() const { ClientStats stats; - stats.stream_id = stream_id; + if (url.empty()) { + stats.url = "-"; + } else { + stats.url = url; + } + stats.sock = sock; + stats.fwmark = fwmark; stats.remote_addr = remote_addr; stats.connect_time = connect_time; - stats.bytes_sent = stream_pos; + stats.bytes_sent = bytes_sent; + stats.bytes_lost = bytes_lost; + stats.num_loss_events = num_loss_events; return stats; }