-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvpn_client_state.cpp
More file actions
71 lines (66 loc) · 1.93 KB
/
vpn_client_state.cpp
File metadata and controls
71 lines (66 loc) · 1.93 KB
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
68
69
70
71
#include "vpn_client_state.hpp"
#include "client_manager.hpp"
#include <stdexcept>
#include <string>
#include <string.h>
#include "utils.h"
client_state::client_state(SSL* _ssl)
{
this->cur_state = this->no_ip;
this->ssl = _ssl;
client_manager::get_instance()->add_client(this);
}
client_state::~client_state()
{
client_manager::get_instance()->delete_client(this);
}
void send_ip(SSL *ssl)
{
static unsigned int ctr = 0;
in_addr dst_raw;
dst_raw.s_addr = \
(VPN_STR_IP & 0x00ffffff) | \
((VPN_STR_IP >> 24) + ctr << 24);
char *dst_ip = inet_ntoa(dst_raw);
std::string buf = "255.255.255.0 ";
buf += dst_ip;
SSL_write_with_check(ssl, (void*)buf.c_str(), buf.length());
ctr ++;
}
void client_state::recv(void *buf, size_t len)
{
switch (this->cur_state)
{
case this->no_ip:
printf("try to send client ip...\n");
send_ip(this->ssl);
this->cur_state = this->assigned_ip;
break;
case this->assigned_ip:
#define CLIENT_RET "OK!"
printf("auth code:\n");
printhex((unsigned char *)buf, len);
putchar('\n');
if (strncmp(CLIENT_RET, (char *)buf, sizeof(CLIENT_RET)) != 0)
throw std::invalid_argument((char*)buf);
this->cur_state = this->ready;
break;
case this->ready:
printf("broadcasting packets except FD(%d)\n", SSL_get_fd(this->ssl));
printf("=== data ===\n");
printhex((unsigned char*)buf, len);
printf("\n");
client_manager::get_instance()->broadcast(this, buf, len);
break;
default:
throw std::invalid_argument("encounter undefined state");
}
}
void client_state::send(void *buf, size_t len)
{
SSL_write_with_check(this->ssl, buf, len);
}
bool client_state::is_ready()
{
return this->cur_state == this->ready;
}