PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Core.h

https://github.com/gunnarbeutner/shroudbnc
C Header | 281 lines | 169 code | 69 blank | 43 comment | 0 complexity | 36df51366eefcd309091e768f592ed26 MD5 | raw file
  1. /*******************************************************************************
  2. * shroudBNC - an object-oriented framework for IRC *
  3. * Copyright (C) 2005-2014 Gunnar Beutner *
  4. * *
  5. * This program is free software; you can redistribute it and/or *
  6. * modify it under the terms of the GNU General Public License *
  7. * as published by the Free Software Foundation; either version 2 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the Free Software *
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. *******************************************************************************/
  19. #ifndef CORE_H
  20. #define CORE_H
  21. #define DEFAULT_SENDQ (10 * 1024)
  22. class CConfig;
  23. class CUser;
  24. class CLog;
  25. class CClientListener;
  26. class CClientConnection;
  27. class CIRCConnection;
  28. class CIdentSupport;
  29. class CModule;
  30. class CConnection;
  31. class CTimer;
  32. class CFakeClient;
  33. struct CSocketEvents;
  34. struct sockaddr_in;
  35. /**
  36. * Settings cache.
  37. *
  38. * Commonly used settings are cached.
  39. */
  40. DEFINE_CACHE(System)
  41. DEFINE_OPTION_INT(dontmatchuser);
  42. DEFINE_OPTION_INT(port);
  43. DEFINE_OPTION_INT(sslport);
  44. DEFINE_OPTION_INT(sendq);
  45. DEFINE_OPTION_INT(md5);
  46. DEFINE_OPTION_INT(interval);
  47. DEFINE_OPTION_STRING(vhost);
  48. DEFINE_OPTION_STRING(users);
  49. DEFINE_OPTION_STRING(ip);
  50. DEFINE_OPTION_STRING(motd);
  51. END_DEFINE_CACHE
  52. /**
  53. * socket_t
  54. *
  55. * A registered socket.
  56. */
  57. typedef struct socket_s {
  58. pollfd *PollFd ; /**< the underlying socket object */
  59. CSocketEvents *Events; /**< the event interface for this socket */
  60. } socket_t;
  61. /**
  62. * sbnc_status_t
  63. *
  64. * Program status.
  65. */
  66. typedef enum sbnc_status_e {
  67. Status_Running,
  68. Status_Shutdown
  69. } sbnc_status_t;
  70. /*
  71. * additionallistener_t
  72. *
  73. * An additional listener for client connections.
  74. */
  75. typedef struct additionallistener_s {
  76. unsigned int Port; /**< the port of the listener */
  77. char *BindAddress; /**< the bind address, or NULL */
  78. bool SSL; /**< whether this is an SSL listener */
  79. CSocketEvents *Listener; /**< IPv4 listener object */
  80. CSocketEvents *ListenerV6; /**< IPv6 listener object */
  81. } additionallistener_t;
  82. /**
  83. * CCore
  84. *
  85. * The main application class.
  86. */
  87. class SBNCAPI CCore {
  88. #ifndef SWIG
  89. friend class CTimer;
  90. friend pollfd *registersocket(int Socket);
  91. friend void unregistersocket(int Socket);
  92. #endif /* SWIG */
  93. FILE *m_PidFile; /**< sbnc.pid file */
  94. CConfig *m_Config; /**< sbnc.conf object */
  95. CClientListener *m_Listener, *m_ListenerV6; /**< the main unencrypted listeners */
  96. CClientListener *m_SSLListener, *m_SSLListenerV6; /**< the main ssl listeners */
  97. CHashtable<CUser *, false> m_Users; /**< the bouncer users */
  98. CVector<CModule *> m_Modules; /**< currently loaded modules */
  99. mutable CList<socket_t> m_OtherSockets; /**< a list of active sockets */
  100. CList<CTimer *> m_Timers; /**< a list of active timers */
  101. time_t m_Startup; /**< TS when the bouncer was started */
  102. CLog *m_Log; /**< the bouncer's main log */
  103. CIdentSupport *m_Ident; /**< ident support interface */
  104. bool m_LoadingModules; /**< are we currently loading modules? */
  105. bool m_LoadingListeners; /**< are we currently loading listeners */
  106. CVector<char *> m_Args; /**< program arguments */
  107. mutable CACHE(System) m_ConfigCache;
  108. SSL_CTX *m_SSLContext; /**< SSL context for client listeners */
  109. SSL_CTX *m_SSLClientContext; /**< SSL context for IRC connections */
  110. CVector<additionallistener_t> m_AdditionalListeners; /**< a list of additional listeners */
  111. CVector<CUser *> m_AdminUsers; /**< cached list of admin users */
  112. CVector<pollfd> m_PollFds; /**< pollfd structures */
  113. sbnc_status_t m_Status; /**< shroudBNC's current status */
  114. CVector<const char *> *m_Capabilities;
  115. void UpdateModuleConfig(void);
  116. void UpdateUserConfig(void);
  117. void UnlockPidFile(void);
  118. void WritePidFile(void);
  119. bool MakeConfig(void);
  120. void InitializeSocket(void);
  121. void UninitializeSocket(void);
  122. void InitializeAdditionalListeners(void);
  123. void UninitializeAdditionalListeners(void);
  124. void UpdateAdditionalListeners(void);
  125. bool Daemonize(void);
  126. public:
  127. #ifndef SWIG
  128. CCore(CConfig *Config, int argc, char **argv);
  129. virtual ~CCore(void);
  130. #endif /* SWIG */
  131. void StartMainLoop(bool ShouldDaemonize);
  132. CUser *GetUser(const char *Name);
  133. void GlobalNotice(const char *Text);
  134. CHashtable<CUser *, false> *GetUsers(void);
  135. RESULT<CModule *> LoadModule(const char *Filename);
  136. bool UnloadModule(CModule *Module);
  137. const CVector<CModule *> *GetModules(void) const;
  138. void SetIdent(const char *Ident);
  139. const char *GetIdent(void) const;
  140. CConfig *GetConfig(void);
  141. void RegisterSocket(SOCKET Socket, CSocketEvents *EventInterface);
  142. void UnregisterSocket(SOCKET Socket);
  143. SOCKET CreateListener(unsigned int Port, const char *BindIp = NULL, int Family = AF_INET) const;
  144. void Log(const char *Format, ...);
  145. void LogUser(CUser *User, const char *Format, ...);
  146. CLog *GetLog(void);
  147. void Shutdown(void);
  148. RESULT<CUser *> CreateUser(const char *Username, const char *Password);
  149. RESULT<bool> RemoveUser(const char *Username, bool RemoveConfig = true);
  150. bool IsValidUsername(const char *Username) const;
  151. time_t GetStartup(void) const;
  152. const char *MD5(const char *String, const char *Salt) const;
  153. int GetArgC(void) const;
  154. const char *const *GetArgV(void) const;
  155. bool IsRegisteredSocket(CSocketEvents *Events) const;
  156. SOCKET SocketAndConnect(const char *Host, unsigned int Port, const char *BindIp);
  157. const socket_t *GetSocketByClass(const char *Class, int Index) const;
  158. CTimer *CreateTimer(unsigned int Interval, bool Repeat, TimerProc Function, void *Cookie) const;
  159. bool Match(const char *Pattern, const char *String) const;
  160. size_t GetSendqSize(void) const;
  161. void SetSendqSize(size_t NewSize);
  162. const char *GetMotd(void) const;
  163. void SetMotd(const char *Motd);
  164. void InternalLogError(const char *Format, ...);
  165. void InternalSetFileAndLine(const char *Filename, unsigned int Line);
  166. void Fatal(void);
  167. SSL_CTX *GetSSLContext(void) ;
  168. SSL_CTX *GetSSLClientContext(void);
  169. int GetSSLCustomIndex(void) const;
  170. const char *DebugImpulse(int impulse);
  171. const char *GetTagString(const char *Tag) const;
  172. int GetTagInteger(const char *Tag) const;
  173. bool SetTagString(const char *Tag, const char *Value);
  174. bool SetTagInteger(const char *Tag, int Value);
  175. const char *GetTagName(int Index) const;
  176. const char *BuildPathConfig(const char *Filename) const;
  177. const char *BuildPathLog(const char *Filename) const;
  178. const char *BuildPathData(const char *Filename) const;
  179. const char *BuildPathExe(const char *Filename) const;
  180. const char *BuildPathModule(const char *Filename) const;
  181. const char *BuildPathShared(const char *Filename) const;
  182. const char *GetBouncerVersion(void) const;
  183. void SetStatus(sbnc_status_t NewStatus);
  184. sbnc_status_t GetStatus(void) const;
  185. CFakeClient *CreateFakeClient(void) const;
  186. void DeleteFakeClient(CFakeClient *FakeClient) const;
  187. CVector<CUser *> *GetAdminUsers(void);
  188. RESULT<bool> AddAdditionalListener(unsigned int Port, const char *BindAddress = NULL, bool SSL = false);
  189. RESULT<bool> RemoveAdditionalListener(unsigned int Port);
  190. CVector<additionallistener_t> *GetAdditionalListeners(void);
  191. CClientListener *GetMainListener(void) const;
  192. CClientListener *GetMainListenerV6(void) const;
  193. CClientListener *GetMainSSLListener(void) const;
  194. CClientListener *GetMainSSLListenerV6(void) const;
  195. int GetResourceLimit(const char *Resource, CUser *User = NULL);
  196. void SetResourceLimit(const char *Resource, int Limit, CUser *User = NULL);
  197. int GetInterval(void) const;
  198. void SetInterval(int Interval);
  199. bool GetMD5(void) const;
  200. void SetMD5(bool MD5Flag);
  201. const char *GetDefaultVHost(void) const;
  202. void SetDefaultVHost(const char *VHost);
  203. bool GetDontMatchUser(void) const;
  204. void SetDontMatchUser(bool Value);
  205. CACHE(System) *GetConfigCache(void);
  206. CVector<const char *> *GetCapabilities(void);
  207. };
  208. #ifndef SWIG
  209. extern CCore *g_Bouncer; /**< the main bouncer object */
  210. extern time_t g_CurrentTime; /**< the current time (updated in main loop) */
  211. #endif /* SWIG */
  212. #endif /* CORE_H */