InspIRCd  4.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].services>
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) 2005-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 #include <cfloat>
35 #include <climits>
36 #include <cmath>
37 #include <csignal>
38 #include <cstdarg>
39 #include <cstdint>
40 #include <cstdio>
41 #include <cstring>
42 #include <ctime>
43 
44 #include <algorithm>
45 #include <array>
46 #include <atomic>
47 #include <bitset>
48 #include <deque>
49 #include <functional>
50 #include <list>
51 #include <memory>
52 #include <map>
53 #include <set>
54 #include <sstream>
55 #include <string>
56 #include <string_view>
57 #include <type_traits>
58 #include <unordered_map>
59 #include <vector>
60 
61 #include "utility/aligned_storage.h"
62 #include "utility/iterator_range.h"
63 #include "utility/string_view.h"
64 
65 #include "intrusive_list.h"
66 #include "flat_map.h"
67 #include "compat.h"
68 #include "typedefs.h"
69 #include "convto.h"
70 #include "stdalgo.h"
71 
72 CoreExport extern InspIRCd* ServerInstance;
73 
74 #include "config.h"
75 #include "dynref.h"
76 #include "consolecolors.h"
77 #include "cull.h"
78 #include "serialize.h"
79 #include "extensible.h"
80 #include "fileutils.h"
81 #include "ctables.h"
82 #include "numerics.h"
83 #include "numeric.h"
84 #include "uid.h"
85 #include "server.h"
86 #include "users.h"
87 #include "channels.h"
88 #include "timer.h"
89 #include "hashcomp.h"
90 #include "logger.h"
91 #include "channelmanager.h"
92 #include "usermanager.h"
93 #include "socket.h"
94 #include "command_parse.h"
95 #include "mode.h"
96 #include "socketengine.h"
97 #include "snomasks.h"
98 #include "filelogger.h"
99 #include "message.h"
100 #include "modules.h"
101 #include "clientprotocol.h"
102 #include "thread.h"
103 #include "threadsocket.h"
104 #include "configreader.h"
105 #include "inspstring.h"
106 #include "protocol.h"
107 #include "bancache.h"
108 
114 {
115  public:
118  unsigned long Accept = 0;
119 
122  unsigned long Refused = 0;
123 
126  unsigned long Unknown = 0;
127 
130  unsigned long Collisions = 0;
131 
134  unsigned long Dns = 0;
135 
140  unsigned long DnsGood = 0;
141 
146  unsigned long DnsBad = 0;
147 
150  unsigned long Connects = 0;
151 
154  unsigned long Sent = 0;
155 
158  unsigned long Recv = 0;
159 
160 #ifdef _WIN32
163  FILETIME LastCPU;
164 
167  LARGE_INTEGER LastSampled;
168 
171  LARGE_INTEGER QPFrequency;
172 #else
175  timeval LastCPU;
176 
179  timespec LastSampled;
180 #endif
181 };
182 
189 class CoreExport InspIRCd
190 {
191  private:
194  struct timespec TIME;
195 
199  char ReadBuffer[65535];
200 
201  ClientProtocol::RFCEvents rfcevents;
202 
203  public:
204 
205  UIDGenerator UIDGen;
206 
212 
221  FakeUser* FakeClient = nullptr;
222 
225  time_t startup_time;
226 
229  std::string ConfigFileName = INSPIRCD_CONFIG_PATH "/inspircd.conf";
230 
231  ExtensionManager Extensions;
232 
236 
240 
243  ConfigReaderThread* ConfigThread = nullptr;
244 
248 
253 
258 
262 
265  ServerConfig* Config = nullptr;
266 
271 
275 
278  XLineManager* XLines = nullptr;
279 
283 
286 
289  std::vector<ListenSocket*> ports;
290 
293  static sig_atomic_t s_signal;
294 
298 
302 
308  inline time_t Time() { return TIME.tv_sec; }
310  inline long Time_ns() { return TIME.tv_nsec; }
312  void UpdateTime();
313 
319  std::string GenRandomStr(size_t length, bool printable = true);
323  unsigned long GenRandomInt(unsigned long max);
324 
326  std::function<void(char*, size_t)> GenRandom;
327 
333  static void DefaultGenRandom(char* output, size_t max);
334 
340  bool BindPort(std::shared_ptr<ConfigTag> tag, const irc::sockets::sockaddrs& sa, std::vector<ListenSocket*>& old_ports);
341 
345  size_t BindPorts(FailedPortList &failed_ports);
346 
351  static bool IsHost(const std::string& host);
352 
356  static bool IsSID(const std::string& sid);
357 
361  void SignalHandler(int signal);
362 
366  static void SetSignal(int signal);
367 
374  [[noreturn]]
375  void Exit(int status);
376 
382  static std::string Format(const char* formatString, ...) CUSTOM_PRINTF(1, 2);
383  static std::string Format(va_list& vaList, const char* formatString) CUSTOM_PRINTF(2, 0);
384 
386  std::function<bool(const std::string&)> IsNick;
387 
393  static bool DefaultIsNick(const std::string& nick);
394 
396  std::function<bool(const std::string&)> IsIdent;
397 
403  static bool DefaultIsIdent(const std::string& ident);
404 
411  static bool Match(const std::string& str, const std::string& mask, unsigned const char* map = NULL);
412  static bool Match(const char* str, const char* mask, unsigned const char* map = NULL);
413 
421  static bool MatchCIDR(const std::string& str, const std::string& mask, unsigned const char* map = NULL);
422  static bool MatchCIDR(const char* str, const char* mask, unsigned const char* map = NULL);
423 
429  static bool MatchMask(const std::string& masks, const std::string& hostname, const std::string& ipaddr);
430 
435  static bool IsValidMask(const std::string& mask);
436 
440  static void StripColor(std::string &sentence);
441 
445  static void ProcessColors(file_cache& input);
446 
450  void Rehash(const std::string& uuid = "");
451 
457  static unsigned long Duration(const std::string& str);
458 
465  static bool Duration(const std::string& str, unsigned long& duration);
466 
471  static bool IsValidDuration(const std::string& str);
472 
477  static std::string DurationString(time_t duration);
478 
488  bool PassCompare(Extensible* ex, const std::string& data, const std::string& input, const std::string& hashtype);
489 
493  std::string GetVersionString(bool getFullVersion = false);
494 
498  void WritePID(bool exitonfail);
499 
507  InspIRCd(int argc, char** argv);
508 
513  void Cleanup();
514 
522  static std::string TimeString(time_t curtime, const char* format = NULL, bool utc = false);
523 
531  static bool TimingSafeCompare(const std::string& one, const std::string& two);
532 
537  void Run();
538 
539  char* GetReadBuffer()
540  {
541  return this->ReadBuffer;
542  }
543 
544  ClientProtocol::RFCEvents& GetRFCEvents() { return rfcevents; }
545 };
546 
547 ENTRYPOINT;
548 
549 inline void stdalgo::cull_delete::operator()(Cullable* item)
550 {
551  if (item)
552  ServerInstance->GlobalCulls.AddItem(item);
553 }
554 
555 inline void Channel::Write(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg, char status, const CUList& except_list)
556 {
557  ClientProtocol::Event event(protoevprov, msg);
558  Write(event, status, except_list);
559 }
560 
561 inline void LocalUser::Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg)
562 {
563  ClientProtocol::Event event(protoevprov, msg);
564  Send(event);
565 }
566 
567 #include "numericbuilder.h"
568 #include "clientprotocolmsg.h"
569 #include "clientprotocolevent.h"
Definition: cull.h:81
Definition: bancache.h:56
void Write(ClientProtocol::Event &protoev, char status=0, const CUList &except_list=CUList())
Definition: channels.cpp:368
Definition: channelmanager.h:26
Definition: clientprotocol.h:470
Definition: clientprotocol.h:628
Definition: clientprotocol.h:182
Definition: command_parse.h:35
Definition: configreader.h:503
Definition: cull.h:57
void AddItem(Cullable *item)
Definition: cull.h:64
Definition: cull.h:30
Definition: extensible.h:132
Definition: extensible.h:172
Definition: users.h:841
Definition: inspircd.h:190
ChannelManager Channels
Definition: inspircd.h:285
static sig_atomic_t s_signal
Definition: inspircd.h:293
ActionList AtomicActions
Definition: inspircd.h:211
CullList GlobalCulls
Definition: inspircd.h:209
UserManager Users
Definition: inspircd.h:282
ProtocolInterface * PI
Definition: inspircd.h:297
TimerManager Timers
Definition: inspircd.h:274
CommandParser Parser
Definition: inspircd.h:239
time_t Time()
Definition: inspircd.h:308
BanCacheManager BanCache
Definition: inspircd.h:257
LogManager Logs
Definition: inspircd.h:247
serverstats stats
Definition: inspircd.h:261
time_t startup_time
Definition: inspircd.h:225
std::vector< ListenSocket * > ports
Definition: inspircd.h:289
long Time_ns()
Definition: inspircd.h:310
ModeParser Modes
Definition: inspircd.h:235
std::function< void(char *, size_t)> GenRandom
Definition: inspircd.h:326
ModuleManager Modules
Definition: inspircd.h:252
ProtocolInterface DefaultProtocolInterface
Definition: inspircd.h:301
SnomaskManager SNO
Definition: inspircd.h:270
Definition: logger.h:122
Definition: mode.h:558
Definition: modules.h:1014
Definition: protocol.h:40
Definition: configreader.h:239
Definition: snomasks.h:77
Definition: timer.h:124
Definition: uid.h:26
Definition: usermanager.h:32
Definition: xline.h:380
Definition: inspircd.h:114
unsigned long Collisions
Definition: inspircd.h:130
unsigned long Connects
Definition: inspircd.h:150
unsigned long Unknown
Definition: inspircd.h:126
unsigned long Refused
Definition: inspircd.h:122
timespec LastSampled
Definition: inspircd.h:179
unsigned long Dns
Definition: inspircd.h:134
unsigned long DnsGood
Definition: inspircd.h:140
unsigned long Sent
Definition: inspircd.h:154
unsigned long Accept
Definition: inspircd.h:118
timeval LastCPU
Definition: inspircd.h:175
unsigned long DnsBad
Definition: inspircd.h:146
unsigned long Recv
Definition: inspircd.h:158
Definition: clientprotocol.h:645
Definition: socket.h:63