@@ -462,8 +462,10 @@ static int start_chain(int *fd, proxy_data * pd, char *begin_mark) {
462
462
error1 :
463
463
proxychains_write_log (TP " timeout\n" );
464
464
error :
465
- if (* fd != -1 )
465
+ if (* fd != -1 ) {
466
466
close (* fd );
467
+ * fd = -1 ;
468
+ }
467
469
return SOCKET_ERROR ;
468
470
}
469
471
@@ -520,9 +522,9 @@ static unsigned int calc_alive(proxy_data * pd, unsigned int proxy_count) {
520
522
}
521
523
522
524
523
- static int chain_step (int ns , proxy_data * pfrom , proxy_data * pto ) {
525
+ static int chain_step (int * ns , proxy_data * pfrom , proxy_data * pto ) {
524
526
int retcode = -1 ;
525
- char * hostname ;
527
+ char * hostname , * errmsg = 0 ;
526
528
char hostname_buf [MSG_LEN_MAX ];
527
529
char ip_buf [INET6_ADDRSTRLEN ];
528
530
int v6 = pto -> ip .is_v6 ;
@@ -536,31 +538,34 @@ static int chain_step(int ns, proxy_data * pfrom, proxy_data * pto) {
536
538
usenumericip :
537
539
if (!inet_ntop (v6 ?AF_INET6 :AF_INET ,pto -> ip .addr .v6 ,ip_buf ,sizeof ip_buf )) {
538
540
pto -> ps = DOWN_STATE ;
539
- proxychains_write_log ( "<--ip conversion error!\n" ) ;
540
- close ( ns ) ;
541
- return SOCKET_ERROR ;
541
+ errmsg = "<--ip conversion error!\n" ;
542
+ retcode = SOCKET_ERROR ;
543
+ goto err ;
542
544
}
543
545
hostname = ip_buf ;
544
546
}
545
547
546
548
proxychains_write_log (TP " %s:%d " , hostname , htons (pto -> port ));
547
- retcode = tunnel_to (ns , pto -> ip , pto -> port , pfrom -> pt , pfrom -> user , pfrom -> pass );
549
+ retcode = tunnel_to (* ns , pto -> ip , pto -> port , pfrom -> pt , pfrom -> user , pfrom -> pass );
548
550
switch (retcode ) {
549
551
case SUCCESS :
550
552
pto -> ps = BUSY_STATE ;
551
553
break ;
552
554
case BLOCKED :
553
555
pto -> ps = BLOCKED_STATE ;
554
- proxychains_write_log ("<--denied\n" );
555
- close (ns );
556
- break ;
556
+ errmsg = "<--denied\n" ;
557
+ goto err ;
557
558
case SOCKET_ERROR :
558
559
pto -> ps = DOWN_STATE ;
559
- proxychains_write_log ("<--socket error or timeout!\n" );
560
- close (ns );
561
- break ;
560
+ errmsg = "<--socket error or timeout!\n" ;
561
+ goto err ;
562
562
}
563
563
return retcode ;
564
+ err :
565
+ if (errmsg ) proxychains_write_log (errmsg );
566
+ if (* ns != -1 ) close (* ns );
567
+ * ns = -1 ;
568
+ return retcode ;
564
569
}
565
570
566
571
int connect_proxy_chain (int sock , ip_type target_ip ,
@@ -596,7 +601,7 @@ int connect_proxy_chain(int sock, ip_type target_ip,
596
601
p2 = select_proxy (FIFOLY , pd , proxy_count , & offset );
597
602
if (!p2 )
598
603
break ;
599
- if (SUCCESS != chain_step (ns , p1 , p2 )) {
604
+ if (SUCCESS != chain_step (& ns , p1 , p2 )) {
600
605
PDEBUG ("GOTO AGAIN 1\n" );
601
606
goto again ;
602
607
}
@@ -605,7 +610,7 @@ int connect_proxy_chain(int sock, ip_type target_ip,
605
610
//proxychains_write_log(TP);
606
611
p3 -> ip = target_ip ;
607
612
p3 -> port = target_port ;
608
- if (SUCCESS != chain_step (ns , p1 , p3 ))
613
+ if (SUCCESS != chain_step (& ns , p1 , p3 ))
609
614
goto error ;
610
615
break ;
611
616
@@ -643,7 +648,7 @@ int connect_proxy_chain(int sock, ip_type target_ip,
643
648
/* Try from the beginning to where we started */
644
649
offset = 0 ;
645
650
continue ;
646
- } else if (SUCCESS != chain_step (ns , p1 , p2 )) {
651
+ } else if (SUCCESS != chain_step (& ns , p1 , p2 )) {
647
652
PDEBUG ("GOTO AGAIN 1\n" );
648
653
goto again ;
649
654
} else
@@ -655,7 +660,7 @@ int connect_proxy_chain(int sock, ip_type target_ip,
655
660
p3 -> port = target_port ;
656
661
proxychains_proxy_offset = offset + 1 ;
657
662
PDEBUG ("pd_offset = %d, curr_len = %d\n" , proxychains_proxy_offset , curr_len );
658
- if (SUCCESS != chain_step (ns , p1 , p3 ))
663
+ if (SUCCESS != chain_step (& ns , p1 , p3 ))
659
664
goto error ;
660
665
break ;
661
666
@@ -673,7 +678,7 @@ int connect_proxy_chain(int sock, ip_type target_ip,
673
678
while (offset < proxy_count ) {
674
679
if (!(p2 = select_proxy (FIFOLY , pd , proxy_count , & offset )))
675
680
break ;
676
- if (SUCCESS != chain_step (ns , p1 , p2 )) {
681
+ if (SUCCESS != chain_step (& ns , p1 , p2 )) {
677
682
PDEBUG ("chain_step failed\n" );
678
683
goto error_strict ;
679
684
}
@@ -682,7 +687,7 @@ int connect_proxy_chain(int sock, ip_type target_ip,
682
687
//proxychains_write_log(TP);
683
688
p3 -> ip = target_ip ;
684
689
p3 -> port = target_port ;
685
- if (SUCCESS != chain_step (ns , p1 , p3 ))
690
+ if (SUCCESS != chain_step (& ns , p1 , p3 ))
686
691
goto error ;
687
692
break ;
688
693
@@ -698,7 +703,7 @@ int connect_proxy_chain(int sock, ip_type target_ip,
698
703
while (++ curr_len < max_chain ) {
699
704
if (!(p2 = select_proxy (RANDOMLY , pd , proxy_count , & offset )))
700
705
goto error_more ;
701
- if (SUCCESS != chain_step (ns , p1 , p2 )) {
706
+ if (SUCCESS != chain_step (& ns , p1 , p2 )) {
702
707
PDEBUG ("GOTO AGAIN 2\n" );
703
708
goto again ;
704
709
}
@@ -707,7 +712,7 @@ int connect_proxy_chain(int sock, ip_type target_ip,
707
712
//proxychains_write_log(TP);
708
713
p3 -> ip = target_ip ;
709
714
p3 -> port = target_port ;
710
- if (SUCCESS != chain_step (ns , p1 , p3 ))
715
+ if (SUCCESS != chain_step (& ns , p1 , p3 ))
711
716
goto error ;
712
717
713
718
}
0 commit comments