summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2004-04-11 18:00:44 +0000
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2004-04-11 18:00:44 +0000
commit550ca03638ce77f59f3af4b768fa077ae11a084d (patch)
tree583370ad8925d77d3a95b469bbd92bbebaae500a
parent43e8ea4f5d54146d8b5fdf49d10fd3778dbdd3c0 (diff)
Packet buffering
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@541 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r--include/connection.h11
-rw-r--r--include/servers.h1
-rw-r--r--src/connection.cpp48
3 files changed, 56 insertions, 4 deletions
diff --git a/include/connection.h b/include/connection.h
index c996e0abd..57e970431 100644
--- a/include/connection.h
+++ b/include/connection.h
@@ -13,6 +13,7 @@
#include <unistd.h>
#include <errno.h>
#include <time.h>
+#include <vector>
#ifndef __CONNECTION_H__
#define __CONNECTION_H__
@@ -35,6 +36,15 @@ class packet : public classbase
~packet();
};
+class packet_buf : public classbase
+{
+ public:
+ packet p;
+ char host[128];
+ int port;
+};
+
+
class connection : public classbase
{
@@ -58,6 +68,7 @@ class connection : public classbase
time_t nping;
char internal_addr[1024];
int internal_port;
+ std::vector<packet_buf> buffer;
connection();
bool CreateListener(char* host, int p);
diff --git a/include/servers.h b/include/servers.h
index fe6920d2a..af678f5d6 100644
--- a/include/servers.h
+++ b/include/servers.h
@@ -60,6 +60,7 @@ class serverrec : public connection
/** Destructor
*/
~serverrec();
+
};
diff --git a/src/connection.cpp b/src/connection.cpp
index 7a92afcd6..8edad9590 100644
--- a/src/connection.cpp
+++ b/src/connection.cpp
@@ -5,6 +5,7 @@
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <errno.h>
+#include <vector>
#include "inspircd.h"
#include "modules.h"
@@ -161,7 +162,7 @@ bool connection::SendPacket(char *message, char* host, int port, long ourkey)
int res = select(65535, &sfd, NULL, NULL, &tval);
cycles++;
}
- while ((recvfrom(fd,&p2,sizeof(p2),MSG_PEEK,(sockaddr*)&host_address,&host_address_size)<0) && (cycles < 10));
+ while ((recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size)<0) && (cycles < 10));
if (cycles >= 10)
{
@@ -171,21 +172,27 @@ bool connection::SendPacket(char *message, char* host, int port, long ourkey)
{
if (p2.type != PT_ACK_ONLY)
{
- log(DEFAULT,"ERROR! connection::SendPacket() received a data response and was expecting a syn!!!");
+ packet_buf pb;
+ pb.p.id = p.id;
+ pb.p.key = p.key;
+ pb.p.type = p.type;
+ strcpy(pb.host,inet_ntoa(host_address.sin_addr));
+ pb.port = ntohs(host_address.sin_port);
+ this->buffer.push_back(pb);
+
+ log(DEFAULT,"ERROR! connection::SendPacket() received a data response and was expecting an ACK!!!");
this->state = STATE_CLEAR;
return true;
}
if (p2.id != p.id)
{
- recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size);
log(DEFAULT,"ERROR! connection::SendPacket() received an ack for a packet it didnt send!");
this->state = STATE_CLEAR;
return false;
}
else
{
- recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size);
log(DEFAULT,"Successfully received ACK");
this->state = STATE_CLEAR;
return true;
@@ -297,6 +304,19 @@ bool connection::RecvPacket(char *message, char* host, int &prt, long &theirkey)
//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)
{
+ if (this->buffer.size())
+ {
+ log(DEBUG,"Fetching a buffered packet");
+
+ strcpy(message,buffer[0].p.data);
+ theirkey = buffer[0].p.key;
+ strcpy(host,buffer[0].host);
+ prt = buffer[0].port;
+
+ buffer.erase(0);
+
+ return true;
+ }
return false;
}
@@ -327,6 +347,26 @@ bool connection::RecvPacket(char *message, char* host, int &prt, long &theirkey)
theirkey = p.key;
prt = ntohs(host_address.sin_port); // the port we received it on
SendACK(host,prt,p.id);
+
+ if (this->buffer.size())
+ {
+ log(DEBUG,"Fetching a buffered packet");
+ packet_buf pb;
+ pb.p.id = p.id;
+ pb.p.key = p.key;
+ pb.p.type = p.type;
+ strcpy(pb.host,inet_ntoa(host_address.sin_addr));
+ pb.port = ntohs(host_address.sin_port);
+ this->buffer.push_back(pb);
+
+ strcpy(message,buffer[0].p.data);
+ theirkey = buffer[0].p.key;
+ strcpy(host,buffer[0].host);
+ prt = buffer[0].port;
+
+ buffer.erase(0);
+ }
+
return true;
}