Skip to content

Commit 286f54a

Browse files
committed
Try to close nicely, waiting for a real proto
1 parent 6ef8ca4 commit 286f54a

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

src/main.c

+50-23
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ struct netio {
2929
int fd;
3030
struct {
3131
buffer_t buf;
32-
ssize_t ret;
3332
} write, read;
3433
};
3534

@@ -41,7 +40,7 @@ struct crypto_ctx {
4140
uint8_t skey[crypto_generichash_KEYBYTES];
4241
};
4342

44-
volatile sig_atomic_t running;
43+
volatile sig_atomic_t gt_close = 0;
4544

4645
static int64_t dt_ms (struct timeval *ta, struct timeval *tb)
4746
{
@@ -282,7 +281,7 @@ static void gt_sa_stop (int sig)
282281
switch (sig) {
283282
case SIGINT:
284283
case SIGTERM:
285-
running = 0;
284+
gt_close = 1;
286285
}
287286
}
288287

@@ -291,7 +290,6 @@ static void gt_set_signal (void)
291290
struct sigaction sa;
292291

293292
byte_set(&sa, 0, sizeof(sa));
294-
running = 1;
295293

296294
sa.sa_handler = gt_sa_stop;
297295
sigaction(SIGINT, &sa, NULL);
@@ -660,12 +658,12 @@ int main (int argc, char **argv)
660658
return 1;
661659
}
662660

663-
while (running) {
661+
while (1) {
664662
sock.fd = listener?sk_accept(fd):sk_create(ai, sk_connect);
665663

666664
if (sock.fd==-1) {
667665
usleep(100000);
668-
continue;
666+
goto restart;
669667
}
670668

671669
char *sockname = sk_get_name(sock.fd);
@@ -703,8 +701,22 @@ int main (int argc, char **argv)
703701
FD_ZERO(&rfds);
704702
FD_ZERO(&wfds);
705703

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+
708720
FD_SET(sock.fd, &rfds);
709721

710722
if (select(sock.fd+1, &rfds, &wfds, NULL, NULL)==-1 && errno!=EINTR) {
@@ -761,28 +773,35 @@ int main (int argc, char **argv)
761773
FD_CLR(sock.fd, &wfds);
762774

763775
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));
768777

769-
if (sock.write.ret==-1)
778+
if (r==-1)
770779
FD_SET(sock.fd, &wfds);
771780

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+
}
774793
}
775794

776795
buffer_shift(&sock.read.buf);
777796

778797
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));
780799

781-
if (!sock.read.ret)
782-
goto restart;
800+
if (!r)
801+
stop_loop |= (1<<1);
783802

784-
if (sock.read.ret>0)
785-
sock.read.buf.write += sock.read.ret;
803+
if (r>0)
804+
sock.read.buf.write += r;
786805
}
787806

788807
if (FD_ISSET(tun.fd, &wfds))
@@ -796,8 +815,11 @@ int main (int argc, char **argv)
796815
if (!ip_size)
797816
goto restart;
798817

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);
800821
break;
822+
}
801823

802824
if (decrypt_packet(&ctx, tunw.buf, ip_size, &sock.read.buf)) {
803825
gt_log("%s: message could not be verified!\n", sockname);
@@ -829,8 +851,13 @@ int main (int argc, char **argv)
829851
sockname = NULL;
830852
}
831853

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;
834861
}
835862

836863
freeaddrinfo(ai);

0 commit comments

Comments
 (0)