metrics_util.cc 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. // Copyright 2021 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. #include "chromecast/metrics/metrics_util.h"
  5. #include "base/logging.h"
  6. namespace chromecast {
  7. uint32_t GetIPAddressFragmentForLogging(const net::IPAddressBytes& sender_ip) {
  8. // Check if address is valid IPv4 or IPv6 byte array. If not then fill in
  9. // with zeros as a default value.
  10. if (sender_ip.size() != net::IPAddress::kIPv4AddressSize &&
  11. sender_ip.size() != net::IPAddress::kIPv6AddressSize) {
  12. DVLOG(1) << "Sender IP is not IPv4 or IPv6; zeroing out sender fragment.";
  13. return 0;
  14. }
  15. // Grab the last 2 bytes of sender IP address in network order and store as
  16. // packed 16-bit integer. The unused bits in the final packed value should
  17. // be empty.
  18. uint32_t packed_address = 0;
  19. if (sender_ip.size() >= 2) {
  20. int i = sender_ip.size() - 1;
  21. packed_address |= (sender_ip[i--]);
  22. packed_address |= (sender_ip[i--] << 8);
  23. }
  24. return packed_address;
  25. }
  26. } // namespace chromecast