PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/znc-0.079-r1722/znc-msvc/znc.h

https://gitlab.com/BGCX261/znc-msvc-svn-to-git
C Header | 278 lines | 212 code | 33 blank | 33 comment | 12 complexity | 61eb9e2f237827403c803e83e96170ce MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Copyright (C) 2004-2010 See the AUTHORS file for details.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published
  6. * by the Free Software Foundation.
  7. */
  8. #ifndef _ZNC_H
  9. #define _ZNC_H
  10. #include "main.h"
  11. #include "Client.h"
  12. #include "FileUtils.h"
  13. #ifdef _MODULES
  14. #include "Modules.h"
  15. #endif
  16. #include "Socket.h"
  17. #include <map>
  18. using std::map;
  19. class CListener;
  20. class CUser;
  21. class CConnectUserTimer;
  22. class ZNC_API CZNC {
  23. public:
  24. CZNC();
  25. ~CZNC();
  26. enum ConfigState {
  27. ECONFIG_NOTHING,
  28. ECONFIG_NEED_REHASH,
  29. ECONFIG_NEED_WRITE
  30. };
  31. void DeleteUsers();
  32. void LoopDoMaintenance();
  33. #ifdef _WIN32
  34. void Loop(bool* bLoop);
  35. #else
  36. void Loop();
  37. #endif
  38. bool WriteISpoof(CUser* pUser);
  39. void ReleaseISpoof();
  40. bool WritePidFile(int iPid);
  41. bool DeletePidFile();
  42. CUser* GetUser(const CString& sUser);
  43. Csock* FindSockByName(const CString& sSockName);
  44. bool IsHostAllowed(const CString& sHostMask) const;
  45. // This returns false if there are too many anonymous connections from this ip
  46. bool AllowConnectionFrom(const CString& sIP) const;
  47. void InitDirs(const CString& sArgvPath, const CString& sDataDir);
  48. bool OnBoot();
  49. CString ExpandConfigPath(const CString& sConfigFile);
  50. bool WriteNewConfig(const CString& sConfigFile);
  51. bool WriteConfig();
  52. bool ParseConfig(const CString& sConfig);
  53. bool RehashConfig(CString& sError);
  54. static CString GetVersion();
  55. static CString GetTag(bool bIncludeVersion = true);
  56. CString GetUptime() const;
  57. static double GetCoreVersion();
  58. void ClearVHosts();
  59. bool AddVHost(const CString& sHost);
  60. bool RemVHost(const CString& sHost);
  61. void Broadcast(const CString& sMessage, bool bAdminOnly = false,
  62. CUser* pSkipUser = NULL, CClient* pSkipClient = NULL);
  63. void AddBytesRead(unsigned long long u) { m_uBytesRead += u; }
  64. void AddBytesWritten(unsigned long long u) { m_uBytesWritten += u; }
  65. unsigned long long BytesRead() const { return m_uBytesRead; }
  66. unsigned long long BytesWritten() const { return m_uBytesWritten; }
  67. // Traffic fun
  68. typedef std::pair<unsigned long long, unsigned long long> TrafficStatsPair;
  69. typedef std::map<CString, TrafficStatsPair> TrafficStatsMap;
  70. // Returns a map which maps user names to <traffic in, traffic out>
  71. // while also providing the traffic of all users together, traffic which
  72. // couldn't be accounted to any particular user and the total traffic
  73. // generated through ZNC.
  74. TrafficStatsMap GetTrafficStats(TrafficStatsPair &Users,
  75. TrafficStatsPair &ZNC, TrafficStatsPair &Total);
  76. // Authenticate a user.
  77. // The result is passed back via callbacks to CAuthBase.
  78. // CSmartPtr handles freeing this pointer!
  79. void AuthUser(CSmartPtr<CAuthBase> AuthClass);
  80. // Setters
  81. void SetConfigState(enum ConfigState e) { m_eConfigState = e; }
  82. void SetStatusPrefix(const CString& s) { m_sStatusPrefix = (s.empty()) ? "*" : s; }
  83. void SetISpoofFile(const CString& s) { m_sISpoofFile = s; }
  84. void SetISpoofFormat(const CString& s) { m_sISpoofFormat = (s.empty()) ? "global { reply \"%\" }" : s; }
  85. // !Setters
  86. // Getters
  87. enum ConfigState GetConfigState() const { return m_eConfigState; }
  88. CSockManager& GetManager() { return m_Manager; }
  89. const CSockManager& GetManager() const { return m_Manager; }
  90. #ifdef _MODULES
  91. CGlobalModules& GetModules() { return *m_pModules; }
  92. size_t FilterUncommonModules(set<CModInfo>& ssModules);
  93. #endif
  94. const CString& GetStatusPrefix() const { return m_sStatusPrefix; }
  95. const CString& GetCurPath() const { if (!CFile::Exists(m_sCurPath)) { CDir::MakeDir(m_sCurPath); } return m_sCurPath; }
  96. const CString& GetHomePath() const { if (!CFile::Exists(m_sHomePath)) { CDir::MakeDir(m_sHomePath); } return m_sHomePath; }
  97. const CString& GetZNCPath() const { if (!CFile::Exists(m_sZNCPath)) { CDir::MakeDir(m_sZNCPath); } return m_sZNCPath; }
  98. CString GetConfPath() const;
  99. CString GetUserPath() const;
  100. CString GetModPath() const;
  101. CString GetPemLocation() const { return GetZNCPath() + "/znc.pem"; }
  102. const CString& GetConfigFile() const { return m_sConfigFile; }
  103. bool WritePemFile();
  104. const CString& GetISpoofFile() const { return m_sISpoofFile; }
  105. const CString& GetISpoofFormat() const { return m_sISpoofFormat; }
  106. const VCString& GetVHosts() const { return m_vsVHosts; }
  107. const vector<CListener*>& GetListeners() const { return m_vpListeners; }
  108. time_t TimeStarted() const { return m_TimeStarted; }
  109. // !Getters
  110. // Static allocator
  111. static CZNC& Get();
  112. static void _Reset();
  113. void HookOutput(outputHook fHook);
  114. CUser* FindUser(const CString& sUsername);
  115. bool DeleteUser(const CString& sUsername);
  116. bool AddUser(CUser* pUser, CString& sErrorRet);
  117. const map<CString,CUser*> & GetUserMap() const { return(m_msUsers); }
  118. // Message of the Day
  119. void SetMotd(const CString& sMessage) { ClearMotd(); AddMotd(sMessage); }
  120. void AddMotd(const CString& sMessage) { if (!sMessage.empty()) { m_vsMotd.push_back(sMessage); } }
  121. void ClearMotd() { m_vsMotd.clear(); }
  122. const VCString& GetMotd() const { return m_vsMotd; }
  123. // !MOTD
  124. // Create a CIRCSocket. Return false if user cant connect
  125. bool ConnectUser(CUser *pUser);
  126. // This creates a CConnectUserTimer if we haven't got one yet
  127. void EnableConnectUser();
  128. void DisableConnectUser();
  129. // Never call this unless you are CConnectUserTimer::~CConnectUserTimer()
  130. void LeakConnectUser(CConnectUserTimer *pTimer);
  131. private:
  132. bool DoRehash(CString& sError);
  133. // Returns true if something was done
  134. bool HandleUserDeletion();
  135. protected:
  136. time_t m_TimeStarted;
  137. enum ConfigState m_eConfigState;
  138. vector<CListener*> m_vpListeners;
  139. map<CString,CUser*> m_msUsers;
  140. map<CString,CUser*> m_msDelUsers;
  141. CSockManager m_Manager;
  142. CString m_sCurPath;
  143. CString m_sHomePath;
  144. CString m_sZNCPath;
  145. CString m_sConfigFile;
  146. CString m_sStatusPrefix;
  147. CString m_sISpoofFile;
  148. CString m_sOrigISpoof;
  149. CString m_sISpoofFormat;
  150. CString m_sPidFile;
  151. VCString m_vsVHosts;
  152. VCString m_vsMotd;
  153. CFile m_LockFile;
  154. CFile* m_pISpoofLockFile;
  155. unsigned int m_uiConnectDelay;
  156. unsigned int m_uiAnonIPLimit;
  157. #ifdef _MODULES
  158. CGlobalModules* m_pModules;
  159. #endif
  160. unsigned long long m_uBytesRead;
  161. unsigned long long m_uBytesWritten;
  162. CConnectUserTimer *m_pConnectUserTimer;
  163. TCacheMap<CString> m_sConnectThrottle;
  164. };
  165. class CRealListener : public CZNCSock {
  166. public:
  167. CRealListener() : CZNCSock() {}
  168. virtual ~CRealListener() {}
  169. virtual bool ConnectionFrom(const CString& sHost, unsigned short uPort) {
  170. DEBUG(GetSockName() << " == ConnectionFrom(" << sHost << ", " << uPort << ")");
  171. return CZNC::Get().IsHostAllowed(sHost);
  172. }
  173. virtual Csock* GetSockObj(const CString& sHost, unsigned short uPort) {
  174. CClient *pClient = new CClient(sHost, uPort);
  175. if (CZNC::Get().AllowConnectionFrom(sHost)) {
  176. #ifdef _MODULES
  177. CZNC::Get().GetModules().OnClientConnect(pClient, sHost, uPort);
  178. #endif
  179. } else {
  180. pClient->RefuseLogin("Too many anonymous connections from your IP");
  181. #ifdef _MODULES
  182. CZNC::Get().GetModules().OnFailedLogin("", sHost);
  183. #endif
  184. }
  185. return pClient;
  186. }
  187. virtual void SockError(int iErrno) {
  188. DEBUG(GetSockName() << " == SockError(" << strerror(iErrno) << ")");
  189. if (iErrno == EMFILE) {
  190. // We have too many open fds, let's close this listening port to be able to continue
  191. // to work, next rehash will (try to) reopen it.
  192. Close();
  193. }
  194. }
  195. };
  196. class CListener {
  197. public:
  198. CListener(unsigned short uPort, const CString& sBindHost, bool bSSL, bool bIPV6) {
  199. m_uPort = uPort;
  200. m_sBindHost = sBindHost;
  201. m_bSSL = bSSL;
  202. m_bIPV6 = bIPV6;
  203. m_pListener = NULL;
  204. }
  205. ~CListener() {
  206. if (m_pListener)
  207. CZNC::Get().GetManager().DelSockByAddr(m_pListener);
  208. }
  209. // Setters
  210. void SetSSL(bool b) { m_bSSL = b; }
  211. void SetIPV6(bool b) { m_bIPV6 = b; }
  212. void SetPort(unsigned short u) { m_uPort = u; }
  213. void SetBindHost(const CString& s) { m_sBindHost = s; }
  214. // !Setters
  215. // Getters
  216. bool IsSSL() const { return m_bSSL; }
  217. bool IsIPV6() const { return m_bIPV6; }
  218. unsigned short GetPort() const { return m_uPort; }
  219. const CString& GetBindHost() const { return m_sBindHost; }
  220. CRealListener* GetRealListener() const { return m_pListener; }
  221. // !Getters
  222. bool Listen() {
  223. if (!m_uPort || m_pListener) {
  224. return false;
  225. }
  226. m_pListener = new CRealListener;
  227. bool bSSL = false;
  228. #ifdef HAVE_LIBSSL
  229. if (IsSSL()) {
  230. bSSL = true;
  231. m_pListener->SetPemLocation(CZNC::Get().GetPemLocation());
  232. }
  233. #endif
  234. return CZNC::Get().GetManager().ListenHost(m_uPort, "_LISTENER", m_sBindHost, bSSL, SOMAXCONN,
  235. m_pListener, 0, m_bIPV6);
  236. }
  237. private:
  238. protected:
  239. bool m_bSSL;
  240. bool m_bIPV6;
  241. unsigned short m_uPort;
  242. CString m_sBindHost;
  243. CRealListener* m_pListener;
  244. };
  245. #endif // !_ZNC_H