The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

inspircd.h

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *          the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #ifndef __INSPIRCD_H__
00015 #define __INSPIRCD_H__
00016 
00017 #ifndef WIN32
00018 #define DllExport
00019 #define CoreExport
00020 #define printf_c printf
00021 #else
00022 #include "inspircd_win32wrapper.h"
00024 #undef DELETE
00025 #undef ERROR
00026 #endif
00027 
00028 #ifdef __GNUC__
00029 #define CUSTOM_PRINTF(STRING, FIRST) __attribute__((format(printf, STRING, FIRST)))
00030 #else
00031 #define CUSTOM_PRINTF(STRING, FIRST)
00032 #endif
00033 
00034 // Required system headers.
00035 #include <ctime>
00036 #include <cstdarg>
00037 #include <algorithm>
00038 #include <cmath>
00039 #include <cstring>
00040 #include <climits>
00041 
00042 #include <sstream>
00043 #include <string>
00044 #include <vector>
00045 #include <deque>
00046 #include <map>
00047 #include <bitset>
00048 
00049 
00051 typedef std::vector<std::pair<std::string, std::string> > FailedPortList;
00052 
00055 typedef std::deque< std::string > file_cache;
00056 
00059 typedef std::pair< std::string, std::string > KeyVal;
00060 
00063 typedef std::vector< KeyVal > KeyValList;
00064 
00067 typedef std::multimap< std::string, KeyValList > ConfigDataHash;
00068 
00069 #include "inspircd_config.h"
00070 #include "numerics.h"
00071 #include "uid.h"
00072 #include "users.h"
00073 #include "channels.h"
00074 #include "timer.h"
00075 #include "hashcomp.h"
00076 #include "typedefs.h"
00077 #include "logger.h"
00078 #include "usermanager.h"
00079 #include "socket.h"
00080 #include "ctables.h"
00081 #include "command_parse.h"
00082 #include "mode.h"
00083 #include "socketengine.h"
00084 #include "snomasks.h"
00085 #include "cull_list.h"
00086 #include "filelogger.h"
00087 #include "caller.h"
00088 #include "modules.h"
00089 #include "configreader.h"
00090 #include "inspstring.h"
00091 #include "protocol.h"
00092 
00093 #ifndef PATH_MAX
00094 #warning Potentially broken system, PATH_MAX undefined
00095 #define PATH_MAX 4096
00096 #endif
00097 
00101 #define MAXPARAMETERS 127
00102 
00105 #define ERROR -1
00106 
00110 #define ETIREDHAMSTERS EAGAIN
00111 
00114 template<typename T> inline std::string ConvNumeric(const T &in)
00115 {
00116         if (in == 0) return "0";
00117         char res[MAXBUF];
00118         char* out = res;
00119         T quotient = in;
00120         while (quotient) {
00121                 *out = "0123456789"[ std::abs( (long)quotient % 10 ) ];
00122                 ++out;
00123                 quotient /= 10;
00124         }
00125         if (in < 0)
00126                 *out++ = '-';
00127         *out = 0;
00128         std::reverse(res,out);
00129         return res;
00130 }
00131 
00134 inline std::string ConvToStr(const int in)
00135 {
00136         return ConvNumeric(in);
00137 }
00138 
00141 inline std::string ConvToStr(const long in)
00142 {
00143         return ConvNumeric(in);
00144 }
00145 
00148 inline std::string ConvToStr(const char* in)
00149 {
00150         return in;
00151 }
00152 
00155 inline std::string ConvToStr(const bool in)
00156 {
00157         return (in ? "1" : "0");
00158 }
00159 
00162 inline std::string ConvToStr(char in)
00163 {
00164         return std::string(in,1);
00165 }
00166 
00169 template <class T> inline std::string ConvToStr(const T &in)
00170 {
00171         std::stringstream tmp;
00172         if (!(tmp << in)) return std::string();
00173         return tmp.str();
00174 }
00175 
00179 template<typename T> inline long ConvToInt(const T &in)
00180 {
00181         std::stringstream tmp;
00182         if (!(tmp << in)) return 0;
00183         return atoi(tmp.str().c_str());
00184 }
00185 
00192 template<typename T, typename V, typename R> inline char* itoa(const T &in, V *res, R base)
00193 {
00194         if (base < 2 || base > 16) { *res = 0; return res; }
00195         char* out = res;
00196         int quotient = in;
00197         while (quotient) {
00198                 *out = "0123456789abcdef"[ std::abs( quotient % base ) ];
00199                 ++out;
00200                 quotient /= base;
00201         }
00202         if ( in < 0 && base == 10) *out++ = '-';
00203         std::reverse( res, out );
00204         *out = 0;
00205         return res;
00206 }
00207 
00212 class serverstats : public classbase
00213 {
00214   public:
00217         unsigned long statsAccept;
00220         unsigned long statsRefused;
00223         unsigned long statsUnknown;
00226         unsigned long statsCollisions;
00229         unsigned long statsDns;
00234         unsigned long statsDnsGood;
00239         unsigned long statsDnsBad;
00242         unsigned long statsConnects;
00245         double statsSent;
00248         double statsRecv;
00251         timeval LastCPU;
00254         timeval LastSampled;
00257         serverstats()
00258                 : statsAccept(0), statsRefused(0), statsUnknown(0), statsCollisions(0), statsDns(0),
00259                 statsDnsGood(0), statsDnsBad(0), statsConnects(0), statsSent(0.0), statsRecv(0.0)
00260         {
00261         }
00262 };
00263 
00264 class InspIRCd;
00265 
00266 DEFINE_HANDLER1(ProcessUserHandler, void, User*);
00267 DEFINE_HANDLER2(IsNickHandler, bool, const char*, size_t);
00268 DEFINE_HANDLER1(IsIdentHandler, bool, const char*);
00269 DEFINE_HANDLER1(FindDescriptorHandler, User*, int);
00270 DEFINE_HANDLER1(FloodQuitUserHandler, void, User*);
00271 DEFINE_HANDLER2(IsChannelHandler, bool, const char*, size_t);
00272 DEFINE_HANDLER1(IsSIDHandler, bool, const std::string&);
00273 DEFINE_HANDLER1(RehashHandler, void, const std::string&);
00274 
00275 /* Forward declaration - required */
00276 class XLineManager;
00277 class BanCacheManager;
00278 
00282 class CoreExport ConfigReaderThread : public Thread
00283 {
00284         InspIRCd* ServerInstance;
00285         bool do_bail;
00286         std::string TheUserUID;
00287  public:
00288         ConfigReaderThread(InspIRCd* Instance, bool bail, const std::string &useruid) : Thread(), ServerInstance(Instance), do_bail(bail), TheUserUID(useruid)
00289         {
00290         }
00291 
00292         virtual ~ConfigReaderThread()
00293         {
00294         }
00295 
00296         void Run();
00297 };
00298 
00307 class CoreExport InspIRCd : public classbase
00308 {
00309  private:
00312         char current_uid[UUID_LENGTH];
00313 
00316         void SetSignals();
00317 
00321         bool DaemonSeed();
00322 
00326         void DoSocketTimeouts(time_t TIME);
00327 
00330         void IncrementUID(int pos);
00331 
00334         void DoBackgroundUserStuff();
00335 
00340         bool AllModulesReportReady(User* user);
00341 
00344         char LogFileName[MAXBUF];
00345 
00348         time_t TIME;
00349 
00352         time_t OLDTIME;
00353 
00357         char ReadBuffer[65535];
00358 
00361         irc::sockets::insp_sockaddr client, server;
00362 
00365         socklen_t length;
00366 
00367 #ifdef WIN32
00368         IPC* WindowsIPC;
00369 #endif
00370 
00371  public:
00372 
00375         CullList GlobalCulls;
00376 
00377         /**** Functors ****/
00378 
00379         ProcessUserHandler HandleProcessUser;
00380         IsNickHandler HandleIsNick;
00381         IsIdentHandler HandleIsIdent;
00382         FindDescriptorHandler HandleFindDescriptor;
00383         FloodQuitUserHandler HandleFloodQuitUser;
00384         IsChannelHandler HandleIsChannel;
00385         IsSIDHandler HandleIsSID;
00386         RehashHandler HandleRehash;
00387 
00391         std::map<BufferedSocket*,BufferedSocket*> SocketCull;
00392 
00401         User *FakeClient;
00402 
00405         std::string GetUID();
00406 
00411         User *FindUUID(const std::string &);
00412 
00417         User *FindUUID(const char *);
00418 
00421         void BuildISupport();
00422 
00425         servernamelist servernames;
00426 
00429         time_t startup_time;
00430 
00433         char ConfigFileName[MAXBUF];
00434 
00437         ModeParser* Modes;
00438 
00441         CommandParser* Parser;
00442 
00445         SocketEngine* SE;
00446 
00449         ThreadEngine* Threads;
00450 
00453         MutexFactory* Mutexes;
00454 
00457         ConfigReaderThread* ConfigThread;
00458 
00461         LogManager *Logs;
00462 
00466         ModuleManager* Modules;
00467 
00471         BanCacheManager *BanCache;
00472 
00475         serverstats* stats;
00476 
00479         ServerConfig* Config;
00480 
00484         SnomaskManager* SNO;
00485 
00488         DNS* Res;
00489 
00492         TimerManager* Timers;
00493 
00496         XLineManager* XLines;
00497 
00500         UserManager *Users;
00501 
00504         chan_hash* chanlist;
00505 
00508         int s_signal;
00509 
00512         ProtocolInterface* PI;
00513 
00519         time_t Time();
00520 
00526         caller1<void, User*> ProcessUser;
00527 
00533         int BindPorts(bool bail, int &found_ports, FailedPortList &failed_ports);
00534 
00541         bool BindSocket(int sockfd, int port, const char* addr, bool dolisten = true);
00542 
00546         void AddServerName(const std::string &servername);
00547 
00553         const char* FindServerNamePtr(const std::string &servername);
00554 
00559         bool FindServerName(const std::string &servername);
00560 
00568         std::string GetServerDescription(const char* servername);
00569 
00575         User* FindNick(const std::string &nick);
00576 
00582         User* FindNick(const char* nick);
00583 
00586         User* FindNickOnly(const char* nick);
00587 
00590         User* FindNickOnly(const std::string &nick);
00591 
00596         Channel* FindChan(const std::string &chan);
00597 
00602         Channel* FindChan(const char* chan);
00603 
00607         void CheckDie();
00608 
00612         void CheckRoot();
00613 
00619         bool OpenLog(char** argv, int argc);
00620 
00625         caller2<bool, const char*, size_t> IsChannel;
00626 
00630         caller1<bool, const std::string&> IsSID;
00631 
00634         caller1<void, const std::string&> Rehash;
00635 
00639         void SignalHandler(int signal);
00640 
00644         static void SetSignal(int signal);
00645 
00652         void Exit(int status);
00653 
00657         static void QuickExit(int status);
00658 
00662         long ChannelCount();
00663 
00667         void SendError(const std::string &s);
00668 
00673         caller2<bool, const char*, size_t> IsNick;
00674 
00679         caller1<bool, const char*> IsIdent;
00680 
00686         caller1<User*, int> FindDescriptor;
00687 
00704         bool AddResolver(Resolver* r, bool cached);
00705 
00710         void AddCommand(Command *f);
00711 
00719         void SendMode(const std::vector<std::string>& parameters, User *user);
00720 
00726         static bool Match(const std::string &str, const std::string &mask, unsigned const char *map = NULL);
00727         static bool Match(const  char *str, const char *mask, unsigned const char *map = NULL);
00728 
00735         static bool MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map = NULL);
00736         static bool MatchCIDR(const  char *str, const char *mask, unsigned const char *map = NULL);
00737 
00745         CmdResult CallCommandHandler(const std::string &commandname, const std::vector<std::string>& parameters, User* user);
00746 
00753         bool IsValidModuleCommand(const std::string &commandname, int pcnt, User* user);
00754 
00759         bool IsValidMask(const std::string &mask);
00760 
00763         void RehashServer();
00764 
00769         Channel* GetChannelIndex(long index);
00770 
00776         void DumpText(User* User, const std::string &LinePrefix, std::stringstream &TextStream);
00777 
00783         bool NickMatchesEveryone(const std::string &nick, User* user);
00784 
00790         bool IPMatchesEveryone(const std::string &ip, User* user);
00791 
00797         bool HostMatchesEveryone(const std::string &mask, User* user);
00798 
00804         long Duration(const std::string &str);
00805 
00815         int PassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype);
00816 
00822         bool ULine(const char* server);
00823 
00827         bool SilentULine(const char* server);
00828 
00832         std::string GetRevision();
00833 
00837         std::string GetVersionString();
00838 
00843         void WritePID(const std::string &filename);
00844 
00852         InspIRCd(int argc, char** argv);
00853 
00860         void SendWhoisLine(User* user, User* dest, int numeric, const std::string &text);
00861 
00869         void SendWhoisLine(User* user, User* dest, int numeric, const char* format, ...) CUSTOM_PRINTF(5, 6);
00870 
00875         caller1<void, User*> FloodQuitUser;
00876 
00883         void Restart(const std::string &reason);
00884 
00889         void Cleanup();
00890 
00895         void RehashUsersAndChans();
00896 
00900         void ResetMaxBans();
00901 
00904         std::string TimeString(time_t curtime);
00905 
00911         int Run();
00912 
00916         void BufferedSocketCull();
00917 
00920         void AddExtBanChar(char c);
00921 
00922         char* GetReadBuffer()
00923         {
00924                 return this->ReadBuffer;
00925         }
00926 };
00927 
00928 ENTRYPOINT;
00929 
00930 #endif