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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../PICA_client.h"
struct sockaddr_in a;
struct PICA_conninfo *c;
struct PICA_chaninfo *chn;
int _outgoing_chnl=0;
unsigned char buf[64*1024];
int accept_cb(unsigned int id)
{
printf("accept_cb: %u\n",id);
return 1;
}
//получение сообщения.
void newmsg_cb(unsigned int peer_id,char* msgbuf,unsigned int nb,int type)
{
memcpy(buf,msgbuf,nb);
buf[nb]=0;
printf(" %u: ",peer_id);
puts(buf);
}
//получение подтверждения о доставке сообщения
void msgok_cb(unsigned int peer_id)
{
puts("[V]");
}
//создание канала с собеседником
void channel_established_cb(unsigned int peer_id)
{
puts("channel_established_cb");
}
void channel_failed_cb(unsigned int peer_id)
{
printf("Failed to create channel to %u\n",peer_id);
chn=0;
}
//входящий запрос на создание канала от пользователя с номером caller_id
//<<int accept_cb(unsigned int caller_id);
//запрошенный пользователь не найден, в оффлайне или отказался от общения
void notfound_cb(unsigned int callee_id)
{
puts("notfound_cb");
}
void channel_closed_cb(unsigned int peer_id, int reason)
{
printf("channel_closed_cb( peer_id = %u, reason = %i)\n", peer_id, reason);
}
void nodelist_cb(int type, void *addr_bin, const char *addr_str, unsigned int port)
{
switch(type)
{
case 0xA0:
printf("node (IP %s port %u)\n", addr_str, port);
break;
case 0xA1:
printf("node (IPv6 %s port %u)", addr_str, port);
break;
case 0xA2:
printf("node (hostname %s port %u)\n", addr_str, port);
break;
default:
puts("unknown node address type");
return;
}
}
int peer_cert_verify_cb(unsigned int peer_id, const char *cert_pem, unsigned int nb)
{
printf("peer's certificate (%u): \n%.*s\n", peer_id, nb,cert_pem);
return 1;
}
struct PICA_client_callbacks cbs = {
newmsg_cb,
msgok_cb,
channel_established_cb,
channel_failed_cb,
accept_cb,
notfound_cb,
channel_closed_cb,
nodelist_cb,
peer_cert_verify_cb
};
int main(int argc,char** argv)
{
int ret;
unsigned int peer_id;
if (argc<3)
{
puts("usage: test_client address cert_filename [peer id]");
return 0;
}
puts(SSLeay_version(SSLEAY_VERSION));
printf("Calling PICA_init...\n");
PICA_client_init(&cbs);
// printf("setting address...\n");
//
// memset(&a,0,sizeof(a));
//
// a.sin_family = AF_INET;
// a.sin_addr.s_addr = inet_addr (argv[1]);//("127.0.0.1");
// a.sin_port = htons(51914);
printf("making connection...\n");
//PICA_new_connection(const char *nodeaddr, unsigned int port, const char *CA_file, const char *cert_file, const char *pkey_file, const char* password, struct PICA_conninfo **ci)
ret=PICA_new_connection(argv[1], 51914,"trusted_CA.pem",argv[2],argv[2],NULL,&c);
ERR_print_errors_fp(stdout);
if (ret==PICA_OK)
printf("PICA_OK Connected to server...\n");
else
{
printf("PICA_new_connection: ret=%i\n",ret);
return 1;
}
if (argc==4)
{
peer_id=atoi(argv[3]);
printf("Creating channel to %u...\n",peer_id);
ret=PICA_create_channel(c,peer_id,&chn);
//sleep(17);//timeout test
if (ret!=PICA_OK)
{
printf("error: ret=%i\n",ret);
return 1;
}
}
do
{
if (chn && chn->state==PICA_CHANSTATE_ACTIVE)
{
unsigned int msglen;
gets(buf);
msglen=strlen(buf);
ret=PICA_send_msg(chn,buf,msglen);
if (ret!=PICA_OK)
printf("PICA_send_msg ret=%i\n",ret);
}
}
while(PICA_read(c,100000) == PICA_OK && PICA_write(c) == PICA_OK);
/*
puts("Enter peer_id to talk with or . for waiting incoming sessions:");
gets(buf);
if (buf[0]=='.')
{
unsigned int br;
ret=PICA_read_conn(c,accept_cb,&chn);
if (ret==PICA_NEWCHAN)
{
puts("PICA_NEWCHAN!");
}
else
printf("read_conn error: ret=%i\n",ret);
ret=PICA_read_msg(chn,buf,&br);
switch(ret)
{
case PICA_MSGUTF8:
printf("%u (%u bytes): ",c->id,br);
puts(buf);
break;
case PICA_MSGOK:
puts("[v]");
break;
default:
printf("PICA_read_msg ret=%i\n",ret);
}
}
else
{
peer_id=atoi(buf);
printf("Creating channel to %u...\n",peer_id);
ret=PICA_create_channel(c,peer_id,&chn,1);
if (ret==PICA_OK)
{
unsigned int msglen;
puts("Channel established...");
gets(buf);
msglen=strlen(buf);
ret=PICA_send_msg(chn,buf,msglen);
if (ret!=PICA_OK)
printf("PICA_send_msg ret=%i\n",ret);
}
else
printf("error: ret=%i\n",ret);
}
*/
sleep(10);
if (chn)
{
puts("--------");
PICA_close_channel(chn);
}
PICA_close_connection(c);
return 0;
}
|