Commit 845b7cc7 authored by Bin Wu's avatar Bin Wu Committed by Commit Bot

Replace QuicSocketUtils::kSpaceForCmsg by a correctly calculated quic::kCmsgSpaceForReadPacket.

Merge internal change: 197832805

R=rch@chromium.org

Change-Id: I2f583a74507647737b330921b25488dc0053e957
Reviewed-on: https://chromium-review.googlesource.com/1070804Reviewed-by: default avatarRyan Hamilton <rch@chromium.org>
Commit-Queue: Bin Wu <wub@chromium.org>
Cr-Commit-Position: refs/heads/master@{#562564}
parent ed7f2506
...@@ -48,7 +48,7 @@ void QuicPacketReader::Initialize() { ...@@ -48,7 +48,7 @@ void QuicPacketReader::Initialize() {
hdr->msg_iovlen = 1; hdr->msg_iovlen = 1;
hdr->msg_control = packets_[i].cbuf; hdr->msg_control = packets_[i].cbuf;
hdr->msg_controllen = QuicSocketUtils::kSpaceForCmsg; hdr->msg_controllen = kCmsgSpaceForReadPacket;
} }
#endif #endif
} }
...@@ -83,7 +83,8 @@ bool QuicPacketReader::ReadAndDispatchManyPackets( ...@@ -83,7 +83,8 @@ bool QuicPacketReader::ReadAndDispatchManyPackets(
msghdr* hdr = &mmsg_hdr_[i].msg_hdr; msghdr* hdr = &mmsg_hdr_[i].msg_hdr;
hdr->msg_namelen = sizeof(sockaddr_storage); hdr->msg_namelen = sizeof(sockaddr_storage);
DCHECK_EQ(1, hdr->msg_iovlen); DCHECK_EQ(1, hdr->msg_iovlen);
hdr->msg_controllen = QuicSocketUtils::kSpaceForCmsg; hdr->msg_controllen = kCmsgSpaceForReadPacket;
hdr->msg_flags = 0;
} }
int packets_read = int packets_read =
...@@ -99,10 +100,10 @@ bool QuicPacketReader::ReadAndDispatchManyPackets( ...@@ -99,10 +100,10 @@ bool QuicPacketReader::ReadAndDispatchManyPackets(
continue; continue;
} }
if (mmsg_hdr_[i].msg_hdr.msg_controllen >= QuicSocketUtils::kSpaceForCmsg) { if (mmsg_hdr_[i].msg_hdr.msg_flags & MSG_CTRUNC) {
QUIC_BUG << "Incorrectly set control length: " QUIC_BUG << "Incorrectly set control length: "
<< mmsg_hdr_[i].msg_hdr.msg_controllen << ", expected " << mmsg_hdr_[i].msg_hdr.msg_controllen << ", expected "
<< QuicSocketUtils::kSpaceForCmsg; << kCmsgSpaceForReadPacket;
continue; continue;
} }
......
...@@ -79,7 +79,7 @@ class QuicPacketReader { ...@@ -79,7 +79,7 @@ class QuicPacketReader {
// call on the packets. // call on the packets.
struct sockaddr_storage raw_address; struct sockaddr_storage raw_address;
// cbuf is used for ancillary data from the kernel on recvmmsg. // cbuf is used for ancillary data from the kernel on recvmmsg.
char cbuf[QuicSocketUtils::kSpaceForCmsg]; char cbuf[kCmsgSpaceForReadPacket];
// buf is used for the data read from the kernel on recvmmsg. // buf is used for the data read from the kernel on recvmmsg.
char buf[kMaxPacketSize]; char buf[kMaxPacketSize];
}; };
......
...@@ -139,8 +139,7 @@ int QuicSocketUtils::ReadPacket(int fd, ...@@ -139,8 +139,7 @@ int QuicSocketUtils::ReadPacket(int fd,
QuicWallTime* walltimestamp, QuicWallTime* walltimestamp,
QuicSocketAddress* peer_address) { QuicSocketAddress* peer_address) {
DCHECK(peer_address != nullptr); DCHECK(peer_address != nullptr);
char cbuf[kSpaceForCmsg]; char cbuf[kCmsgSpaceForReadPacket];
memset(cbuf, 0, arraysize(cbuf));
iovec iov = {buffer, buf_len}; iovec iov = {buffer, buf_len};
struct sockaddr_storage raw_address; struct sockaddr_storage raw_address;
...@@ -168,7 +167,7 @@ int QuicSocketUtils::ReadPacket(int fd, ...@@ -168,7 +167,7 @@ int QuicSocketUtils::ReadPacket(int fd,
return -1; return -1;
} }
if (hdr.msg_controllen >= arraysize(cbuf)) { if (hdr.msg_flags & MSG_CTRUNC) {
QUIC_BUG << "Incorrectly set control length: " << hdr.msg_controllen QUIC_BUG << "Incorrectly set control length: " << hdr.msg_controllen
<< ", expected " << arraysize(cbuf); << ", expected " << arraysize(cbuf);
return -1; return -1;
......
...@@ -36,18 +36,28 @@ struct LinuxTimestamping { ...@@ -36,18 +36,28 @@ struct LinuxTimestamping {
struct timespec hwtimeraw; struct timespec hwtimeraw;
}; };
const int kCmsgSpaceForIpv4 = CMSG_SPACE(sizeof(in_pktinfo));
const int kCmsgSpaceForIpv6 = CMSG_SPACE(sizeof(in6_pktinfo));
// kCmsgSpaceForIp should be big enough to hold both IPv4 and IPv6 packet info.
const int kCmsgSpaceForIp = (kCmsgSpaceForIpv4 < kCmsgSpaceForIpv6)
? kCmsgSpaceForIpv6
: kCmsgSpaceForIpv4;
const int kCmsgSpaceForSegmentSize = CMSG_SPACE(sizeof(uint16_t));
const int kCmsgSpaceForRecvQueueOverflow = CMSG_SPACE(sizeof(int));
const int kCmsgSpaceForLinuxTimestamping =
CMSG_SPACE(sizeof(LinuxTimestamping));
const int kCmsgSpaceForTTL = CMSG_SPACE(sizeof(int));
// The minimum cmsg buffer size when receiving a packet. It is possible for a
// received packet to contain both IPv4 and IPv6 addresses.
const int kCmsgSpaceForReadPacket =
kCmsgSpaceForRecvQueueOverflow + kCmsgSpaceForIpv4 + kCmsgSpaceForIpv6 +
kCmsgSpaceForLinuxTimestamping + kCmsgSpaceForTTL;
class QuicSocketUtils { class QuicSocketUtils {
public: public:
// The first integer is for overflow. The in6_pktinfo is the larger of the
// address structures present. LinuxTimestamping is present for socket
// timestamping. The subsequent int is for ttl.
// The final int is a sentinel so the msg_controllen feedback
// can be used to detect larger control messages than there is space for.
static const int kSpaceForCmsg =
CMSG_SPACE(CMSG_LEN(sizeof(int)) + CMSG_LEN(sizeof(in6_pktinfo)) +
CMSG_LEN(sizeof(LinuxTimestamping)) + CMSG_LEN(sizeof(int)) +
CMSG_LEN(sizeof(int)));
// Fills in |address| if |hdr| contains IP_PKTINFO or IPV6_PKTINFO. Fills in // Fills in |address| if |hdr| contains IP_PKTINFO or IPV6_PKTINFO. Fills in
// |timestamp| if |hdr| contains |SO_TIMESTAMPING|. |address| and |timestamp| // |timestamp| if |hdr| contains |SO_TIMESTAMPING|. |address| and |timestamp|
// must not be null. // must not be null.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment