#include #include #include #include #include #include #include #include "core.h" #ifdef __linux__ #define SIGINFO SIGTSTP #endif #define NUM_S 32 #define NUM_C 256 #define MSG_RATE 65 #define CTYPE_TEST 0 #define MTYPE_MSG 8 ECPContext ctx_s; ECPDHKey key_perma_s; ECPConnHandler handler_s; ECPSocket *sock_s; ECPConnHandler handler_c; ECPContext *ctx_c; ECPSocket *sock_c; ECPDHKey *key_perma_c; ECPNode *node; ECPConnection *conn; pthread_t *s_thd; pthread_t *r_thd; pthread_mutex_t *t_mtx; int *t_sent, *t_rcvd; uint64_t t_start = 0, t_end = 0; int c_start = 0; int num_s = NUM_S, num_c = NUM_C; int msg_rate = MSG_RATE; static void display(void) { int i, s = 0, r = 0; for (i=0; iconn_data); ECPBuffer packet; ECPBuffer payload; unsigned char pkt_buf[ECP_MAX_PKT]; unsigned char pld_buf[ECP_MAX_PLD]; packet.buffer = pkt_buf; packet.size = ECP_MAX_PKT; payload.buffer = pld_buf; payload.size = ECP_MAX_PLD; printf("OPEN:%d\n", idx); while(1) { uint32_t rnd; c->sock->ctx->rng(&rnd, sizeof(uint32_t)); usleep(rnd % (2000000/msg_rate)); ecp_pld_set_type(payload.buffer, payload.size, MTYPE_MSG); ssize_t _rv = 0; // XXX refactor // _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0)); if (c_start && (_rv > 0)) { pthread_mutex_lock(&t_mtx[idx]); t_sent[idx]++; pthread_mutex_unlock(&t_mtx[idx]); } } } ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { int idx = (int)(conn->conn_data); int rv = 0; ecp_conn_handle_open(conn, sq, t, p, s, b); rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)conn); if (rv) { char msg[256]; sprintf(msg, "THD %d CREATE\n", idx); perror(msg); exit(1); } return s; } ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { int idx = (int)(conn->conn_data); ECPBuffer packet; ECPBuffer payload; unsigned char pkt_buf[ECP_MAX_PKT]; unsigned char pld_buf[ECP_MAX_PLD]; packet.buffer = pkt_buf; packet.size = ECP_MAX_PKT; payload.buffer = pld_buf; payload.size = ECP_MAX_PLD; if (c_start) { pthread_mutex_lock(&t_mtx[idx]); t_rcvd[idx]++; pthread_mutex_unlock(&t_mtx[idx]); } // ecp_pld_set_type(payload.buffer, payload.size, MTYPE_MSG); // ssize_t _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0)); return s; } ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { ECPBuffer packet; ECPBuffer payload; unsigned char pkt_buf[ECP_MAX_PKT]; unsigned char pld_buf[ECP_MAX_PLD]; packet.buffer = pkt_buf; packet.size = ECP_MAX_PKT; payload.buffer = pld_buf; payload.size = ECP_MAX_PLD; ecp_pld_set_type(payload.buffer, payload.size, MTYPE_MSG); // XXX refactor // ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(1000, 0)); return s; } int main(int argc, char *argv[]) { char addr[256]; int rv; int i; ECPConnHandler handler_c; ECPContext *ctx_c; ECPSocket *sock_c; ECPDHKey *key_perma_c; ECPNode *node; ECPConnection *conn; sock_s = malloc(num_s * sizeof(ECPSocket)); ctx_c = malloc(num_c * sizeof(ECPContext)); sock_c = malloc(num_c * sizeof(ECPSocket)); key_perma_c = malloc(num_c * sizeof(ECPDHKey)); node = malloc(num_c * sizeof(ECPNode)); conn = malloc(num_c * sizeof(ECPConnection)); s_thd = malloc(num_c * sizeof(pthread_t)); r_thd = malloc(num_c * sizeof(pthread_t)); t_mtx = malloc(num_c * sizeof(pthread_mutex_t)); t_sent = malloc(num_c * sizeof(int)); t_rcvd = malloc(num_c * sizeof(int)); memset(t_rcvd, 0, num_c * sizeof(int)); memset(t_sent, 0, num_c * sizeof(int)); struct sigaction actINFO; memset(&actINFO, 0, sizeof(actINFO)); actINFO.sa_handler = &catchINFO; sigaction(SIGINFO, &actINFO, NULL); rv = ecp_init(&ctx_s); if (!rv) rv = ecp_conn_handler_init(&handler_s); handler_s.msg[MTYPE_MSG] = handle_msg_s; ctx_s.handler[CTYPE_TEST] = &handler_s; if (!rv) rv = ecp_dhkey_gen(&ctx_s, &key_perma_s); for (i=0; i