]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
for tcp, fix : bind port failed, try next sequence 259/head
authorweiyixuan <weiyixuan@netpas.co>
Mon, 7 May 2018 07:24:54 +0000 (15:24 +0800)
committerweiyixuan <weiyixuan@netpas.co>
Mon, 7 May 2018 07:24:54 +0000 (15:24 +0800)
packet/probe_unix.c

index d6cca0666109119ef84feb293885a6bc332588f1..985caaa713bb2d597d4eb2082632aa552d9115b6 100644 (file)
@@ -545,8 +545,9 @@ void send_probe(
 {
     char packet[PACKET_BUFFER_SIZE];
     struct probe_t *probe;
-    int packet_size;
     struct sockaddr_storage src_sockaddr;
+    int trytimes = 0;
+    int packet_size;
 
     probe = alloc_probe(net_state, param->command_token);
     if (probe == NULL) {
@@ -566,10 +567,29 @@ void send_probe(
         exit(EXIT_FAILURE);
     }
 
-    packet_size =
-        construct_packet(net_state, &probe->platform.socket,
+       while (1) {
+               if (trytimes >= (MAX_PORT - MIN_PORT + 1))
+                       break;
+                       
+               packet_size = construct_packet(net_state, &probe->platform.socket,
                          probe->sequence, packet, PACKET_BUFFER_SIZE,
                          &probe->remote_addr, &src_sockaddr, param);
+        
+        if ((packet_size < 0) &&
+               (param->protocol == IPPROTO_TCP || param->protocol == IPPROTO_SCTP) &&
+               (errno == EADDRINUSE)) {
+               
+               probe->sequence = net_state->platform.next_sequence++;
+               
+               if (net_state->platform.next_sequence > MAX_PORT) {
+                net_state->platform.next_sequence = MIN_PORT;
+            }
+            
+            trytimes++;
+        } else {
+               break;
+        }
+       }
 
     if (packet_size < 0) {
         /*