]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd.cpp
Fixed server descriptions in /whois and /links (untested)
[user/henk/code/inspircd.git] / src / inspircd.cpp
index ca587b58d328649c0a7ff052deac6de4a2784ccc..851473bf9929a9e0fadd738e010ca641a886a1e3 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"
@@ -716,6 +718,26 @@ int c_count(userrec* u)
 
 }
 
+
+std::string GetServerDescription(char* servername)
+{
+       for (int j = 0; j < 32; j++)
+       {
+               if (me[j] != NULL)
+               {
+                       for (int k = 0; k < me[j]->connectors.size(); k++)
+                       {
+                               if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),servername))
+                               {
+                                       return me[j]->connectors[k].GetDescription();
+                               }
+                       }
+               }
+               return "";
+       }
+}
+
+
 /* return 0 or 1 depending if users u and u2 share one or more common channels
  * (used by QUIT, NICK etc which arent channel specific notices) */
 
@@ -929,6 +951,32 @@ void NetSendToAll(char* s)
        }
 }
 
+void NetSendToAllAlive(char* s)
+{
+       char buffer[MAXBUF];
+       snprintf(buffer,MAXBUF,"%s",s);
+       
+       log(DEBUG,"NetSendToAllAlive: '%s'",s);
+
+       for (int j = 0; j < 32; j++)
+       {
+               if (me[j] != NULL)
+               {
+                       for (int k = 0; k < me[j]->connectors.size(); k++)
+                       {
+                               if (me[j]->connectors[k].GetState() != STATE_DISCONNECTED)
+                               {
+                                       me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
+                               }
+                               else
+                               {
+                                       log(DEBUG,"%s is dead, not sending to it.",me[j]->connectors[k].GetServerName().c_str());
+                               }
+                       }
+               }
+       }
+}
+
 
 void NetSendToOne(char* target,char* s)
 {
@@ -952,7 +1000,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);
@@ -2819,7 +2867,6 @@ bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
        }
        else
        {
-               log(DEBUG,"*** BUG *** Non-module umode passed to process_module_umode!");
                if (faked)
                {
                        delete s2;
@@ -4650,7 +4697,7 @@ void handle_whois(char **parameters, int pcnt, userrec *user)
                        {
                                WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
                        }
-                       WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
+                       WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, GetServerDescription(dest->server));
                        if (strcmp(dest->awaymsg,""))
                        {
                                WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
@@ -5336,7 +5383,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
@@ -5345,34 +5392,100 @@ 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(serverrec* s)
+char islast(const char* s)
 {
        char c = '`';
-       /*for (int j = 0; j < 255; j++)
+       for (int j = 0; j < 32; j++)
        {
-               if (servers[j] != NULL)
-               {
-                       c = '|';
-               }
-               if (servers[j] == s)
-               {
-                       c = '`';
+               if (me[j] != NULL)
+               {
+                       for (int k = 0; k < me[j]->connectors.size(); k++)
+                       {
+                               if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
+                               {
+                                       c = '|';
+                               }
+                               if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
+                               {
+                                       c = '`';
+                               }
+                       }
                }
-       }*/
+       }
        return c;
 }
 
-long map_count(serverrec* s)
+long map_count(const char* s)
 {
        int c = 0;
        for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
        {
-               if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,s->name))) c++;
+               if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,s))) c++;
        }
        return c;
 }
@@ -5380,12 +5493,15 @@ long map_count(serverrec* s)
 void handle_links(char **parameters, int pcnt, userrec *user)
 {
        WriteServ(user->fd,"364 %s %s %s :0 %s",user->nick,ServerName,ServerName,ServerDesc);
-       for (int j = 0; j < 255; j++)
+       for (int j = 0; j < 32; j++)
        {
-       //      if (servers[j] != NULL)
-       //      {
-       //              WriteServ(user->fd,"364 %s %s %s :1 %s",user->nick,servers[j]->name,ServerName,servers[j]->description);
-       //      }
+               if (me[j] != NULL)
+               {
+                       for (int k = 0; k < me[j]->connectors.size(); k++)
+                       {
+                               WriteServ(user->fd,"364 %s %s %s :1 %s",user->nick,me[j]->connectors[k].GetServerName().c_str(),ServerName,me[j]->connectors[k].GetDescription().c_str());
+                       }
+               }
        }
        WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
 }
@@ -5397,15 +5513,18 @@ void handle_map(char **parameters, int pcnt, userrec *user)
        while (strlen(line) < 50)
                strcat(line," ");
        WriteServ(user->fd,"%s%d (%.2f%%)",line,local_count(),(float)(((float)local_count()/(float)usercnt())*100));
-       for (int j = 0; j < 255; j++)
+       for (int j = 0; j < 32; j++)
        {
-       //      if (servers[j] != NULL)
-       //      {
-       //              snprintf(line,MAXBUF,"006 %s :%c-%s",user->nick,islast(servers[j]),servers[j]->name);
-       //              while (strlen(line) < 50)
-       //                      strcat(line," ");
-       //              WriteServ(user->fd,"%s%d (%.2f%%)",line,map_count(servers[j]),(float)(((float)map_count(servers[j])/(float)usercnt())*100));
-       //      }
+               if (me[j] != NULL)
+               {
+                       for (int k = 0; k < me[j]->connectors.size(); k++)
+                       {
+                               snprintf(line,MAXBUF,"006 %s :%c-%s",user->nick,islast(me[j]->connectors[k].GetServerName().c_str()),me[j]->connectors[k].GetServerName().c_str());
+                               while (strlen(line) < 50)
+                                       strcat(line," ");
+                               WriteServ(user->fd,"%s%d (%.2f%%)",line,map_count(me[j]->connectors[k].GetServerName().c_str()),(float)(((float)map_count(me[j]->connectors[k].GetServerName().c_str())/(float)usercnt())*100));
+                       }
+               }
        }
        WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
 }
@@ -5438,7 +5557,7 @@ void handle_oper(char **parameters, int pcnt, userrec *user)
                                {
                                        /* found this oper's opertype */
                                        ConfValue("type","host",j,Hostname,&config_f);
-                                       strncpy(user->dhost,Hostname,256);
+                                       ChangeDisplayedHost(user,Hostname);
                                }
                        }
                        if (!strchr(user->modes,'o'))
@@ -5745,7 +5864,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;
@@ -5910,7 +6029,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);
@@ -6312,14 +6431,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
@@ -6377,17 +6492,43 @@ void handle_b(char token,char* params,serverrec* source,serverrec* reply, char*
 void handle_plus(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
 {
        // %s %s %d %d
+       // + test3.chatspike.net 7010 -2016508415
        char* servername = strtok(params," ");
        char* ipaddr = strtok(NULL," ");
        char* ipport = strtok(NULL," ");
        char* cookie = strtok(NULL," ");
-       log(DEBUG," ");
-       log(DEBUG," ");
-       log(DEBUG,"*** Connecting back to %s:%d",ipaddr,ipport);
-       me[defaultRoute]->MeshCookie(ipaddr,atoi(ipport),atoi(cookie),servername);
-       log(DEBUG," ");
+       log(DEBUG,"*** Connecting back to %s:%d",ipaddr,atoi(ipport));
+
+
+       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)
+{
+       char* server = strtok(params," ");
+       char* data = strtok(NULL,"\r\n");
+       log(DEBUG,"Forwarded packet '%s' to '%s'",data,server);
+       NetSendToOne(server,data);
+}
 
 void handle_J(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
 {
@@ -6441,9 +6582,165 @@ void handle_J(char token,char* params,serverrec* source,serverrec* reply, char*
        }
 }
 
+void NetSendMyRoutingTable()
+{
+       // send out a line saying what is reachable to us.
+       // E.g. if A is linked to B C and D, send out:
+       // $ A B C D
+       // if its only linked to B and D send out:
+       // $ A B D
+       // if it has no links, dont even send out the line at all.
+       char buffer[MAXBUF];
+       sprintf(buffer,"$ %s",ServerName);
+       bool sendit = false;
+       for (int i = 0; i < 32; i++)
+       {
+               if (me[i] != NULL)
+               {
+                       for (int j = 0; j < me[i]->connectors.size(); j++)
+                       {
+                               if (me[i]->connectors[j].GetState() != STATE_DISCONNECTED)
+                               {
+                                       strncat(buffer," ",MAXBUF);
+                                       strncat(buffer,me[i]->connectors[j].GetServerName().c_str(),MAXBUF);
+                                       sendit = true;
+                               }
+                       }
+               }
+       }
+       if (sendit)
+               NetSendToAll(buffer);
+}
+
+void handle_dollar(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
+{
+       log(DEBUG,"Storing routing table...");
+       char* sourceserver = strtok(params," ");
+       char* server = strtok(NULL," ");
+       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(),sourceserver))
+                               {
+                                       me[i]->connectors[j].routes.clear();
+                                       log(DEBUG,"Found entry for source server.");
+                                       while (server)
+                                       {
+                                               // store each route
+                                               me[i]->connectors[j].routes.push_back(server);
+                                               log(DEBUG,"*** Stored route: %s -> %s -> %s",ServerName,sourceserver,server);
+                                               server = strtok(NULL," ");
+                                       }
+                                       return;
+                               }
+                       }
+               }
+       }
+       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);
+       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;
+                       }
+               }
+       }
+}
+
+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)
@@ -6455,13 +6752,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
-                       snprintf(buffer,MAXBUF,"+ %s %s %d %d",udp_host,ipaddr,port,authcookie);
-                       NetSendToAllExcept(udp_host,buffer);
+                       NetSendToAll(buffer);
                break;
                // ~
                // Store authcookie
@@ -6475,6 +6768,21 @@ void process_restricted_commands(char token,char* params,serverrec* source,serve
                case '+':
                        handle_plus(token,params,source,reply,udp_host);
                break;
+               // routing table
+               case '$':
+                       handle_dollar(token,params,source,reply,udp_host);
+               break;
+               // node unreachable - we cant route to a server, sooooo we slit it off.
+               // servers can generate these for themselves for an squit.
+               case '&':
+                       handle_amp(token,params,source,reply,udp_host);
+               break;
+               // R <server> <data>
+               // redirect token, send all of <data> along to the given 
+               // server as this server has been found to still have a route to it
+               case 'R':
+                       handle_R(token,params,source,reply,udp_host);
+               break;
                // ?
                // ping
                case '?':
@@ -6581,6 +6889,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
@@ -6588,7 +6900,7 @@ void process_restricted_commands(char token,char* params,serverrec* source,serve
                        WriteOpers("Sending my netburst to %s",udp_host);
                        DoSync(source,udp_host);
                        WriteOpers("Send of netburst to %s completed",udp_host);
-               
+                       NetSendMyRoutingTable();
                break;
                // anything else
                default:
@@ -6605,14 +6917,16 @@ void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
        char* params = udp_msg + 2;
        char finalparam[1024];
        strcpy(finalparam," :xxxx");
-       if (strstr(params," :")) {
-               strncpy(finalparam,strstr(params," :"),1024);
+       if (strstr(udp_msg," :")) {
+               strncpy(finalparam,strstr(udp_msg," :"),1024);
        }
        if (token == '-') {
                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))
@@ -6629,6 +6943,9 @@ void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
                                                        if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),udp_host))
                                                        {
                                                                me[j]->connectors[k].SetServerName(servername);
+                                                               me[j]->connectors[k].SetDescription(serverdesc);
+                                                               me[j]->connectors[k].SetState(STATE_CONNECTED);
+                                                               NetSendMyRoutingTable();
                                                                return;
                                                        }
                                                }
@@ -6641,22 +6958,26 @@ void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
                                return;
                        }
                }
+               // bad cookie, bad bad! go sit in the corner!
                WriteOpers("Bad cookie from %s!",servername);
                return;
        }
        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].SetDescription(serverdesc);
+                               serv->connectors[j].SetServerPort(atoi(myport));
                        }
                }
                
@@ -6684,6 +7005,15 @@ void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
                                // send a 'diminutive' server message back...
                                snprintf(response,10240,"s %s %s :%s",ServerName,Link_SendPass,ServerDesc);
                                serv->SendPacket(response,servername);
+
+                               for (int t = 0; t < serv->connectors.size(); t++)
+                               {
+                                       if (!strcasecmp(serv->connectors[t].GetServerName().c_str(),servername))
+                                       {
+                                               serv->connectors[t].SetState(STATE_CONNECTED);
+                                       }
+                               }
+               
                                return;
                        }
                }
@@ -6705,9 +7035,6 @@ void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
                // to an 'S' command. If we didn't recently send an 'S' to this server, theyre trying to spoof
                // a connect, so put out an oper alert!
                
-               
-               
-               
                // for now, just accept all, we'll fix that later.
                WriteOpers("%s accepted our link credentials ",servername);
                
@@ -6734,7 +7061,7 @@ void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
                                // at this point we must begin key exchange and insert this
                                // server into our 'active' table.
                                for (int j = 0; j < 32; j++)
-                               {
+                               {
                                        if (me[j] != NULL)
                                        {
                                                for (int k = 0; k < me[j]->connectors.size(); k++)
@@ -6742,9 +7069,12 @@ void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
                                                        if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),udp_host))
                                                        {
                                                                char buffer[MAXBUF];
+                                                               me[j]->connectors[k].SetDescription(serverdesc);
+                                                               me[j]->connectors[k].SetState(STATE_CONNECTED);
                                                                sprintf(buffer,"X 0");
                                                                serv->SendPacket(buffer,udp_host);
                                                                DoSync(me[j],udp_host);
+                                                               NetSendMyRoutingTable();
                                                                return;
                                                        }
                                                }
@@ -6783,12 +7113,12 @@ void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
                                        log(DEBUG,"Servers are: '%s' '%s'",udp_host,me[j]->connectors[x].GetServerName().c_str());
                                        if (!strcasecmp(me[j]->connectors[x].GetServerName().c_str(),udp_host))
                                        {
-                                               log(DEBUG,"match! process restricted stuff here");
-                                               // found a valid ircd_connector.
-                                               // TODO: Fix this so it only lets servers in that are in the 
-                                               // STATE_CONNECTED state!!!
-                                               process_restricted_commands(token,params,me[j],serv,udp_host,me[j]->connectors[x].GetServerIP(),me[j]->connectors[x].GetServerPort());
-                                               return;
+                                               if (me[j]->connectors[x].GetState() == STATE_CONNECTED)
+                                               {
+                                                       // found a valid ircd_connector.
+                                                       process_restricted_commands(token,params,me[j],serv,udp_host,me[j]->connectors[x].GetServerIP(),me[j]->connectors[x].GetServerPort());
+                                                       return;
+                                               }
                                        }
                                }
                        }
@@ -6980,6 +7310,9 @@ int InspIRCd(void)
        /* main loop, this never returns */
        for (;;)
        {
+#ifdef _POSIX_PRIORITY_SCHEDULING
+               sched_yield();
+#endif
 
                fd_set sfd;
                timeval tval;
@@ -6989,7 +7322,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>5000)
+               if (reap_counter>300)
                {
                        if (fd_reap.size() > 0)
                        {
@@ -7003,6 +7336,7 @@ int InspIRCd(void)
                        fd_reap.clear();
                        reap_counter=0;
                }
+               reap_counter++;
 
                fd_set serverfds;
                FD_ZERO(&serverfds);
@@ -7023,11 +7357,17 @@ int InspIRCd(void)
                        {
                                if (FD_ISSET (me[x]->fd, &serverfds))
                                {
-                                       char remotehost[MAXBUF];
+                                       char remotehost[MAXBUF],resolved[MAXBUF];
+                                       length = sizeof (client);
                                        incomingSockfd = accept (me[x]->fd, (sockaddr *) &client, &length);
-                                       strncpy (remotehost,(char *) inet_ntoa (client.sin_addr),MAXBUF);
+                                       strncpy(remotehost,(char *)inet_ntoa(client.sin_addr),MAXBUF);
+                                       if(CleanAndResolve(resolved, remotehost) != TRUE)
+                                       {
+                                               strncpy(resolved,remotehost,MAXBUF);
+                                       }
                                        // add to this connections ircd_connector vector
-                                       me[x]->AddIncoming(incomingSockfd,remotehost,ntohs(client.sin_port));
+                                       // *FIX* - we need the LOCAL port not the remote port in &client!
+                                       me[x]->AddIncoming(incomingSockfd,resolved,me[x]->port);
                                }
                        }
                }
@@ -7043,7 +7383,7 @@ int InspIRCd(void)
                                        char udp_msg[MAXBUF];
                                        strncpy(udp_msg,msgs[ctr].c_str(),MAXBUF);
                                        if (strlen(udp_msg)<1)
-                                       {
+                                               {
                                                log(DEBUG,"Invalid string from %s [route%d]",udp_host,x);
                                                break;
                                        }
@@ -7130,6 +7470,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)))
                                {
@@ -7139,7 +7484,8 @@ int InspIRCd(void)
                                        
                                        if (result)
                                        {
-                                               log(DEBUG,"Read %d characters from socket",result);
+                                               if (result > 0)
+                                                       log(DEBUG,"Read %d characters from socket",result);
                                                userrec* current = count2a->second;
                                                int currfd = current->fd;
                                                char* l = strtok(data,"\n");
@@ -7238,7 +7584,6 @@ int InspIRCd(void)
                FD_SET (openSockfd[count], &selectFds);
        }
 
-       reap_counter++;
        tv.tv_usec = 1;
        selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
 
@@ -7250,6 +7595,7 @@ int InspIRCd(void)
                {
                        if (FD_ISSET (openSockfd[count], &selectFds))
                        {
+                               length = sizeof (client);
                                incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
                              
                                address_cache::iterator iter = IP.find(client.sin_addr);