]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/connection.cpp
SVSJOIN fixes
[user/henk/code/inspircd.git] / src / connection.cpp
index a36e9a1df78756f860157b1c5d41ebad7e11fc53..61c3c5bfedd130baf796dc1ac36010be7a103d28 100644 (file)
 
 using namespace std;
 
+
 extern std::vector<Module*> modules;
 extern std::vector<ircd_module*> factory;
 
 extern int MODCOUNT;
 
-packet::packet()
-{
-       srand(time(NULL));
-       id = random();
-}
 
-packet::~packet()
-{
-}
 
 connection::connection()
 {
-       key = GenKey();
        fd = 0;
 }
 
@@ -42,6 +34,8 @@ bool connection::CreateListener(char* host, int p)
        int on = 0;
        struct linger linger = { 0 };
        
+       this->port = p;
+       
        fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (fd <= 0)
        {
@@ -120,28 +114,36 @@ bool ircd_connector::SetHostAddress(char* host, int port)
        return true;
 }
 
+void ircd_connector::SetServerPort(int p)
+{
+       this->port = p;
+}
+
 bool ircd_connector::MakeOutboundConnection(char* host, int port)
 {
+       log(DEBUG,"MakeOutboundConnection: Original param: %s",host);
        hostent* hoste = gethostbyname(host);
        if (!hoste)
        {
-               WriteOpers("Failed to look up hostname for %s, using as an ip address",host);
+               log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",host);
                this->SetHostAddress(host,port);
                SetHostAndPort(host,port);
        }
        else
        {
-               WriteOpers("Found hostname for %s",host);
-               this->SetHostAddress(hoste->h_addr,port);
-               SetHostAndPort(hoste->h_addr,port);
+               struct in_addr* ia = (in_addr*)hoste->h_addr;
+               log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
+               this->SetHostAddress(inet_ntoa(*ia),port);
+               SetHostAndPort(inet_ntoa(*ia),port);
        }
 
        this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (this->fd >= 0)
        {
-               if(connect(this->fd, (sockaddr*)&addr,sizeof(addr)))
+               if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
                {
                        WriteOpers("connect() failed for %s",host);
+                       RemoveServer(this->servername.c_str());
                        return false;
                }
                int flags = fcntl(this->fd, F_GETFL, 0);
@@ -155,17 +157,27 @@ bool ircd_connector::MakeOutboundConnection(char* host, int port)
        else
        {
                WriteOpers("socket() failed!");
+               RemoveServer(this->servername.c_str());
        }
 
        return false;
 }
 
 
-bool connection::BeginLink(char* targethost, int port, char* password, char* servername)
+bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
 {
        char connect[MAXBUF];
        
        ircd_connector connector;
+       ircd_connector *cn = this->FindHost(servername);
+
+
+       if (cn)
+       {
+               WriteOpers("CONNECT aborted: Server %s already exists",servername);
+               return false;
+       }
+
        
        if (this->fd)
        {
@@ -174,7 +186,7 @@ bool connection::BeginLink(char* targethost, int port, char* password, char* ser
                        // targethost has been turned into an ip...
                        // we dont want this as the server name.
                        connector.SetServerName(servername);
-                       sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
+                       sprintf(connect,"S %s %s %d %d :%s",getservername().c_str(),password,myport,GetRevision(),getserverdesc().c_str());
                        connector.SetState(STATE_NOAUTH_OUTBOUND);
                        connector.SetHostAndPort(targethost, port);
                        this->connectors.push_back(connector);
@@ -282,7 +294,7 @@ void ircd_connector::SetServerName(std::string serv)
 
 void ircd_connector::SetDescription(std::string desc)
 {
-       this->servername = desc;
+       this->description = desc;
 }
 
 
@@ -322,6 +334,9 @@ void ircd_connector::SetDescriptor(int fd)
 
 bool connection::SendPacket(char *message, const char* host)
 {
+       if ((!message) || (!host))
+               return true;
+
        ircd_connector* cn = this->FindHost(host);
        
        if (!strchr(message,'\n'))
@@ -336,26 +351,31 @@ bool connection::SendPacket(char *message, const char* host)
                if (cn->GetState() == STATE_DISCONNECTED)
                {
                        log(DEBUG,"Main route to %s is down, seeking alternative",host);
-                       // this route is down, we must re-route the packet through an available point in the mesh.
-                       for (int k = 0; k < this->connectors.size(); k++)
+                       // fix: can only route one hop to avoid a loop
+                       if (strncat(message,"R ",2))
                        {
-                               // search for another point in the mesh which can 'reach' where we want to go
-                               for (int m = 0; m < this->connectors[k].routes.size(); m++)
+                               // this route is down, we must re-route the packet through an available point in the mesh.
+                               for (int k = 0; k < this->connectors.size(); k++)
                                {
-                                       if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
+                                       // search for another point in the mesh which can 'reach' where we want to go
+                                       for (int m = 0; m < this->connectors[k].routes.size(); m++)
                                        {
-                                               log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
-                                               char buffer[MAXBUF];
-                                               snprintf(buffer,MAXBUF,"R %s %s",host,message);
-                                               this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
-                                               return true;
+                                               if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
+                                               {
+                                                       log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
+                                                       char buffer[MAXBUF];
+                                                       snprintf(buffer,MAXBUF,"R %s %s",host,message);
+                                                       this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
+                                                       return true;
+                                               }
                                        }
                                }
                        }
                        char buffer[MAXBUF];
                        snprintf(buffer,MAXBUF,"& %s",host);
-                       NetSendToAll(buffer);
+                       NetSendToAllExcept(host,buffer);
                        log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
+                       DoSplit(host);
                        return false;
                }
 
@@ -366,7 +386,8 @@ bool connection::SendPacket(char *message, const char* host)
                        log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
                        cn->CloseConnection();
                        cn->SetState(STATE_DISCONNECTED);
-                       return false;
+                       // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
+                       return this->SendPacket(message,host);
                }
                return true;
        }