-
Notifications
You must be signed in to change notification settings - Fork 8
/
GRE.h
67 lines (58 loc) · 1.67 KB
/
GRE.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <iostream>
#include <wintun.h>
#include "wintun_helper.h"
#include "easylogging++.h"
#define IPPROTO_GRE 47
#define GRE_SIZE 4
#define SEND_BUFFER 32768
extern bool reset_adapter;
extern WINTUN_SESSION_HANDLE Session;
extern WINTUN_ADAPTER_HANDLE Adapter;
class GRE
{
SOCKET s{};
sockaddr_in RecvAddr{};
char* sendBuf;
public:
GRE(const char* _server_ip, const char* _bind_ip) {
// Pre-allocate sending buffer to avoid runtime allocation
sendBuf = (char*)malloc(SEND_BUFFER);
if (sendBuf == NULL) {
LOG(FATAL) << "sendBuf: Could not allocate " << SEND_BUFFER << " bytes";
return;
}
memcpy(sendBuf, "\0\0\x08\0", GRE_SIZE); // GRE size
// GRE Server IP address
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(IPPROTO_GRE);
inet_pton(AF_INET, _server_ip, &RecvAddr.sin_addr.s_addr);
//-----------------
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
LOG(FATAL) << "WSAStartup failed: " << iResult;
throw;
}
s = socket(AF_INET, SOCK_RAW, IPPROTO_GRE);
if (s == INVALID_SOCKET) {
LOG(FATAL) << "Socket creation failed: " << WSAGetLastError();
throw;
}
struct sockaddr_in local{};
local.sin_family = AF_INET;
local.sin_port = htons(IPPROTO_GRE);
inet_pton(AF_INET, _bind_ip, &local.sin_addr.s_addr);
if (local.sin_addr.s_addr == INADDR_NONE || bind(s, (struct sockaddr*)&local, sizeof(local)) != 0)
{
LOG(FATAL) << "Binding failed: " << WSAGetLastError();
throw;
}
WSASetIPUserMtu(s, 1476);
}
[[noreturn]] void receiver(HMODULE wintun);
void sender(char* packet, unsigned int size);
};