OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/tools/quic/quic_packet_reader.h" | 5 #include "net/tools/quic/quic_packet_reader.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #ifndef __APPLE__ | 8 #ifndef __APPLE__ |
9 // This is a GNU header that is not present in /usr/include on MacOS | 9 // This is a GNU header that is not present in /usr/include on MacOS |
10 #include <features.h> | 10 #include <features.h> |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 hdr->msg_controllen = QuicSocketUtils::kSpaceForCmsg; | 91 hdr->msg_controllen = QuicSocketUtils::kSpaceForCmsg; |
92 } | 92 } |
93 | 93 |
94 int packets_read = | 94 int packets_read = |
95 recvmmsg(fd, mmsg_hdr_, kNumPacketsPerReadMmsgCall, 0, nullptr); | 95 recvmmsg(fd, mmsg_hdr_, kNumPacketsPerReadMmsgCall, 0, nullptr); |
96 | 96 |
97 if (packets_read <= 0) { | 97 if (packets_read <= 0) { |
98 return false; // recvmmsg failed. | 98 return false; // recvmmsg failed. |
99 } | 99 } |
100 | 100 |
101 QuicTime fallback_timestamp = QuicTime::Zero(); | |
102 QuicWallTime fallback_walltimestamp = QuicWallTime::Zero(); | 101 QuicWallTime fallback_walltimestamp = QuicWallTime::Zero(); |
103 for (int i = 0; i < packets_read; ++i) { | 102 for (int i = 0; i < packets_read; ++i) { |
104 if (mmsg_hdr_[i].msg_len == 0) { | 103 if (mmsg_hdr_[i].msg_len == 0) { |
105 continue; | 104 continue; |
106 } | 105 } |
107 | 106 |
108 if (mmsg_hdr_[i].msg_hdr.msg_controllen >= QuicSocketUtils::kSpaceForCmsg) { | 107 if (mmsg_hdr_[i].msg_hdr.msg_controllen >= QuicSocketUtils::kSpaceForCmsg) { |
109 QUIC_BUG << "Incorrectly set control length: " | 108 QUIC_BUG << "Incorrectly set control length: " |
110 << mmsg_hdr_[i].msg_hdr.msg_controllen << ", expected " | 109 << mmsg_hdr_[i].msg_hdr.msg_controllen << ", expected " |
111 << QuicSocketUtils::kSpaceForCmsg; | 110 << QuicSocketUtils::kSpaceForCmsg; |
112 continue; | 111 continue; |
113 } | 112 } |
114 | 113 |
115 IPEndPoint client_address = IPEndPoint(packets_[i].raw_address); | 114 IPEndPoint client_address = IPEndPoint(packets_[i].raw_address); |
116 IPAddress server_ip; | 115 IPAddress server_ip; |
117 QuicTime packet_timestamp = QuicTime::Zero(); | |
118 QuicWallTime packet_walltimestamp = QuicWallTime::Zero(); | 116 QuicWallTime packet_walltimestamp = QuicWallTime::Zero(); |
119 bool latched_walltimestamps = FLAGS_quic_socket_walltimestamps; | |
120 QuicSocketUtils::GetAddressAndTimestampFromMsghdr( | 117 QuicSocketUtils::GetAddressAndTimestampFromMsghdr( |
121 &mmsg_hdr_[i].msg_hdr, &server_ip, &packet_timestamp, | 118 &mmsg_hdr_[i].msg_hdr, &server_ip, &packet_walltimestamp); |
122 &packet_walltimestamp, latched_walltimestamps); | |
123 if (!IsInitializedAddress(server_ip)) { | 119 if (!IsInitializedAddress(server_ip)) { |
124 QUIC_BUG << "Unable to get server address."; | 120 QUIC_BUG << "Unable to get server address."; |
125 continue; | 121 continue; |
126 } | 122 } |
127 | 123 |
128 // This isn't particularly desirable, but not all platforms support socket | 124 // This isn't particularly desirable, but not all platforms support socket |
129 // timestamping. | 125 // timestamping. |
130 if (latched_walltimestamps) { | 126 if (packet_walltimestamp.IsZero()) { |
131 if (packet_walltimestamp.IsZero()) { | 127 if (fallback_walltimestamp.IsZero()) { |
132 if (fallback_walltimestamp.IsZero()) { | 128 fallback_walltimestamp = clock.WallNow(); |
133 fallback_walltimestamp = clock.WallNow(); | |
134 } | |
135 packet_walltimestamp = fallback_walltimestamp; | |
136 } | 129 } |
137 packet_timestamp = clock.ConvertWallTimeToQuicTime(packet_walltimestamp); | 130 packet_walltimestamp = fallback_walltimestamp; |
138 } else { | |
139 if (packet_timestamp == QuicTime::Zero()) { | |
140 if (fallback_timestamp == QuicTime::Zero()) { | |
141 fallback_timestamp = clock.Now(); | |
142 } | |
143 packet_timestamp = fallback_timestamp; | |
144 } | |
145 } | 131 } |
| 132 QuicTime timestamp = clock.ConvertWallTimeToQuicTime(packet_walltimestamp); |
| 133 int ttl = 0; |
| 134 bool has_ttl = |
| 135 QuicSocketUtils::GetTtlFromMsghdr(&mmsg_hdr_[i].msg_hdr, &ttl); |
146 QuicReceivedPacket packet(reinterpret_cast<char*>(packets_[i].iov.iov_base), | 136 QuicReceivedPacket packet(reinterpret_cast<char*>(packets_[i].iov.iov_base), |
147 mmsg_hdr_[i].msg_len, packet_timestamp, false); | 137 mmsg_hdr_[i].msg_len, timestamp, false, ttl, |
| 138 has_ttl); |
148 IPEndPoint server_address(server_ip, port); | 139 IPEndPoint server_address(server_ip, port); |
149 processor->ProcessPacket(server_address, client_address, packet); | 140 processor->ProcessPacket(server_address, client_address, packet); |
150 } | 141 } |
151 | 142 |
152 if (packets_dropped != nullptr) { | 143 if (packets_dropped != nullptr) { |
153 QuicSocketUtils::GetOverflowFromMsghdr(&mmsg_hdr_[0].msg_hdr, | 144 QuicSocketUtils::GetOverflowFromMsghdr(&mmsg_hdr_[0].msg_hdr, |
154 packets_dropped); | 145 packets_dropped); |
155 } | 146 } |
156 | 147 |
157 // We may not have read all of the packets available on the socket. | 148 // We may not have read all of the packets available on the socket. |
158 return packets_read == kNumPacketsPerReadMmsgCall; | 149 return packets_read == kNumPacketsPerReadMmsgCall; |
159 #else | 150 #else |
160 LOG(FATAL) << "Unsupported"; | 151 LOG(FATAL) << "Unsupported"; |
161 return false; | 152 return false; |
162 #endif | 153 #endif |
163 } | 154 } |
164 | 155 |
165 /* static */ | 156 /* static */ |
166 bool QuicPacketReader::ReadAndDispatchSinglePacket( | 157 bool QuicPacketReader::ReadAndDispatchSinglePacket( |
167 int fd, | 158 int fd, |
168 int port, | 159 int port, |
169 const QuicClock& clock, | 160 const QuicClock& clock, |
170 ProcessPacketInterface* processor, | 161 ProcessPacketInterface* processor, |
171 QuicPacketCount* packets_dropped) { | 162 QuicPacketCount* packets_dropped) { |
172 bool latched_walltimestamps = FLAGS_quic_socket_walltimestamps; | |
173 char buf[kMaxPacketSize]; | 163 char buf[kMaxPacketSize]; |
174 | 164 |
175 IPEndPoint client_address; | 165 IPEndPoint client_address; |
176 IPAddress server_ip; | 166 IPAddress server_ip; |
177 QuicTime timestamp = QuicTime::Zero(); | |
178 QuicWallTime walltimestamp = QuicWallTime::Zero(); | 167 QuicWallTime walltimestamp = QuicWallTime::Zero(); |
179 int bytes_read = QuicSocketUtils::ReadPacket( | 168 int bytes_read = |
180 fd, buf, arraysize(buf), packets_dropped, &server_ip, ×tamp, | 169 QuicSocketUtils::ReadPacket(fd, buf, arraysize(buf), packets_dropped, |
181 &walltimestamp, latched_walltimestamps, &client_address); | 170 &server_ip, &walltimestamp, &client_address); |
182 | |
183 if (bytes_read < 0) { | 171 if (bytes_read < 0) { |
184 return false; // ReadPacket failed. | 172 return false; // ReadPacket failed. |
185 } | 173 } |
186 | 174 |
187 if (server_ip.empty()) { | 175 if (server_ip.empty()) { |
188 QUIC_BUG << "Unable to get server address."; | 176 QUIC_BUG << "Unable to get server address."; |
189 return false; | 177 return false; |
190 } | 178 } |
191 // This isn't particularly desirable, but not all platforms support socket | 179 // This isn't particularly desirable, but not all platforms support socket |
192 // timestamping. | 180 // timestamping. |
193 if (latched_walltimestamps) { | 181 if (walltimestamp.IsZero()) { |
194 if (walltimestamp.IsZero()) { | 182 walltimestamp = clock.WallNow(); |
195 walltimestamp = clock.WallNow(); | |
196 } | |
197 timestamp = clock.ConvertWallTimeToQuicTime(walltimestamp); | |
198 } else { | |
199 if (timestamp == QuicTime::Zero()) { | |
200 timestamp = clock.Now(); | |
201 } | |
202 } | 183 } |
| 184 QuicTime timestamp = clock.ConvertWallTimeToQuicTime(walltimestamp); |
203 | 185 |
204 QuicReceivedPacket packet(buf, bytes_read, timestamp, false); | 186 QuicReceivedPacket packet(buf, bytes_read, timestamp, false); |
205 IPEndPoint server_address(server_ip, port); | 187 IPEndPoint server_address(server_ip, port); |
206 processor->ProcessPacket(server_address, client_address, packet); | 188 processor->ProcessPacket(server_address, client_address, packet); |
207 | 189 |
208 // The socket read was successful, so return true even if packet dispatch | 190 // The socket read was successful, so return true even if packet dispatch |
209 // failed. | 191 // failed. |
210 return true; | 192 return true; |
211 } | 193 } |
212 | 194 |
213 | 195 |
214 } // namespace net | 196 } // namespace net |
OLD | NEW |