Flow 1.0.1
Flow project: Full implementation reference.
endpoint.cpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
20
21namespace flow::net_flow
22{
23
25{
26 using boost::hash_combine;
27 using boost::hash_range;
28 using boost::asio::ip::address;
29
30 size_t val = 0;
31
32 // Hash Flow port.
33 hash_combine(val, m_flow_port);
34
35 // Combine with hash of m_udp_endpoint. Port is straightforward.
36 hash_combine(val, m_udp_endpoint.port());
37
38 /* @todo The following is kind of slow... lots of copies.... Maybe just use the above and allow
39 * for some hash collisions? Or just use std::set/map instead of hash sets/maps? */
40
41 /* Hashing the address is trickier.
42 * Could just hash to_string(), but seems slicker/faster to use the byte representation itself. */
43 const address& addr = m_udp_endpoint.address();
44 if (addr.is_v4())
45 {
46 // Host byte order, but that's cool, as the hash value is computed and used only on this machine.
47 hash_combine(val, addr.to_v4().to_ulong());
48 }
49 else if (addr.is_v6())
50 {
51 // Network byte order. Hash each byte of this byte array.
52 const util::Ip_address_v6::bytes_type& addr_bytes = addr.to_v6().to_bytes();
53 hash_range(val, addr_bytes.begin(), addr_bytes.end());
54 }
55 // else they've invented another IP version... so we'll just have to deal with some hash collisions.
56
57 return val;
58} // Remote_endpoint::hash()
59
60bool operator==(const Remote_endpoint& lhs, const Remote_endpoint& rhs)
61{
62 return (lhs.m_flow_port == rhs.m_flow_port) && (lhs.m_udp_endpoint == rhs.m_udp_endpoint);
63}
64
65std::ostream& operator<<(std::ostream& os, const Remote_endpoint& endpoint)
66{
67 return os << "NetFlow [UDP " << endpoint.m_udp_endpoint << "]:" << endpoint.m_flow_port;
68}
69
70size_t hash_value(const Remote_endpoint& remote_endpoint)
71{
72 return remote_endpoint.hash();
73}
74
75} // namespace flow::net_flow
Flow module containing the API and implementation of the Flow network protocol, a TCP-inspired stream...
Definition: node.cpp:25
size_t hash_value(const Sequence_number &seq_num)
Free function that returns seq_num.hash(); has to be a free function named hash_value for boost....
Definition: seq_num.cpp:275
bool operator==(const Remote_endpoint &lhs, const Remote_endpoint &rhs)
Whether lhs is equal to rhs.
Definition: endpoint.cpp:60
std::ostream & operator<<(std::ostream &os, const Congestion_control_selector::Strategy_choice &strategy_choice)
Serializes a Peer_socket_options::Congestion_control_strategy_choice enum to a standard ostream – the...
Definition: cong_ctl.cpp:146
Represents the remote endpoint of a Flow-protocol connection; identifies the UDP endpoint of the remo...
Definition: endpoint.hpp:93
size_t hash() const
Hash value of this Remote_endpoint for unordered_*<>.
Definition: endpoint.cpp:24
util::Udp_endpoint m_udp_endpoint
UDP address (IP address/UDP port) where the Node identified by this endpoint bound its low-level UDP ...
Definition: endpoint.hpp:97
flow_port_t m_flow_port
The logical Flow port within the Node for the particular connection identified by this endpoint.
Definition: endpoint.hpp:99