summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2018-01-11 15:32:30 +0100
committerUros Majstorovic <majstor@majstor.org>2018-01-11 15:32:30 +0100
commiteb8c9fe176721d5e8dd24febec88e1665ca70c26 (patch)
treeff5e58d57867f8a8283a04f0a008a93fee3d2c02
parent848f44266dd4b9213af632d55747d66f03881422 (diff)
transport refactoring
-rw-r--r--code/core/core.c37
-rw-r--r--code/core/core.h9
-rw-r--r--code/core/posix/transport.c34
3 files changed, 38 insertions, 42 deletions
diff --git a/code/core/core.c b/code/core/core.c
index b273fcd..6eecce2 100644
--- a/code/core/core.c
+++ b/code/core/core.c
@@ -20,7 +20,7 @@ int ecp_node_init(ECPContext *ctx, ECPNode *node, ecp_dh_public_t *public, void
memset(node, 0, sizeof(ECPNode));
memcpy(&node->public, public, sizeof(node->public));
- if (addr) rv = ctx->tr.addr_set(&node->addr, addr);
+ if (addr && ctx->tr.addr_set) rv = ctx->tr.addr_set(&node->addr, addr);
if (rv) return ECP_ERR_NET_ADDR;
return ECP_OK;
@@ -123,9 +123,11 @@ static int ctable_insert(ECPConnection *conn) {
}
#endif
} else {
+#ifndef ECP_WITH_HTABLE
if (sock->conn.size == ECP_MAX_SOCK_CONN) return ECP_ERR_MAX_SOCK_CONN;
sock->conn.array[sock->conn.size] = conn;
sock->conn.size++;
+#endif
}
return ECP_OK;
@@ -150,6 +152,7 @@ static void ctable_remove(ECPConnection *conn) {
}
#endif
} else {
+#ifndef ECP_WITH_HTABLE
for (i=0; i<sock->conn.size; i++) {
if (conn == sock->conn.array[i]) {
sock->conn.array[i] = sock->conn.array[sock->conn.size-1];
@@ -158,6 +161,7 @@ static void ctable_remove(ECPConnection *conn) {
return;
}
}
+#endif
}
}
@@ -174,6 +178,7 @@ static ECPConnection *ctable_search(ECPSocket *sock, unsigned char c_idx, unsign
return sock->ctx->ht.search(sock->conn.htable, c_public);
#endif
} else {
+#ifndef ECP_WITH_HTABLE
if (c_public) {
for (i=0; i<sock->conn.size; i++) {
conn = sock->conn.array[i];
@@ -192,6 +197,7 @@ static ECPConnection *ctable_search(ECPSocket *sock, unsigned char c_idx, unsign
if (conn->out && conn->sock->ctx->tr.addr_eq(&conn->node.addr, addr)) return conn;
}
}
+#endif
}
return NULL;
@@ -1354,14 +1360,6 @@ ssize_t ecp_pkt_send(ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, s
return rv;
}
-ssize_t ecp_pkt_recv(ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size) {
- ssize_t rv = sock->ctx->tr.recv(&sock->sock, packet, pkt_size, addr);
- if (rv < 0) return rv;
- if (rv < ECP_MIN_PKT) return ECP_ERR_RECV;
-
- return rv;
-}
-
int ecp_seq_item_init(ECPSeqItem *seq_item) {
memset(seq_item, 0, sizeof(ECPSeqItem));
@@ -1671,12 +1669,10 @@ ssize_t ecp_receive(ECPConnection *conn, unsigned char mtype, unsigned char *msg
#endif
}
-static int recv_p(ECPSocket *sock) {
- ECPNetAddr addr;
+static int recv_p(ECPSocket *sock, ECPNetAddr *addr, unsigned char *pkt_buf, size_t size) {
ECP2Buffer bufs;
ECPBuffer packet;
ECPBuffer payload;
- unsigned char pkt_buf[ECP_MAX_PKT];
unsigned char pld_buf[ECP_MAX_PLD];
bufs.packet = &packet;
@@ -1687,23 +1683,24 @@ static int recv_p(ECPSocket *sock) {
payload.buffer = pld_buf;
payload.size = ECP_MAX_PLD;
- ssize_t rv = ecp_pkt_recv(sock, &addr, pkt_buf, ECP_MAX_PKT);
- if (rv < 0) return rv;
-
- rv = ecp_pkt_handle(sock, &addr, NULL, &bufs, rv);
+ ssize_t rv = ecp_pkt_handle(sock, addr, NULL, &bufs, size);
if (rv < 0) return rv;
return ECP_OK;
}
int ecp_receiver(ECPSocket *sock) {
+ ECPNetAddr addr;
+ unsigned char pkt_buf[ECP_MAX_PKT];
ecp_cts_t next = 0;
+
+ if (sock->ctx->tr.recv == NULL) return ECP_ERR;
sock->running = 1;
while(sock->running) {
- int rv = sock->ctx->tr.poll(&sock->sock, next ? next : sock->poll_timeout);
- if (rv == 1) {
- rv = recv_p(sock);
- DPRINT(rv, "ERR:recv_p - RV:%d\n", rv);
+ ssize_t rv = sock->ctx->tr.recv(&sock->sock, pkt_buf, ECP_MAX_PKT, &addr, next ? next : sock->poll_timeout);
+ if (rv > 0) {
+ int _rv = recv_p(sock, &addr, pkt_buf, rv);
+ DPRINT(_rv, "ERR:recv_p - RV:%d\n", _rv);
}
next = ecp_timer_exe(sock);
}
diff --git a/code/core/core.h b/code/core/core.h
index 88a8263..7029bd7 100644
--- a/code/core/core.h
+++ b/code/core/core.h
@@ -183,9 +183,8 @@ typedef struct ECPTransportIface {
int init;
int (*open) (ECPNetSock *, void *addr);
void (*close) (ECPNetSock *);
- int (*poll) (ECPNetSock *, int);
ssize_t (*send) (ECPNetSock *, void *, size_t, ECPNetAddr *);
- ssize_t (*recv) (ECPNetSock *, void *, size_t, ECPNetAddr *);
+ ssize_t (*recv) (ECPNetSock *, void *, size_t, ECPNetAddr *, int);
int (*addr_eq) (ECPNetAddr *, ECPNetAddr *);
int (*addr_set) (ECPNetAddr *, void *addr);
} ECPTransportIface;
@@ -271,10 +270,11 @@ typedef struct ECPConnHandler {
} ECPConnHandler;
typedef struct ECPSockCTable {
- struct ECPConnection *array[ECP_MAX_SOCK_CONN];
- unsigned short size;
#ifdef ECP_WITH_HTABLE
void *htable;
+#else
+ struct ECPConnection *array[ECP_MAX_SOCK_CONN];
+ unsigned short size;
#endif
#ifdef ECP_WITH_PTHREAD
pthread_mutex_t mutex;
@@ -395,7 +395,6 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, uns
ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size);
ssize_t ecp_pkt_send(ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size);
-ssize_t ecp_pkt_recv(ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size);
int ecp_seq_item_init(ECPSeqItem *seq_item);
int ecp_frag_iter_init(ECPFragIter *iter, unsigned char *buffer, size_t buf_size);
diff --git a/code/core/posix/transport.c b/code/core/posix/transport.c
index e3fe612..df8e0f5 100644
--- a/code/core/posix/transport.c
+++ b/code/core/posix/transport.c
@@ -71,13 +71,6 @@ static void t_close(int *sock) {
close(*sock);
}
-static int t_poll(int *sock, int timeout) {
- struct pollfd fds[] = {
- {*sock, POLLIN, 0}
- };
- return poll(fds, 1, timeout);
-}
-
static ssize_t t_send(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr) {
struct sockaddr_in servaddr;
@@ -88,26 +81,33 @@ static ssize_t t_send(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr) {
return sendto(*sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
}
-static ssize_t t_recv(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr) {
+static ssize_t t_recv(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr, int timeout) {
struct sockaddr_in servaddr;
socklen_t addrlen = sizeof(servaddr);
- memset((void *)&servaddr, 0, sizeof(servaddr));
-
- ssize_t recvlen = recvfrom(*sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, &addrlen);
- if (recvlen < 0) return recvlen;
+ struct pollfd fds[] = {
+ {*sock, POLLIN, 0}
+ };
- if (addr) {
- addr->port = servaddr.sin_port;
- memcpy(addr->host, (void *)&servaddr.sin_addr, sizeof(addr->host));
+ int rv = poll(fds, 1, timeout);
+ memset((void *)&servaddr, 0, sizeof(servaddr));
+ if (rv == 1) {
+ ssize_t recvlen = recvfrom(*sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, &addrlen);
+ if (recvlen < 0) return recvlen;
+ if (recvlen < ECP_MIN_PKT) return ECP_ERR_RECV;
+
+ if (addr) {
+ addr->port = servaddr.sin_port;
+ memcpy(addr->host, (void *)&servaddr.sin_addr, sizeof(addr->host));
+ }
+ return recvlen;
}
- return recvlen;
+ return ECP_ERR_TIMEOUT;
}
int ecp_transport_init(ECPTransportIface *t) {
t->init = 1;
t->open = t_open;
t->close = t_close;
- t->poll = t_poll;
t->send = t_send;
t->recv = t_recv;
t->addr_eq = t_addr_eq;