Skip to content

Commit 21238ce

Browse files
fix: Improve error handling
1 parent 2211b50 commit 21238ce

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

src/fakehttp.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ int main(int argc, char *argv[])
652652
unsigned long long tmp;
653653
struct nfq_handle *h;
654654
struct nfq_q_handle *qh;
655-
int res, fd, opt, exitcode;
655+
int res, fd, opt, exitcode, err_cnt;
656656
ssize_t recv_len;
657657
char *buff;
658658

@@ -830,24 +830,41 @@ int main(int argc, char *argv[])
830830
/*
831831
Main Loop
832832
*/
833+
err_cnt = 0;
833834
while (!g_exit) {
835+
if (err_cnt >= 20) {
836+
E("too many errors, exiting...");
837+
goto cleanup_iptables;
838+
}
839+
834840
recv_len = recv(fd, buff, buffsize, 0);
835841
if (recv_len < 0) {
836-
if (errno == EINTR) {
837-
g_exit = 1;
838-
goto exit_success;
842+
switch (errno) {
843+
case EINTR:
844+
continue;
845+
case EAGAIN:
846+
case ETIMEDOUT:
847+
case ENOBUFS:
848+
E("ERROR: recv(): %s", strerror(errno));
849+
err_cnt++;
850+
continue;
851+
default:
852+
E("ERROR: recv(): %s", strerror(errno));
853+
err_cnt++;
854+
goto cleanup_iptables;
839855
}
840-
E("ERROR: recv(): %s", strerror(errno));
841-
goto cleanup_iptables;
842856
}
857+
843858
res = nfq_handle_packet(h, buff, recv_len);
844859
if (res < 0) {
845860
E("ERROR: nfq_handle_packet()");
861+
err_cnt++;
846862
continue;
847863
}
864+
865+
err_cnt = 0;
848866
}
849867

850-
exit_success:
851868
E("exiting normally...");
852869
exitcode = EXIT_SUCCESS;
853870

0 commit comments

Comments
 (0)