]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/connection.cpp
Fixes
[user/henk/code/inspircd.git] / src / connection.cpp
index 33eb405ade0c02f75ce1876bc1c5c8d7076e90ca..7393d7d77f2208149a8cc20eefb3409bdb0e67e4 100644 (file)
@@ -4,6 +4,8 @@
 #include <sys/errno.h>
 #include <sys/ioctl.h>
 #include <sys/utsname.h>
+#include <errno.h>
+#include <vector>
 #include "inspircd.h"
 #include "modules.h"
 
@@ -12,6 +14,9 @@ extern std::vector<ircd_module*> factory;
 
 extern int MODCOUNT;
 
+#define STATE_CLEAR 1
+#define STATE_WAIT_FOR_ACK 2
+
 packet::packet()
 {
        srand(time(NULL));
@@ -26,6 +31,8 @@ connection::connection()
 {
        key = GenKey();
        fd = 0;
+       state = STATE_CLEAR;
+       buffer.clear();
 }
 
 
@@ -70,11 +77,13 @@ bool connection::CreateListener(char* host, int p)
 
        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));
+       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));
 
+       buffer.clear();
+       
        return true;
 }
 
@@ -86,8 +95,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;
 }
@@ -99,7 +107,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;
@@ -115,17 +123,23 @@ 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);
 
-    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: %s",sizeof(p),strerror(errno));
                return false;
        }
+
+       this->state = STATE_CLEAR;
        return true;
+       break;
 
 }
 
@@ -178,7 +192,6 @@ bool connection::SendACK(char* host, int port, int reply_id)
        {
                return false;
        }
-       return true;
 
 }
 
@@ -213,7 +226,7 @@ long connection::GenKey()
 
 // host: in dot notation a.b.c.d
 // port: host byte order
-bool connection::RecvPacket(char *message, char* host, int &prt)
+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;
@@ -225,11 +238,14 @@ bool connection::RecvPacket(char *message, char* host, int &prt)
        host_address.sin_family=AF_INET;
        host_address_size=sizeof(host_address);
 
+       //int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
        if (recvfrom(fd,&p,sizeof(p),0,(sockaddr*)&host_address,&host_address_size)<0)
        {
                return false;
        }
 
+       log(DEBUG,"connection::RecvPacket(): received packet type %d '%s' from '%s'",p.type,p.data,inet_ntoa(host_address.sin_addr));
+
        if (p.type == PT_SYN_ONLY)
        {
                strcpy(message,p.data);
@@ -244,6 +260,7 @@ bool connection::RecvPacket(char *message, char* host, int &prt)
                strcpy(message,p.data);
                strcpy(host,inet_ntoa(host_address.sin_addr));
                prt = ntohs(host_address.sin_port);
+               this->state = STATE_CLEAR;
                return false;
        }
 
@@ -251,10 +268,13 @@ bool connection::RecvPacket(char *message, char* host, int &prt)
        {
                strcpy(message,p.data);
                strcpy(host,inet_ntoa(host_address.sin_addr));
-               prt = ntohs(host_address.sin_port);
-               SendACK(host,this->port,p.id);
+               theirkey = p.key;
+               prt = ntohs(host_address.sin_port); // the port we received it on
+               SendACK(host,prt,p.id);
+               return true;
        }
 
+       log(DEBUG,"connection::RecvPacket(): Invalid packet type %d (protocol error)",p.type);
        return true;
 }