diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2019-04-17 20:49:25 +0000 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2019-04-17 22:33:12 +0000 |
commit | 23bb6ecd15373546363296baf38651f8dc5d9df9 (patch) | |
tree | 10ab02094845ba6359c6a2bc00c4fafb09c4f298 | |
parent | 9ebd3828b8b42a013169c88e62f1191765b9fc34 (diff) |
quit on error EBADFD
Read fails with EBADFD if the gre link is deleted
with "ip link del".
-rw-r--r-- | gre.c | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -42,7 +42,7 @@ static struct sockaddr_in remote; uint8_t buf[4096]; static void gre_cb(void); -static void tun_cb(void); +static int tun_cb(void); static int tun_new(const char *dev); static int setnonblock(int fd); static int runas(const char *user); @@ -132,7 +132,8 @@ int main(int argc, char **argv) if (FD_ISSET(tun, &readset)) { - tun_cb(); + if (tun_cb() < 0) + return 0; } } @@ -176,19 +177,24 @@ static void gre_cb(void) write(tun, buf + ihl + 4, n - ihl - 4); } -static void tun_cb(void) +static int tun_cb(void) { int n; n = read(tun, buf + 4, sizeof(buf) - 4); if (n < 0) { + int err = errno; perror("read"); - return; + if (err == EBADFD) + return -1; + + return 0; } *(uint16_t *)(buf) = 0; *(uint16_t *)(buf + 2) = htons(0x0800); sendto(sock, buf, n + 4, 0, (struct sockaddr *)&remote, sizeof(struct sockaddr)); + return 0; } static int tun_new(const char *dev) |