@@ -29,7 +29,6 @@ struct netio {
29
29
int fd ;
30
30
struct {
31
31
buffer_t buf ;
32
- ssize_t ret ;
33
32
} write , read ;
34
33
};
35
34
@@ -41,7 +40,7 @@ struct crypto_ctx {
41
40
uint8_t skey [crypto_generichash_KEYBYTES ];
42
41
};
43
42
44
- volatile sig_atomic_t running ;
43
+ volatile sig_atomic_t gt_close = 0 ;
45
44
46
45
static int64_t dt_ms (struct timeval * ta , struct timeval * tb )
47
46
{
@@ -282,7 +281,7 @@ static void gt_sa_stop (int sig)
282
281
switch (sig ) {
283
282
case SIGINT :
284
283
case SIGTERM :
285
- running = 0 ;
284
+ gt_close = 1 ;
286
285
}
287
286
}
288
287
@@ -291,7 +290,6 @@ static void gt_set_signal (void)
291
290
struct sigaction sa ;
292
291
293
292
byte_set (& sa , 0 , sizeof (sa ));
294
- running = 1 ;
295
293
296
294
sa .sa_handler = gt_sa_stop ;
297
295
sigaction (SIGINT , & sa , NULL );
@@ -660,12 +658,12 @@ int main (int argc, char **argv)
660
658
return 1 ;
661
659
}
662
660
663
- while (running ) {
661
+ while (1 ) {
664
662
sock .fd = listener ?sk_accept (fd ):sk_create (ai , sk_connect );
665
663
666
664
if (sock .fd == -1 ) {
667
665
usleep (100000 );
668
- continue ;
666
+ goto restart ;
669
667
}
670
668
671
669
char * sockname = sk_get_name (sock .fd );
@@ -703,8 +701,22 @@ int main (int argc, char **argv)
703
701
FD_ZERO (& rfds );
704
702
FD_ZERO (& wfds );
705
703
706
- while (running ) {
707
- FD_SET (tun .fd , & rfds );
704
+ int stop_loop = 0 ;
705
+
706
+ while (1 ) {
707
+ if (gt_close )
708
+ stop_loop = 1 ;
709
+
710
+ if (stop_loop ) {
711
+ if (((stop_loop >>1 )== 3 ) &&
712
+ (buffer_read_size (& sock .write .buf )== 0 ) &&
713
+ (buffer_read_size (& sock .read .buf )== 0 ))
714
+ goto restart ;
715
+ FD_CLR (tun .fd , & rfds );
716
+ } else {
717
+ FD_SET (tun .fd , & rfds );
718
+ }
719
+
708
720
FD_SET (sock .fd , & rfds );
709
721
710
722
if (select (sock .fd + 1 , & rfds , & wfds , NULL , NULL )== -1 && errno != EINTR ) {
@@ -761,28 +773,35 @@ int main (int argc, char **argv)
761
773
FD_CLR (sock .fd , & wfds );
762
774
763
775
if (buffer_read_size (& sock .write .buf )) {
764
- sock .write .ret = fd_write (sock .fd , sock .write .buf .read , buffer_read_size (& sock .write .buf ));
765
-
766
- if (!sock .write .ret )
767
- goto restart ;
776
+ ssize_t r = fd_write (sock .fd , sock .write .buf .read , buffer_read_size (& sock .write .buf ));
768
777
769
- if (sock . write . ret == -1 )
778
+ if (r == -1 )
770
779
FD_SET (sock .fd , & wfds );
771
780
772
- if (sock .write .ret > 0 )
773
- sock .write .buf .read += sock .write .ret ;
781
+ if (!r ) {
782
+ stop_loop |= (1 <<2 );
783
+ buffer_format (& sock .write .buf );
784
+ }
785
+
786
+ if (r > 0 )
787
+ sock .write .buf .read += r ;
788
+ } else {
789
+ if (stop_loop ) {
790
+ stop_loop |= (1 <<2 );
791
+ shutdown (sock .fd , SHUT_WR );
792
+ }
774
793
}
775
794
776
795
buffer_shift (& sock .read .buf );
777
796
778
797
if (FD_ISSET (sock .fd , & rfds )) {
779
- sock . read . ret = fd_read (sock .fd , sock .read .buf .write , buffer_write_size (& sock .read .buf ));
798
+ ssize_t r = fd_read (sock .fd , sock .read .buf .write , buffer_write_size (& sock .read .buf ));
780
799
781
- if (!sock . read . ret )
782
- goto restart ;
800
+ if (!r )
801
+ stop_loop |= ( 1 << 1 ) ;
783
802
784
- if (sock . read . ret > 0 )
785
- sock .read .buf .write += sock . read . ret ;
803
+ if (r > 0 )
804
+ sock .read .buf .write += r ;
786
805
}
787
806
788
807
if (FD_ISSET (tun .fd , & wfds ))
@@ -796,8 +815,11 @@ int main (int argc, char **argv)
796
815
if (!ip_size )
797
816
goto restart ;
798
817
799
- if (ip_size < 0 || (size_t )ip_size + 16 > size )
818
+ if (ip_size < 0 || (size_t )ip_size + 16 > size ) {
819
+ if (stop_loop & (1 <<1 ))
820
+ buffer_format (& sock .read .buf );
800
821
break ;
822
+ }
801
823
802
824
if (decrypt_packet (& ctx , tunw .buf , ip_size , & sock .read .buf )) {
803
825
gt_log ("%s: message could not be verified!\n" , sockname );
@@ -829,8 +851,13 @@ int main (int argc, char **argv)
829
851
sockname = NULL ;
830
852
}
831
853
832
- close (sock .fd );
833
- sock .fd = -1 ;
854
+ if (sock .fd != -1 ) {
855
+ close (sock .fd );
856
+ sock .fd = -1 ;
857
+ }
858
+
859
+ if (gt_close )
860
+ break ;
834
861
}
835
862
836
863
freeaddrinfo (ai );
0 commit comments