diff --git a/plugins/tuntap/common.c b/plugins/tuntap/common.c index b82d5b7e..ad32cc8f 100644 --- a/plugins/tuntap/common.c +++ b/plugins/tuntap/common.c @@ -2,6 +2,16 @@ extern struct uwsgi_tuntap utt; +// error reporting +void uwsgi_tuntap_error(struct uwsgi_tuntap_peer *uttp, char *msg) { + if (uttp) { + uwsgi_log_verbose("[tuntap] peer fd: %d ip: %s %s: %s [%s line %d]\n", uttp->fd, uttp->ip, msg, strerror(errno), __FILE__, __LINE__); + } + else { + uwsgi_log_verbose("[tuntap] %s: %s [%s line %d]\n", msg, strerror(errno), __FILE__, __LINE__); + } +} + // create a new peer struct uwsgi_tuntap_peer *uwsgi_tuntap_peer_create(struct uwsgi_tuntap_router *uttr, int fd, int is_router) { @@ -183,7 +193,7 @@ int uwsgi_tuntap_register_addr(struct uwsgi_tuntap_router *uttr, struct uwsgi_tu char ip[INET_ADDRSTRLEN + 1]; memset(ip, 0, INET_ADDRSTRLEN + 1); if (!inet_ntop(AF_INET, &uttp->addr, ip, INET_ADDRSTRLEN)) { - uwsgi_error("uwsgi_tuntap_register_addr()/inet_ntop()"); + uwsgi_tuntap_error(uttp, "uwsgi_tuntap_register_addr()/inet_ntop()"); return -1; } if (uttp != tmp_uttp) { @@ -205,7 +215,7 @@ int uwsgi_tuntap_peer_dequeue(struct uwsgi_tuntap_router *uttr, struct uwsgi_tun if (rlen < 0) { if (uwsgi_is_again()) return 0; - uwsgi_error("uwsgi_tuntap_peer_dequeue()/read()"); + uwsgi_tuntap_error(uttp, "uwsgi_tuntap_peer_dequeue()/read()"); return -1; } uttp->buf_pos += rlen; @@ -265,7 +275,7 @@ enqueue: if (rlen < 0) { if (uwsgi_is_again()) return 0; - uwsgi_error("uwsgi_tuntap_peer_dequeue()/read()"); + uwsgi_tuntap_error(uttp, "uwsgi_tuntap_peer_dequeue()/read()"); return -1; } uttp->header_pos += rlen; @@ -282,14 +292,14 @@ int uwsgi_tuntap_peer_enqueue(struct uwsgi_tuntap_router *uttr, struct uwsgi_tun ssize_t rlen = write(uttp->fd, uttp->write_buf + uttp->written, uttp->write_buf_pktsize - uttp->written); if (rlen == 0) { - uwsgi_error("uwsgi_tuntap_peer_enqueue()/write()"); + uwsgi_tuntap_error(uttp, "uwsgi_tuntap_peer_enqueue()/write()"); return -1; } if (rlen < 0) { if (uwsgi_is_again()) goto retry; - uwsgi_error("uwsgi_tuntap_peer_enqueue()/write()"); + uwsgi_tuntap_error(uttp, "uwsgi_tuntap_peer_enqueue()/write()"); return -1; } @@ -303,13 +313,13 @@ int uwsgi_tuntap_peer_enqueue(struct uwsgi_tuntap_router *uttr, struct uwsgi_tun if (uttr->wait_for_write) { uttp->blocked_read = 1; if (event_queue_del_fd(uttr->queue, uttp->fd, event_queue_write())) { - uwsgi_error("uwsgi_tuntap_peer_enqueue()/event_queue_del_fd()"); + uwsgi_tuntap_error(uttp, "uwsgi_tuntap_peer_enqueue()/event_queue_del_fd()"); return -1; } } else { if (event_queue_fd_readwrite_to_read(uttr->queue, uttp->fd)) { - uwsgi_error("uwsgi_tuntap_peer_enqueue()/event_queue_fd_write_to_read()"); + uwsgi_tuntap_error(uttp, "uwsgi_tuntap_peer_enqueue()/event_queue_fd_write_to_read()"); return -1; } } @@ -324,7 +334,7 @@ int uwsgi_tuntap_peer_enqueue(struct uwsgi_tuntap_router *uttr, struct uwsgi_tun retry: if (!uttp->wait_for_write) { if (event_queue_fd_read_to_readwrite(uttr->queue, uttp->fd)) { - uwsgi_error("uwsgi_tuntap_peer_enqueue()/event_queue_fd_read_to_write()"); + uwsgi_tuntap_error(uttp, "uwsgi_tuntap_peer_enqueue()/event_queue_fd_read_to_write()"); return -1; } uttp->wait_for_write = 1; diff --git a/plugins/tuntap/common.h b/plugins/tuntap/common.h index eeba96ff..1d5e5387 100644 --- a/plugins/tuntap/common.h +++ b/plugins/tuntap/common.h @@ -116,3 +116,4 @@ int uwsgi_tuntap_register_addr(struct uwsgi_tuntap_router *, struct uwsgi_tuntap void uwsgi_tuntap_peer_send_rules(int, struct uwsgi_tuntap_peer *); int uwsgi_tuntap_peer_rules_check(struct uwsgi_tuntap_router *, struct uwsgi_tuntap_peer *, char *, size_t, int); +void uwsgi_tuntap_error(struct uwsgi_tuntap_peer *, char *); diff --git a/plugins/tuntap/firewall.c b/plugins/tuntap/firewall.c index d56e1d64..5215f659 100644 --- a/plugins/tuntap/firewall.c +++ b/plugins/tuntap/firewall.c @@ -61,15 +61,15 @@ int uwsgi_tuntap_peer_rules_check(struct uwsgi_tuntap_router *uttr, struct uwsgi int ret = poll(&pfd, 1, uwsgi.socket_timeout * 1000); if (ret > 0) { if (sendto(uttr->gateway_fd, pkt, len, 0, (struct sockaddr *) &sin, sizeof(struct sockaddr_in)) < 0) { - uwsgi_error("uwsgi_tuntap_route_check()/sendto()"); + uwsgi_tuntap_error(uttp,"uwsgi_tuntap_route_check()/sendto()"); } } else { - uwsgi_error("uwsgi_tuntap_route_check()/poll()"); + uwsgi_tuntap_error(uttp,"uwsgi_tuntap_route_check()/poll()"); } } else { - uwsgi_error("uwsgi_tuntap_route_check()/sendto()"); + uwsgi_tuntap_error(uttp,"uwsgi_tuntap_route_check()/sendto()"); } } } @@ -324,7 +324,7 @@ void uwsgi_tuntap_peer_send_rules(int fd, struct uwsgi_tuntap_peer *peer) { *slash = 0; } if (inet_pton(AF_INET, argv[1], &utpr.src) != 1) { - uwsgi_error("uwsgi_tuntap_peer_send_rules()/inet_pton()"); + uwsgi_tuntap_error(peer, "uwsgi_tuntap_peer_send_rules()/inet_pton()"); exit(1); } if (slash) *slash = '/'; @@ -336,7 +336,7 @@ void uwsgi_tuntap_peer_send_rules(int fd, struct uwsgi_tuntap_peer *peer) { *slash = 0; } if (inet_pton(AF_INET, argv[2], &utpr.dst) != 1) { - uwsgi_error("uwsgi_tuntap_peer_send_rules()/inet_pton()"); + uwsgi_tuntap_error(peer, "uwsgi_tuntap_peer_send_rules()/inet_pton()"); exit(1); } if (slash) *slash = '/'; @@ -371,7 +371,7 @@ void uwsgi_tuntap_peer_send_rules(int fd, struct uwsgi_tuntap_peer *peer) { } *colon = 0; if (inet_pton(AF_INET, argv[4], &utpr.target) != 1) { - uwsgi_error("uwsgi_tuntap_peer_send_rules()/inet_pton()"); + uwsgi_tuntap_error(peer, "uwsgi_tuntap_peer_send_rules()/inet_pton()"); exit(1); } *colon = ':'; @@ -389,7 +389,7 @@ void uwsgi_tuntap_peer_send_rules(int fd, struct uwsgi_tuntap_peer *peer) { size_t len = ub->pos; uwsgi_buffer_destroy(ub); if (write(fd,peer->rules, len) != (ssize_t)len) { - uwsgi_error("uwsgi_tuntap_peer_send_rules()/write()"); + uwsgi_tuntap_error(peer, "uwsgi_tuntap_peer_send_rules()/write()"); exit(1); } return; diff --git a/plugins/tuntap/tuntap.c b/plugins/tuntap/tuntap.c index def027a5..3d01c1ec 100644 --- a/plugins/tuntap/tuntap.c +++ b/plugins/tuntap/tuntap.c @@ -126,7 +126,7 @@ static void *uwsgi_tuntap_loop(void *arg) { ptr[3] = 0; uttp->write_buf_pktsize+= pktsize+4; if (uwsgi_tuntap_peer_enqueue(uttr, uttp)) { - uwsgi_log("server disconnected...\n"); + uwsgi_log_verbose("tuntap server disconnected...\n"); exit(1); } continue; @@ -137,7 +137,7 @@ static void *uwsgi_tuntap_loop(void *arg) { // read from the client if (!uttp->wait_for_write) { if (uwsgi_tuntap_peer_dequeue(uttr, uttp, 0)) { - uwsgi_log("server disconnected...\n"); + uwsgi_log_verbose("tuntap server disconnected...\n"); exit(1); } } @@ -149,7 +149,7 @@ static void *uwsgi_tuntap_loop(void *arg) { // write to the client if (uwsgi_tuntap_peer_enqueue(uttr, uttp)) { - uwsgi_log("server disconnected...\n"); + uwsgi_log_verbose("tuntap server disconnected...\n"); exit(1); } } @@ -507,7 +507,7 @@ void tuntaprouter_send_stats(struct uwsgi_tuntap_router *uttr) { ssize_t res = write(client_fd, us->base + pos, remains); if (res <= 0) { if (res < 0) { - uwsgi_error("write()"); + uwsgi_error("tuntaprouter_send_stats()/write()"); } goto end0; }