1 // Copyright (c) 2019 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 "quiche/quic/qbone/qbone_packet_processor_test_tools.h"
6 
7 #include <netinet/ip6.h>
8 
9 namespace quic {
10 
PrependIPv6HeaderForTest(const std::string & body,int hops)11 std::string PrependIPv6HeaderForTest(const std::string& body, int hops) {
12   ip6_hdr header;
13   memset(&header, 0, sizeof(header));
14 
15   header.ip6_vfc = 6 << 4;
16   header.ip6_plen = htons(body.size());
17   header.ip6_nxt = IPPROTO_UDP;
18   header.ip6_hops = hops;
19   header.ip6_src = in6addr_loopback;
20   header.ip6_dst = in6addr_loopback;
21 
22   std::string packet(sizeof(header) + body.size(), '\0');
23   memcpy(&packet[0], &header, sizeof(header));
24   memcpy(&packet[sizeof(header)], body.data(), body.size());
25   return packet;
26 }
27 
DecrementIPv6HopLimit(std::string & packet)28 bool DecrementIPv6HopLimit(std::string& packet) {
29   if (packet.size() < sizeof(ip6_hdr)) {
30     return false;
31   }
32   ip6_hdr* header = reinterpret_cast<ip6_hdr*>(&packet[0]);
33   if (header->ip6_vfc >> 4 != 6 || header->ip6_hops == 0) {
34     return false;
35   }
36   header->ip6_hops--;
37   return true;
38 }
39 
40 }  // namespace quic
41