Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: net/tools/quic/quic_packet_reader.cc

Issue 1008323002: Land Recent QUIC Changes until 03/15/2015. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@Final_0315
Patch Set: Add quic_packet_reader to BUILD.gn to fix compiler errors Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/tools/quic/quic_packet_reader.h ('k') | net/tools/quic/quic_server.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "net/tools/quic/quic_packet_reader.h"
6
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>
14
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"
19
20 #define MMSG_MORE 0
21
22 #ifndef SO_RXQ_OVFL
23 #define SO_RXQ_OVFL 40
24 #endif
25
26 namespace net {
27
28 namespace tools {
29
30 QuicPacketReader::QuicPacketReader() {
31 Initialize();
32 }
33
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_));
40
41 for (int i = 0; i < kNumPacketsPerReadMmsgCall; ++i) {
42 iov_[i].iov_base = buf_ + (2 * kMaxPacketSize * i);
43 iov_[i].iov_len = 2 * kMaxPacketSize;
44
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;
50
51 hdr->msg_control = cbuf_ + kSpaceForOverflowAndIp * i;
52 hdr->msg_controllen = kSpaceForOverflowAndIp;
53 }
54 }
55
56 QuicPacketReader::~QuicPacketReader() {
57 }
58
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;
73 }
74
75 int packets_read =
76 recvmmsg(fd, mmsg_hdr_, kNumPacketsPerReadMmsgCall, 0, nullptr);
77
78 if (packets_read <= 0) {
79 return false; // recvmmsg failed.
80 }
81
82 for (int i = 0; i < packets_read; ++i) {
83 if (mmsg_hdr_[i].msg_len == 0) {
84 continue;
85 }
86
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;
93 }
94
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);
99 }
100
101 if (packets_dropped != nullptr) {
102 QuicSocketUtils::GetOverflowFromMsghdr(&mmsg_hdr_[0].msg_hdr,
103 packets_dropped);
104 }
105
106 return true;
107 #else
108 LOG(FATAL) << "Unsupported";
109 return false;
110 #endif
111 }
112
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];
122
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);
127
128 if (bytes_read < 0) {
129 return false; // ReadPacket failed.
130 }
131
132 QuicEncryptedPacket packet(buf, bytes_read, false);
133 IPEndPoint server_address(server_ip, port);
134 processor->ProcessPacket(server_address, client_address, packet);
135
136 // The socket read was successful, so return true even if packet dispatch
137 // failed.
138 return true;
139 }
140
141 } // namespace tools
142
143 } // namespace net
OLDNEW
« no previous file with comments | « net/tools/quic/quic_packet_reader.h ('k') | net/tools/quic/quic_server.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698