InspIRCd  3.0
inspircd.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019 Matt Schatz <[email protected]>
5  * Copyright (C) 2018 linuxdaemon <[email protected]>
6  * Copyright (C) 2013 Daniel Vassdal <[email protected]>
7  * Copyright (C) 2012-2016, 2018 Attila Molnar <[email protected]>
8  * Copyright (C) 2012-2014, 2017-2019 Sadie Powell <[email protected]ces>
9  * Copyright (C) 2012, 2019 Robby <[email protected]>
10  * Copyright (C) 2012 ChrisTX <[email protected]>
11  * Copyright (C) 2009-2010 Daniel De Graaf <[email protected]>
12  * Copyright (C) 2008 Thomas Stagner <[email protected]>
13  * Copyright (C) 2007-2009 Dennis Friis <[email protected]>
14  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
15  * Copyright (C) 2007 Oliver Lupton <[email protected]>
16  * Copyright (C) 2006-2008, 2010 Craig Edwards <[email protected]>
17  *
18  * This file is part of InspIRCd. InspIRCd is free software: you can
19  * redistribute it and/or modify it under the terms of the GNU General Public
20  * License as published by the Free Software Foundation, version 2.
21  *
22  * This program is distributed in the hope that it will be useful, but WITHOUT
23  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25  * details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 
32 #pragma once
33 
34 // If the system has a pre-C11 stdint header this must be defined in
35 // order to use the numeric limit macros.
36 #define __STDC_LIMIT_MACROS
37 
38 #include <cfloat>
39 #include <climits>
40 #include <cmath>
41 #include <csignal>
42 #include <cstdarg>
43 #include <cstdio>
44 #include <cstring>
45 #include <ctime>
46 #include <stdint.h>
47 
48 #include <algorithm>
49 #include <bitset>
50 #include <deque>
51 #include <list>
52 #include <map>
53 #include <set>
54 #include <sstream>
55 #include <string>
56 #include <vector>
57 
58 #include "intrusive_list.h"
59 #include "flat_map.h"
60 #include "compat.h"
61 #include "aligned_storage.h"
62 #include "typedefs.h"
63 #include "convto.h"
64 #include "stdalgo.h"
65 
66 CoreExport extern InspIRCd* ServerInstance;
67 
70 template <typename T>
71 struct fakederef
72 {
73  T* operator->()
74  {
75  return static_cast<T*>(this);
76  }
77 };
78 
79 #include "config.h"
80 #include "dynref.h"
81 #include "consolecolors.h"
82 #include "cull_list.h"
83 #include "serialize.h"
84 #include "extensible.h"
85 #include "fileutils.h"
86 #include "ctables.h"
87 #include "numerics.h"
88 #include "numeric.h"
89 #include "uid.h"
90 #include "server.h"
91 #include "users.h"
92 #include "channels.h"
93 #include "timer.h"
94 #include "hashcomp.h"
95 #include "logger.h"
96 #include "usermanager.h"
97 #include "socket.h"
98 #include "command_parse.h"
99 #include "mode.h"
100 #include "socketengine.h"
101 #include "snomasks.h"
102 #include "filelogger.h"
103 #include "message.h"
104 #include "modules.h"
105 #include "clientprotocol.h"
106 #include "threadengine.h"
107 #include "configreader.h"
108 #include "inspstring.h"
109 #include "protocol.h"
110 #include "bancache.h"
111 #include "isupportmanager.h"
112 
118 {
119  public:
122  unsigned long Accept;
125  unsigned long Refused;
128  unsigned long Unknown;
131  unsigned long Collisions;
134  unsigned long Dns;
139  unsigned long DnsGood;
144  unsigned long DnsBad;
147  unsigned long Connects;
150  unsigned long Sent;
153  unsigned long Recv;
154 #ifdef _WIN32
155 
157  FILETIME LastCPU;
160  LARGE_INTEGER LastSampled;
163  LARGE_INTEGER QPFrequency;
164 #else
165 
167  timeval LastCPU;
170  timespec LastSampled;
171 #endif
172 
175  : Accept(0), Refused(0), Unknown(0), Collisions(0), Dns(0),
176  DnsGood(0), DnsBad(0), Connects(0), Sent(0), Recv(0)
177  {
178  }
179 };
180 
187 class CoreExport InspIRCd
188 {
189  private:
192  struct timespec TIME;
193 
197  char ReadBuffer[65535];
198 
199  ClientProtocol::RFCEvents rfcevents;
200 
201  public:
202 
203  UIDGenerator UIDGen;
204 
210 
220 
225  User* FindUUID(const std::string &uid);
226 
229  time_t startup_time;
230 
233  std::string ConfigFileName;
234 
235  ExtensionManager Extensions;
236 
240 
244 
248 
252 
256 
261 
266 
270 
274 
279 
283 
287 
291 
294  chan_hash chanlist;
295 
298  std::vector<ListenSocket*> ports;
299 
302  static sig_atomic_t s_signal;
303 
307 
311 
314 
320  inline time_t Time() { return TIME.tv_sec; }
322  inline long Time_ns() { return TIME.tv_nsec; }
324  void UpdateTime();
325 
331  std::string GenRandomStr(unsigned int length, bool printable = true);
335  unsigned long GenRandomInt(unsigned long max);
336 
338  TR1NS::function<void(char*, size_t)> GenRandom;
339 
345  static void DefaultGenRandom(char* output, size_t max);
346 
352  bool BindPort(ConfigTag* tag, const irc::sockets::sockaddrs& sa, std::vector<ListenSocket*>& old_ports);
353 
357  size_t BindPorts(FailedPortList &failed_ports);
358 
364  User* FindNick(const std::string &nick);
365 
368  User* FindNickOnly(const std::string &nick);
369 
374  Channel* FindChan(const std::string &chan);
375 
379  chan_hash& GetChans() { return chanlist; }
380 
382  TR1NS::function<bool(const std::string&)> IsChannel;
383 
389  static bool DefaultIsChannel(const std::string& channel);
390 
395  static bool IsHost(const std::string& host);
396 
400  static bool IsSID(const std::string& sid);
401 
405  void SignalHandler(int signal);
406 
410  static void SetSignal(int signal);
411 
418  void Exit(int status);
419 
425  static std::string Format(const char* formatString, ...) CUSTOM_PRINTF(1, 2);
426  static std::string Format(va_list& vaList, const char* formatString) CUSTOM_PRINTF(2, 0);
427 
429  TR1NS::function<bool(const std::string&)> IsNick;
430 
436  static bool DefaultIsNick(const std::string& nick);
437 
439  TR1NS::function<bool(const std::string&)> IsIdent;
440 
446  static bool DefaultIsIdent(const std::string& ident);
447 
454  static bool Match(const std::string& str, const std::string& mask, unsigned const char* map = NULL);
455  static bool Match(const char* str, const char* mask, unsigned const char* map = NULL);
456 
464  static bool MatchCIDR(const std::string& str, const std::string& mask, unsigned const char* map = NULL);
465  static bool MatchCIDR(const char* str, const char* mask, unsigned const char* map = NULL);
466 
472  static bool MatchMask(const std::string& masks, const std::string& hostname, const std::string& ipaddr);
473 
478  static bool IsValidMask(const std::string& mask);
479 
483  static void StripColor(std::string &sentence);
484 
488  static void ProcessColors(file_cache& input);
489 
493  void Rehash(const std::string& uuid = "");
494 
500  static unsigned long Duration(const std::string& str);
501 
508  static bool Duration(const std::string& str, unsigned long& duration);
509 
514  static bool IsValidDuration(const std::string& str);
515 
520  static std::string DurationString(time_t duration);
521 
531  bool PassCompare(Extensible* ex, const std::string& data, const std::string& input, const std::string& hashtype);
532 
536  std::string GetVersionString(bool getFullVersion = false);
537 
543  void WritePID(const std::string& filename, bool exitonfail = true);
544 
552  InspIRCd(int argc, char** argv);
553 
558  void Cleanup();
559 
567  static std::string TimeString(time_t curtime, const char* format = NULL, bool utc = false);
568 
576  static bool TimingSafeCompare(const std::string& one, const std::string& two);
577 
582  void Run();
583 
584  char* GetReadBuffer()
585  {
586  return this->ReadBuffer;
587  }
588 
589  ClientProtocol::RFCEvents& GetRFCEvents() { return rfcevents; }
590 };
591 
592 ENTRYPOINT;
593 
594 inline void stdalgo::culldeleter::operator()(classbase* item)
595 {
596  if (item)
597  ServerInstance->GlobalCulls.AddItem(item);
598 }
599 
600 inline void Channel::Write(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg, char status, const CUList& except_list)
601 {
602  ClientProtocol::Event event(protoevprov, msg);
603  Write(event, status, except_list);
604 }
605 
606 inline void LocalUser::Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg)
607 {
608  ClientProtocol::Event event(protoevprov, msg);
609  Send(event);
610 }
611 
612 #include "numericbuilder.h"
613 #include "clientprotocolmsg.h"
614 #include "clientprotocolevent.h"
fakederef
Definition: inspircd.h:71
ClientProtocol::EventProvider
Definition: clientprotocol.h:625
InspIRCd::Modes
ModeParser Modes
Definition: inspircd.h:239
Extensible
Definition: extensible.h:139
FakeUser
Definition: users.h:908
serverstats::DnsGood
unsigned long DnsGood
Definition: inspircd.h:139
InspIRCd::Time_ns
long Time_ns()
Definition: inspircd.h:322
LogManager
Definition: logger.h:126
serverstats::Sent
unsigned long Sent
Definition: inspircd.h:150
ConfigReaderThread
Definition: configreader.h:478
InspIRCd::GenRandom
std::tr1 ::function< void(char *, size_t)> GenRandom
Definition: inspircd.h:338
InspIRCd::BanCache
BanCacheManager BanCache
Definition: inspircd.h:265
ClientProtocol::Event
Definition: clientprotocol.h:462
ISupportManager
Definition: isupportmanager.h:24
serverstats::Connects
unsigned long Connects
Definition: inspircd.h:147
CommandParser
Definition: command_parse.h:34
InspIRCd::Modules
ModuleManager Modules
Definition: inspircd.h:260
CullList
Definition: cull_list.h:32
serverstats::LastSampled
timespec LastSampled
Definition: inspircd.h:170
ActionList
Definition: cull_list.h:56
serverstats::Recv
unsigned long Recv
Definition: inspircd.h:153
serverstats::Refused
unsigned long Refused
Definition: inspircd.h:125
serverstats::Collisions
unsigned long Collisions
Definition: inspircd.h:131
ModeParser
Definition: mode.h:556
InspIRCd::ConfigThread
ConfigReaderThread * ConfigThread
Definition: inspircd.h:251
irc::sockets::sockaddrs
Definition: socket.h:62
InspIRCd::Parser
CommandParser Parser
Definition: inspircd.h:243
InspIRCd::AtomicActions
ActionList AtomicActions
Definition: inspircd.h:209
SnomaskManager
Definition: snomasks.h:80
TimerManager
Definition: timer.h:123
ProtocolInterface
Definition: protocol.h:41
UIDGenerator
Definition: uid.h:25
ModuleManager
Definition: modules.h:976
InspIRCd::PI
ProtocolInterface * PI
Definition: inspircd.h:306
classbase
Definition: base.h:46
InspIRCd::ports
std::vector< ListenSocket * > ports
Definition: inspircd.h:298
InspIRCd::startup_time
time_t startup_time
Definition: inspircd.h:229
ThreadEngine
Definition: threadengine_pthread.h:39
InspIRCd::Logs
LogManager Logs
Definition: inspircd.h:255
ServerConfig
Definition: configreader.h:215
InspIRCd::GetChans
chan_hash & GetChans()
Definition: inspircd.h:379
InspIRCd::DefaultProtocolInterface
ProtocolInterface DefaultProtocolInterface
Definition: inspircd.h:310
InspIRCd::Timers
TimerManager Timers
Definition: inspircd.h:282
InspIRCd::FakeClient
FakeUser * FakeClient
Definition: inspircd.h:219
Channel
Definition: channels.h:40
serverstats::LastCPU
timeval LastCPU
Definition: inspircd.h:167
InspIRCd::Users
UserManager Users
Definition: inspircd.h:290
ClientProtocol::RFCEvents
Definition: clientprotocol.h:642
InspIRCd::s_signal
static sig_atomic_t s_signal
Definition: inspircd.h:302
ConfigTag
Definition: configreader.h:43
InspIRCd::ISupport
ISupportManager ISupport
Definition: inspircd.h:313
UserManager
Definition: usermanager.h:31
BanCacheManager
Definition: bancache.h:55
InspIRCd::XLines
XLineManager * XLines
Definition: inspircd.h:286
InspIRCd::Threads
ThreadEngine Threads
Definition: inspircd.h:247
serverstats
Definition: inspircd.h:117
InspIRCd::chanlist
chan_hash chanlist
Definition: inspircd.h:294
CullList::AddItem
void AddItem(classbase *item)
Definition: cull_list.h:40
serverstats::Accept
unsigned long Accept
Definition: inspircd.h:122
serverstats::Dns
unsigned long Dns
Definition: inspircd.h:134
User
Definition: users.h:233
InspIRCd::SNO
SnomaskManager SNO
Definition: inspircd.h:278
ClientProtocol::Message
Definition: clientprotocol.h:181
InspIRCd::stats
serverstats stats
Definition: inspircd.h:269
InspIRCd::Config
ServerConfig * Config
Definition: inspircd.h:273
XLineManager
Definition: xline.h:410
serverstats::DnsBad
unsigned long DnsBad
Definition: inspircd.h:144
ExtensionManager
Definition: extensible.h:181
InspIRCd
Definition: inspircd.h:187
InspIRCd::ConfigFileName
std::string ConfigFileName
Definition: inspircd.h:233
InspIRCd::Time
time_t Time()
Definition: inspircd.h:320
Channel::Write
void Write(ClientProtocol::Event &protoev, char status=0, const CUList &except_list=CUList())
Definition: channels.cpp:420
InspIRCd::IsChannel
std::tr1 ::function< bool(const std::string &)> IsChannel
Definition: inspircd.h:382
serverstats::Unknown
unsigned long Unknown
Definition: inspircd.h:128
serverstats::serverstats
serverstats()
Definition: inspircd.h:174
InspIRCd::GlobalCulls
CullList GlobalCulls
Definition: inspircd.h:207