summaryrefslogtreecommitdiff
path: root/ecp/src
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2022-03-15 04:33:24 +0100
committerUros Majstorovic <majstor@majstor.org>2022-03-15 04:33:24 +0100
commit620c0560567b1143afb0477c6c2ec9f44ab63a77 (patch)
tree177392c4cbc82736eda171a65afef9900fda171b /ecp/src
parentfb521b5d33ccb57c2f56d0548f172edbe31e9e91 (diff)
added thd locks for dir server
Diffstat (limited to 'ecp/src')
-rw-r--r--ecp/src/ecp/core.h11
-rw-r--r--ecp/src/ecp/dir/dir_srv.c85
-rw-r--r--ecp/src/ecp/dir/dir_srv.h17
3 files changed, 92 insertions, 21 deletions
diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h
index b5c260f..6fb165f 100644
--- a/ecp/src/ecp/core.h
+++ b/ecp/src/ecp/core.h
@@ -20,9 +20,10 @@
#define ECP_ERR_MTYPE -9
#define ECP_ERR_CTYPE -10
#define ECP_ERR_HANDLER -11
+#define ECP_ERR_COOKIE -12
-#define ECP_ERR_NET_ADDR -12
-#define ECP_ERR_MAX_PARENT -13
+#define ECP_ERR_NET_ADDR -13
+#define ECP_ERR_MAX_PARENT -14
#define ECP_ERR_ECDH_KEY_DUP -21
#define ECP_ERR_ECDH_IDX -22
@@ -242,8 +243,7 @@ typedef struct ECPContext {
ecp_conn_free_t conn_free;
ECPConnHandler *handler[ECP_MAX_CTYPE];
#ifdef ECP_WITH_DIRSRV
- struct ECPDirList *dir_online;
- struct ECPDirList *dir_shadow;
+ struct ECPDirSrv *dir_srv;
#endif
} ECPContext;
@@ -318,6 +318,9 @@ int ecp_sock_dhkey_get(ECPSocket *sock, unsigned char idx, ECPDHKey *key);
int ecp_sock_dhkey_get_pub(ECPSocket *sock, unsigned char *idx, ecp_ecdh_public_t *public);
void ecp_sock_get_nonce(ECPSocket *sock, ecp_nonce_t *nonce);
+int ecp_cookie_gen(ECPSocket *sock, unsigned char *cookie, unsigned char *public_buf);
+int ecp_cookie_verify(ECPSocket *sock, unsigned char *cookie, unsigned char *public_buf);
+
ECPConnection *ecp_sock_keys_search(ECPSocket *sock, ecp_ecdh_public_t *public);
int ecp_sock_keys_insert(ECPSocket *sock, ecp_ecdh_public_t *public, ECPConnection *conn);
void ecp_sock_keys_remove(ECPSocket *sock, ecp_ecdh_public_t *public);
diff --git a/ecp/src/ecp/dir/dir_srv.c b/ecp/src/ecp/dir/dir_srv.c
index b4d030d..cbaafc5 100644
--- a/ecp/src/ecp/dir/dir_srv.c
+++ b/ecp/src/ecp/dir/dir_srv.c
@@ -6,14 +6,38 @@
#include "dir.h"
#include "dir_srv.h"
-int ecp_dir_init(ECPContext *ctx, ECPDirList *dir_online, ECPDirList *dir_shadow) {
- ctx->dir_online = dir_online;
- ctx->dir_shadow = dir_shadow;
+int ecp_dir_create(ECPContext *ctx, ECPDirSrv *dir_srv, ECPDirList *dir_online, ECPDirList *dir_shadow) {
+ int rv;
+
+#ifdef ECP_WITH_PTHREAD
+ rv = pthread_mutex_init(&dir_srv->online.mutex, NULL);
+ if (rv) return ECP_ERR;
+
+ rv = pthread_mutex_init(&dir_srv->shadow.mutex, NULL);
+ if (rv) {
+ pthread_mutex_destroy(&dir_srv->online.mutex);
+ return ECP_ERR;
+ }
+#endif
+
+ dir_srv->online.list = dir_online;
+ dir_srv->shadow.list = dir_shadow;
+ ctx->dir_srv = dir_srv;
return ECP_OK;
}
-ssize_t ecp_dir_send_list(ECPConnection *conn, unsigned char mtype, ECPDirList *list) {
+void ecp_dir_destroy(ECPContext *ctx) {
+ ECPDirSrv *dir_srv = ctx->dir_srv;
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_destroy(&dir_srv->shadow.mutex);
+ pthread_mutex_destroy(&dir_srv->online.mutex);
+#endif
+
+}
+
+ssize_t ecp_dir_send_list(ECPConnection *conn, unsigned char mtype, ECPDirTable *dir_table) {
ECPBuffer packet;
ECPBuffer payload;
unsigned char pkt_buf[ECP_MAX_PKT];
@@ -33,7 +57,16 @@ ssize_t ecp_dir_send_list(ECPConnection *conn, unsigned char mtype, ECPDirList *
hdr_size = msg - payload.buffer;
msg_size = payload.size - hdr_size;
- rv = ecp_dir_serialize(list, msg, msg_size);
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&dir_table->mutex);
+#endif
+
+ rv = ecp_dir_serialize(dir_table->list, msg, msg_size);
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_unlock(&dir_table->mutex);
+#endif
+
if (rv < 0) return rv;
rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(rv, mtype), 0);
@@ -41,22 +74,33 @@ ssize_t ecp_dir_send_list(ECPConnection *conn, unsigned char mtype, ECPDirList *
}
ssize_t ecp_dir_send_upd(ECPConnection *conn) {
- ECPContext *ctx = conn->sock->ctx;
+ ECPDirSrv *dir_srv = conn->sock->ctx->dir_srv;
+ ECPDirTable *dir_table = &dir_srv->shadow;
ssize_t rv;
- rv = ecp_dir_send_list(conn, ECP_MTYPE_DIR_UPD, ctx->dir_shadow);
+ rv = ecp_dir_send_list(conn, ECP_MTYPE_DIR_UPD, dir_table);
return rv;
}
ssize_t ecp_dir_handle_upd(ECPConnection *conn, unsigned char *msg, size_t msg_size) {
- ECPContext *ctx = conn->sock->ctx;
+ ECPDirSrv *dir_srv = conn->sock->ctx->dir_srv;
+ ECPDirTable *dir_table = &dir_srv->shadow;
ssize_t rsize;
ssize_t rv;
- rsize = ecp_dir_parse(ctx->dir_shadow, msg, msg_size);
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&dir_table->mutex);
+#endif
+
+ rsize = ecp_dir_parse(dir_table->list, msg, msg_size);
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_unlock(&dir_table->mutex);
+#endif
+
if (rsize < 0) return rsize;
- rv = ecp_dir_send_list(conn, ECP_MTYPE_DIR_REP, ctx->dir_shadow);
+ rv = ecp_dir_send_list(conn, ECP_MTYPE_DIR_REP, dir_table);
if (rv < 0) return rv;
return rsize;
@@ -72,17 +116,28 @@ ssize_t ecp_dir_handle_req(ECPConnection *conn, unsigned char *msg, size_t msg_s
}
ssize_t ecp_dir_send_rep(ECPConnection *conn) {
- ECPContext *ctx = conn->sock->ctx;
+ ECPDirSrv *dir_srv = conn->sock->ctx->dir_srv;
+ ECPDirTable *dir_table = &dir_srv->online;
ssize_t rv;
- rv = ecp_dir_send_list(conn, ECP_MTYPE_DIR_REP, ctx->dir_online);
+ rv = ecp_dir_send_list(conn, ECP_MTYPE_DIR_REP, dir_table);
return rv;
}
ssize_t ecp_dir_handle_rep(ECPConnection *conn, unsigned char *msg, size_t msg_size) {
- ECPContext *ctx = conn->sock->ctx;
+ ECPDirSrv *dir_srv = conn->sock->ctx->dir_srv;
+ ECPDirTable *dir_table = &dir_srv->shadow;
ssize_t rv;
- rv = ecp_dir_parse(ctx->dir_shadow, msg, msg_size);
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&dir_table->mutex);
+#endif
+
+ rv = ecp_dir_parse(dir_table->list, msg, msg_size);
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_unlock(&dir_table->mutex);
+#endif
+
return rv;
-} \ No newline at end of file
+}
diff --git a/ecp/src/ecp/dir/dir_srv.h b/ecp/src/ecp/dir/dir_srv.h
index 85606c8..5e107df 100644
--- a/ecp/src/ecp/dir/dir_srv.h
+++ b/ecp/src/ecp/dir/dir_srv.h
@@ -1,5 +1,18 @@
-int ecp_dir_init(ECPContext *ctx, ECPDirList *dir_online, ECPDirList *dir_shadow);
-ssize_t ecp_dir_send_list(ECPConnection *conn, unsigned char mtype, ECPDirList *list);
+typedef struct ECPDirTable {
+ ECPDirList *list;
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_t mutex;
+#endif
+} ECPDirTable;
+
+typedef struct ECPDirSrv {
+ ECPDirTable online;
+ ECPDirTable shadow;
+} ECPDirSrv;
+
+int ecp_dir_create(ECPContext *ctx, ECPDirSrv *dir_srv, ECPDirList *dir_online, ECPDirList *dir_shadow);
+void ecp_dir_destroy(ECPContext *ctx);
+ssize_t ecp_dir_send_list(ECPConnection *conn, unsigned char mtype, ECPDirTable *list);
ssize_t ecp_dir_send_upd(ECPConnection *conn);
ssize_t ecp_dir_handle_upd(ECPConnection *conn, unsigned char *msg, size_t msg_size);