1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file. 4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/quic/quic_address_mismatch.h" 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/logging.h" 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/base/ip_endpoint.h" 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace net { 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)int GetAddressMismatch(const IPEndPoint& first_address, 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const IPEndPoint& second_address) { 14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (first_address.address().empty() || second_address.address().empty()) { 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return -1; 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) IPAddressNumber first_ip_address = first_address.address(); 18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (IsIPv4Mapped(first_ip_address)) { 19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) first_ip_address = ConvertIPv4MappedToIPv4(first_ip_address); 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) IPAddressNumber second_ip_address = second_address.address(); 22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (IsIPv4Mapped(second_ip_address)) { 23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) second_ip_address = ConvertIPv4MappedToIPv4(second_ip_address); 24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) int sample; 27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) if (first_ip_address != second_ip_address) { 28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) sample = QUIC_ADDRESS_MISMATCH_BASE; 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } else if (first_address.port() != second_address.port()) { 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) sample = QUIC_PORT_MISMATCH_BASE; 31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } else { 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) sample = QUIC_ADDRESS_AND_PORT_MATCH_BASE; 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Add an offset to |sample|: 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // V4_V4: add 0 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // V6_V6: add 1 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // V4_V6: add 2 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // V6_V4: add 3 40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool first_ipv4 = (first_ip_address.size() == kIPv4AddressSize); 41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool second_ipv4 = (second_ip_address.size() == kIPv4AddressSize); 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (first_ipv4 != second_ipv4) { 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) CHECK_EQ(sample, QUIC_ADDRESS_MISMATCH_BASE); 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) sample += 2; 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (!first_ipv4) { 47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) sample += 1; 48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return sample; 50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace net 53