]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd.cpp
Signed octets in dns forward lookup! GAH
[user/henk/code/inspircd.git] / src / inspircd.cpp
index 04bfbbabc06bb3ebd7218d7ddd9741bddaa5134b..c2b5ebf9bacd625a955d3e9d0b57d173d6be72a1 100644 (file)
@@ -51,9 +51,8 @@ using namespace std;
 #include <vector>
 #include <deque>
 #include <sched.h>
-#include "connection.h"
+#include <pthread.h>
 #include "users.h"
-#include "servers.h"
 #include "ctables.h"
 #include "globals.h"
 #include "modules.h"
@@ -69,6 +68,7 @@ using namespace std;
 #include "hashcomp.h"
 #include "socketengine.h"
 #include "socket.h"
+#include "dns.h"
 
 int LogLevel = DEFAULT;
 char ServerName[MAXBUF];
@@ -142,7 +142,7 @@ userrec* fd_ref_table[65536];
 
 int statsAccept = 0, statsRefused = 0, statsUnknown = 0, statsCollisions = 0, statsDns = 0, statsDnsGood = 0, statsDnsBad = 0, statsConnects = 0, statsSent= 0, statsRecv = 0;
 
-serverrec* me[32];
+
 
 FILE *log_file;
 
@@ -162,7 +162,7 @@ struct linger linger = { 0 };
 char MyExecutable[1024];
 int boundPortCount = 0;
 int portCount = 0, SERVERportCount = 0, ports[MAXSOCKS];
-int defaultRoute = 0;
+
 char ModPath[MAXBUF];
 
 /* prototypes */
@@ -174,7 +174,6 @@ char* Passwd(userrec *user);
 bool IsDenied(userrec *user);
 void AddWhoWas(userrec* u);
 
-std::vector<long> auth_cookies;
 std::stringstream config_f(stringstream::in | stringstream::out);
 
 std::vector<userrec*> all_opers;
@@ -205,7 +204,8 @@ const char* FindServerNamePtr(std::string servername)
                if (*a == servername)
                        return a->c_str();
        }
-       return "";
+       AddServerName(servername);
+       return FindServerNamePtr(servername);
 }
 
 void DeleteOper(userrec* user)
@@ -295,7 +295,7 @@ void ReadConfig(bool bail, userrec* user)
                        }
                        else
                        {
-                               WriteOpers("There were errors in the configuration file:",user->nick);
+                               WriteOpers("There were errors in the configuration file:");
                                while (!errstr.eof())
                                {
                                        errstr.getline(dataline,1024);
@@ -1099,7 +1099,7 @@ void kill_link(userrec *user,const char* r)
        log(DEBUG,"closing fd %lu",(unsigned long)user->fd);
 
        if (user->registered == 7) {
-               FOREACH_MOD OnUserQuit(user);
+               FOREACH_MOD OnUserQuit(user,reason);
                WriteCommonExcept(user,"QUIT :%s",reason);
        }
 
@@ -1154,7 +1154,7 @@ void kill_link_silent(userrec *user,const char* r)
        user->FlushWriteBuf();
 
        if (user->registered == 7) {
-               FOREACH_MOD OnUserQuit(user);
+               FOREACH_MOD OnUserQuit(user,reason);
                WriteCommonExcept(user,"QUIT :%s",reason);
        }
 
@@ -1182,6 +1182,15 @@ void kill_link_silent(userrec *user,const char* r)
 }
 
 
+/*void *task(void *arg)
+{
+       for (;;) { 
+               cout << (char *)arg;
+               cout.flush();
+       }
+       return NULL;
+}*/
+
 int main(int argc, char** argv)
 {
        Start();
@@ -1208,6 +1217,7 @@ int main(int argc, char** argv)
                        }
                }
        }
+
        strlcpy(MyExecutable,argv[0],MAXBUF);
        
        // initialize the lowercase mapping table
@@ -1320,6 +1330,48 @@ void AddWhoWas(userrec* u)
        }
 }
 
+void* dns_task(void* arg)
+{
+       userrec* u = (userrec*)arg;
+       log(DEBUG,"DNS thread for user %s",u->nick);
+       DNS dns1;
+       DNS dns2;
+       std::string host;
+       std::string ip;
+       if (dns1.ReverseLookup(u->ip))
+       {
+               log(DEBUG,"DNS Step 1");
+               while (!dns1.HasResult())
+               {
+                       usleep(100);
+               }
+               host = dns1.GetResult();
+               if (host != "")
+               {
+                       log(DEBUG,"DNS Step 2: '%s'",host.c_str());
+                       if (dns2.ForwardLookup(host))
+                       {
+                               while (!dns2.HasResult())
+                               {
+                                       usleep(100);
+                               }
+                               ip = dns2.GetResultIP();
+                               log(DEBUG,"DNS Step 3 '%s'(%d) '%s'(%d)",ip.c_str(),ip.length(),u->ip,strlen(u->ip));
+                               if (ip == std::string(u->ip))
+                               {
+                                       log(DEBUG,"DNS Step 4");
+                                       if (host.length() < 160)
+                                       {
+                                               log(DEBUG,"DNS Step 5");
+                                               strcpy(u->host,host.c_str());
+                                       }
+                               }
+                       }
+               }
+       }
+       u->dns_done = true;
+       return NULL;
+}
 
 /* add a client connection to the sockets list */
 void AddClient(int socket, char* host, int port, bool iscached, char* ip)
@@ -1445,6 +1497,12 @@ void AddClient(int socket, char* host, int port, bool iscached, char* ip)
        }
        fd_ref_table[socket] = clientlist[tempnick];
        engine_add_fd;
+
+       // initialize their dns lookup thread
+       //if (pthread_create(&clientlist[tempnick]->dnsthread, NULL, dns_task, (void *)clientlist[tempnick]) != 0)
+       //{
+       //      log(DEBUG,"Failed to create DNS lookup thread for user %s",clientlist[tempnick]->nick);
+       //}
 }
 
 /* shows the message of the day, and any other on-logon stuff */
@@ -1558,64 +1616,31 @@ std::string GetVersionString()
 
 void handle_version(char **parameters, int pcnt, userrec *user)
 {
-       if (!pcnt)
-       {
-               WriteServ(user->fd,"351 %s :%s",user->nick,GetVersionString().c_str());
-       }
-       else
+       WriteServ(user->fd,"351 %s :%s",user->nick,GetVersionString().c_str());
+}
+
+
+bool is_valid_cmd(const char* commandname, int pcnt, userrec * user)
+{
+       for (unsigned int i = 0; i < cmdlist.size(); i++)
        {
-               if (!strcmp(parameters[0],"*"))
+               if (!strcasecmp(cmdlist[i].command,commandname))
                {
-                       for (int j = 0; j < 32; j++)
+                       if (cmdlist[i].handler_function)
                        {
-                               if (me[j] != NULL)
+                               if ((pcnt>=cmdlist[i].min_params) && (strcasecmp(cmdlist[i].source,"<core>")))
                                {
-                                       for (unsigned int x = 0; x < me[j]->connectors.size(); x++)
+                                       if (strchr(user->modes,cmdlist[i].flags_needed))
                                        {
-                                               WriteServ(user->fd,"351 %s :Server %d:%d (%s): %s",user->nick,j,x,me[j]->connectors[x].GetServerName().c_str(),me[j]->connectors[x].GetVersionString().c_str());
+                                               return true;
                                        }
                                }
                        }
-                       return;
-               }
-               if (match(ServerName,parameters[0]))
-               {
-                       WriteServ(user->fd,"351 %s :%s",user->nick,GetVersionString().c_str());
-                       return;
-               }
-               bool displayed = false, found = false;
-                for (int j = 0; j < 32; j++)
-                {
-                        if (me[j] != NULL)
-                        {
-                                for (unsigned int x = 0; x < me[j]->connectors.size(); x++)
-                                {
-                                        if (match(me[j]->connectors[x].GetServerName().c_str(),parameters[0]))
-                                        {
-                                               found = true;
-                                               if ((me[j]->connectors[x].GetVersionString() != "") && (!displayed))
-                                               {
-                                                       displayed = true;
-                                                       WriteServ(user->fd,"351 %s :%s",user->nick,me[j]->connectors[x].GetVersionString().c_str());
-                                               }
-                                       }
-                               }
-                       }
-               }
-               if ((!displayed) && (found))
-               {
-                       WriteServ(user->fd,"402 %s %s :Server %s has no version information",user->nick,parameters[0],parameters[0]);
-                       return;
-               }
-               if (!found)
-               {
-                       WriteServ(user->fd,"402 %s %s :No such server",user->nick,parameters[0]);
                }
        }
-       return;
+       return false;
 }
 
-
 // calls a handler function for a command
 
 void call_handler(const char* commandname,char **parameters, int pcnt, userrec *user)
@@ -2209,25 +2234,6 @@ bool LoadModule(const char* filename)
 }
 
 
-bool GotServer(std::string name)
-{
-        for (int j = 0; j < 32; j++)
-        {
-                if (me[j] != NULL)
-                {
-                        for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
-                        {
-                               if (name == me[j]->connectors[k].GetServerName())
-                               {
-                                       return true;
-                               }
-                        }
-                }
-        }
-       return false;
-}
-
-
 int InspIRCd(char** argv, int argc)
 {
        struct sockaddr_in client,server;
@@ -2413,15 +2419,20 @@ int InspIRCd(char** argv, int argc)
 
                dns_poll();
 
+               unsigned int numsockets = module_sockets.size();
                for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
                {
                        InspSocket* s = (InspSocket*)*a;
-                       if (!s->Poll())
+                       if ((s) && (!s->Poll()))
                        {
-                               delete s;
+                               log(DEBUG,"Socket poll returned false, close and bail");
+                               s->Close();
                                module_sockets.erase(a);
+                               delete s;
                                break;
                        }
+                       // we gained a socket, sarper
+                       if (module_sockets.size() != numsockets) break;
                }
 
                // *FIX* Instead of closing sockets in kill_link when they receive the ERROR :blah line, we should queue