Commit 0ef61b02 authored by Marek Vavrusa's avatar Marek Vavrusa

Fixed client disconnecting from TCP sessions.

Commit refs #774.
parent 6bfdf972
......@@ -264,8 +264,8 @@ static int server_bind_handlers(server_t *server)
/* Estimate number of threads/manager. */
int thr_count = dt_optimal_size();
int tcp_unit_size = (thr_count >> 1);
if (tcp_unit_size < 2) {
tcp_unit_size = 2;
if (tcp_unit_size < 3) {
tcp_unit_size = 3;
}
/* Lock config. */
......
......@@ -107,6 +107,11 @@ static inline int tcp_recv(int fd, uint8_t *buf, size_t len, sockaddr_t *addr)
debug_net("tcp: incoming packet size on %d: %hu buffer size: %zu\n",
fd, pktsize, len);
// Check packet size for NULL
if (pktsize == 0) {
return KNOT_ERROR;
}
// Check packet size
if (len < pktsize) {
return KNOT_ENOMEM;
......@@ -144,6 +149,9 @@ static inline int tcp_handle(tcp_pool_t *pool, int fd,
/* Receive data. */
int n = tcp_recv(fd, qbuf, qbuf_maxlen, &addr);
if (n <= 0) {
return KNOT_ERROR;
}
/* Parse query. */
dnslib_response_t *resp = dnslib_response_new(qbuf_maxlen);
......@@ -152,10 +160,10 @@ static inline int tcp_handle(tcp_pool_t *pool, int fd,
/* Parse query. */
dnslib_query_t qtype = DNSLIB_QUERY_NORMAL;
int res = ns_parse_query(qbuf, n, resp, &qtype);
if (unlikely(res < 0)) {
if (unlikely(res != KNOT_EOK)) {
/* Send error response. */
if (res != KNOT_EMALF ) {
/* Send error response on dnslib RCODE. */
if (res > 0) {
uint16_t pkt_id = dnslib_packet_get_id(qbuf);
ns_error_response(pool->ns, pkt_id, res,
qbuf, &resp_len);
......@@ -201,11 +209,10 @@ static inline int tcp_handle(tcp_pool_t *pool, int fd,
debug_net("tcp: %s: failed: %d - %d.\n",
"socket_send()",
res, errno);
return res;
}
}
return KNOT_EOK;
return res;
}
/*!
......@@ -451,11 +458,14 @@ static int tcp_pool(dthread_t *thread)
pool->epfd, fd);
/* Handle TCP request. */
if (pool->events[i].events & EPOLLIN) {
tcp_handle(pool, fd, qbuf, sizeof(qbuf));
} else {
// Disconnect
int ret = KNOT_EOK;
if (pool->events[i].events & EPOLLERR) {
tcp_disconnect(pool, fd);
} else {
ret = tcp_handle(pool, fd, qbuf, sizeof(qbuf));
if (ret != KNOT_EOK) {
tcp_disconnect(pool, fd);
}
}
debug_net("tcp: pool #%d finished fd=%d (%d remain).\n",
......
......@@ -108,10 +108,10 @@ int udp_master(dthread_t *thread)
/* Parse query. */
res = ns_parse_query(qbuf, n, resp, &qtype);
if (unlikely(res < 0)) {
if (unlikely(res != KNOT_EOK)) {
/* Send error response. */
if (res != KNOT_EMALF ) {
/* Send error response on dnslib RCODE. */
if (res > 0) {
uint16_t pkt_id = dnslib_packet_get_id(qbuf);
ns_error_response(ns, pkt_id, res,
qbuf, &resp_len);
......@@ -145,9 +145,9 @@ int udp_master(dthread_t *thread)
if (res == KNOT_EOK) {
assert(resp_len > 0);
debug_net("udp: answer wire format (size %zd):\n",
(unsigned) answer_size);
debug_net_hex((const char *) outbuf, resp_len);
//debug_net("udp: answer wire format (size %zd):\n",
// resp_len);
//debug_net_hex((const char *) outbuf, resp_len);
// Send datagram
res = sendto(sock, qbuf, resp_len,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment