1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <ecp/core.h>
#include <ecp/dir/dir.h>
#include <ecp/dir/dir_client.h>
#include <ecp/vconn/vconn.h>
#include <util.h>
#include "init_vconn.h"
ECPDirList *_dir_list = NULL;
ecp_ecdh_public_t srv_public;
#define CTYPE_TEST 0
#define MTYPE_MSG 0
static void vconn_open(ECPSocket *sock) {
ECPConnection *conn;
ECPVConnOutb *vconn;
ecp_tr_addr_t addr;
ecp_ecdh_public_t vconn_pub[ECP_MAX_PARENT];
ssize_t vconn_size, _rv;
int rv;
_rv = ecp_dir_sel_vconn(_dir_list, &srv_public, vconn_pub, ECP_MAX_PARENT, &addr);
LOG("ecp_dir_sel_vconn", (int)_rv);
vconn_size = _rv;
conn = malloc(sizeof(ECPConnection));
if (conn == NULL) LOG("conn malloc", ECP_ERR_ALLOC);
vconn = malloc(vconn_size * sizeof(ECPVConnOutb));
if (vconn == NULL) LOG("vconn malloc", ECP_ERR_ALLOC);
ecp_conn_init(conn, sock, CTYPE_TEST);
ecp_vconn_init(vconn, vconn_size, conn, sock);
rv = ecp_vconn_open(vconn, vconn_pub, &addr, &srv_public);
LOG("ecp_vconn_open", rv);
}
static int handle_open(ECPConnection *conn, ECP2Buffer *b) {
char *_msg = "PERA JE CAR!";
ssize_t rv;
printf("Press ENTER key to start\n");
while(getchar()!='\n');
rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1);
return ECP_OK;
}
static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) {
printf("MSG:%s size:%ld\n", msg, msg_size);
return msg_size;
}
static void handle_err(ECPConnection *conn, unsigned char mtype, int err) {
if (mtype == ECP_MTYPE_OPEN_REP) {
ecp_dir_set_unavailable(_dir_list, &conn->remote.key_perma.public);
vconn_open(conn->sock);
}
ecp_vconn_close(conn);
}
static void handle_dir_list(ECPConnection *conn, void *list, int err) {
ECPDirList *dir_list = list;
if (err && _dir_list) {
ECPConnection *dir_conn;
ecp_tr_addr_t addr;
ecp_ecdh_public_t public;
int rv;
ecp_dir_set_unavailable(_dir_list, &conn->remote.key_perma.public);
rv = ecp_dir_sel_dir(_dir_list, &public, &addr);
LOG("ecp_dir_sel_dir", rv);
dir_conn = malloc(sizeof(ECPConnection));
if (dir_conn == NULL) LOG("dir_conn malloc", ECP_ERR_ALLOC);
rv = ecp_dir_get(dir_conn, conn->sock, &public, &addr, 0);
LOG("ecp_dir_get", rv);
return;
}
LOG("handle_dir_list", err);
if (_dir_list) ecp_dir_list_destroy(_dir_list);
_dir_list = dir_list;
vconn_open(conn->sock);
}
static void usage(char *arg) {
fprintf(stderr, "Usage: %s <server.pub> <address> <dir.pub>\n", arg);
exit(1);
}
int main(int argc, char *argv[]) {
ECPContext ctx;
ECPSocket sock;
ECPDHKey key_perma;
ECPConnHandler handler;
ECPConnHandler vconn_handler;
ECPConnHandler vlink_handler;
ECPConnHandler dir_handler;
ECPConnection *dir_conn;
ecp_tr_addr_t addr;
ecp_ecdh_public_t public;
int rv;
if (argc != 4) usage(argv[0]);
rv = ecp_init(&ctx, &vconn_handler, &vlink_handler);
LOG("ecp_init", rv);
vconn_handler.handle_err = handle_err;
rv = ecp_dir_set_handler(&ctx, &dir_handler, handle_dir_list);
LOG("ecp_dir_set_handler", rv);
ecp_conn_handler_init(&handler, handle_open, NULL, handle_msg, NULL);
ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler);
rv = ecp_dhkey_gen(&key_perma);
LOG("ecp_dhkey_gen", rv);
rv = ecp_sock_create(&sock, &ctx, &key_perma);
LOG("ecp_sock_create", rv);
rv = ecp_vconn_sock_create(&sock);
LOG("ecp_vconn_sock_create", rv);
rv = ecp_sock_open(&sock, NULL);
LOG("ecp_sock_open", rv);
rv = ecp_start_receiver(&sock);
LOG("ecp_start_receiver", rv);
rv = ecp_util_load_key(argv[1], &srv_public, NULL);
LOG("ecp_util_load_key", rv);
rv = ecp_addr_init(&addr, argv[2]);
LOG("ecp_addr_init", rv);
rv = ecp_util_load_key(argv[3], &public, NULL);
LOG("ecp_util_load_key", rv);
dir_conn = malloc(sizeof(ECPConnection));
if (dir_conn == NULL) LOG("dir_conn malloc", ECP_ERR_ALLOC);
rv = ecp_dir_get(dir_conn, &sock, &public, &addr, 0);
LOG("ecp_dir_get", rv);
while (1) sleep(1);
}
|