2 * Copyright (c) 2015, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
10 #include <folly/wangle/acceptor/TransportInfo.h>
12 #include <sys/socket.h>
13 #include <sys/types.h>
14 #include <folly/io/async/AsyncSocket.h>
16 using std::chrono::microseconds;
22 bool TransportInfo::initWithSocket(const AsyncSocket* sock) {
23 #if defined(__linux__) || defined(__FreeBSD__)
24 if (!TransportInfo::readTcpInfo(&tcpinfo, sock)) {
28 rtt = microseconds(tcpinfo.tcpi_rtt);
31 tcpinfoErrno = EINVAL;
32 rtt = microseconds(-1);
37 int64_t TransportInfo::readRTT(const AsyncSocket* sock) {
38 #if defined(__linux__) || defined(__FreeBSD__)
39 struct tcp_info tcpinfo;
40 if (!TransportInfo::readTcpInfo(&tcpinfo, sock)) {
43 return tcpinfo.tcpi_rtt;
49 #if defined(__linux__) || defined(__FreeBSD__)
50 bool TransportInfo::readTcpInfo(struct tcp_info* tcpinfo,
51 const AsyncSocket* sock) {
52 socklen_t len = sizeof(struct tcp_info);
56 if (getsockopt(sock->getFd(), IPPROTO_TCP,
57 TCP_INFO, (void*) tcpinfo, &len) < 0) {
58 VLOG(4) << "Error calling getsockopt(): " << strerror(errno);