Overte C++ Documentation
SockAddr.h
1 //
2 // SockAddr.h
3 // libraries/networking/src
4 //
5 // Created by Stephen Birarda on 11/26/2013.
6 // Copyright 2013 High Fidelity, Inc.
7 // Copyright 2021 Vircadia contributors.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 //
12 
13 #ifndef hifi_SockAddr_h
14 #define hifi_SockAddr_h
15 
16 #include <cstdint>
17 #include <string>
18 
19 struct sockaddr;
20 
21 #include <QtNetwork/QHostInfo>
22 
23 #include "SocketType.h"
24 
25 
26 class SockAddr : public QObject {
27  Q_OBJECT
28 public:
29  SockAddr();
30  SockAddr(SocketType socketType, const QHostAddress& address, quint16 port);
31  SockAddr(const SockAddr& otherSockAddr);
32  SockAddr(SocketType socketType, const QString& hostname, quint16 hostOrderPort, bool shouldBlockForLookup = false);
33 
34  bool isNull() const { return _address.isNull() && _port == 0; }
35  void clear() { _address.clear(); _port = 0;}
36 
37  SockAddr& operator=(const SockAddr& rhsSockAddr);
38  void swap(SockAddr& otherSockAddr);
39 
40  bool operator==(const SockAddr& rhsSockAddr) const;
41  bool operator!=(const SockAddr& rhsSockAddr) const { return !(*this == rhsSockAddr); }
42 
43  SocketType getType() const { return _socketType; }
44  SocketType* getSocketTypePointer() { return &_socketType; }
45  void setType(const SocketType socketType) { _socketType = socketType; }
46 
47  const QHostAddress& getAddress() const { return _address; }
48  QHostAddress* getAddressPointer() { return &_address; }
49  void setAddress(const QHostAddress& address) { _address = address; }
50 
51  quint16 getPort() const { return _port; }
52  quint16* getPortPointer() { return &_port; }
53  void setPort(quint16 port) { _port = port; }
54 
55  static int packSockAddr(unsigned char* packetData, const SockAddr& packSockAddr);
56  static int unpackSockAddr(const unsigned char* packetData, SockAddr& unpackDestSockAddr);
57 
58  QString toString() const;
59  QString toShortString() const;
60 
61  bool hasPrivateAddress() const; // checks if the address behind this sock addr is private per RFC 1918
62 
63  friend QDebug operator<<(QDebug debug, const SockAddr& sockAddr);
64  friend QDataStream& operator<<(QDataStream& dataStream, const SockAddr& sockAddr);
65  friend QDataStream& operator>>(QDataStream& dataStream, SockAddr& sockAddr);
66 
67 private slots:
68  void handleLookupResult(const QHostInfo& hostInfo);
69 signals:
70  void lookupCompleted();
71  void lookupFailed();
72 private:
73  SocketType _socketType { SocketType::Unknown };
74  QHostAddress _address;
75  quint16 _port;
76 };
77 
78 uint qHash(const SockAddr& key, uint seed);
79 
80 namespace std {
81  template <>
82  struct hash<SockAddr> {
83  // NOTE: this hashing specifically ignores IPv6 addresses - if we begin to support those we will need
84  // to conditionally hash the bytes that represent an IPv6 address
85  size_t operator()(const SockAddr& sockAddr) const {
86  // use XOR of implemented std::hash templates for new hash
87  // depending on the type of address we're looking at
88 
89  if (sockAddr.getAddress().protocol() == QAbstractSocket::IPv4Protocol) {
90  return hash<uint32_t>()((uint32_t) sockAddr.getAddress().toIPv4Address())
91  ^ hash<uint16_t>()((uint16_t) sockAddr.getPort());
92  } else {
93  // NOTE: if we start to use IPv6 addresses, it's possible their hashing
94  // can be faster by XORing the hash for each 64 bits in the address
95  return hash<string>()(sockAddr.getAddress().toString().toStdString())
96  ^ hash<uint16_t>()((uint16_t) sockAddr.getPort());
97  }
98  }
99  };
100 }
101 
102 Q_DECLARE_METATYPE(SockAddr);
103 
104 #endif // hifi_SockAddr_h
SocketType
The types of network socket.
Definition: SocketType.h:22
@ Unknown
Socket type unknown or not set.