forked from greensea/mptunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mptunnel.h
112 lines (91 loc) · 3.29 KB
/
mptunnel.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef __MPTUNNEL_H__
#define __MPTUNNEL_H__ 1
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include <stdint.h>
#include <ev.h>
#include <locale.h>
#include <libintl.h>
#include "linklist.h"
#include "rbtree.h"
#define _(STR) gettext(STR)
// 转发时最大的包长度
#define MAX_PACKET_SIZE 8000
// 客户端检测的到桥端的连接超时时间
#define CLIENT_BRIDGE_TIMEOUT 60
#define LOG_ERROR 1
#define LOG_WARNING 2
#define LOG_NOTICE 3
#define LOG_INFO 4
#define LOG_LDEBUG 5
#define LOG_DEBUG 6
#define LOGD(FMT, ...) LOG(LOG_DEBUG, FMT, ##__VA_ARGS__)
#define LOGW(FMT, ...) LOG(LOG_WARNING, FMT, ##__VA_ARGS__)
#define LOGE(FMT, ...) LOG(LOG_ERROR, FMT, ##__VA_ARGS__)
#define LOGN(FMT, ...) LOG(LOG_NOTICE, FMT, ##__VA_ARGS__)
#define LOGI(FMT, ...) LOG(LOG_INFO, FMT, ##__VA_ARGS__)
#define LOG(level, FMT, ...) do { \
static struct tm *tmp = NULL; static time_t t1, t2 = (time_t)NULL; struct timeval tv; char timestr[128] = {0}; char ms[4] = {0}; \
if ((t1 = time(NULL)) != t2) {t2 = t1; tmp = localtime(&t2); } \
gettimeofday(&tv, NULL); \
if (tmp == NULL) { \
strncpy(timestr, "unknow", sizeof(timestr) - 1); \
} else { \
strftime(timestr, sizeof(timestr), "%F %H:%M:%S.", tmp); \
sprintf(ms, "%03d", (int)tv.tv_usec / 1000); \
strncat(timestr, ms, sizeof(timestr) - strlen(timestr) - 1); \
} \
fputc('[', stderr); fputs(timestr, stderr); fputs("]", stderr); fprintf(stderr, "(%s:%d) ", __FILE__, __LINE__); fprintf(stderr, FMT, ##__VA_ARGS__); \
} while(0);
enum packet_type {
PKT_TYPE_NONE,
PKT_TYPE_CTL,
PKT_TYPE_DATA
};
typedef struct packet_t {
uint32_t iv; /** 加密使用的 IV */
enum packet_type type;
int id;
int buflen; /** 数据长度(不包括 packet_t 自身) */
} packet_t;
typedef struct received_list_t {
struct rb_node rbnode;
long ctime;
int id;
} received_list_t;
typedef struct received_t {
int min_con_id; // 连续收到的最小包编号
int max_id; // 目前已经收到的最大包编号
time_t last_dropdead_time;
struct list_head rlist;
pthread_mutex_t rlist_mutex;
} received_t;
typedef struct connections_t {
struct list_head list;
int fd;
char *host;
int port;
ev_io *watcher;
int rc_time; // 最后一次收到服务器端数据的时间
int st_time; // 最后一次向服务器发送数据包的时间
unsigned char broken; // 连接是否已经中断并需要重连,该记号由人工进行标记
} connections_t;
packet_t* packet_make(enum packet_type type, const char *buf, int buflen, int);
int packet_free(packet_t *p);
int packet_send(int fd, char *buf, int buflen, int);
int received_is_received(received_t *r, int id);
int received_try_dropdead(received_t *r, int ttl);
int received_init(received_t *r);
int received_add(received_t *r, int id);
int received_destroy(received_t *r);
received_list_t * received_rbtree_get(struct rb_root *, int);
int received_rbtree_add(struct rb_root * , received_list_t *);
void encrypt_lfsr(char *_buf, int _size, uint32_t *);
void decrypt_lfsr(char *_buf, int _size, uint32_t *);
void mpdecrypt(char *_buf);
void mpencrypt(char *_buf, int _buflen);
uint32_t lfsr_rand(uint32_t *);
#endif