summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-05-23 11:42:08 +0300
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-05-27 14:37:26 +0300
commitaef4eba5572d6b42f8ef2913ec41c0e778731960 (patch)
tree2eaa07f03734ef69ed1f93f7ba9ce59e1ad12c5a
parent1fffc262c572e8ba4b962af329fb0d821d95b883 (diff)
downloadusurpation-aef4eba5572d6b42f8ef2913ec41c0e778731960.tar.gz
usurpation-aef4eba5572d6b42f8ef2913ec41c0e778731960.tar.bz2
usurpation-aef4eba5572d6b42f8ef2913ec41c0e778731960.zip
dameon: net: implement sending data.
Also, dropped some adhockery and rebased some code. Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
-rw-r--r--include/net.h7
-rw-r--r--include/settings.h2
-rw-r--r--src/daemon/net.c95
3 files changed, 79 insertions, 25 deletions
diff --git a/include/net.h b/include/net.h
index e4ca0a6..02767dc 100644
--- a/include/net.h
+++ b/include/net.h
@@ -22,6 +22,9 @@
#ifndef USURPATION_NET_H_INCLUDED
#define USURPATION_NET_H_INCLUDED
+#include <netinet/in.h>
+#include <stddef.h>
+
#define MTU 1500
enum response {
@@ -29,8 +32,6 @@ enum response {
A_OK = 0, /* would be "OK", but clashes with some lib definitions */
NONEWDATA,
DEAD,
- NO_ESPTOOL,
- TMPFILE,
};
/**
@@ -49,5 +50,7 @@ int net_close(int nd);
* and needs to be free()'d later. Otherwise, the supplied buffer is reused.
*/
int net_getlastdata(int nd, char ** const data);
+int net_send(int nd, const char * const buf, size_t buf_size);
+int net_send_addr(int nd, const char * const buf, size_t buf_size, const struct sockaddr_in * const addr);
#endif /* USURPATION_NET_H_INCLUDED */
diff --git a/include/settings.h b/include/settings.h
index e2e9644..538c7fd 100644
--- a/include/settings.h
+++ b/include/settings.h
@@ -26,7 +26,7 @@ enum verbosity {
SILENT = 0,
ERR = 1,
WARN,
- DEBUG,
+ INFO,
ALL
};
diff --git a/src/daemon/net.c b/src/daemon/net.c
index 9180344..29f1fde 100644
--- a/src/daemon/net.c
+++ b/src/daemon/net.c
@@ -45,10 +45,11 @@ struct netstate {
size_t bufsize;
unsigned int available;
int status;
- pthread_mutex_t datamutex;
+ pthread_mutex_t mutex;
+ struct sockaddr_in clientaddr;
};
-static pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static struct netstate *state;
static ssize_t count;
static size_t available_count;
@@ -63,7 +64,7 @@ int net_init(const unsigned short int port)
ssize_t i;
int sizechanged = 0;
- pthread_mutex_lock(&initmutex);
+ pthread_mutex_lock(&mutex);
if (available_count) {
for (i = 0; !state[i].available; ++i) {
if (i == count) {
@@ -98,7 +99,7 @@ int net_init(const unsigned short int port)
memset(state[i].data, 0, MTU);
state[i].bufsize = MTU;
state[i].status = NONEWDATA;
- pthread_mutex_init(&state[i].datamutex, NULL);
+ pthread_mutex_init(&state[i].mutex, NULL);
if (pthread_create(&state[i].listner, NULL, dolisten, state + i)) {
ret = ERROR;
@@ -111,7 +112,7 @@ out:
state = realloc(state, sizeof(struct netstate) * --count);
}
- pthread_mutex_unlock(&initmutex);
+ pthread_mutex_unlock(&mutex);
return ret;
}
@@ -119,7 +120,7 @@ int net_close(int nd)
{
int ret;
- pthread_mutex_lock(&initmutex);
+ pthread_mutex_lock(&mutex);
if (nd > count || nd < 1 || state[nd - 1].available) {
ret = ERROR;
} else {
@@ -132,7 +133,7 @@ int net_close(int nd)
ret = A_OK;
}
- pthread_mutex_unlock(&initmutex);
+ pthread_mutex_unlock(&mutex);
return ret;
}
@@ -158,14 +159,69 @@ out:
return ret;
}
-static void *dolisten(void * state)
+int net_send(int nd, const char * const buf, size_t buf_size)
{
+ int ret = A_OK;
+ ssize_t sent;
+ struct timespec now;
+
+ if (nd > count || nd < 1 || state[--nd].available) {
+ ret = ERROR;
+ } else {
+ if (setting_verbose() >= INFO) {
+ clock_gettime(CLOCK_MONOTONIC_RAW, &now);
+ fprintf(stderr, "Sending DATA, timestap: %li \n", now.tv_sec);
+ }
- static const char servermagic[] = "I love coffee!";
- static const char clientmagic[] = "I love tea!";
+ sent = sendto( state[nd].sock, buf, buf_size, MSG_DONTWAIT,
+ (struct sockaddr *) &state[nd].clientaddr,
+ sizeof(state[nd].clientaddr));
+
+ if (sent == -1) {
+ ret = DEAD;
+ if (setting_verbose() >= ERR) {
+ fprintf(stderr, "Sending packet to %s failed.\n", inet_ntoa(state[nd].clientaddr.sin_addr));
+ }
+ }
+ }
+
+ return ret;
+}
+
+int net_send_addr(int nd, const char * const buf, size_t buf_size, const struct sockaddr_in * const addr)
+{
+ int ret = A_OK;
+ ssize_t sent;
+ struct timespec now;
+
+
+ if (nd > count || nd < 1 || state[--nd].available) {
+ ret = ERROR;
+ } else {
+ if (setting_verbose() >= INFO) {
+ clock_gettime(CLOCK_MONOTONIC_RAW, &now);
+ fprintf(stderr, "Sending DATA, timestap: %li \n", now.tv_sec);
+ }
+
+ sent = sendto( state[nd].sock, buf, buf_size, MSG_DONTWAIT,
+ (struct sockaddr *) addr,
+ sizeof(*addr));
+
+ if (sent == -1) {
+ ret = DEAD;
+ if (setting_verbose() >= ERR) {
+ fprintf(stderr, "Sending packet to %s failed.\n", inet_ntoa(addr->sin_addr));
+ }
+ }
+ }
+
+ return ret;
+}
+
+static void *dolisten(void * state)
+{
struct timespec now;
struct timespec wait = {0, 10 * 1000 * 1000}; /* 10 ms */
- struct sockaddr_in clientaddr;
struct netstate *st;
ssize_t recvbufsize;
int cancelstate;
@@ -176,17 +232,12 @@ static void *dolisten(void * state)
do {
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
- pthread_mutex_lock(&st->datamutex);
+ pthread_mutex_lock(&st->mutex);
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
- st->status = getpacket(st->data, st->bufsize, &recvbufsize, st->sock, &clientaddr);
+ st->status = getpacket(st->data, st->bufsize, &recvbufsize, st->sock, &st->clientaddr);
if (!st->status) {
st->lastreply = now;
- if (st->data[0] == 'I' && !(strcmp(st->data, servermagic))) {
- sendto(st->sock, clientmagic, sizeof(clientmagic),
- MSG_DONTWAIT, (struct sockaddr *) &clientaddr, sizeof(clientaddr));
- setting_verbose() ? fprintf(stderr, "Sending DATA, timestap: %li \n", st->lastreply.tv_sec) : 0;
- }
}
/* no packets in five seconds */
@@ -201,13 +252,13 @@ static void *dolisten(void * state)
oldstatus = st->status;
if(st->status == DEAD) {
/* this timestamp is arbitraty */
- setting_verbose() ? fprintf(stderr, "Connection with the client has been lost. Last reply since: %li \n", st->lastreply.tv_sec) : 0;
+ setting_verbose() >= INFO ? fprintf(stderr, "Connection with the client has been lost. Last reply since: %li \n", st->lastreply.tv_sec) : 0;
} else {
- ipstring = inet_ntoa(clientaddr.sin_addr);
- setting_verbose() ? fprintf(stderr, "Successful incoming connection from %s\n", ipstring) : 0;
+ ipstring = inet_ntoa(st->clientaddr.sin_addr);
+ setting_verbose() >= INFO ? fprintf(stderr, "Successful incoming connection from %s\n", ipstring) : 0;
}
}
- pthread_mutex_unlock(&st->datamutex);
+ pthread_mutex_unlock(&st->mutex);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &cancelstate);
pthread_testcancel();