diff options
Diffstat (limited to 'src/daemon')
-rw-r--r-- | src/daemon/main.c | 79 | ||||
-rw-r--r-- | src/daemon/net.c | 74 | ||||
-rw-r--r-- | src/daemon/proto_stdio.c | 15 | ||||
-rw-r--r-- | src/daemon/proto_stdio_private.h | 5 | ||||
-rw-r--r-- | src/daemon/settings_private.h | 4 |
5 files changed, 98 insertions, 79 deletions
diff --git a/src/daemon/main.c b/src/daemon/main.c index 07da36d..fd7932e 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -37,6 +37,7 @@ static struct _state { static void cleanup(void); static int __main_loop(const struct timespec * const iter_len); +static int does_my_kokoro_go_doki_doki(struct tlv *tlv); /* the logic is a placeholder right now */ int main(int argc, char **argv) @@ -79,40 +80,49 @@ int main(int argc, char **argv) /* TODO: semi-stub */ static int __main_loop(const struct timespec * const iter_len) { - struct tlv field; struct tlv_packet pack; + struct tlv field; struct timespec t; char *buf = NULL; size_t recvsize; int ret = A_OK; t = *iter_len; - tlv_init(&field, HEARTBEAT); - field.data = strdup(heartbeat_server); - field.head.size = sizeof(heartbeat_server); tlv_packet_init(&pack); - tlv_pack(&pack, &field); - tlv_packet_finalize(&pack); - tlv_destroy(&field); - - /* flush any pending discovery packets from the socker, - * as those can be quite numerous on startup. - */ - net_flush_read_buffer(__progstate.nd); while (ret != ERROR) { ret = net_getlastdata(__progstate.nd, &buf, &recvsize); - if (ret == A_OK) { - /* we're only sending one TLV per packet as of now, - * so no need to save the pointer for further parsing - */ - ret = tlv_get(buf, &field, NULL); - if (ret == A_OK && field.head.type == HEARTBEAT && !(strcmp(field.data, heartbeat_device))) { - /* error checking? what error checking? */ - ret = net_send(__progstate.nd, pack.data, pack.cursor + 1); + if (ret != A_OK) { + goto skiprecv; + } + /* we're only sending one TLV per packet as of now, + * so no need to save the pointer for further parsing + */ + ret = tlv_get(buf, &field, NULL); + + /* handles discovery */ + if (ret != A_OK || !(does_my_kokoro_go_doki_doki(&field))) { + goto skiprecv; + } + + /* handles receiving messages */ + if (field.head.type == TEXT) { + message_receive(field.data); + } +/* jumping here if did not receive any data */ +skiprecv: + /* handles sending messages */ + if (message_send(&field)) { + if (field.head.size) { + tlv_pack(&pack, &field); + tlv_packet_finalize(&pack); + tlv_destroy(&field); + net_send(__progstate.nd, pack.data, pack.cursor + 1); + tlv_packet_reset(&pack); } } + nanosleep(&t, NULL); } @@ -121,6 +131,35 @@ static int __main_loop(const struct timespec * const iter_len) return ret; } +static int does_my_kokoro_go_doki_doki(struct tlv *tlv) +{ + int ret = ERROR; + static struct tlv_packet pack; + static size_t firstrun = 1; + + if (firstrun) { + struct tlv field; + tlv_init(&field, HEARTBEAT); + field.data = strdup(heartbeat_server); + field.head.size = sizeof(heartbeat_server); + tlv_packet_init(&pack); + tlv_pack(&pack, &field); + tlv_packet_finalize(&pack); + tlv_destroy(&field); + firstrun = 0; + } + + if (tlv->head.type == HEARTBEAT && !(strcmp(tlv->data, heartbeat_device))) { + if (setting_verbose() >= INFO) { + fprintf(stderr, "heartbeat.\n"); + } + + ret = net_send(__progstate.nd, pack.data, pack.cursor + 1); + } + + return ret; +} + static void cleanup(void) { purple_close(); 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 { diff --git a/src/daemon/proto_stdio.c b/src/daemon/proto_stdio.c index 9aecdf0..6735746 100644 --- a/src/daemon/proto_stdio.c +++ b/src/daemon/proto_stdio.c @@ -23,7 +23,6 @@ #include "proto_stdio.h" #include "proto_stdio_private.h" - void message_receive(char *arg) { int cancelstate; @@ -37,19 +36,25 @@ void message_receive(char *arg) nanosleep(&respite, NULL); } -char *message_send(void) +struct tlv *message_send(struct tlv *in) { - char *ret = NULL; + struct tlv *ret = in; int cancelstate; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate); pthread_mutex_lock(&state.out_m); if (state.readbuf) { - ret = strdup(state.readbuf); + tlv_init(ret, TEXT); + ret->data = malloc(state.bytes_read); + ret->head.size = state.bytes_read; + memcpy(ret->data, state.readbuf, state.bytes_read); free(state.readbuf); state.readbuf = NULL; state.readbufsize = 0; + state.bytes_read = 0; + } else { + ret = NULL; } pthread_mutex_unlock(&state.out_m); @@ -70,7 +75,7 @@ static void *read_stdin(void *arg) pthread_mutex_lock(&state.in_m); if (!state.readbuf) { - state.readbufsize = getline(&state.readbuf, NULL, stdin); + state.bytes_read = getline(&state.readbuf, &state.readbufsize, stdin); } pthread_mutex_unlock(&state.in_m); diff --git a/src/daemon/proto_stdio_private.h b/src/daemon/proto_stdio_private.h index 209d191..a0b891a 100644 --- a/src/daemon/proto_stdio_private.h +++ b/src/daemon/proto_stdio_private.h @@ -35,9 +35,10 @@ static struct state { pthread_mutex_t out_m; char *readbuf; char *writebuf; - ssize_t readbufsize; + size_t readbufsize; + ssize_t bytes_read; ssize_t writebufsize; -} state = {PTHREAD_MUTEX_INITIALIZER, 0, 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, NULL, NULL, 0, 0}; +} state = {PTHREAD_MUTEX_INITIALIZER, 0, 0, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, NULL, NULL, 0, 0, 0}; static void *read_stdin(void *arg); static void *write_stdout(void *arg); diff --git a/src/daemon/settings_private.h b/src/daemon/settings_private.h index 69ed25a..f345a74 100644 --- a/src/daemon/settings_private.h +++ b/src/daemon/settings_private.h @@ -22,7 +22,9 @@ #ifndef USURPATION_SETTINGS_PRIVATE_H #define USURPATION_SETTINGS_PRIVATE_H -#ifndef USURP_VERBOSITY +#if DEBUG_BUILD > 0 + #define USURP_VERBOSITY INFO +#else #define USURP_VERBOSITY ERR #endif |