quic_address_mismatch.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2014 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 "net/quic/quic_address_mismatch.h"
  5. #include "base/check_op.h"
  6. #include "net/base/ip_address.h"
  7. namespace net {
  8. int GetAddressMismatch(const IPEndPoint& first_address,
  9. const IPEndPoint& second_address) {
  10. if (first_address.address().empty() || second_address.address().empty()) {
  11. return -1;
  12. }
  13. IPAddress first_ip_address = first_address.address();
  14. if (first_ip_address.IsIPv4MappedIPv6()) {
  15. first_ip_address = ConvertIPv4MappedIPv6ToIPv4(first_ip_address);
  16. }
  17. IPAddress second_ip_address = second_address.address();
  18. if (second_ip_address.IsIPv4MappedIPv6()) {
  19. second_ip_address = ConvertIPv4MappedIPv6ToIPv4(second_ip_address);
  20. }
  21. int sample;
  22. if (first_ip_address != second_ip_address) {
  23. sample = QUIC_ADDRESS_MISMATCH_BASE;
  24. } else if (first_address.port() != second_address.port()) {
  25. sample = QUIC_PORT_MISMATCH_BASE;
  26. } else {
  27. sample = QUIC_ADDRESS_AND_PORT_MATCH_BASE;
  28. }
  29. // Add an offset to |sample|:
  30. // V4_V4: add 0
  31. // V6_V6: add 1
  32. // V4_V6: add 2
  33. // V6_V4: add 3
  34. bool first_ipv4 = first_ip_address.IsIPv4();
  35. if (first_ipv4 != second_ip_address.IsIPv4()) {
  36. CHECK_EQ(sample, QUIC_ADDRESS_MISMATCH_BASE);
  37. sample += 2;
  38. }
  39. if (!first_ipv4) {
  40. sample += 1;
  41. }
  42. return sample;
  43. }
  44. } // namespace net