InspIRCd  2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations
inspircd.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6  * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7  * Copyright (C) 2003-2008 Craig Edwards <craigedwards@brainbox.cc>
8  * Copyright (C) 2006-2007 Oliver Lupton <oliverlupton@gmail.com>
9  * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  * Copyright (C) 2003 randomdan <???@???>
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 #ifndef INSPIRCD_H
27 #define INSPIRCD_H
28 
29 #define _FILE_OFFSET_BITS 64
30 #ifndef _LARGEFILE_SOURCE
31 #define _LARGEFILE_SOURCE
32 #endif
33 
34 #ifndef _WIN32
35 #define DllExport
36 #define CoreExport
37 #else
38 #include "inspircd_win32wrapper.h"
40 #undef ERROR
41 #endif
42 
43 #ifdef __GNUC__
44 #define CUSTOM_PRINTF(STRING, FIRST) __attribute__((format(printf, STRING, FIRST)))
45 #else
46 #define CUSTOM_PRINTF(STRING, FIRST)
47 #endif
48 
49 // Required system headers.
50 #include <csignal>
51 #include <ctime>
52 #include <cstdarg>
53 #include <algorithm>
54 #include <cmath>
55 #include <cstring>
56 #include <climits>
57 #include <cstdio>
58 #ifndef _WIN32
59 #include <unistd.h>
60 #endif
61 
62 #include <sstream>
63 #include <string>
64 #include <vector>
65 #include <list>
66 #include <deque>
67 #include <map>
68 #include <bitset>
69 #include <set>
70 #include <time.h>
71 #include "inspircd_config.h"
72 #include "inspircd_version.h"
73 #include "typedefs.h"
74 #include "consolecolors.h"
75 
76 CoreExport extern InspIRCd* ServerInstance;
77 
78 #include "caller.h"
79 #include "cull_list.h"
80 #include "extensible.h"
81 #include "numerics.h"
82 #include "uid.h"
83 #include "users.h"
84 #include "channels.h"
85 #include "timer.h"
86 #include "hashcomp.h"
87 #include "logger.h"
88 #include "usermanager.h"
89 #include "socket.h"
90 #include "ctables.h"
91 #include "command_parse.h"
92 #include "mode.h"
93 #include "socketengine.h"
94 #include "snomasks.h"
95 #include "filelogger.h"
96 #include "modules.h"
97 #include "threadengine.h"
98 #include "configreader.h"
99 #include "inspstring.h"
100 #include "protocol.h"
101 
102 #ifndef PATH_MAX
103 #warning Potentially broken system, PATH_MAX undefined
104 #define PATH_MAX 4096
105 #endif
106 
110 #define MAXPARAMETERS 127
111 
114 #define ERROR -1
115 
119 #define ETIREDHAMSTERS EAGAIN
120 
123 template<typename T> inline std::string ConvNumeric(const T &in)
124 {
125  if (in == 0) return "0";
126  char res[MAXBUF];
127  char* out = res;
128  T quotient = in;
129  while (quotient) {
130  *out = "0123456789"[ std::abs( (long)quotient % 10 ) ];
131  ++out;
132  quotient /= 10;
133  }
134  if (in < 0)
135  *out++ = '-';
136  *out = 0;
137  std::reverse(res,out);
138  return res;
139 }
140 
143 inline std::string ConvToStr(const int in)
144 {
145  return ConvNumeric(in);
146 }
147 
150 inline std::string ConvToStr(const long in)
151 {
152  return ConvNumeric(in);
153 }
154 
157 inline std::string ConvToStr(const char* in)
158 {
159  return in;
160 }
161 
164 inline std::string ConvToStr(const bool in)
165 {
166  return (in ? "1" : "0");
167 }
168 
171 inline std::string ConvToStr(char in)
172 {
173  return std::string(1, in);
174 }
175 
178 template <class T> inline std::string ConvToStr(const T &in)
179 {
180  std::stringstream tmp;
181  if (!(tmp << in)) return std::string();
182  return tmp.str();
183 }
184 
188 template<typename T> inline long ConvToInt(const T &in)
189 {
190  std::stringstream tmp;
191  if (!(tmp << in)) return 0;
192  return atol(tmp.str().c_str());
193 }
194 
200 {
201  public:
204  unsigned long statsAccept;
207  unsigned long statsRefused;
210  unsigned long statsUnknown;
213  unsigned long statsCollisions;
216  unsigned long statsDns;
221  unsigned long statsDnsGood;
226  unsigned long statsDnsBad;
229  unsigned long statsConnects;
232  unsigned long statsSent;
235  unsigned long statsRecv;
236 #ifdef _WIN32
237 
239  FILETIME LastCPU;
242  LARGE_INTEGER LastSampled;
245  LARGE_INTEGER QPFrequency;
246 #else
247 
249  timeval LastCPU;
253 #endif
254 
259  {
260  }
261 };
262 
263 DEFINE_HANDLER2(IsNickHandler, bool, const char*, size_t);
264 DEFINE_HANDLER2(GenRandomHandler, void, char*, size_t);
265 DEFINE_HANDLER1(IsIdentHandler, bool, const char*);
266 DEFINE_HANDLER1(FloodQuitUserHandler, void, User*);
267 DEFINE_HANDLER2(IsChannelHandler, bool, const char*, size_t);
268 DEFINE_HANDLER1(IsSIDHandler, bool, const std::string&);
269 DEFINE_HANDLER1(RehashHandler, void, const std::string&);
270 DEFINE_HANDLER3(OnCheckExemptionHandler, ModResult, User*, Channel*, const std::string&);
271 
272 class TestSuite;
273 
280 class CoreExport InspIRCd
281 {
282  private:
285  char current_uid[UUID_LENGTH];
286 
289  void SetSignals();
290 
294  bool DaemonSeed();
295 
299  void DoSocketTimeouts(time_t TIME);
300 
303  void IncrementUID(int pos);
304 
307  void DoBackgroundUserStuff();
308 
313  bool AllModulesReportReady(LocalUser* user);
314 
317  struct timespec TIME;
318 
322  char ReadBuffer[65535];
323 
324  public:
325 
331 
332  /**** Functors ****/
333 
334  IsNickHandler HandleIsNick;
335  IsIdentHandler HandleIsIdent;
336  FloodQuitUserHandler HandleFloodQuitUser;
337  OnCheckExemptionHandler HandleOnCheckExemption;
338  IsChannelHandler HandleIsChannel;
339  IsSIDHandler HandleIsSID;
340  RehashHandler HandleRehash;
341  GenRandomHandler HandleGenRandom;
342 
352 
355  std::string GetUID();
356 
357  static const char LogHeader[];
358 
363  User* FindUUID(const std::string &uid);
364 
369  User* FindUUID(const char *uid);
370 
373  void BuildISupport();
374 
377  time_t startup_time;
378 
381  std::string ConfigFileName;
382 
383  ExtensionManager Extensions;
384 
388 
392 
396 
400 
404 
408 
413 
418 
422 
426 
431 
435 
439 
443 
447 
450  chan_hash* chanlist;
451 
454  std::vector<ListenSocket*> ports;
455 
458  static sig_atomic_t s_signal;
459 
463 
467 
471 
477  inline time_t Time() { return TIME.tv_sec; }
479  inline long Time_ns() { return TIME.tv_nsec; }
481  void UpdateTime();
482 
488  std::string GenRandomStr(int length, bool printable = true);
492  unsigned long GenRandomInt(unsigned long max);
493 
496 
500  int BindPorts(FailedPortList &failed_ports);
501 
509  bool BindSocket(int sockfd, int port, const char* addr, bool dolisten = true);
510 
518  std::string GetServerDescription(const std::string& servername);
519 
525  User* FindNick(const std::string &nick);
526 
532  User* FindNick(const char* nick);
533 
536  User* FindNickOnly(const char* nick);
537 
540  User* FindNickOnly(const std::string &nick);
541 
546  Channel* FindChan(const std::string &chan);
547 
552  Channel* FindChan(const char* chan);
553 
557  void CheckRoot();
558 
564  bool OpenLog(char** argv, int argc);
565 
571 
576 
580 
584  void SignalHandler(int signal);
585 
589  static void SetSignal(int signal);
590 
597  void Exit(int status);
598 
602  static void QuickExit(int status);
603 
607  long ChannelCount();
608 
612  void SendError(const std::string &s);
613 
619 
625 
642  bool AddResolver(Resolver* r, bool cached);
643 
648  inline void AddCommand(Command *f)
649  {
650  Modules->AddService(*f);
651  }
652 
659  void SendMode(const std::vector<std::string>& parameters, User *user);
660 
667  void SendGlobalMode(const std::vector<std::string>& parameters, User *user);
668 
675  static bool Match(const std::string &str, const std::string &mask, unsigned const char *map = NULL);
676  static bool Match(const char *str, const char *mask, unsigned const char *map = NULL);
677 
685  static bool MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map = NULL);
686  static bool MatchCIDR(const char *str, const char *mask, unsigned const char *map = NULL);
687 
694  CmdResult CallCommandHandler(const std::string &commandname, const std::vector<std::string>& parameters, User* user);
695 
702  bool IsValidModuleCommand(const std::string &commandname, int pcnt, User* user);
703 
708  bool IsValidMask(const std::string &mask);
709 
713  static void StripColor(std::string &sentence);
714 
718  static void ProcessColors(file_cache& input);
719 
722  void RehashServer();
723 
729  bool NickMatchesEveryone(const std::string &nick, User* user);
730 
736  bool IPMatchesEveryone(const std::string &ip, User* user);
737 
743  bool HostMatchesEveryone(const std::string &mask, User* user);
744 
750  long Duration(const std::string &str);
751 
761  int PassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype);
762 
768  bool ULine(const std::string& server);
769 
773  bool SilentULine(const std::string& server);
774 
778  std::string GetVersionString(bool rawversion = false);
779 
784  void WritePID(const std::string &filename);
785 
793  InspIRCd(int argc, char** argv);
794 
801  void SendWhoisLine(User* user, User* dest, int numeric, const std::string &text);
802 
810  void SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...) CUSTOM_PRINTF(5, 6);
811 
814  void DoWhois(User* user, User* dest,unsigned long signon, unsigned long idle, const char* nick);
815 
820  caller1<void, User*> FloodQuitUser;
821 
827  caller3<ModResult, User*, Channel*, const std::string&> OnCheckExemption;
828 
835  void Restart(const std::string &reason);
836 
841  void Cleanup();
842 
847  void RehashUsersAndChans();
848 
852  void ResetMaxBans();
853 
856  std::string TimeString(time_t curtime);
857 
863  int Run();
864 
867  void AddExtBanChar(char c);
868 
869  char* GetReadBuffer()
870  {
871  return this->ReadBuffer;
872  }
873 
874  friend class TestSuite;
875 };
876 
877 ENTRYPOINT;
878 
879 template<class Cmd>
880 class CommandModule : public Module
881 {
882  Cmd cmd;
883  public:
884  CommandModule() : cmd(this)
885  {
886  }
887 
888  void init()
889  {
890  ServerInstance->Modules->AddService(cmd);
891  }
892 
894  {
895  return Version(cmd.name, VF_VENDOR|VF_CORE);
896  }
897 };
898 
899 #endif
Definition: threadengine_pthread.h:37
Definition: inspircd.h:264
Definition: inspircd.h:267
Definition: modules.h:192
Definition: dns.h:287
unsigned long statsRecv
Definition: inspircd.h:235
ServerConfig * Config
Definition: inspircd.h:425
ConfigReaderThread * ConfigThread
Definition: inspircd.h:403
Definition: inspircd.h:280
Definition: extensible.h:76
time_t Time()
Definition: inspircd.h:477
unsigned long statsConnects
Definition: inspircd.h:229
long Time_ns()
Definition: inspircd.h:479
Definition: cull_list.h:47
Definition: logger.h:105
Definition: users.h:230
SocketEngine * SE
Definition: inspircd.h:395
Definition: command_parse.h:31
unsigned long statsRefused
Definition: inspircd.h:207
FakeUser * FakeClient
Definition: inspircd.h:351
ProtocolInterface * PI
Definition: inspircd.h:462
Definition: ctables.h:102
Definition: users.h:742
serverstats * stats
Definition: inspircd.h:421
chan_hash * chanlist
Definition: inspircd.h:450
Definition: configreader.h:590
LocalIntExt NICKForced
Definition: inspircd.h:466
caller2< bool, const char *, size_t > IsNick
Definition: inspircd.h:618
Definition: cull_list.h:31
Definition: inspircd.h:263
caller1< bool, const char * > IsIdent
Definition: inspircd.h:624
Definition: inspircd.h:266
Definition: inspircd.h:269
TimerManager * Timers
Definition: inspircd.h:438
unsigned long statsDnsBad
Definition: inspircd.h:226
timespec LastSampled
Definition: inspircd.h:252
Definition: extensible.h:162
Version GetVersion()
Definition: inspircd.h:893
unsigned long statsCollisions
Definition: inspircd.h:213
void AddService(ServiceProvider &)
Definition: modules.cpp:445
LogManager * Logs
Definition: inspircd.h:407
caller1< bool, const std::string & > IsSID
Definition: inspircd.h:575
Definition: modules.h:1482
unsigned long statsDnsGood
Definition: inspircd.h:221
Definition: bancache.h:72
ThreadEngine * Threads
Definition: inspircd.h:399
Definition: mode.h:408
UserManager * Users
Definition: inspircd.h:446
XLineManager * XLines
Definition: inspircd.h:442
Definition: socketengine.h:232
unsigned long statsUnknown
Definition: inspircd.h:210
Definition: inspircd.h:199
Definition: modules.h:350
Definition: modules.h:75
caller2< void, char *, size_t > GenRandom
Definition: inspircd.h:495
Definition: usermanager.h:28
SnomaskManager * SNO
Definition: inspircd.h:430
timeval LastCPU
Definition: inspircd.h:249
ActionList AtomicActions
Definition: inspircd.h:330
unsigned long statsAccept
Definition: inspircd.h:204
LocalStringExt OperQuit
Definition: inspircd.h:470
ModuleManager * Modules
Definition: inspircd.h:412
Definition: users.h:899
CommandParser * Parser
Definition: inspircd.h:391
Definition: inspircd.h:270
BanCacheManager * BanCache
Definition: inspircd.h:417
Definition: snomasks.h:54
Definition: caller.h:171
Definition: inspircd.h:880
time_t startup_time
Definition: inspircd.h:377
Definition: xline.h:409
caller2< bool, const char *, size_t > IsChannel
Definition: inspircd.h:570
unsigned long statsDns
Definition: inspircd.h:216
std::vector< ListenSocket * > ports
Definition: inspircd.h:454
CullList GlobalCulls
Definition: inspircd.h:328
Definition: inspircd.h:268
Definition: inspircd.h:265
static sig_atomic_t s_signal
Definition: inspircd.h:458
Definition: extensible.h:170
void init()
Definition: inspircd.h:888
Definition: inspircd_win32wrapper.h:139
std::string ConfigFileName
Definition: inspircd.h:381
DNS * Res
Definition: inspircd.h:434
Definition: extensible.h:100
Definition: testsuite.h:22
Definition: protocol.h:43
ModeParser * Modes
Definition: inspircd.h:387
serverstats()
Definition: inspircd.h:256
unsigned long statsSent
Definition: inspircd.h:232
Definition: configreader.h:185
Definition: timer.h:120
void AddCommand(Command *f)
Definition: inspircd.h:648
caller1< void, const std::string & > Rehash
Definition: inspircd.h:579
Definition: channels.h:59
Definition: dns.h:164