InspIRCd  3.0
inspircd.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
5  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
6  * Copyright (C) 2008 Pippijn van Steenhoven <[email protected]>
7  * Copyright (C) 2003-2008 Craig Edwards <[email protected]>
8  * Copyright (C) 2006-2007 Oliver Lupton <[email protected]>
9  * Copyright (C) 2007 Dennis Friis <[email protected]>
10  * Copyright (C) 2003 randomdan <[email protected]???>
11  *
12  * This file is part of InspIRCd. InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 
26 #pragma once
27 
28 #include <climits>
29 #include <cmath>
30 #include <csignal>
31 #include <cstdarg>
32 #include <cstdio>
33 #include <cstring>
34 #include <ctime>
35 #include <stdint.h>
36 
37 #include <algorithm>
38 #include <bitset>
39 #include <deque>
40 #include <list>
41 #include <map>
42 #include <set>
43 #include <sstream>
44 #include <string>
45 #include <vector>
46 
47 #include "intrusive_list.h"
48 #include "flat_map.h"
49 #include "compat.h"
50 #include "aligned_storage.h"
51 #include "typedefs.h"
52 #include "stdalgo.h"
53 
54 CoreExport extern InspIRCd* ServerInstance;
55 
58 template <typename T>
59 struct fakederef
60 {
61  T* operator->()
62  {
63  return static_cast<T*>(this);
64  }
65 };
66 
67 #include "config.h"
68 #include "convto.h"
69 #include "dynref.h"
70 #include "consolecolors.h"
71 #include "caller.h"
72 #include "cull_list.h"
73 #include "extensible.h"
74 #include "fileutils.h"
75 #include "numerics.h"
76 #include "numeric.h"
77 #include "uid.h"
78 #include "server.h"
79 #include "users.h"
80 #include "channels.h"
81 #include "timer.h"
82 #include "hashcomp.h"
83 #include "logger.h"
84 #include "usermanager.h"
85 #include "socket.h"
86 #include "ctables.h"
87 #include "command_parse.h"
88 #include "mode.h"
89 #include "socketengine.h"
90 #include "snomasks.h"
91 #include "filelogger.h"
92 #include "modules.h"
93 #include "threadengine.h"
94 #include "configreader.h"
95 #include "inspstring.h"
96 #include "protocol.h"
97 #include "bancache.h"
98 #include "isupportmanager.h"
99 
105 {
106  public:
109  unsigned long Accept;
112  unsigned long Refused;
115  unsigned long Unknown;
118  unsigned long Collisions;
121  unsigned long Dns;
126  unsigned long DnsGood;
131  unsigned long DnsBad;
134  unsigned long Connects;
137  unsigned long Sent;
140  unsigned long Recv;
141 #ifdef _WIN32
142 
144  FILETIME LastCPU;
147  LARGE_INTEGER LastSampled;
150  LARGE_INTEGER QPFrequency;
151 #else
152 
154  timeval LastCPU;
157  timespec LastSampled;
158 #endif
159 
162  : Accept(0), Refused(0), Unknown(0), Collisions(0), Dns(0),
163  DnsGood(0), DnsBad(0), Connects(0), Sent(0), Recv(0)
164  {
165  }
166 };
167 
168 DEFINE_HANDLER1(IsNickHandler, bool, const std::string&);
169 DEFINE_HANDLER2(GenRandomHandler, void, char*, size_t);
170 DEFINE_HANDLER1(IsIdentHandler, bool, const std::string&);
171 DEFINE_HANDLER1(IsChannelHandler, bool, const std::string&);
172 
179 class CoreExport InspIRCd
180 {
181  private:
184  void SetSignals();
185 
189  bool DaemonSeed();
190 
193  struct timespec TIME;
194 
198  char ReadBuffer[65535];
199 
203  void CheckRoot();
204 
205  public:
206 
207  UIDGenerator UIDGen;
208 
214 
215  /**** Functors ****/
216 
217  IsNickHandler HandleIsNick;
218  IsIdentHandler HandleIsIdent;
219  IsChannelHandler HandleIsChannel;
220  GenRandomHandler HandleGenRandom;
221 
231 
236  User* FindUUID(const std::string &uid);
237 
240  time_t startup_time;
241 
244  std::string ConfigFileName;
245 
246  ExtensionManager Extensions;
247 
251 
255 
259 
263 
267 
272 
277 
281 
285 
290 
294 
298 
302 
305  chan_hash chanlist;
306 
309  std::vector<ListenSocket*> ports;
310 
313  static sig_atomic_t s_signal;
314 
318 
322 
325 
331  inline time_t Time() { return TIME.tv_sec; }
333  inline long Time_ns() { return TIME.tv_nsec; }
335  void UpdateTime();
336 
342  std::string GenRandomStr(int length, bool printable = true);
346  unsigned long GenRandomInt(unsigned long max);
347 
350 
354  int BindPorts(FailedPortList &failed_ports);
355 
361  User* FindNick(const std::string &nick);
362 
365  User* FindNickOnly(const std::string &nick);
366 
371  Channel* FindChan(const std::string &chan);
372 
376  chan_hash& GetChans() { return chanlist; }
377 
383 
387  static bool IsSID(const std::string& sid);
388 
392  void SignalHandler(int signal);
393 
397  static void SetSignal(int signal);
398 
405  void Exit(int status);
406 
410  static void QuickExit(int status);
411 
417  static const char* Format(const char* formatString, ...) CUSTOM_PRINTF(1, 2);
418  static const char* Format(va_list &vaList, const char* formatString) CUSTOM_PRINTF(2, 0);
419 
424  caller1<bool, const std::string&> IsNick;
425 
430  caller1<bool, const std::string&> IsIdent;
431 
438  static bool Match(const std::string& str, const std::string& mask, unsigned const char* map = NULL);
439  static bool Match(const char* str, const char* mask, unsigned const char* map = NULL);
440 
448  static bool MatchCIDR(const std::string& str, const std::string& mask, unsigned const char* map = NULL);
449  static bool MatchCIDR(const char* str, const char* mask, unsigned const char* map = NULL);
450 
456  static bool MatchMask(const std::string& masks, const std::string& hostname, const std::string& ipaddr);
457 
462  static bool IsValidMask(const std::string& mask);
463 
467  static void StripColor(std::string &sentence);
468 
472  static void ProcessColors(file_cache& input);
473 
477  void Rehash(const std::string& uuid = "");
478 
484  static unsigned long Duration(const std::string& str);
485 
495  bool PassCompare(Extensible* ex, const std::string& data, const std::string& input, const std::string& hashtype);
496 
500  std::string GetVersionString(bool getFullVersion = false);
501 
507  void WritePID(const std::string& filename, bool exitonfail = true);
508 
516  InspIRCd(int argc, char** argv);
517 
522  void Cleanup();
523 
531  static std::string TimeString(time_t curtime, const char* format = NULL, bool utc = false);
532 
540  static bool TimingSafeCompare(const std::string& one, const std::string& two);
541 
546  void Run();
547 
548  char* GetReadBuffer()
549  {
550  return this->ReadBuffer;
551  }
552 };
553 
554 ENTRYPOINT;
555 
556 template<class Cmd>
557 class CommandModule : public Module
558 {
559  Cmd cmd;
560  public:
561  CommandModule() : cmd(this)
562  {
563  }
564 
566  {
567  return Version(cmd.name, VF_VENDOR|VF_CORE);
568  }
569 };
570 
571 inline void stdalgo::culldeleter::operator()(classbase* item)
572 {
573  if (item)
574  ServerInstance->GlobalCulls.AddItem(item);
575 }
576 
577 #include "numericbuilder.h"
578 #include "modules/whois.h"
579 #include "modules/stats.h"
Definition: threadengine_pthread.h:36
Definition: inspircd.h:169
Definition: inspircd.h:171
unsigned long DnsGood
Definition: inspircd.h:126
Definition: modules.h:185
ServerConfig * Config
Definition: inspircd.h:284
ConfigReaderThread * ConfigThread
Definition: inspircd.h:262
ModuleManager Modules
Definition: inspircd.h:271
unsigned long Unknown
Definition: inspircd.h:115
Definition: inspircd.h:179
unsigned long Connects
Definition: inspircd.h:134
Definition: extensible.h:90
Definition: isupportmanager.h:23
caller1< bool, const std::string & > IsChannel
Definition: inspircd.h:382
time_t Time()
Definition: inspircd.h:331
long Time_ns()
Definition: inspircd.h:333
Definition: cull_list.h:46
Definition: logger.h:121
Definition: caller.h:217
STL namespace.
Definition: users.h:224
unsigned long Recv
Definition: inspircd.h:140
Definition: command_parse.h:30
unsigned long Refused
Definition: inspircd.h:112
unsigned long Sent
Definition: inspircd.h:137
FakeUser * FakeClient
Definition: inspircd.h:230
LogManager Logs
Definition: inspircd.h:266
ProtocolInterface * PI
Definition: inspircd.h:317
ProtocolInterface DefaultProtocolInterface
Definition: inspircd.h:321
Definition: configreader.h:503
Definition: base.h:41
Definition: cull_list.h:30
Definition: inspircd.h:168
UserManager Users
Definition: inspircd.h:301
unsigned long Collisions
Definition: inspircd.h:118
TimerManager Timers
Definition: inspircd.h:293
timespec LastSampled
Definition: inspircd.h:157
Version GetVersion()
Definition: inspircd.h:565
Definition: modules.h:1010
Definition: bancache.h:50
Definition: mode.h:503
CommandParser Parser
Definition: inspircd.h:254
XLineManager * XLines
Definition: inspircd.h:297
Definition: inspircd.h:104
Definition: modules.h:245
caller2< void, char *, size_t > GenRandom
Definition: inspircd.h:349
unsigned long Dns
Definition: inspircd.h:121
Definition: usermanager.h:24
timeval LastCPU
Definition: inspircd.h:154
ActionList AtomicActions
Definition: inspircd.h:213
Definition: users.h:894
Definition: inspircd.h:59
Definition: snomasks.h:75
Definition: inspircd.h:557
time_t startup_time
Definition: inspircd.h:240
Definition: xline.h:398
serverstats stats
Definition: inspircd.h:280
chan_hash & GetChans()
Definition: inspircd.h:376
std::vector< ListenSocket * > ports
Definition: inspircd.h:309
CullList GlobalCulls
Definition: inspircd.h:211
Definition: inspircd.h:170
static sig_atomic_t s_signal
Definition: inspircd.h:313
unsigned long DnsBad
Definition: inspircd.h:131
std::string ConfigFileName
Definition: inspircd.h:244
ThreadEngine Threads
Definition: inspircd.h:258
Definition: extensible.h:124
SnomaskManager SNO
Definition: inspircd.h:289
unsigned long Accept
Definition: inspircd.h:109
ModeParser Modes
Definition: inspircd.h:250
ISupportManager ISupport
Definition: inspircd.h:324
Definition: uid.h:23
Definition: protocol.h:39
serverstats()
Definition: inspircd.h:161
chan_hash chanlist
Definition: inspircd.h:305
void AddItem(classbase *item)
Definition: cull_list.h:38
Definition: configreader.h:197
BanCacheManager BanCache
Definition: inspircd.h:276
Definition: timer.h:118
Definition: channels.h:37