@@ -43,8 +43,6 @@ int g_loc_io_count = 0; // bytes read from local port
43
43
int g_rem_io_count = 0 ; // bytes read from remote port
44
44
45
45
static int g_terminated = 0 ;
46
- static char g_buf [1024 * 32 ];
47
-
48
46
49
47
typedef unsigned short tui16 ;
50
48
@@ -67,6 +65,67 @@ g_tcp_socket_ok(int sck)
67
65
return 0 ;
68
66
}
69
67
68
+ /*****************************************************************************/
69
+ static int
70
+ copy_sck_to_sck (int from_sck , int to_sck , int hexdump , int local )
71
+ {
72
+ char buff [1024 * 32 ];
73
+ int rv = -1 ;
74
+
75
+ int count = g_tcp_recv (from_sck , buff , sizeof (buff ), 0 );
76
+ if (count > 0 && count <= (int )sizeof (buff ))
77
+ {
78
+ rv = count ; // Assume we'll return the amount of data copied
79
+ if (local )
80
+ {
81
+ g_loc_io_count += count ;
82
+ if (hexdump )
83
+ {
84
+ LOG_HEXDUMP (LOG_LEVEL_INFO , "from local:" , buff , count );
85
+ }
86
+ }
87
+ else
88
+ {
89
+ g_rem_io_count += count ;
90
+ if (hexdump )
91
+ {
92
+ LOG_HEXDUMP (LOG_LEVEL_INFO , "from remote:" , buff , count );
93
+ }
94
+ }
95
+
96
+
97
+ LOG (LOG_LEVEL_DEBUG , "local_io_count: %d\tremote_io_count: %d" ,
98
+ g_loc_io_count , g_rem_io_count );
99
+
100
+ const char * p = buff ;
101
+ while ((count > 0 ) && (!g_terminated ))
102
+ {
103
+ int error = g_tcp_send (to_sck , p , count , 0 );
104
+
105
+ if (error > 0 && error <= count )
106
+ {
107
+ // We wrote some data
108
+ count -= error ;
109
+ p += error ;
110
+ }
111
+ else if ((error == -1 ) && g_tcp_last_error_would_block (to_sck ))
112
+ {
113
+ if (g_tcp_can_send (to_sck , 1000 ))
114
+ {
115
+ g_tcp_socket_ok (to_sck );
116
+ }
117
+ }
118
+ else
119
+ {
120
+ count = 0 ; // Terminate loop
121
+ rv = -1 ; // tell user
122
+ }
123
+ }
124
+ }
125
+
126
+ return rv ;
127
+ }
128
+
70
129
/*****************************************************************************/
71
130
static int
72
131
main_loop (char * local_port , char * remote_ip , char * remote_port , int hexdump )
@@ -76,7 +135,6 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
76
135
int con_sck = -1 ;
77
136
int sel ;
78
137
int count ;
79
- int sent ;
80
138
int error ;
81
139
int i ;
82
140
int acc_to_con = 0 ;
@@ -165,9 +223,8 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
165
223
error = 0 ;
166
224
i = 0 ;
167
225
168
- while (!(g_tcp_can_send (con_sck , 100 ) && g_tcp_socket_ok (con_sck ))
169
- && (!g_terminated )
170
- && (i < 100 ))
226
+ while (!g_terminated && i < 100 &&
227
+ !g_tcp_can_send (con_sck , 100 ))
171
228
{
172
229
g_sleep (100 );
173
230
i ++ ;
@@ -178,8 +235,7 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
178
235
LOG (LOG_LEVEL_ERROR , "timeout connecting" );
179
236
error = 1 ;
180
237
}
181
-
182
- if (g_terminated )
238
+ else if (!g_tcp_socket_ok (con_sck ))
183
239
{
184
240
error = 1 ;
185
241
}
@@ -191,7 +247,7 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
191
247
}
192
248
}
193
249
194
- while (( !g_terminated ) && ( error == 0 ) )
250
+ while (!g_terminated )
195
251
{
196
252
sel = g_tcp_select (con_sck , acc_sck );
197
253
@@ -204,87 +260,22 @@ main_loop(char *local_port, char *remote_ip, char *remote_port, int hexdump)
204
260
if (sel & 1 )
205
261
{
206
262
// can read from con_sck w/o blocking
207
- count = g_tcp_recv (con_sck , g_buf , 1024 * 16 , 0 );
208
- error = count < 1 ;
209
-
210
- if (error == 0 )
263
+ count = copy_sck_to_sck (con_sck , acc_sck , hexdump , 1 );
264
+ if (count < 0 )
211
265
{
212
- g_loc_io_count += count ;
213
- con_to_acc += count ;
214
-
215
- if (hexdump )
216
- {
217
- LOG_HEXDUMP (LOG_LEVEL_INFO , "from remove, the socket from connect" , g_buf , count );
218
- }
219
-
220
- LOG (LOG_LEVEL_DEBUG , "local_io_count: %d\tremote_io_count: %d" ,
221
- g_loc_io_count , g_rem_io_count );
222
- sent = 0 ;
223
-
224
- while ((sent < count ) && (error == 0 ) && (!g_terminated ))
225
- {
226
- i = g_tcp_send (acc_sck , g_buf + sent , count - sent , 0 );
227
-
228
- if ((i == -1 ) && g_tcp_last_error_would_block (acc_sck ))
229
- {
230
- if (g_tcp_can_send (acc_sck , 1000 ))
231
- {
232
- g_tcp_socket_ok (acc_sck );
233
- }
234
- }
235
- else if (i < 1 )
236
- {
237
- error = 1 ;
238
- }
239
- else
240
- {
241
- sent += i ;
242
- }
243
- }
266
+ break ;
244
267
}
268
+ con_to_acc += count ;
245
269
}
246
-
247
270
if (sel & 2 )
248
271
{
249
272
// can read from acc_sck w/o blocking
250
- count = g_tcp_recv (acc_sck , g_buf , 1024 * 16 , 0 );
251
- error = count < 1 ;
252
-
253
- if (error == 0 )
273
+ count = copy_sck_to_sck (acc_sck , con_sck , hexdump , 0 );
274
+ if (count < 0 )
254
275
{
255
- g_rem_io_count += count ;
256
- acc_to_con += count ;
257
-
258
- if (hexdump )
259
- {
260
- LOG_HEXDUMP (LOG_LEVEL_INFO , "from accepted, the socket from accept" , g_buf , count );
261
- }
262
-
263
- LOG (LOG_LEVEL_DEBUG , "local_io_count: %d\tremote_io_count: %d" ,
264
- g_loc_io_count , g_rem_io_count );
265
- sent = 0 ;
266
-
267
- while ((sent < count ) && (error == 0 ) && (!g_terminated ))
268
- {
269
- i = g_tcp_send (con_sck , g_buf + sent , count - sent , 0 );
270
-
271
- if ((i == -1 ) && g_tcp_last_error_would_block (con_sck ))
272
- {
273
- if (g_tcp_can_send (con_sck , 1000 ))
274
- {
275
- g_tcp_socket_ok (con_sck );
276
- }
277
- }
278
- else if (i < 1 || i > (count - sent ))
279
- {
280
- error = 1 ;
281
- }
282
- else
283
- {
284
- sent += i ;
285
- }
286
- }
287
- }
276
+ break ;
277
+ };
278
+ acc_to_con += count ;
288
279
}
289
280
}
290
281
0 commit comments