]> git.sesse.net Git - greproxy/blobdiff - greprotocol.cpp
Merge branch 'master' of /srv/git.sesse.net/www/greproxy
[greproxy] / greprotocol.cpp
index b826a521d7828c4e18421250f07c8a3ee8d09d9d..6f3513039ed67e00706bd2458cab0970c0cb3764 100644 (file)
@@ -1,9 +1,11 @@
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 
 #include "greprotocol.h"
-#include "reorderer.h"
 
 using namespace std;
 
@@ -22,7 +24,6 @@ struct gre_header {
 
 
 GREProtocol::GREProtocol(const in6_addr &src, const in6_addr &dst)
-       : seq(0)
 {
        memset(&dstaddr, 0, sizeof(dstaddr));
        dstaddr.sin6_family = AF_INET6;
@@ -42,9 +43,15 @@ GREProtocol::GREProtocol(const in6_addr &src, const in6_addr &dst)
                perror("bind");
                exit(1);
        }
+
+       int buf = 10 << 20;  // 20 MB.
+       if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &buf, sizeof(buf)) == -1) {
+               perror("setsockopt(SO_RCVBUF)");
+               exit(1);
+       }
 }
 
-void GREProtocol::send_packet(uint16_t proto, const string &data)
+void GREProtocol::send_packet(uint16_t proto, const string &data, uint32_t incoming_seq)
 {
        char buf[4096];
        gre_header *gre = (gre_header *)buf;
@@ -55,7 +62,7 @@ void GREProtocol::send_packet(uint16_t proto, const string &data)
        gre->protocol_type = htons(proto);
 
        char *ptr = buf + sizeof(*gre);
-       int seq_be = htonl(seq++);
+       uint32_t seq_be = htonl(incoming_seq);
        memcpy(ptr, &seq_be, sizeof(seq_be));
        ptr += sizeof(seq_be);
 
@@ -65,6 +72,8 @@ void GREProtocol::send_packet(uint16_t proto, const string &data)
                perror("sendto");
                return;
        }
+
+       ++sent_packets;
 }
 
 int GREProtocol::fd() const
@@ -72,7 +81,7 @@ int GREProtocol::fd() const
        return sock;
 }
 
-void GREProtocol::read_packet(Reorderer *sender)
+void GREProtocol::read_packet(Sender *sender)
 {
        struct sockaddr_storage addr;
        socklen_t addrlen = sizeof(addr);
@@ -107,6 +116,7 @@ void GREProtocol::read_packet(Reorderer *sender)
 
        //printf("gre packet: proto=%x\n", ntohs(gre->protocol_type));
 
-       sender->handle_packet(ntohs(gre->protocol_type), string(ptr, buf + ret), seq);
+       ++received_packets;
+       sender->send_packet(ntohs(gre->protocol_type), string(ptr, buf + ret), seq);
 }