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 // If the system has a pre-C11 stdint header this must be defined in
29 // order to use the numeric limit macros.
30 #define __STDC_LIMIT_MACROS
31 
32 #include <cfloat>
33 #include <climits>
34 #include <cmath>
35 #include <csignal>
36 #include <cstdarg>
37 #include <cstdio>
38 #include <cstring>
39 #include <ctime>
40 #include <stdint.h>
41 
42 #include <algorithm>
43 #include <bitset>
44 #include <deque>
45 #include <list>
46 #include <map>
47 #include <set>
48 #include <sstream>
49 #include <string>
50 #include <vector>
51 
52 #include "intrusive_list.h"
53 #include "flat_map.h"
54 #include "compat.h"
55 #include "aligned_storage.h"
56 #include "typedefs.h"
57 #include "convto.h"
58 #include "stdalgo.h"
59 
60 CoreExport extern InspIRCd* ServerInstance;
61 
64 template <typename T>
65 struct fakederef
66 {
67  T* operator->()
68  {
69  return static_cast<T*>(this);
70  }
71 };
72 
73 #include "config.h"
74 #include "dynref.h"
75 #include "consolecolors.h"
76 #include "cull_list.h"
77 #include "serialize.h"
78 #include "extensible.h"
79 #include "fileutils.h"
80 #include "ctables.h"
81 #include "numerics.h"
82 #include "numeric.h"
83 #include "uid.h"
84 #include "server.h"
85 #include "users.h"
86 #include "channels.h"
87 #include "timer.h"
88 #include "hashcomp.h"
89 #include "logger.h"
90 #include "usermanager.h"
91 #include "socket.h"
92 #include "command_parse.h"
93 #include "mode.h"
94 #include "socketengine.h"
95 #include "snomasks.h"
96 #include "filelogger.h"
97 #include "message.h"
98 #include "modules.h"
99 #include "clientprotocol.h"
100 #include "threadengine.h"
101 #include "configreader.h"
102 #include "inspstring.h"
103 #include "protocol.h"
104 #include "bancache.h"
105 #include "isupportmanager.h"
106 
112 {
113  public:
116  unsigned long Accept;
119  unsigned long Refused;
122  unsigned long Unknown;
125  unsigned long Collisions;
128  unsigned long Dns;
133  unsigned long DnsGood;
138  unsigned long DnsBad;
141  unsigned long Connects;
144  unsigned long Sent;
147  unsigned long Recv;
148 #ifdef _WIN32
149 
151  FILETIME LastCPU;
154  LARGE_INTEGER LastSampled;
157  LARGE_INTEGER QPFrequency;
158 #else
159 
161  timeval LastCPU;
164  timespec LastSampled;
165 #endif
166 
169  : Accept(0), Refused(0), Unknown(0), Collisions(0), Dns(0),
170  DnsGood(0), DnsBad(0), Connects(0), Sent(0), Recv(0)
171  {
172  }
173 };
174 
181 class CoreExport InspIRCd
182 {
183  private:
186  void SetSignals();
187 
191  bool DaemonSeed();
192 
195  struct timespec TIME;
196 
200  char ReadBuffer[65535];
201 
202  ClientProtocol::RFCEvents rfcevents;
203 
207  void CheckRoot();
208 
209  public:
210 
211  UIDGenerator UIDGen;
212 
218 
228 
233  User* FindUUID(const std::string &uid);
234 
237  time_t startup_time;
238 
241  std::string ConfigFileName;
242 
243  ExtensionManager Extensions;
244 
248 
252 
256 
260 
264 
269 
274 
278 
282 
287 
291 
295 
299 
302  chan_hash chanlist;
303 
306  std::vector<ListenSocket*> ports;
307 
310  static sig_atomic_t s_signal;
311 
315 
319 
322 
328  inline time_t Time() { return TIME.tv_sec; }
330  inline long Time_ns() { return TIME.tv_nsec; }
332  void UpdateTime();
333 
339  std::string GenRandomStr(unsigned int length, bool printable = true);
343  unsigned long GenRandomInt(unsigned long max);
344 
346  TR1NS::function<void(char*, size_t)> GenRandom;
347 
353  static void DefaultGenRandom(char* output, size_t max);
354 
360  bool BindPort(ConfigTag* tag, const irc::sockets::sockaddrs& sa, std::vector<ListenSocket*>& old_ports);
361 
365  int BindPorts(FailedPortList &failed_ports);
366 
372  User* FindNick(const std::string &nick);
373 
376  User* FindNickOnly(const std::string &nick);
377 
382  Channel* FindChan(const std::string &chan);
383 
387  chan_hash& GetChans() { return chanlist; }
388 
390  TR1NS::function<bool(const std::string&)> IsChannel;
391 
397  static bool DefaultIsChannel(const std::string& channel);
398 
403  static bool IsHost(const std::string& host);
404 
408  static bool IsSID(const std::string& sid);
409 
413  void SignalHandler(int signal);
414 
418  static void SetSignal(int signal);
419 
426  void Exit(int status);
427 
433  static std::string Format(const char* formatString, ...) CUSTOM_PRINTF(1, 2);
434  static std::string Format(va_list& vaList, const char* formatString) CUSTOM_PRINTF(2, 0);
435 
437  TR1NS::function<bool(const std::string&)> IsNick;
438 
444  static bool DefaultIsNick(const std::string& nick);
445 
447  TR1NS::function<bool(const std::string&)> IsIdent;
448 
454  static bool DefaultIsIdent(const std::string& ident);
455 
462  static bool Match(const std::string& str, const std::string& mask, unsigned const char* map = NULL);
463  static bool Match(const char* str, const char* mask, unsigned const char* map = NULL);
464 
472  static bool MatchCIDR(const std::string& str, const std::string& mask, unsigned const char* map = NULL);
473  static bool MatchCIDR(const char* str, const char* mask, unsigned const char* map = NULL);
474 
480  static bool MatchMask(const std::string& masks, const std::string& hostname, const std::string& ipaddr);
481 
486  static bool IsValidMask(const std::string& mask);
487 
491  static void StripColor(std::string &sentence);
492 
496  static void ProcessColors(file_cache& input);
497 
501  void Rehash(const std::string& uuid = "");
502 
508  static unsigned long Duration(const std::string& str);
509 
516  static bool Duration(const std::string& str, unsigned long& duration);
517 
522  static bool IsValidDuration(const std::string& str);
523 
528  static std::string DurationString(time_t duration);
529 
539  bool PassCompare(Extensible* ex, const std::string& data, const std::string& input, const std::string& hashtype);
540 
544  std::string GetVersionString(bool getFullVersion = false);
545 
551  void WritePID(const std::string& filename, bool exitonfail = true);
552 
560  InspIRCd(int argc, char** argv);
561 
566  void Cleanup();
567 
575  static std::string TimeString(time_t curtime, const char* format = NULL, bool utc = false);
576 
584  static bool TimingSafeCompare(const std::string& one, const std::string& two);
585 
590  void Run();
591 
592  char* GetReadBuffer()
593  {
594  return this->ReadBuffer;
595  }
596 
597  ClientProtocol::RFCEvents& GetRFCEvents() { return rfcevents; }
598 };
599 
600 ENTRYPOINT;
601 
602 inline void stdalgo::culldeleter::operator()(classbase* item)
603 {
604  if (item)
605  ServerInstance->GlobalCulls.AddItem(item);
606 }
607 
608 inline void Channel::Write(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg, char status, const CUList& except_list)
609 {
610  ClientProtocol::Event event(protoevprov, msg);
611  Write(event, status, except_list);
612 }
613 
614 inline void LocalUser::Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg)
615 {
616  ClientProtocol::Event event(protoevprov, msg);
617  Send(event);
618 }
619 
620 #include "numericbuilder.h"
621 #include "clientprotocolmsg.h"
622 #include "clientprotocolevent.h"
fakederef
Definition: inspircd.h:65
ClientProtocol::EventProvider
Definition: clientprotocol.h:614
InspIRCd::Modes
ModeParser Modes
Definition: inspircd.h:247
Extensible
Definition: extensible.h:136
FakeUser
Definition: users.h:899
serverstats::DnsGood
unsigned long DnsGood
Definition: inspircd.h:133
InspIRCd::Time_ns
long Time_ns()
Definition: inspircd.h:330
LogManager
Definition: logger.h:121
serverstats::Sent
unsigned long Sent
Definition: inspircd.h:144
ConfigReaderThread
Definition: configreader.h:471
InspIRCd::GenRandom
std::tr1 ::function< void(char *, size_t)> GenRandom
Definition: inspircd.h:346
InspIRCd::BanCache
BanCacheManager BanCache
Definition: inspircd.h:273
ClientProtocol::Event
Definition: clientprotocol.h:461
ISupportManager
Definition: isupportmanager.h:23
serverstats::Connects
unsigned long Connects
Definition: inspircd.h:141
CommandParser
Definition: command_parse.h:30
InspIRCd::Modules
ModuleManager Modules
Definition: inspircd.h:268
CullList
Definition: cull_list.h:30
serverstats::LastSampled
timespec LastSampled
Definition: inspircd.h:164
ActionList
Definition: cull_list.h:54
serverstats::Recv
unsigned long Recv
Definition: inspircd.h:147
serverstats::Refused
unsigned long Refused
Definition: inspircd.h:119
serverstats::Collisions
unsigned long Collisions
Definition: inspircd.h:125
ModeParser
Definition: mode.h:544
InspIRCd::ConfigThread
ConfigReaderThread * ConfigThread
Definition: inspircd.h:259
irc::sockets::sockaddrs
Definition: socket.h:58
InspIRCd::Parser
CommandParser Parser
Definition: inspircd.h:251
InspIRCd::AtomicActions
ActionList AtomicActions
Definition: inspircd.h:217
SnomaskManager
Definition: snomasks.h:75
TimerManager
Definition: timer.h:118
ProtocolInterface
Definition: protocol.h:37
UIDGenerator
Definition: uid.h:21
ModuleManager
Definition: modules.h:964
InspIRCd::PI
ProtocolInterface * PI
Definition: inspircd.h:314
classbase
Definition: base.h:41
InspIRCd::ports
std::vector< ListenSocket * > ports
Definition: inspircd.h:306
InspIRCd::startup_time
time_t startup_time
Definition: inspircd.h:237
ThreadEngine
Definition: threadengine_pthread.h:36
InspIRCd::Logs
LogManager Logs
Definition: inspircd.h:263
ServerConfig
Definition: configreader.h:208
InspIRCd::GetChans
chan_hash & GetChans()
Definition: inspircd.h:387
InspIRCd::DefaultProtocolInterface
ProtocolInterface DefaultProtocolInterface
Definition: inspircd.h:318
InspIRCd::Timers
TimerManager Timers
Definition: inspircd.h:290
InspIRCd::FakeClient
FakeUser * FakeClient
Definition: inspircd.h:227
Channel
Definition: channels.h:37
serverstats::LastCPU
timeval LastCPU
Definition: inspircd.h:161
InspIRCd::Users
UserManager Users
Definition: inspircd.h:298
ClientProtocol::RFCEvents
Definition: clientprotocol.h:631
InspIRCd::s_signal
static sig_atomic_t s_signal
Definition: inspircd.h:310
ConfigTag
Definition: configreader.h:37
InspIRCd::ISupport
ISupportManager ISupport
Definition: inspircd.h:321
UserManager
Definition: usermanager.h:24
BanCacheManager
Definition: bancache.h:50
InspIRCd::XLines
XLineManager * XLines
Definition: inspircd.h:294
InspIRCd::Threads
ThreadEngine Threads
Definition: inspircd.h:255
serverstats
Definition: inspircd.h:111
InspIRCd::chanlist
chan_hash chanlist
Definition: inspircd.h:302
CullList::AddItem
void AddItem(classbase *item)
Definition: cull_list.h:38
serverstats::Accept
unsigned long Accept
Definition: inspircd.h:116
serverstats::Dns
unsigned long Dns
Definition: inspircd.h:128
User
Definition: users.h:224
InspIRCd::SNO
SnomaskManager SNO
Definition: inspircd.h:286
ClientProtocol::Message
Definition: clientprotocol.h:180
InspIRCd::stats
serverstats stats
Definition: inspircd.h:277
InspIRCd::Config
ServerConfig * Config
Definition: inspircd.h:281
XLineManager
Definition: xline.h:406
serverstats::DnsBad
unsigned long DnsBad
Definition: inspircd.h:138
ExtensionManager
Definition: extensible.h:178
InspIRCd
Definition: inspircd.h:181
InspIRCd::ConfigFileName
std::string ConfigFileName
Definition: inspircd.h:241
InspIRCd::Time
time_t Time()
Definition: inspircd.h:328
Channel::Write
void Write(ClientProtocol::Event &protoev, char status=0, const CUList &except_list=CUList())
Definition: channels.cpp:418
InspIRCd::IsChannel
std::tr1 ::function< bool(const std::string &)> IsChannel
Definition: inspircd.h:390
serverstats::Unknown
unsigned long Unknown
Definition: inspircd.h:122
serverstats::serverstats
serverstats()
Definition: inspircd.h:168
InspIRCd::GlobalCulls
CullList GlobalCulls
Definition: inspircd.h:215