We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / tools / quic / quic_packet_reader.cc
blob1982269f30bc3b47c49750d9f745a67a38507ecc
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/tools/quic/quic_packet_reader.h"
7 #include <errno.h>
8 #ifndef __APPLE__
9 // This is a GNU header that is not present in /usr/include on MacOS
10 #include <features.h>
11 #endif
12 #include <string.h>
13 #include <sys/epoll.h>
15 #include "base/logging.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/tools/quic/quic_dispatcher.h"
18 #include "net/tools/quic/quic_socket_utils.h"
20 #define MMSG_MORE 0
22 #ifndef SO_RXQ_OVFL
23 #define SO_RXQ_OVFL 40
24 #endif
26 namespace net {
28 namespace tools {
30 QuicPacketReader::QuicPacketReader() {
31 Initialize();
34 void QuicPacketReader::Initialize() {
35 // Zero initialize uninitialized memory.
36 memset(cbuf_, 0, arraysize(cbuf_));
37 memset(buf_, 0, arraysize(buf_));
38 memset(raw_address_, 0, sizeof(raw_address_));
39 memset(mmsg_hdr_, 0, sizeof(mmsg_hdr_));
41 for (int i = 0; i < kNumPacketsPerReadMmsgCall; ++i) {
42 iov_[i].iov_base = buf_ + (2 * kMaxPacketSize * i);
43 iov_[i].iov_len = 2 * kMaxPacketSize;
45 msghdr* hdr = &mmsg_hdr_[i].msg_hdr;
46 hdr->msg_name = &raw_address_[i];
47 hdr->msg_namelen = sizeof(sockaddr_storage);
48 hdr->msg_iov = &iov_[i];
49 hdr->msg_iovlen = 1;
51 hdr->msg_control = cbuf_ + kSpaceForOverflowAndIp * i;
52 hdr->msg_controllen = kSpaceForOverflowAndIp;
56 QuicPacketReader::~QuicPacketReader() {
59 bool QuicPacketReader::ReadAndDispatchPackets(
60 int fd,
61 int port,
62 ProcessPacketInterface* processor,
63 QuicPacketCount* packets_dropped) {
64 #if MMSG_MORE
65 // Re-set the length fields in case recvmmsg has changed them.
66 for (int i = 0; i < kNumPacketsPerReadMmsgCall; ++i) {
67 iov_[i].iov_len = 2 * kMaxPacketSize;
68 mmsg_hdr_[i].msg_len = 0;
69 msghdr* hdr = &mmsg_hdr_[i].msg_hdr;
70 hdr->msg_namelen = sizeof(sockaddr_storage);
71 hdr->msg_iovlen = 1;
72 hdr->msg_controllen = kSpaceForOverflowAndIp;
75 int packets_read =
76 recvmmsg(fd, mmsg_hdr_, kNumPacketsPerReadMmsgCall, 0, nullptr);
78 if (packets_read <= 0) {
79 return false; // recvmmsg failed.
82 for (int i = 0; i < packets_read; ++i) {
83 if (mmsg_hdr_[i].msg_len == 0) {
84 continue;
87 IPEndPoint client_address = IPEndPoint(raw_address_[i]);
88 IPAddressNumber server_ip =
89 QuicSocketUtils::GetAddressFromMsghdr(&mmsg_hdr_[i].msg_hdr);
90 if (!IsInitializedAddress(server_ip)) {
91 LOG(DFATAL) << "Unable to get server address.";
92 continue;
95 QuicEncryptedPacket packet(reinterpret_cast<char*>(iov_[i].iov_base),
96 mmsg_hdr_[i].msg_len, false);
97 IPEndPoint server_address(server_ip, port);
98 processor->ProcessPacket(server_address, client_address, packet);
101 if (packets_dropped != nullptr) {
102 QuicSocketUtils::GetOverflowFromMsghdr(&mmsg_hdr_[0].msg_hdr,
103 packets_dropped);
106 return true;
107 #else
108 LOG(FATAL) << "Unsupported";
109 return false;
110 #endif
113 /* static */
114 bool QuicPacketReader::ReadAndDispatchSinglePacket(
115 int fd,
116 int port,
117 ProcessPacketInterface* processor,
118 QuicPacketCount* packets_dropped) {
119 // Allocate some extra space so we can send an error if the packet is larger
120 // than kMaxPacketSize.
121 char buf[2 * kMaxPacketSize];
123 IPEndPoint client_address;
124 IPAddressNumber server_ip;
125 int bytes_read = QuicSocketUtils::ReadPacket(
126 fd, buf, arraysize(buf), packets_dropped, &server_ip, &client_address);
128 if (bytes_read < 0) {
129 return false; // ReadPacket failed.
132 QuicEncryptedPacket packet(buf, bytes_read, false);
133 IPEndPoint server_address(server_ip, port);
134 processor->ProcessPacket(server_address, client_address, packet);
136 // The socket read was successful, so return true even if packet dispatch
137 // failed.
138 return true;
141 } // namespace tools
143 } // namespace net