]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd.cpp
Minor code tweaks, tidyups
[user/henk/code/inspircd.git] / src / inspircd.cpp
index 70a4735fd7cc0f353a6bd016507de131ba1ee822..874f9b9ff9f3697bfa137b8e7b9f1bf3e46920a0 100644 (file)
@@ -41,6 +41,8 @@ using namespace std;
 #include <errno.h>
 #include <deque>
 #include <errno.h>
+#include <unistd.h>
+#include <sched.h>
 #include "connection.h"
 #include "users.h"
 #include "servers.h"
@@ -948,7 +950,7 @@ void NetSendToAllAlive(char* s)
                                }
                                else
                                {
-                                       log(DEBUG,"%s is dead, not sending to it.",me[j]->connectors[k].GetServerName.c_str());
+                                       log(DEBUG,"%s is dead, not sending to it.",me[j]->connectors[k].GetServerName().c_str());
                                }
                        }
                }
@@ -978,7 +980,7 @@ void NetSendToOne(char* target,char* s)
        }
 }
 
-void NetSendToAllExcept(char* target,char* s)
+void NetSendToAllExcept(const char* target,char* s)
 {
        char buffer[MAXBUF];
        snprintf(buffer,MAXBUF,"%s",s);
@@ -5361,7 +5363,7 @@ void handle_connect(char **parameters, int pcnt, userrec *user)
 
        if (me[defaultRoute])
        {
-               me[defaultRoute]->BeginLink(Link_IPAddr,LinkPort,Link_Pass,Link_ServerName);
+               me[defaultRoute]->BeginLink(Link_IPAddr,LinkPort,Link_Pass,Link_ServerName,me[defaultRoute]->port);
                return;
        }
        else
@@ -5370,9 +5372,69 @@ void handle_connect(char **parameters, int pcnt, userrec *user)
        }
 }
 
+void DoSplitEveryone()
+{
+       bool go_again = true;
+       while (go_again)
+       {
+               go_again = false;
+               for (int i = 0; i < 32; i++)
+               {
+                       if (me[i] != NULL)
+                       {
+                               for (vector<ircd_connector>::iterator j = me[i]->connectors.begin(); j != me[i]->connectors.end(); j++)
+                               {
+                                       if (strcasecmp(j->GetServerName().c_str(),ServerName))
+                                       {
+                                               j->routes.clear();
+                                               j->CloseConnection();
+                                               me[i]->connectors.erase(j);
+                                               go_again = true;
+                                               break;
+                                       }
+                               }
+                       }
+               }
+       }
+       log(DEBUG,"Removed server. Will remove clients...");
+       // iterate through the userlist and remove all users on this server.
+       // because we're dealing with a mesh, we dont have to deal with anything
+       // "down-route" from this server (nice huh)
+       go_again = true;
+       char reason[MAXBUF];
+       while (go_again)
+       {
+               go_again = false;
+               for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
+               {
+                       if (strcasecmp(u->second->server,ServerName))
+                       {
+                               snprintf(reason,MAXBUF,"%s %s",ServerName,u->second->server);
+                               kill_link(u->second,reason);
+                               go_again = true;
+                               break;
+                       }
+               }
+       }
+}
+
+
+
 void handle_squit(char **parameters, int pcnt, userrec *user)
 {
        // send out an squit across the mesh and then clear the server list (for local squit)
+       if (!pcnt)
+       {
+               WriteOpers("SQUIT command issued by %s",user->nick);
+               char buffer[MAXBUF];
+               snprintf(buffer,MAXBUF,"& %s",ServerName);
+               NetSendToAll(buffer);
+               DoSplitEveryone();
+       }
+       else
+       {
+               WriteServ(user->fd,"NOTICE :*** Remote SQUIT not supported yet.");
+       }
 }
 
 char islast(const char* s)
@@ -5782,7 +5844,7 @@ void process_command(userrec *user, char* cmd)
                {
                        if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
                        {
-                               if (strchr("@!\"$%^&*(){}[]_-=+;:'#~,.<>/?\\|`",command[x]))
+                               if (!strchr("@!\"$%^&*(){}[]_-=+;:'#~,.<>/?\\|`",command[x]))
                                {
                                        kill_link(user,"Protocol violation (3)");
                                        return;
@@ -5947,7 +6009,7 @@ void SetupCommandTable(void)
        createcommand("TRACE",handle_trace,'o',0);
        createcommand("WHOWAS",handle_whowas,0,1);
        createcommand("CONNECT",handle_connect,'o',1);
-       createcommand("SQUIT",handle_squit,'o',1);
+       createcommand("SQUIT",handle_squit,'o',0);
        createcommand("MODULES",handle_modules,'o',0);
        createcommand("LINKS",handle_links,0,0);
        createcommand("MAP",handle_map,0,0);
@@ -6349,14 +6411,10 @@ void handle_N(char token,char* params,serverrec* source,serverrec* reply, char*
        {
                // nick collision
                WriteOpers("Nickname collision: %s@%s != %s@%s",nick,server,iter->second->nick,iter->second->server);
-               if (TS >= iter->second->age)
-               {
-                       char str[MAXBUF];
-                       snprintf(str,MAXBUF,"Killed (Nick Collision (%s@%s < %s@%s))",nick,server,iter->second->nick,iter->second->server);
-                       WriteServ(iter->second->fd, "KILL %s :%s",iter->second->nick,str);
-                       // client on remote server is older than the local user, kill the local user
-                       kill_link(iter->second,str);
-               }
+               char str[MAXBUF];
+               snprintf(str,MAXBUF,"Killed (Nick Collision (%s@%s < %s@%s))",nick,server,iter->second->nick,iter->second->server);
+               WriteServ(iter->second->fd, "KILL %s :%s",iter->second->nick,str);
+               kill_link(iter->second,str);
        }
        clientlist[nick] = new userrec();
        // remote users have an fd of -1. This is so that our Write abstraction
@@ -6419,11 +6477,29 @@ void handle_plus(char token,char* params,serverrec* source,serverrec* reply, cha
        char* ipaddr = strtok(NULL," ");
        char* ipport = strtok(NULL," ");
        char* cookie = strtok(NULL," ");
-       log(DEBUG," ");
-       log(DEBUG," ");
        log(DEBUG,"*** Connecting back to %s:%d",ipaddr,atoi(ipport));
-       me[defaultRoute]->MeshCookie(ipaddr,atoi(ipport),atoi(cookie),servername);
-       log(DEBUG," ");
+
+
+       bool conn_already = false;
+       for (int i = 0; i < 32; i++)
+       {
+               if (me[i] != NULL)
+               {
+                       for (int j = 0; j < me[i]->connectors.size(); j++)
+                       {
+                               if (!strcasecmp(me[i]->connectors[j].GetServerName().c_str(),servername))
+                               {
+                                       if (me[i]->connectors[j].GetServerPort() == atoi(ipport))
+                                       {
+                                               log(DEBUG,"Already got a connection to %s:%d, ignoring +",ipaddr,atoi(ipport));
+                                               conn_already = true;
+                                       }
+                               }
+                       }
+               }
+       }
+       if (!conn_already)
+               me[defaultRoute]->MeshCookie(ipaddr,atoi(ipport),atoi(cookie),servername);
 }
 
 void handle_R(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
@@ -6546,6 +6622,53 @@ void handle_dollar(char token,char* params,serverrec* source,serverrec* reply, c
        log(DEBUG,"Warning! routing table received from nonexistent server!");
 }
 
+
+void DoSplit(const char* params)
+{
+       bool go_again = true;
+       while (go_again)
+       {
+               go_again = false;
+               for (int i = 0; i < 32; i++)
+               {
+                       if (me[i] != NULL)
+                       {
+                               for (vector<ircd_connector>::iterator j = me[i]->connectors.begin(); j != me[i]->connectors.end(); j++)
+                               {
+                                       if (!strcasecmp(j->GetServerName().c_str(),params))
+                                       {
+                                               j->routes.clear();
+                                               j->CloseConnection();
+                                               me[i]->connectors.erase(j);
+                                               go_again = true;
+                                               break;
+                                       }
+                               }
+                       }
+               }
+       }
+       log(DEBUG,"Removed server. Will remove clients...");
+       // iterate through the userlist and remove all users on this server.
+       // because we're dealing with a mesh, we dont have to deal with anything
+       // "down-route" from this server (nice huh)
+       go_again = true;
+       char reason[MAXBUF];
+       snprintf(reason,MAXBUF,"%s %s",ServerName,params);
+       while (go_again)
+       {
+               go_again = false;
+               for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
+               {
+                       if (!strcasecmp(u->second->server,params))
+                       {
+                               kill_link(u->second,reason);
+                               go_again = true;
+                               break;
+                       }
+               }
+       }
+}
+
 void handle_amp(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
 {
        log(DEBUG,"Netsplit! %s split from mesh, removing!",params);
@@ -6593,10 +6716,11 @@ void handle_amp(char token,char* params,serverrec* source,serverrec* reply, char
        }
 }
 
+long authcookie;
+
 
 void process_restricted_commands(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,char* ipaddr,int port)
 {
-       long authcookie = rand()*rand();
        char buffer[MAXBUF];
 
        switch(token)
@@ -6608,14 +6732,9 @@ void process_restricted_commands(char token,char* params,serverrec* source,serve
                        WriteOpers("Server %s is starting netburst.",udp_host);
                        // now broadcast this new servers address out to all servers that are linked to us,
                        // except the newcomer. They'll all attempt to connect back to it.
-
-                       // give the server its authcookie.
+                       authcookie = rand()*rand();
                        snprintf(buffer,MAXBUF,"~ %d",authcookie);
-                       source->SendPacket(buffer,udp_host);
-                       // tell all the other servers to use this authcookie to connect back again
-                       // got '+ test3.chatspike.net 7010 -2016508415' from test.chatspike.net
-                       snprintf(buffer,MAXBUF,"+ %s %s %d %d",udp_host,ipaddr,port,authcookie);
-                       NetSendToAllExcept(udp_host,buffer);
+                       NetSendToAll(buffer);
                break;
                // ~
                // Store authcookie
@@ -6750,6 +6869,10 @@ void process_restricted_commands(char token,char* params,serverrec* source,serve
                        WriteOpers("Server %s has completed netburst. (%d secs)",udp_host,time(NULL)-nb_start);
                        handle_F(token,params,source,reply,udp_host);
                        nb_start = 0;
+                       // tell all the other servers to use this authcookie to connect back again
+                       // got '+ test3.chatspike.net 7010 -2016508415' from test.chatspike.net
+                       snprintf(buffer,MAXBUF,"+ %s %s %d %d",udp_host,ipaddr,port,authcookie);
+                       NetSendToAllExcept(udp_host,buffer);
                break;
                // X <reserved>
                // Send netburst now
@@ -6781,7 +6904,9 @@ void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
                char* cookie = strtok(params," ");
                char* servername = strtok(NULL," ");
                char* serverdesc = finalparam+2;
+
                WriteOpers("AuthCookie CONNECT from %s (%s)",servername,udp_host);
+
                for (int u = 0; u < auth_cookies.size(); u++)
                {
                        if (auth_cookies[u] == atoi(cookie))
@@ -6817,17 +6942,19 @@ void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
        }
        else
        if (token == 'S') {
-               // S test.chatspike.net password :ChatSpike InspIRCd test server
+               // S test.chatspike.net password portn :ChatSpike InspIRCd test server
                char* servername = strtok(params," ");
                char* password = strtok(NULL," ");
+               char* myport = strtok(NULL," ");
                char* serverdesc = finalparam+2;
-               WriteOpers("CONNECT from %s (%s)",servername,udp_host);
+               WriteOpers("CONNECT from %s (%s) (their port: %d)",servername,udp_host,atoi(myport));
                
                for (int j = 0; j < serv->connectors.size(); j++)
                {
                        if (!strcasecmp(serv->connectors[j].GetServerName().c_str(),udp_host))
                        {
                                serv->connectors[j].SetServerName(servername);
+                               serv->connectors[j].SetServerPort(atoi(myport));
                        }
                }
                
@@ -7152,6 +7279,9 @@ int InspIRCd(void)
        /* main loop, this never returns */
        for (;;)
        {
+#ifdef _POSIX_PRIORITY_SCHEDULING
+               sched_yield();
+#endif
 
                fd_set sfd;
                timeval tval;
@@ -7161,7 +7291,7 @@ int InspIRCd(void)
 
                // *FIX* Instead of closing sockets in kill_link when they receive the ERROR :blah line, we should queue
                // them in a list, then reap the list every second or so.
-               if (reap_counter>2500)
+               if (reap_counter>300)
                {
                        if (fd_reap.size() > 0)
                        {
@@ -7205,7 +7335,8 @@ int InspIRCd(void)
                                                strncpy(resolved,remotehost,MAXBUF);
                                        }
                                        // add to this connections ircd_connector vector
-                                       me[x]->AddIncoming(incomingSockfd,resolved,ntohs(client.sin_port));
+                                       // *FIX* - we need the LOCAL port not the remote port in &client!
+                                       me[x]->AddIncoming(incomingSockfd,resolved,me[x]->port);
                                }
                        }
                }
@@ -7308,6 +7439,11 @@ int InspIRCd(void)
                        //if (selectResult2 > 0)
                        for (user_hash::iterator count2a = xcount; count2a != endingiter; count2a++)
                        {
+
+#ifdef _POSIX_PRIORITY_SCHEDULING
+                               sched_yield();
+#endif
+
                                result = EAGAIN;
                                if ((count2a->second->fd != -1) && (FD_ISSET (count2a->second->fd, &sfd)))
                                {