ソースを参照

tun2socks: use a sane lwip configuration (e.g. buffer size), minor changes

ambrop7 15 年 前
コミット
a4b40c3e97
4 ファイル変更57 行追加18 行削除
  1. 7 0
      lwip/custom/arch/cc.h
  2. 12 3
      lwip/custom/lwipopts.h
  3. 37 14
      tun2socks/tun2socks.c
  4. 1 1
      tun2socks/tun2socks.h

+ 7 - 0
lwip/custom/arch/cc.h

@@ -55,4 +55,11 @@
 #define LWIP_PLATFORM_HTONS(x) hton16(x)
 #define LWIP_PLATFORM_HTONL(x) hton32(x)
 
+// for BYTE_ORDER
+#ifdef BADVPN_USE_WINAPI
+    #include <sys/param.h>
+#else
+    #include <endian.h>
+#endif
+
 #endif

+ 12 - 3
lwip/custom/lwipopts.h

@@ -25,9 +25,7 @@
 
 #define NO_SYS 1
 #define MEM_ALIGNMENT 8
-#define MEM_SIZE 4194304
-#define MEMP_NUM_TCP_PCB 8192
-#define MEMP_NUM_TCP_PCB_LISTEN 16
+
 #define LWIP_ARP 0
 #define ARP_QUEUEING 0
 #define IP_FORWARD 0
@@ -50,4 +48,15 @@
 #define LWIP_SOCKET 0
 #define PPP_SUPPORT 0
 
+#define MEMP_NUM_TCP_PCB_LISTEN 16
+
+#define TCP_MSS 1460
+
+#define MEMP_NUM_TCP_PCB 512
+#define TCP_SND_BUF 16384
+#define TCP_SND_QUEUELEN (4 * (TCP_SND_BUF)/(TCP_MSS))
+#define MEMP_NUM_TCP_SEG (MEMP_NUM_TCP_PCB * TCP_SND_QUEUELEN)
+
+#define MEM_SIZE ((4 * MEMP_NUM_TCP_PCB * (TCP_SND_BUF)) + 4194304)
+
 #endif

+ 37 - 14
tun2socks/tun2socks.c

@@ -65,6 +65,9 @@
     BPending_Init(&sync_mark, BReactor_PendingGroup(&ss), NULL, NULL); \
     BPending_Set(&sync_mark);
 
+#define SYNC_BREAK \
+    BPending_Free(&sync_mark);
+
 #define SYNC_COMMIT \
     BReactor_Synchronize(&ss, &sync_mark); \
     BPending_Free(&sync_mark);
@@ -174,9 +177,9 @@ static void client_send_to_socks (struct tcp_client *client);
 static void client_socks_send_handler_done (struct tcp_client *client, int data_len);
 static void client_recv_confirm_job_handler (struct tcp_client *client);
 static int client_force_send_to_socks (struct tcp_client *client);
-static void client_recv_from_socks (struct tcp_client *client);
+static void client_socks_recv_initiate (struct tcp_client *client);
 static void client_socks_recv_handler_done (struct tcp_client *client, int data_len);
-static int client_send_to_client (struct tcp_client *client);
+static int client_socks_recv_send_out (struct tcp_client *client);
 static err_t client_sent_func (void *arg, struct tcp_pcb *tpcb, u16_t len);
 
 int main (int argc, char **argv)
@@ -1035,7 +1038,7 @@ void client_socks_handler (struct tcp_client *client, int event)
             }
             
             // start receiving data
-            client_recv_from_socks(client);
+            client_socks_recv_initiate(client);
         } break;
         
         case BSOCKSCLIENT_EVENT_ERROR_CLOSED: {
@@ -1140,7 +1143,7 @@ int client_force_send_to_socks (struct tcp_client *client)
     return 0;
 }
 
-void client_recv_from_socks (struct tcp_client *client)
+void client_socks_recv_initiate (struct tcp_client *client)
 {
     ASSERT(client->socks_up)
     ASSERT(!client->socks_closed)
@@ -1162,12 +1165,18 @@ void client_socks_recv_handler_done (struct tcp_client *client, int data_len)
     client->socks_recv_buf_sent = 0;
     client->socks_recv_waiting = 0;
     
-    // sent to client
-    client_send_to_client(client);
-    return;
+    // send to client
+    if (client_socks_recv_send_out(client) < 0) {
+        return;
+    }
+    
+    // continue receiving if needed
+    if (client->socks_recv_buf_used == -1 && !client->socks_closed) {
+        client_socks_recv_initiate(client);
+    }
 }
 
-int client_send_to_client (struct tcp_client *client)
+int client_socks_recv_send_out (struct tcp_client *client)
 {
     ASSERT(client->socks_up)
     ASSERT(client->socks_recv_buf_used > 0)
@@ -1205,6 +1214,13 @@ int client_send_to_client (struct tcp_client *client)
     
     // more data to queue?
     if (client->socks_recv_buf_sent < client->socks_recv_buf_used) {
+        if (client->socks_recv_tcp_pending == 0) {
+            client_log(client, BLOG_ERROR, "can't queue data, but all data was confirmed!?!");
+            tcp_abort(client->pcb);
+            return -1;
+        }
+        
+        // continue in client_sent_func
         client->socks_recv_waiting = 1;
         return 0;
     }
@@ -1214,11 +1230,6 @@ int client_send_to_client (struct tcp_client *client)
     
     // we just queued some data, so it can't have been confirmed yet
     ASSERT(client->socks_recv_tcp_pending > 0)
-    
-    if (!client->socks_closed) {
-        // receive more data
-        client_recv_from_socks(client);
-    }
 
     return 0;
 }
@@ -1242,10 +1253,22 @@ err_t client_sent_func (void *arg, struct tcp_pcb *tpcb, u16_t len)
         client->socks_recv_waiting = 0;
         
         // send more data
-        if (client_send_to_client(client) < 0) {
+        if (client_socks_recv_send_out(client) < 0) {
             return ERR_ABRT;
         }
         
+        // continue receiving if needed
+        if (client->socks_recv_buf_used == -1 && !client->socks_closed) {
+            SYNC_DECL
+            SYNC_FROMHERE
+            client_socks_recv_initiate(client);
+            DEAD_ENTER(client->dead)
+            SYNC_COMMIT
+            if (DEAD_LEAVE(client->dead)) {
+                return ERR_ABRT;
+            }
+        }
+        
         return ERR_OK;
     }
     

+ 1 - 1
tun2socks/tun2socks.h

@@ -28,7 +28,7 @@
 #define DEVICE_WRITE_BUFFER_SIZE 1
 
 // size of temporary buffer for passing data from the SOCKS server to TCP for sending
-#define CLIENT_SOCKS_RECV_BUF_SIZE 4096
+#define CLIENT_SOCKS_RECV_BUF_SIZE 8192
 
 // option to override the destination addresses to give the SOCKS server
 //#define OVERRIDE_DEST_ADDR "10.111.0.2:2000"