1 // Copyright 2005-2019 The Mumble Developers. All rights reserved. 2 // Use of this source code is governed by a BSD-style license 3 // that can be found in the LICENSE file at the root of the 4 // Mumble source tree or at <https://www.mumble.info/LICENSE>. 5 6 #ifndef MUMBLE_MURMUR_SERVERUSER_H_ 7 #define MUMBLE_MURMUR_SERVERUSER_H_ 8 9 #include <QtCore/QStringList> 10 #include <QtCore/QElapsedTimer> 11 12 #ifdef Q_OS_UNIX 13 #include <sys/socket.h> 14 #else 15 #include <winsock2.h> 16 #endif 17 18 #include "Connection.h" 19 #include "Timer.h" 20 #include "User.h" 21 #include "HostAddress.h" 22 23 // Unfortunately, this needs to be "large enough" to hold 24 // enough frames to account for both short-term and 25 // long-term "maladjustments". 26 27 #define N_BANDWIDTH_SLOTS 360 28 29 struct BandwidthRecord { 30 int iRecNum; 31 int iSum; 32 Timer tFirst; 33 Timer tIdleControl; 34 unsigned short a_iBW[N_BANDWIDTH_SLOTS]; 35 Timer a_qtWhen[N_BANDWIDTH_SLOTS]; 36 mutable QMutex qmMutex; 37 38 BandwidthRecord(); 39 bool addFrame(int size, int maxpersec); 40 int onlineSeconds() const; 41 int idleSeconds() const; 42 void resetIdleSeconds(); 43 int bandwidth() const; 44 }; 45 46 struct WhisperTarget { 47 struct Channel { 48 int iId; 49 bool bChildren; 50 bool bLinks; 51 QString qsGroup; 52 }; 53 QList<unsigned int> qlSessions; 54 QList<WhisperTarget::Channel> qlChannels; 55 }; 56 57 class Server; 58 59 /// A simple implementation for rate-limiting. 60 /// See https://en.wikipedia.org/wiki/Leaky_bucket 61 class LeakyBucket { 62 private: 63 /// The amount of tokens that are drained per second. 64 /// (The sze of the whole in the bucket) 65 unsigned int m_tokensPerSec; 66 /// The maximum amount of tokens that may be encountered. 67 /// (The capacity of the bucket) 68 unsigned int m_maxTokens; 69 /// The amount of tokens currently stored 70 /// (The amount of whater currently in the bucket) 71 long m_currentTokens; 72 /// A timer that is used to measure time intervals. It is essential 73 /// that this timer uses a monotonic clock (which is why QElapsedTimer is 74 /// used instead of QTime or QDateTime). 75 QElapsedTimer m_timer; 76 77 public: 78 /// @param tokens The amount of tokens that should be added. 79 /// @returns Whether adding this amount of tokens triggers rate 80 /// limiting (true means the corresponding packet has to be 81 /// discared and false means the packet may be processed) 82 bool ratelimit(int tokens); 83 84 LeakyBucket(unsigned int tokensPerSec, unsigned int maxTokens); 85 }; 86 87 class ServerUser : public Connection, public User { 88 private: 89 Q_OBJECT Q_DISABLE_COPY(ServerUser)90 Q_DISABLE_COPY(ServerUser) 91 protected: 92 Server *s; 93 public: 94 enum State { Connected, Authenticated }; 95 State sState; 96 operator QString() const; 97 98 float dUDPPingAvg, dUDPPingVar; 99 float dTCPPingAvg, dTCPPingVar; 100 quint32 uiUDPPackets, uiTCPPackets; 101 102 unsigned int uiVersion; 103 QString qsRelease; 104 QString qsOS; 105 QString qsOSVersion; 106 107 std::string ssContext; 108 QString qsIdentity; 109 110 bool bVerified; 111 QStringList qslEmail; 112 113 HostAddress haAddress; 114 115 /// Holds whether the user is using TCP 116 /// or UDP for voice packets. 117 /// 118 /// If the flag is 0, the user is using 119 /// TCP. 120 /// 121 /// If the flag is 1, the user is using 122 /// UDP. 123 QAtomicInt aiUdpFlag; 124 125 QList<int> qlCodecs; 126 bool bOpus; 127 128 QStringList qslAccessTokens; 129 130 QMap<int, WhisperTarget> qmTargets; 131 typedef QPair<QSet<ServerUser *>, QSet<ServerUser *> > TargetCache; 132 QMap<int, TargetCache> qmTargetCache; 133 QMap<QString, QString> qmWhisperRedirect; 134 135 LeakyBucket leakyBucket; 136 137 int iLastPermissionCheck; 138 QMap<int, unsigned int> qmPermissionSent; 139 #ifdef Q_OS_UNIX 140 int sUdpSocket; 141 #else 142 SOCKET sUdpSocket; 143 #endif 144 BandwidthRecord bwr; 145 struct sockaddr_storage saiUdpAddress; 146 struct sockaddr_storage saiTcpLocalAddress; 147 ServerUser(Server *parent, QSslSocket *socket); 148 }; 149 150 #endif 151