Commit 9bb33ddd authored by Marek Vavrusa's avatar Marek Vavrusa

Fixes for TCP handler.

parent 0ea73051
......@@ -33,9 +33,10 @@
#include "knot/server/xfr-handler.h"
#include "libknot/nameserver/name-server.h"
#include "knot/other/error.h"
#include "knot/stat/stat.h"
#include "libknot/util/wire.h"
#include "knot/server/zones.h"
/* Defines */
#define TCP_BUFFER_SIZE 65536
/*! \brief TCP worker data. */
typedef struct tcp_worker_t {
......@@ -63,7 +64,7 @@ static int xfr_send_cb(int session, sockaddr_t *addr, uint8_t *msg, size_t msgle
* \param w Associated I/O event.
* \param revents Returned events.
*/
static void tcp_handle(tcp_worker_t *w, int fd)
static void tcp_handle(tcp_worker_t *w, int fd, uint8_t *qbuf, size_t qbuf_maxlen)
{
if (fd < 0 || !w || !w->ioh) {
dbg_net("tcp: tcp_handle(%p, %d) - invalid parameters\n", w, fd);
......@@ -86,8 +87,6 @@ static void tcp_handle(tcp_worker_t *w, int fd)
}
/* Receive data. */
uint8_t qbuf[65535]; /*! \todo This may be problematic. */
size_t qbuf_maxlen = sizeof(qbuf);
int n = tcp_recv(fd, qbuf, qbuf_maxlen, &addr);
if (n <= 0) {
dbg_net("tcp: client on fd=%d disconnected\n", fd);
......@@ -242,16 +241,16 @@ static int tcp_accept(int fd)
tcp_worker_t* tcp_worker_create()
{
tcp_worker_t *w = malloc(sizeof(tcp_worker_t));
if (!w) {
if (w == NULL) {
dbg_net("tcp: out of memory when creating worker\n");
return 0;
return NULL;
}
/* Create signal pipes. */
memset(w, 0, sizeof(tcp_worker_t));
if (pipe(w->pipe) < 0) {
free(w);
return 0;
return NULL;
}
/* Create fdset. */
......@@ -260,6 +259,7 @@ tcp_worker_t* tcp_worker_create()
close(w->pipe[0]);
close(w->pipe[1]);
free(w);
return NULL;
}
fdset_add(w->fdset, w->pipe[0], OS_EV_READ);
......@@ -361,13 +361,14 @@ int tcp_loop_master(dthread_t *thread)
{
iohandler_t *handler = (iohandler_t *)thread->data;
dt_unit_t *unit = thread->unit;
tcp_worker_t **workers = handler->data;
/* Check socket. */
if (!handler || handler->fd < 0 || !workers) {
if (!handler || handler->fd < 0 || handler->data == NULL) {
dbg_net("tcp: failed to initialize master thread\n");
return KNOTD_EINVAL;
}
tcp_worker_t **workers = handler->data;
/* Accept connections. */
int id = 0;
......@@ -407,6 +408,13 @@ int tcp_loop_worker(dthread_t *thread)
if (!w) {
return KNOTD_EINVAL;
}
/* Allocate buffer for requests. */
uint8_t *qbuf = malloc(TCP_BUFFER_SIZE);
if (qbuf == NULL) {
dbg_net("tcp: failed to allocate buffers for TCP worker\n");
return KNOTD_EINVAL;
}
/* Accept clients. */
dbg_net_verb("tcp: worker %p started\n", w);
......@@ -443,7 +451,7 @@ int tcp_loop_worker(dthread_t *thread)
fdset_add(w->fdset, client, OS_EV_READ);
} else {
/* Handle other events. */
tcp_handle(w, it.fd);
tcp_handle(w, it.fd, qbuf, TCP_BUFFER_SIZE);
}
/* Check if next exists. */
......@@ -455,6 +463,7 @@ int tcp_loop_worker(dthread_t *thread)
}
/* Stop whole unit. */
free(qbuf);
dbg_net_verb("tcp: worker %p finished\n", w);
tcp_worker_free(w);
return KNOTD_EOK;
......
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