]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/connection.cpp
More fixes
[user/henk/code/inspircd.git] / src / connection.cpp
index 1d79a00a13b841a7dbb9d5c2a2029bfef7af2935..3adaf0d08df612eda0db835d8c8bcbb88aec52ca 100644 (file)
@@ -5,6 +5,15 @@
 #include <sys/ioctl.h>
 #include <sys/utsname.h>
 #include "inspircd.h"
+#include "modules.h"
+
+extern std::vector<Module*> modules;
+extern std::vector<ircd_module*> factory;
+
+extern int MODCOUNT;
+
+#define STATE_CLEAR 1
+#define STATE_WAIT_FOR_ACK 2
 
 packet::packet()
 {
@@ -20,14 +29,17 @@ connection::connection()
 {
        key = GenKey();
        fd = 0;
+       state = STATE_CLEAR;
 }
 
 
-bool connection::CreateListener(char* host, int port)
+bool connection::CreateListener(char* host, int p)
 {
        sockaddr_in host_address;
        int flags;
        in_addr addy;
+       int on = 0;
+       struct linger linger = { 0 };
        
        fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (fd <= 0)
@@ -49,9 +61,9 @@ bool connection::CreateListener(char* host, int port)
                host_address.sin_addr = addy;
        }
 
-       host_address.sin_port = htons(port);
+       host_address.sin_port = htons(p);
 
-       if (bind(fd, (sockaddr*)&host_address, sizeof(host_address))<0)
+       if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
        {
                return false;
        }
@@ -60,6 +72,13 @@ bool connection::CreateListener(char* host, int port)
        flags = fcntl(fd, F_GETFL, 0);
        fcntl(fd, F_SETFL, flags | O_NONBLOCK);
 
+       this->port = p;
+
+    setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
+    linger.l_onoff = 1;
+    linger.l_linger = 0;
+    setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
+
        return true;
 }
 
@@ -71,8 +90,7 @@ bool connection::BeginLink(char* targethost, int port, char* password)
        {
                sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
                this->haspassed = false;
-               this->SendPacket(connect, targethost, port);
-               return true;
+               return this->SendPacket(connect, targethost, port, 0);
        }
        return false;
 }
@@ -84,7 +102,7 @@ void connection::TerminateLink(char* targethost)
 
 // host: in dot notation a.b.c.d
 // port: host byte order
-bool connection::SendPacket(char *message, char* host, int port)
+bool connection::SendPacket(char *message, char* host, int port, long ourkey)
 {
        sockaddr_in host_address;
        in_addr addy;
@@ -100,14 +118,72 @@ bool connection::SendPacket(char *message, char* host, int port)
 
        strcpy(p.data,message);
        p.type = PT_SYN_WITH_DATA;
-       p.key = key;
+       p.key = ourkey;
+
+
+       FOREACH_MOD OnPacketTransmit(p.data);
+
+       log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s:%d",p.data,host,port);
 
        // returns false if the packet could not be sent (e.g. target host down)
-       if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
+       if (sendto(this->fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
        {
+               log(DEBUG,"sendto() failed for Connection::SendPacket() with a packet of size %d",sizeof(p));
                return false;
        }
-       return true;
+       this->state = STATE_WAIT_FOR_ACK;
+
+
+       // host_address remains unchanged. we only want to receive from where we just sent the packet to.
+       
+       // retry the packet up to 5 times
+       for (int retries = 0; retries < 5; retries++)
+       {
+               socklen_t host_address_size;
+               host_address.sin_family=AF_INET;
+               host_address_size=sizeof(host_address);
+       
+               // wait for ack, or timeout.
+               // if reached a timeout, send again.
+               // the packet id in the ack must match that in the original packet
+               // this MUST operate in lock/step fashion!!!
+               int cycles = 0;
+               packet p2;
+               while ((recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size)<0) && (cycles < 10))
+               {
+                       fd_set sfd;
+                       timeval tval;
+                       tval.tv_usec = 100;
+                       tval.tv_sec = 0;
+                       FD_ZERO(&sfd);
+                       FD_SET(fd,&sfd);
+                       int res = select(65535, &sfd, NULL, NULL, &tval);
+                       cycles++;
+               }
+               if (cycles >= 10)
+               {
+                       log(DEFAULT,"ERROR! connection::SendPacket() waited >1000 nanosecs for an ACK. Will resend up to 5 times");
+               }
+               else
+               {
+                       if (p2.id != p.id)
+                       {
+                               log(DEFAULT,"ERROR! connection::SendPacket() received an ack for a packet it didnt send!");
+                               this->state = STATE_CLEAR;
+                               return false;
+                       }
+                       else
+                       {
+                               log(DEFAULT,"Successfully received ACK");
+                               this->state = STATE_CLEAR;
+                               return true;
+                               break;
+                       }
+               }
+       }
+       log(DEFAULT,"We never received an ack. Something fishy going on, host is dead.");
+       this->state = STATE_CLEAR;
+       return false;
 
 }
 
@@ -160,7 +236,6 @@ bool connection::SendACK(char* host, int port, int reply_id)
        {
                return false;
        }
-       return true;
 
 }
 
@@ -195,7 +270,7 @@ long connection::GenKey()
 
 // host: in dot notation a.b.c.d
 // port: host byte order
-bool connection::RecvPacket(char *message, char* host, int &port)
+bool connection::RecvPacket(char *message, char* host, int &prt, long &theirkey)
 {
        // returns false if no packet waiting for receive, e.g. EAGAIN or ECONNRESET
        sockaddr_in host_address;
@@ -212,12 +287,14 @@ bool connection::RecvPacket(char *message, char* host, int &port)
                return false;
        }
 
+       log(DEBUG,"connection::RecvPacket(): received packet type %d '%s'",p.type,p.data);
+
        if (p.type == PT_SYN_ONLY)
        {
                strcpy(message,p.data);
                strcpy(host,inet_ntoa(host_address.sin_addr));
-               port = ntohs(host_address.sin_port);
-               SendACK(host,port,p.id);
+               prt = ntohs(host_address.sin_port);
+               SendACK(host,this->port,p.id);
                return false;
        }
 
@@ -225,8 +302,8 @@ bool connection::RecvPacket(char *message, char* host, int &port)
        {
                strcpy(message,p.data);
                strcpy(host,inet_ntoa(host_address.sin_addr));
-               port = ntohs(host_address.sin_port);
-               SendACK(host,port,p.id);
+               prt = ntohs(host_address.sin_port);
+               this->state = STATE_CLEAR;
                return false;
        }
 
@@ -234,10 +311,13 @@ bool connection::RecvPacket(char *message, char* host, int &port)
        {
                strcpy(message,p.data);
                strcpy(host,inet_ntoa(host_address.sin_addr));
-               port = ntohs(host_address.sin_port);
-               SendACK(host,port,p.id);
+               theirkey = p.key;
+               prt = ntohs(host_address.sin_port);
+               SendACK(host,this->port,p.id);
+               return true;
        }
 
+       log(DEBUG,"connection::RecvPacket(): Invalid packet type %d (protocol error)",p.type);
        return true;
 }