-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathSock_Tunnel.c
189 lines (180 loc) · 5.77 KB
/
Sock_Tunnel.c
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "Sock_Tunnel.h"
#define NULL_SOCK 0
#define SOCK_1_OK 1
#define SOCK_2_OK 2
#define POOL_WORKING_NOW 3
#define POOL_STOP_NOW 4
sockbind_s socks_Pool[MAX_POOL];
int live_num = 0;
int can_write_pool = True;
int tunn_init_Pool(){
int i;
for(i = 0;i<MAX_POOL;i++){
socks_Pool[i].sock_1 = -1;
socks_Pool[i].sock_2 = -1;
socks_Pool[i].flag = False;
socks_Pool[i].state = NULL_SOCK;
socks_Pool[i].usec_time = -1;
}
can_write_pool = True;
return TUNNEL_INIT_OK;
}
//int tunn_clean(int num){
// int i = num;
// socks_Pool[i].sock_1 = -1;
// socks_Pool[i].sock_2 = -1;
// socks_Pool[i].flag = False;
// socks_Pool[i].state = NULL_SOCK;
// socks_Pool[i].usec_time = -1;
// live_num --;
// //can_write_pool = True;
// return TUNNEL_CLEAN_OK;
//}
int tunn_close(int num){
int flag = 0;
if( socks_Pool[ num ].sock_1 > 0){
API_socket_close(socks_Pool[ num ].sock_1);
socks_Pool[ num ].sock_1 = -1;
flag = 1;
}
if( socks_Pool[ num ].sock_2 > 0){
API_socket_close(socks_Pool[ num ].sock_2);
socks_Pool[ num ].sock_2 = -1;
flag = 1;
}
//puts("close tunnel");
socks_Pool[ num ].usec_time = -1;
socks_Pool[ num ].state = NULL_SOCK;
socks_Pool[ num ].flag = False;
if (flag) live_num --;
printf("--> %3d <-- (close)used/unused %d/%d\n",num,live_num,MAX_POOL - live_num);
return 1;
}
MIC_THREAD_FUN_DEF(tunn_run_now,p_num) {
//void * tunn_run_now(void *p_num){
int num;
int read_size , write_size;
char buffer[TUNMAX_BUF];
int times = 0;
// data must be here, so do a normal recv
int n1=1,n2=1;
int sock1,sock2;
num = *(int*)p_num;
//printf(" ---> the num is %d \n",num);
sock1 = socks_Pool[ num ].sock_1;
sock2 = socks_Pool[ num ].sock_2;
//printf("sock1 = %d , sock2 = %d\n",sock1,sock2);
// printf("--> %3d <-- (start)used tunnel %d , unused tunnel %d\n",num,live_num,MAX_POOL - live_num);
while(times < socks_Pool[ num ].usec_time && socks_Pool[num].usec_time>=0){
n1 = API_socket_read_state(sock1,0,1);
n2 = API_socket_read_state(sock2,0,1);
if (n1 == SOCKET_INIT_ERROR || n2 == SOCKET_INIT_ERROR ){ // Sock_Error
//printf("n1 n2 <0 ???\n");
break;
}
if(n1 == SOCKET_CAN_READ_STATE
&& (read_size = API_socket_recv(sock1,buffer,TUNMAX_BUF)) > 0){
times = 0;
write_size = API_socket_send( sock2 ,buffer,read_size);
if( write_size < 0 || write_size != read_size){
// socks_Pool[num].sock_2 = -1;
break;
}
}
else if( n1 == SOCKET_MAY_CLOSE_NOW ||
(n1==SOCKET_CAN_READ_STATE && read_size <= 0) ){
//socks_Pool[num].sock_1 = -1;
// puts("close other side n1 >0");
break;
} // 对方关闭了 sock会话 }
if(n2 == SOCKET_CAN_READ_STATE
&& (read_size = API_socket_recv(sock2,buffer,TUNMAX_BUF)) > 0){
times = 0;
write_size = API_socket_send( sock1 ,buffer,read_size );
if( write_size < 0 || write_size != read_size){
//socks_Pool[num].sock_1 = -1;
break;
}
}
else if( n2 == SOCKET_MAY_CLOSE_NOW ||
(n2==SOCKET_CAN_READ_STATE && read_size <= 0) ){
//socks_Pool[num].sock_2 = -1;
// puts("close other side n2>0");
break;
} // 对方关闭了 sock会话 }
memset(buffer , 0 , TUNMAX_BUF);
read_size = write_size = 0;
n1 = n2 = -1;
MIC_USLEEP(1);
times = times +1;
}
MIC_THREAD_END();
//pthread_detach(pthread_self());
tunn_close( num );
//printf("close tunnel_now !!!\n");
return 0;
}
int tunn_get_pool_id_and_lock_it(){
int i=0;
while(live_num >= MAX_POOL-1 || !can_write_pool){
// wait Pool ready
MIC_USLEEP(1);
}
// lock pool write
can_write_pool = False;
// find a Null Pool
for (i = 0;i<MAX_POOL;i++){
if(socks_Pool[i].flag == False){
live_num ++;
socks_Pool[i].flag = True;
printf("<-- %3d --> (open)used/unused %d/%d\n",i,live_num,MAX_POOL - live_num);
return i;
}
}
return TUNNEL_GET_POOL_ID_FALSE;
}
int tunn_set_first_pool_and_lock_it(int sock,int usec){
int id ;
id = tunn_get_pool_id_and_lock_it();
if(id != TUNNEL_GET_POOL_ID_FALSE){
socks_Pool [ id ].flag = True; // replace this pool
socks_Pool [ id ].sock_1 = sock;
socks_Pool [ id ].state = SOCK_1_OK;
socks_Pool [ id ].usec_time = usec;
}
else {
id = TUNNEL_GET_FIRST_POOL_ID_FALSE;
}
// unlock write pool now
can_write_pool = True;
return id;
}
int tunn_set_second_pool_and_run_it(int num,int sock){
int *poolnum= (int *)malloc(sizeof(int));
*poolnum = num;
MIC_THREAD_HANDLE_ID thread_1;
socks_Pool[ num ] .sock_2 = sock;
socks_Pool[ num ] .state = SOCK_2_OK;
if( MIC_THREAD_CREATE(thread_1,
tunn_run_now,
poolnum) < 0 ){
// if( pthread_create( &thread_1,NULL ,
// tunn_run_now,
// (void *)poolnum)<0){
perror("could not create one way tunnel\n");
tunn_close(num);
return TUNNEL_SET_SECOND_POOL_AND_RUN_IT_FALSE;
}
// pthread_join(thread_1,NULL);
return TUNNEL_SET_SECOND_POOL_AND_RUN_IT_OK;
}
int tunn_sock_to_sock(int from_sock,int to_sock,int usec){
int num = tunn_set_first_pool_and_lock_it(from_sock,usec);
if (num != TUNNEL_GET_FIRST_POOL_ID_FALSE){
if(TUNNEL_SET_SECOND_POOL_AND_RUN_IT_FALSE !=
tunn_set_second_pool_and_run_it(num,to_sock)){
return TUNNEL_SOCK_TO_SOCK_OK;
}
}
return TUNNEL_SOCK_TO_SOCK_ERROR;
}