IPv4SocketTuple.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <Kernel/DoubleBuffer.h>
  9. #include <Kernel/KBuffer.h>
  10. #include <Kernel/Locking/Mutex.h>
  11. #include <Kernel/Net/IPv4.h>
  12. #include <Kernel/Net/Socket.h>
  13. class IPv4SocketTuple {
  14. public:
  15. IPv4SocketTuple(IPv4Address local_address, u16 local_port, IPv4Address peer_address, u16 peer_port)
  16. : m_local_address(local_address)
  17. , m_local_port(local_port)
  18. , m_peer_address(peer_address)
  19. , m_peer_port(peer_port) {};
  20. IPv4Address local_address() const { return m_local_address; };
  21. u16 local_port() const { return m_local_port; };
  22. IPv4Address peer_address() const { return m_peer_address; };
  23. u16 peer_port() const { return m_peer_port; };
  24. bool operator==(const IPv4SocketTuple other) const
  25. {
  26. return other.local_address() == m_local_address && other.local_port() == m_local_port && other.peer_address() == m_peer_address && other.peer_port() == m_peer_port;
  27. };
  28. String to_string() const
  29. {
  30. return String::formatted(
  31. "{}:{} -> {}:{}",
  32. m_local_address,
  33. m_local_port,
  34. m_peer_address,
  35. m_peer_port);
  36. }
  37. private:
  38. IPv4Address m_local_address;
  39. u16 m_local_port { 0 };
  40. IPv4Address m_peer_address;
  41. u16 m_peer_port { 0 };
  42. };
  43. namespace AK {
  44. template<>
  45. struct Traits<IPv4SocketTuple> : public GenericTraits<IPv4SocketTuple> {
  46. static unsigned hash(const IPv4SocketTuple& tuple)
  47. {
  48. auto h1 = pair_int_hash(tuple.local_address().to_u32(), tuple.local_port());
  49. auto h2 = pair_int_hash(tuple.peer_address().to_u32(), tuple.peer_port());
  50. return pair_int_hash(h1, h2);
  51. }
  52. };
  53. }