[CODE] 通过tcpserver实现对同一IP的最大连接数和连接频率的限制

1. 同一IP最大连接数的限制
使用 Balazs Nagy的 periplimit patch 实现同一IP的最大连接数的限制。
相关地址: http://js.hu/package/ucspi-tcp/
实现: ucspi-tcp-0.88-periplimit.6.patch
diff -ru ucspi-tcp-0.88-orig/Makefile ucspi-tcp-0.88/Makefile
--- ucspi-tcp-0.88-orig/Makefile Sat Mar 18 16:18:42 2000
+++ ucspi-tcp-0.88/Makefile Fri May 28 06:48:15 2004
@@ -753,7 +753,7 @@
 alloc.h buffer.h error.h strerr.h sgetopt.h subgetopt.h pathexec.h /
 socket.h uint16.h ndelay.h remoteinfo.h stralloc.h uint16.h rules.h /
 stralloc.h sig.h dns.h stralloc.h iopause.h taia.h tai.h uint64.h /
-taia.h
+taia.h uint32.h
  ./compile tcpserver.c
 
 time.a: /
diff -ru ucspi-tcp-0.88-orig/tcpserver.c ucspi-tcp-0.88/tcpserver.c
--- ucspi-tcp-0.88-orig/tcpserver.c Sat Mar 18 16:18:42 2000
+++ ucspi-tcp-0.88/tcpserver.c Thu Jul  1 08:37:03 2004
@@ -2,6 +2,7 @@
 #include <sys/param.h>
 #include <netdb.h>
 #include "uint16.h"
+#include "uint32.h"
 #include "str.h"
 #include "byte.h"
 #include "fmt.h"
@@ -242,6 +243,7 @@
 tcpserver: usage: tcpserver /
 [ -1UXpPhHrRoOdDqQv ] /
 [ -c limit ] /
+[ -s perip limit ] /
 [ -x rules.cdb ] /
 [ -B banner ] /
 [ -g gid ] /
@@ -254,8 +256,24 @@
 }
 
 unsigned long limit = 40;
+unsigned long periplimit = 0;
 unsigned long numchildren = 0;
 
+typedef struct
+{
+  pid_t pid;
+  int offset;
+} connections;
+
+typedef struct
+{
+  uint32 ipaddr;
+  unsigned long num;
+} ipchildren;
+
+connections *children;
+ipchildren *numipchildren;
+
 int flag1 = 0;
 unsigned long backlog = 20;
 unsigned long uid = 0;
@@ -278,6 +296,7 @@
 {
   int wstat;
   int pid;
+  int i;
  
   while ((pid = wait_nohang(&wstat)) > 0) {
     if (verbosity >= 2) {
@@ -286,6 +305,12 @@
       strerr_warn4("tcpserver: end ",strnum," status ",strnum2,0);
     }
     if (numchildren) --numchildren; printstatus();
+    for (i=0;i<limit;++i)
+      if (children[i].pid == pid) {
+        children[i].pid=0;
+        numipchildren[children[i].offset].num--;
+        break;
+      }
   }
 }
 
@@ -300,10 +325,11 @@
   int s;
   int t;
  
-  while ((opt = getopt(argc,argv,"dDvqQhHrR1UXx:t:u:g:l:b:B:c:pPoO")) != opteof)
+  while ((opt = getopt(argc,argv,"dDvqQhHrR1UXx:t:u:g:l:b:B:c:s:pPoO")) != opteof)
     switch(opt) {
       case 'b': scan_ulong(optarg,&backlog); break;
       case 'c': scan_ulong(optarg,&limit); break;
+      case 's': scan_ulong(optarg,&peripli
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,针对您的问题,可以通过以下步骤实现ARDUINO ESP32自动重连WIFI和TCPSERVER: 1. 首先,需要在代码中定义WIFI和TCPSERVER的参数,包括SSID、密码、服务器IP和端口号。 例如: ``` const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* serverIP = "your_SERVER_IP"; int serverPort = your_SERVER_PORT; ``` 2. 接下来,在setup()函数中,需要连接WIFI并建立TCP连接: 例如: ``` WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("WiFi connected"); WiFiClient client; while (!client.connect(serverIP, serverPort)) { delay(1000); Serial.println("Connecting to server..."); } Serial.println("Server connected"); ``` 3. 为了实现自动重连,需要在loop()函数中添加重连逻辑。首先,需要检查WIFI连接状态和TCP连接状态: 例如: ``` if (WiFi.status() != WL_CONNECTED) { Serial.println("WiFi disconnected"); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Reconnecting to WiFi..."); } Serial.println("WiFi reconnected"); } if (!client.connected()) { Serial.println("Server disconnected"); while (!client.connect(serverIP, serverPort)) { delay(1000); Serial.println("Reconnecting to server..."); } Serial.println("Server reconnected"); } ``` 4. 最后,为了避免频繁重连造成的资源浪费,可以添加一定的时间间隔限制,例如每10秒钟检查一次连接状态: 例如: ``` unsigned long lastCheckTime = 0; unsigned long checkInterval = 10000; void loop() { if (millis() - lastCheckTime > checkInterval) { lastCheckTime = millis(); // Check connection status and reconnect if necessary // ... } // Other code // ... } ``` 通过以上步骤,即可实现ARDUINO ESP32自动重连WIFI和TCPSERVER的功能。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值