aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar zfl9 <zfl9.com@gmail.com> 2020-03-07 13:06:43 +0800
committerGravatar zfl9 <zfl9.com@gmail.com> 2020-03-07 13:06:43 +0800
commit665eb7c8773237f0bb805dfb9433112672f712d6 (patch)
tree7b878e20f20fa61eac93daab77620b92515598f6
parentuse libev instead of libuv (diff)
downloaddns2tcp-665eb7c8773237f0bb805dfb9433112672f712d6.tar.gz
dns2tcp-665eb7c8773237f0bb805dfb9433112672f712d6.tar.bz2
dns2tcp-665eb7c8773237f0bb805dfb9433112672f712d6.zip
use libev instead of libuv
-rw-r--r--dnsudp2tcp.c23
-rwxr-xr-xtestbin77424 -> 77424 bytes
2 files changed, 14 insertions, 9 deletions
diff --git a/dnsudp2tcp.c b/dnsudp2tcp.c
index 9d46294..cf700dc 100644
--- a/dnsudp2tcp.c
+++ b/dnsudp2tcp.c
@@ -457,11 +457,11 @@ static void tcp_connect_cb(evloop_t *evloop, evio_t *watcher, int events __attri
static void tcp_sendmsg_cb(evloop_t *evloop, evio_t *watcher, int events __attribute__((unused))) {
tcpwatcher_t *tcpw = (void *)watcher;
- void *buffer = tcpw->buffer;
- uint16_t bufferlen = 2 + ntohs(*(uint16_t *)buffer);
- ssize_t nsend = send(watcher->fd, buffer + tcpw->nrcvsnd, bufferlen - tcpw->nrcvsnd, 0);
- if (nsend < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
- LOGERR("[tcp_sendmsg_cb] send to %s#%hu failed: (%d) %s", g_remote_ipstr, g_remote_portno, errno, strerror(errno));
+ uint16_t datalen = 2 + ntohs(*(uint16_t *)tcpw->buffer);
+ ssize_t nsend = send(watcher->fd, (void *)tcpw->buffer + tcpw->nrcvsnd, datalen - tcpw->nrcvsnd, 0);
+ if (nsend < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) return;
+ LOGERR("[tcp_sendmsg_cb] send to %s#%hu: (%d) %s", g_remote_ipstr, g_remote_portno, errno, strerror(errno));
ev_io_stop(evloop, watcher);
close(watcher->fd);
free(watcher);
@@ -469,8 +469,8 @@ static void tcp_sendmsg_cb(evloop_t *evloop, evio_t *watcher, int events __attri
}
IF_VERBOSE LOGINF("[tcp_sendmsg_cb] send to %s#%hu, nsend:%zd", g_remote_ipstr, g_remote_portno, nsend);
tcpw->nrcvsnd += nsend;
- if (tcpw->nrcvsnd >= bufferlen) {
- tcpw->nrcvsnd = 0;
+ if (tcpw->nrcvsnd >= datalen) {
+ tcpw->nrcvsnd = 0; /* reset to zero for recv data */
ev_io_stop(evloop, watcher);
ev_io_init(watcher, tcp_recvmsg_cb, watcher->fd, EV_READ);
ev_io_start(evloop, watcher);
@@ -482,8 +482,13 @@ static void tcp_recvmsg_cb(evloop_t *evloop, evio_t *watcher, int events __attri
void *buffer = tcpw->buffer;
ssize_t nrecv = recv(watcher->fd, buffer + tcpw->nrcvsnd, 2 + UDPDGRAM_MAXSIZ - tcpw->nrcvsnd, 0);
- if (nrecv < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
- LOGERR("[tcp_recvmsg_cb] recv from %s#%hu failed: (%d) %s", g_remote_ipstr, g_remote_portno, errno, strerror(errno));
+ if (nrecv < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) return;
+ LOGERR("[tcp_recvmsg_cb] recv from %s#%hu: (%d) %s", g_remote_ipstr, g_remote_portno, errno, strerror(errno));
+ goto FREE_TCP_WATCHER;
+ }
+ if (nrecv == 0) {
+ LOGERR("[tcp_recvmsg_cb] recv from %s#%hu: connection is closed", g_remote_ipstr, g_remote_portno);
goto FREE_TCP_WATCHER;
}
tcpw->nrcvsnd += nrecv;
diff --git a/test b/test
index 8b2c217..ecfc5c0 100755
--- a/test
+++ b/test
Binary files differ