summaryrefslogtreecommitdiffstats
path: root/src/daemon/net.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/net.c')
-rw-r--r--src/daemon/net.c74
1 files changed, 23 insertions, 51 deletions
diff --git a/src/daemon/net.c b/src/daemon/net.c
index 640bd28..11a6a21 100644
--- a/src/daemon/net.c
+++ b/src/daemon/net.c
@@ -39,6 +39,7 @@
struct netstate {
struct timespec lastreply;
+ struct timespec start_time;
int nd;
int sock;
unsigned short int port;
@@ -95,7 +96,6 @@ int net_init(const unsigned short int port)
goto out;
}
- clock_gettime(CLOCK_MONOTONIC_RAW, &state[i].lastreply);
state[i].lastreply.tv_sec -= 5;
state[i].nd = i + 1;
state[i].port = port;
@@ -104,6 +104,8 @@ int net_init(const unsigned short int port)
state[i].bufsize = MTU;
state[i].sock_status = NONEWDATA;
state[i].buffer_status = NONEWDATA;
+ clock_gettime(CLOCK_MONOTONIC_RAW, &state[i].lastreply);
+ state[i].start_time = state[i].lastreply;
pthread_mutex_init(&state[i].mutex, NULL);
if (pthread_create(&state[i].listner, NULL, dolisten, state + i)) {
@@ -185,14 +187,15 @@ int net_send_addr(int nd, const char * const buf, size_t buf_size, const struct
{
int ret = A_OK;
ssize_t sent;
- struct timespec now;
+ struct timespec t;
if (nd > count || nd < 1 || state[--nd].available) {
ret = ERROR;
} else {
if (setting_verbose() >= INFO) {
- clock_gettime(CLOCK_MONOTONIC_RAW, &now);
- fprintf(stderr, "Sending packet to %s, timestap: %li \n", inet_ntoa(state[nd].clientaddr.sin_addr), now.tv_sec);
+ clock_gettime(CLOCK_MONOTONIC_RAW, &t);
+ t = rin_time_sub(&t, &state[nd].start_time);
+ fprintf(stderr, "Sending packet to %s, timestap: %li.%06lis \n", inet_ntoa(state[nd].clientaddr.sin_addr), t.tv_sec, t.tv_nsec / 1000);
}
sent = sendto( state[nd].sock, buf, buf_size, MSG_DONTWAIT,
@@ -210,51 +213,16 @@ int net_send_addr(int nd, const char * const buf, size_t buf_size, const struct
return ret;
}
-void net_flush_read_buffer(int nd)
-{
- struct netstate *st;
- int cancelstate;
- struct timespec now;
- struct timespec later;
- struct timespec delta;
-
- if (setting_verbose() >= INFO) {
- fprintf(stderr, "acquiring net mutex\n");
- }
-
- if (!(nd > count || nd < 1 || state[--nd].available || state[nd].sock_status != A_OK)) {
- st = state + nd;
- pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
- pthread_mutex_lock(&st->mutex);
-
- if (setting_verbose() >= INFO) {
- clock_gettime(CLOCK_MONOTONIC_RAW, &now);
- fprintf(stderr, "flushing netsocket\n");
- }
-
- do {
- st->sock_status = getpacket(st->data, st->bufsize, &st->recvsize, st->sock, &st->clientaddr);
- } while(st->sock_status);
-
- if (setting_verbose() >= INFO) {
- clock_gettime(CLOCK_MONOTONIC_RAW, &later);
- delta = rin_time_sub(&later, &now);
- fprintf(stderr, "finished flushing netsocket in %lis%lins\n", delta.tv_sec, delta.tv_nsec);
- }
-
- pthread_mutex_unlock(&st->mutex);
- pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
- pthread_testcancel();
- }
-}
-
static void *dolisten(void * state)
{
struct timespec now;
- struct timespec wait = {0, 1 * 1000 * 1000}; /* 1 ms */
+ struct timespec delta;
+ struct timespec wait = {0, 1 * 1000 * 1000}; /* 1 ms */
+ struct timespec timeout = {60, 0}; /* 1 minute */
struct netstate *st;
int cancelstate;
int oldstatus = DEAD;
+ char *ipstring;
st = state;
@@ -267,18 +235,25 @@ static void *dolisten(void * state)
if (!st->sock_status) {
st->lastreply = now;
st->buffer_status = A_OK;
+
+ if (setting_verbose() >= INFO) {
+ ipstring = inet_ntoa(st->clientaddr.sin_addr);
+ delta = rin_time_sub(&now, &st->start_time);
+ fprintf(stderr, "packet from %s received at %li.%06li\n", ipstring, delta.tv_sec, delta.tv_nsec / 1000);
+ }
}
- /* no packets in five seconds */
- if ((now.tv_sec - st->lastreply.tv_sec) >= 20) {
+ /* timeout if no packets in over minute */
+ delta = rin_time_sub(&now, &st->lastreply);
+ if (rin_time_cmp_more(&delta, &timeout)) {
st->sock_status = DEAD;
};
if (oldstatus != st->sock_status && st->sock_status != NONEWDATA) {
oldstatus = st->sock_status;
- if (st->sock_status == DEAD) {
- /* this timestamp is arbitraty */
- setting_verbose() >= INFO ? fprintf(stderr, "Connection with the client has been lost. Last reply since: %li \n", st->lastreply.tv_sec) : 0;
+ if (st->sock_status == DEAD && setting_verbose() >= INFO) {
+ delta = rin_time_sub(&now, &st->start_time);
+ fprintf(stderr, "Connection with the client has been lost. Last reply was: %li.%06lis ago\n", delta.tv_sec, delta.tv_nsec / 1000);
}
}
@@ -297,7 +272,6 @@ static int getpacket(char *data, size_t buffsize, ssize_t *recvbufsize, int sock
{
static struct pollfd pfd = {0};
int ret;
- char *ipstring;
socklen_t sender_len = sizeof(*sender);
if (!pfd.fd) {
@@ -316,8 +290,6 @@ static int getpacket(char *data, size_t buffsize, ssize_t *recvbufsize, int sock
if (*recvbufsize < 0) {
ret = ERR;
} else {
- ipstring = inet_ntoa(sender->sin_addr);
- setting_verbose() >= INFO ? fprintf(stderr, "packet from %s received\n", ipstring) : 0;
ret = A_OK;
}
} else {