-
Notifications
You must be signed in to change notification settings - Fork 5
/
PICA_node.c
3793 lines (3056 loc) · 87 KB
/
PICA_node.c
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
(c) Copyright 2012 - 2018 Anton Sviridenko
https://picapica.im
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <signal.h>
#include "PICA_security.h"
#include "PICA_node.h"
#include "PICA_proto.h"
#include "PICA_msgproc.h"
#include "PICA_nodeaddrlist.h"
#include "PICA_netconf.h"
#include "PICA_common.h"
#include "PICA_nodeconfig.h"
#include "PICA_log.h"
#include "PICA_nodewait.h"
#include "PICA_signverify.h"
#ifdef NO_RAND_DEV
#include "PICA_rand_seed.h"
#endif
#ifdef WIN32
#include <shlobj.h>
#ifdef __MINGW32__
#include <stdint.h>
#endif
typedef u_long in_addr_t;
typedef u_short in_port_t;
typedef u_short uint16_t;
#endif
#define MAX_NEWCONNS 64
#define NEWCONN_TIMEOUT 10 //sec
#define SKYNET_REFRESH_TIMEOUT 93
#define SELECT_TIMEOUT 1
#define NOREPLY_TIMEOUT 15
#define KEEPALIVE_TIMEOUT 60
time_t skynet_refresh_tmst;
clock_t TMO_CCLINK_WAITACTIVE = 15; //CONF
char *my_addr;//TEMP CONF FIXME
static struct event_base *pica_event_base;
SOCKET listen_comm_sck;//*
static int stop_node_loop = 0;
SSL_CTX *ctx;
SSL_CTX *anon_ctx;
struct newconn newconns[MAX_NEWCONNS];
unsigned int newconns_pos = 0;
struct client *client_tree_root;
struct client *client_list_head;
struct client *client_list_end;
struct cclink *cclink_list_head;
struct cclink *cclink_list_end;
unsigned int cclink_list_count;
struct nodelink *nodelink_list_head;
struct nodelink *nodelink_list_end;
unsigned int nodelink_list_count;
const char msg_INITRESPOK[] = {PICA_PROTO_INITRESPOK, PICA_PROTO_INITRESPOK, 'O', 'K'};
const char msg_VERDIFFER[] = {PICA_PROTO_VERDIFFER, PICA_PROTO_VERDIFFER, PICA_PROTO_VER_HIGH, PICA_PROTO_VER_LOW};
unsigned int procmsg_INITREQ(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_CONNID(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_NODECONNREQ(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_CONNREQOUTG(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_CONNALLOW(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_CONNDENY(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_NODERESP(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_NEWNODE(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_NODELISTREQ(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_NODELIST(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_N2NFOUND(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_SEARCH(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_N2NCONNREQOUTG(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_N2NALLOW(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_N2NNOTFOUND(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_N2NMSG(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_PINGREQ_client(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_PINGREP_client(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_PINGREQ_node(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_PINGREP_node(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_dummy_multi_c2n(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_MULTILOGIN_client(unsigned char* buf, unsigned int size, void* ptr);
unsigned int procmsg_MULTILOGIN_node(unsigned char* buf, unsigned int size, void* ptr);
struct cclink* cclink_list_add(struct client *clr, struct client *cle);
struct cclink* cclink_list_search(const unsigned char *caller_id, const unsigned char *callee_id);
struct cclink* cclink_list_findwaitsearch(const unsigned char *callee_id);
void cclink_list_addlocal(struct client *clr, struct client *cle);
void cclink_list_delete(struct cclink *l);
void cclink_list_addn2nclr(struct client *clr, const unsigned char *callee_id);
void cclink_setwaitconn(struct cclink *ccl);
void cclink_list_addn2ncle(const unsigned char *caller_id, struct nodelink *caller_node, struct client *cle);
void cclink_activate(struct cclink *ccl);
void cclink_setwaitanontlsshutdown(struct cclink *link, struct newconn *nc);
void cclink_attach_remotecle_node(struct cclink *ccl, struct nodelink *callee_node);
void cclink_list_delete_by_client(struct client *cl);
void cclink_list_delete_by_nodelink(struct nodelink *node);
struct client* client_tree_search(const unsigned char *id);//
struct client *client_list_addnew(struct newconn *nc);
void client_list_delete(struct client* ci);
struct PICA_proto_msg* client_wbuf_push(struct client *c, unsigned int msgid, unsigned int size);
int client_rbuf_grow(struct client *c);
struct PICA_proto_msg* nodelink_wbuf_push(struct nodelink *nl, unsigned int msgid, unsigned int size);
int nodelink_rbuf_grow(struct nodelink *nl);
int nodelink_attach_nodeaddr(struct nodelink *nl, unsigned int addr_type, void* nodeaddr, unsigned int nodeaddr_size);
struct nodelink *nodelink_list_addnew(struct newconn *nc);
void nodelink_list_delete(struct nodelink *n);
struct nodelink *nodelink_search_by_ipv4addr(in_addr_t addr, in_port_t port); /* arguments are in network byte order */
void newconn_close(struct newconn* nc);
void newconn_free(struct newconn* nc);
struct newconn* newconn_add(struct newconn *ncs, int *pos);
void newconns_init();
void PICA_node_joinskynet(const char* addrlistfilename, const char *my_addr);
void process_listen_read(evutil_socket_t listener, short event, void *arg);
void process_timeout_event(evutil_socket_t listener, short event, void *arg);
void process_newconn_read(evutil_socket_t s, short event, void *arg);
void process_c2n_read(evutil_socket_t s, short event, void *arg);
void process_c2n_write(evutil_socket_t s, short event, void *arg);
void process_n2n_read(evutil_socket_t s, short event, void *arg);
void process_n2n_write(evutil_socket_t s, short event, void *arg);
void process_c2c_read(evutil_socket_t s, short event, void *arg);
void process_c2c_write(evutil_socket_t s, short event, void *arg);
void process_nodewait();
#define MSGINFO_MSGSNUM(arr) (sizeof(arr)/sizeof(struct PICA_msginfo))
struct PICA_msginfo _msginfo_comm[] =
{
{PICA_PROTO_CONNREQOUTG, PICA_MSG_FIXED_SIZE, PICA_PROTO_CONNREQOUTG_SIZE, procmsg_CONNREQOUTG},
{PICA_PROTO_CONNALLOW, PICA_MSG_FIXED_SIZE, PICA_PROTO_CONNALLOW_SIZE, procmsg_CONNALLOW},
{PICA_PROTO_CONNDENY, PICA_MSG_FIXED_SIZE, PICA_PROTO_CONNDENY_SIZE, procmsg_CONNDENY},
{PICA_PROTO_CLNODELISTREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_CLNODELISTREQ_SIZE, procmsg_NODELISTREQ},
{PICA_PROTO_PINGREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_PINGREQ_SIZE, procmsg_PINGREQ_client},
{PICA_PROTO_PINGREP, PICA_MSG_FIXED_SIZE, PICA_PROTO_PINGREP_SIZE, procmsg_PINGREP_client},
{PICA_PROTO_MULTILOGIN, PICA_MSG_VAR_SIZE, PICA_MSG_VARSIZE_INT16, procmsg_MULTILOGIN_client}
};
struct PICA_msginfo _msginfo_node[] =
{
{PICA_PROTO_INITRESPOK, PICA_MSG_FIXED_SIZE, PICA_PROTO_INITRESPOK_SIZE, procmsg_NODERESP},
{PICA_PROTO_VERDIFFER, PICA_MSG_FIXED_SIZE, PICA_PROTO_VERDIFFER_SIZE, procmsg_NODERESP},
{PICA_PROTO_NEWNODE_IPV4, PICA_MSG_FIXED_SIZE, PICA_PROTO_NEWNODE_IPV4_SIZE, procmsg_NEWNODE},
{PICA_PROTO_NODELISTREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_NODELISTREQ_SIZE, procmsg_NODELISTREQ},
{PICA_PROTO_NODELIST, PICA_MSG_VAR_SIZE, PICA_MSG_VARSIZE_INT16, procmsg_NODELIST},
{PICA_PROTO_SEARCH, PICA_MSG_FIXED_SIZE, PICA_PROTO_SEARCH_SIZE, procmsg_SEARCH},
{PICA_PROTO_N2NFOUND, PICA_MSG_FIXED_SIZE, PICA_PROTO_N2NFOUND_SIZE, procmsg_N2NFOUND},
{PICA_PROTO_N2NFOUNDCACHE, PICA_MSG_VAR_SIZE, PICA_MSG_VARSIZE_INT16, procmsg_N2NFOUND},
{PICA_PROTO_N2NCONNREQOUTG, PICA_MSG_FIXED_SIZE, PICA_PROTO_N2NCONNREQOUTG_SIZE, procmsg_N2NCONNREQOUTG},
{PICA_PROTO_N2NALLOW, PICA_MSG_FIXED_SIZE, PICA_PROTO_N2NALLOW_SIZE, procmsg_N2NALLOW},
{PICA_PROTO_N2NNOTFOUND, PICA_MSG_FIXED_SIZE, PICA_PROTO_N2NNOTFOUND_SIZE, procmsg_N2NNOTFOUND},
{PICA_PROTO_N2NMSG, PICA_MSG_VAR_SIZE, PICA_MSG_VARSIZE_INT16, procmsg_N2NMSG},
{PICA_PROTO_PINGREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_PINGREQ_SIZE, procmsg_PINGREQ_node},
{PICA_PROTO_PINGREP, PICA_MSG_FIXED_SIZE, PICA_PROTO_PINGREP_SIZE, procmsg_PINGREP_node},
{PICA_PROTO_MULTILOGIN, PICA_MSG_VAR_SIZE, PICA_MSG_VARSIZE_INT16, procmsg_MULTILOGIN_node}
};
struct PICA_msginfo _msginfo_newconn[] =
{
{PICA_PROTO_INITREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_INITREQ_SIZE, procmsg_INITREQ},
{PICA_PROTO_CONNID, PICA_MSG_FIXED_SIZE, PICA_PROTO_CONNID_SIZE, procmsg_CONNID},
{PICA_PROTO_NODECONNREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_NODECONNREQ_SIZE, procmsg_NODECONNREQ}
};
struct PICA_msginfo _msginfo_multi[] =
{
{PICA_PROTO_CONNREQOUTG, PICA_MSG_FIXED_SIZE, PICA_PROTO_CONNREQOUTG_SIZE, procmsg_dummy_multi_c2n},
{PICA_PROTO_CONNALLOW, PICA_MSG_FIXED_SIZE, PICA_PROTO_CONNALLOW_SIZE, procmsg_dummy_multi_c2n},
{PICA_PROTO_CONNDENY, PICA_MSG_FIXED_SIZE, PICA_PROTO_CONNDENY_SIZE, procmsg_dummy_multi_c2n},
{PICA_PROTO_CLNODELISTREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_CLNODELISTREQ_SIZE, procmsg_NODELISTREQ},
{PICA_PROTO_PINGREQ, PICA_MSG_FIXED_SIZE, PICA_PROTO_PINGREQ_SIZE, procmsg_PINGREQ_client},
{PICA_PROTO_PINGREP, PICA_MSG_FIXED_SIZE, PICA_PROTO_PINGREP_SIZE, procmsg_PINGREP_client},
{PICA_PROTO_MULTILOGIN, PICA_MSG_VAR_SIZE, PICA_MSG_VARSIZE_INT16, procmsg_MULTILOGIN_client}
};
static int process_async_ssl_readwrite(SSL *ssl, int ret)
{
if (ret == 0)
return 0;
if (ret < 0)
{
switch(SSL_get_error(ssl, ret))
{
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_WANT_READ:
break;
default:
return 0;
}
}
return 1;
}
static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
{
//return 1 for self-signed certificates
char buf[256];
X509 *err_cert;
int err, depth;
err_cert = X509_STORE_CTX_get_current_cert(ctx);
err = X509_STORE_CTX_get_error(ctx);
depth = X509_STORE_CTX_get_error_depth(ctx);
X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT && depth == 0)
preverify_ok = 1;
if (!preverify_ok)
{
PICA_info("certificate verification error:num=%d:%s:depth=%d:%s\n", err, X509_verify_cert_error_string(err), depth, buf);
}
return preverify_ok;
}
unsigned int procmsg_INITREQ(unsigned char* buf, unsigned int size, void* ptr)
{
struct newconn *nc = (struct newconn*)ptr;
struct PICA_proto_msg *mp;
PICA_debug1("received INITREQ");
nc->type = NEWCONN_C2N;
if (buf[2] == PICA_PROTO_VER_HIGH && buf[3] == PICA_PROTO_VER_LOW)
{
struct client *c;
c = client_list_addnew(nc);
if (!c)
return 0;
c->state = PICA_CLSTATE_WAITANONTLSSHUTDOWN;
nc->iconn.cl = c;
nc->state = PICA_NEWCONN_SENDINGOK;
/*
newconn_free(nc);
if ((mp = client_wbuf_push(c, PICA_PROTO_INITRESPOK, PICA_PROTO_INITRESPOK_SIZE)))
{
mp->tail[0] = 'O';
mp->tail[1] = 'K';
}
c->state = PICA_CLSTATE_SENDINGRESP;*/
}///////---------<<<<<<<<<<< new client
else
{
nc->state = PICA_NEWCONN_SENDINGREJECT;
if (buf[2] > PICA_PROTO_VER_HIGH || (buf[2] == PICA_PROTO_VER_HIGH && buf[3] > PICA_PROTO_VER_LOW))
{
PICA_warn("Client has newer protocol version than node. Please check if update for pica-node is available");
}
else
{
PICA_info("Client has older protocol version. Disconnecting ...");
}
}
return 1;
}
void cclink_sendcleconnected(struct cclink *link)
{
struct PICA_proto_msg *mp;
if (link->state == PICA_CCLINK_LOCAL_WAITCONNCLEANONTLSSHUTDOWN)
{
if((mp = client_wbuf_push(link->p1, PICA_PROTO_FOUND, PICA_PROTO_FOUND_SIZE)))
{
memcpy(mp->tail, link->callee_id, PICA_ID_SIZE);
}
else
{
cclink_list_delete(link);
return;
}
}
if (link->state == PICA_CCLINK_N2NCLE_WAITCONNCLEANONTLSSHUTDOWN)
{
if((mp = nodelink_wbuf_push(link->caller_node, PICA_PROTO_N2NALLOW, PICA_PROTO_N2NALLOW_SIZE)))
{
memcpy(mp->tail, link->caller_id, PICA_ID_SIZE);
memcpy(mp->tail + PICA_ID_SIZE, link->callee_id, PICA_ID_SIZE);
}
else
{
cclink_list_delete(link);
return;
}
}
}
unsigned int procmsg_CONNID(unsigned char* buf, unsigned int size, void* ptr)
{
struct PICA_proto_msg *mp;
struct newconn *nc = (struct newconn*)ptr;
unsigned char *caller_id, *callee_id;
struct cclink *link;
PICA_debug1("received CONNID");
nc->type = NEWCONN_C2C;
caller_id = buf + 2;
callee_id = buf + 2 + PICA_ID_SIZE;
{
char caller_id_buf[2 * PICA_ID_SIZE], callee_id_buf[2 * PICA_ID_SIZE];
PICA_debug1("CONNID: caller_id=%s callee_id=%s", PICA_id_to_base64(caller_id, caller_id_buf), PICA_id_to_base64(callee_id, callee_id_buf));
}
link = cclink_list_search(caller_id, callee_id);
if (!link)
{
PICA_debug3("corresponding cclink not found");
return 0;
}
if (link->state != PICA_CCLINK_LOCAL_WAITCONNCLE && link->state != PICA_CCLINK_LOCAL_WAITCONNCLR
&& link->state != PICA_CCLINK_N2NCLR_WAITCONNCLR && link->state != PICA_CCLINK_N2NCLE_WAITCONNCLE)
{
PICA_debug2("found cclink is not in expected state");
return 0;
}
{
struct client *c;
//проверка IP адреса.
//IP адрес должен совпадать с адресом клиента в управляющем соединении
if (link->state == PICA_CCLINK_LOCAL_WAITCONNCLE || link->state == PICA_CCLINK_N2NCLE_WAITCONNCLE)
c = link->p2;
else if (link->state == PICA_CCLINK_LOCAL_WAITCONNCLR || link->state == PICA_CCLINK_N2NCLR_WAITCONNCLR)
c = link->p1;
if (nc->addr.sin_addr.s_addr != c->addr.sin_addr.s_addr)
{
PICA_warn("CONNID: IP check failed. IP address of sender does not match IP in c2n connection");
return 0;
}
}
PICA_debug3("CONNID processed successfully");
cclink_setwaitanontlsshutdown(link, nc);
return 1;
}
unsigned int procmsg_NODECONNREQ(unsigned char* buf, unsigned int size, void* ptr)
{
struct newconn *nc = (struct newconn*)ptr;
struct PICA_proto_msg *mp;
PICA_debug1("received NODECONNREQ");
nc->type = NEWCONN_N2N;
if (buf[2] == PICA_PROTO_VER_HIGH &&
buf[3] == PICA_PROTO_VER_LOW)
{
////-прием входящего подключения
//--------------------------
nc->state = PICA_NEWCONN_SENDINGOK;
/*struct nodelink *nl;
nl = nodelink_list_addnew(nc);
newconn_free(nc);
if (!nl)
return 0;
if ((mp = nodelink_wbuf_push(nl, PICA_PROTO_INITRESPOK, PICA_PROTO_INITRESPOK_SIZE)))
{
mp->tail[0] = 'O';
mp->tail[1] = 'K';
}*/
}
else
{
nc->state = PICA_NEWCONN_SENDINGREJECT;
/*
//// PICA_PROTO_VERDIFFER
send(nc->sck, msg_VERDIFFER, msg_VERDIFFER_len, 0);
if (buf[2] > PICA_PROTO_VER_HIGH || (buf[2] == PICA_PROTO_VER_HIGH && buf[3] > PICA_PROTO_VER_LOW))
{
PICA_warn("Node %s has newer protocol version [%u, %u]. Please check if update for pica-node is available",
inet_ntoa(nc->addr.sin_addr), buf[2], buf[3]);
}
else
{
PICA_info("Node %s has older protocol version. Disconnecting ...", inet_ntoa(nc->addr.sin_addr));
}
return 0;*/
}
return 1;
}
void mapfunc_sendmultilogin(struct nodelink *p, const unsigned char *msgbuf)
{
struct PICA_proto_msg *mp;
uint16_t payload_size = *(uint16_t*)(msgbuf + 2);
mp = nodelink_wbuf_push(p, PICA_PROTO_MULTILOGIN, payload_size + 4);
if (mp)
memcpy(mp->tail, msgbuf + 2, payload_size + 2);
}
void mapfunc_sendsearchreq(struct nodelink *p, const unsigned char *id)
{
struct PICA_proto_msg *mp;
mp = nodelink_wbuf_push(p, PICA_PROTO_SEARCH, PICA_PROTO_SEARCH_SIZE);
if (mp)
memcpy(mp->tail, id, PICA_ID_SIZE);
}
int nodelink_send_searchreq(const unsigned char *id)
{
if (!nodelink_list_count)
return 0;
LISTMAP(nodelink, nodelink, mapfunc_sendsearchreq, id);
return 1;
}
unsigned int procmsg_CONNREQOUTG(unsigned char* buf, unsigned int size, void* ptr)
{
struct client *i, *o; //i - указатель на вызывающего клиента, o - вызываемого(если найден)
unsigned char *callee_id;
struct PICA_proto_msg *mp;
i = (struct client *)ptr;
callee_id = buf + 2;
char caller_id_buf[2 * PICA_ID_SIZE], callee_id_buf[2 * PICA_ID_SIZE];
PICA_debug1("received CONNREQOUTG from %s\n searching for %s", PICA_id_to_base64(i->id, caller_id_buf),
PICA_id_to_base64(callee_id, callee_id_buf));
if ((o = client_tree_search(callee_id)))
{
if (cclink_list_search(i->id, callee_id))
{
PICA_info("duplicate request for existing c2c connection is ignored");
return 1;
}
if (memcmp(i->id, callee_id, PICA_ID_SIZE) == 0)
{
PICA_info("request to create c2c connection to self is ignored");
return 1;
}
//отправить o _CONNREQINC
if ((mp = client_wbuf_push(o, PICA_PROTO_CONNREQINC, PICA_PROTO_CONNREQINC_SIZE)))
{
memcpy(mp->tail, i->id, PICA_ID_SIZE);
cclink_list_addlocal(i, o);
o->tmst = time(0);
o->disconnect_ticking = 1;//if client does not reply in given time period, client is disconnected
return 1;
}
return 1;//сообщение отправить не удалось, но все равно возвращаем 1
}
if (cclink_list_findwaitsearch(callee_id)/*search request for this id is already in progress*/
|| nodelink_send_searchreq(callee_id) /*make new search*/)
{
cclink_list_addn2nclr(i, callee_id);
return 1;
}
//отправить i _NOTFOUND
if ((mp = client_wbuf_push(i, PICA_PROTO_NOTFOUND, PICA_PROTO_NOTFOUND_SIZE)))
{
memcpy(mp->tail, callee_id, PICA_ID_SIZE);
}
return 1;
}
unsigned int procmsg_CONNALLOW(unsigned char* buf, unsigned int size, void* ptr)
{
struct client *callee;
struct cclink *link;
unsigned char *caller_id;
PICA_debug1("received CONALLOW");
callee = (struct client *)ptr;
caller_id = buf + 2;
callee->disconnect_ticking = 0;
callee->tmst = time(0);
link = cclink_list_search(caller_id, callee->id);
if (!link)
return 0;
if (link->state != PICA_CCLINK_LOCAL_WAITREP &&
link->state != PICA_CCLINK_N2NCLE_WAITREP)
return 0;
cclink_setwaitconn(link);
return 1;
}
unsigned int procmsg_CONNDENY(unsigned char* buf, unsigned int size, void* ptr)
{
struct client *caller, *callee;
struct cclink *link;
unsigned char *caller_id;
struct PICA_proto_msg *mp;
PICA_debug1("received CONNDENY");
callee = (struct client *)ptr;
caller_id = buf + 2;
callee->disconnect_ticking = 0;
callee->tmst = time(0);
link = cclink_list_search(caller_id, callee->id);
if (!link)
return 0;
caller = link -> p1;//check for NULL if not LOCAL
if (link->state != PICA_CCLINK_LOCAL_WAITREP &&
link->state != PICA_CCLINK_N2NCLE_WAITREP)
{
cclink_list_delete(link);
return 0;
}
if (link->state == PICA_CCLINK_LOCAL_WAITREP)
{
//отправить caller _NOTFOUND
if ((mp = client_wbuf_push(caller, PICA_PROTO_NOTFOUND, PICA_PROTO_NOTFOUND_SIZE)))
{
memcpy(mp->tail, callee->id, PICA_ID_SIZE);
}
}
else if (link->state == PICA_CCLINK_N2NCLE_WAITREP)
{
//отправить caller N2NNOTFOUND
if ((mp = nodelink_wbuf_push(link->caller_node, PICA_PROTO_N2NNOTFOUND, PICA_PROTO_N2NNOTFOUND_SIZE)))
{
memcpy(mp->tail, caller_id, PICA_ID_SIZE);
memcpy(mp->tail + PICA_ID_SIZE, callee->id, PICA_ID_SIZE);
}
}
cclink_list_delete(link);
return 1;
}
unsigned int procmsg_PINGREQ_client(unsigned char* buf, unsigned int size, void* ptr)
{
struct client *c = (struct client*)ptr;
struct PICA_proto_msg *mp;
PICA_debug3("PINGREQ");
if ((mp = client_wbuf_push(c, PICA_PROTO_PINGREP, PICA_PROTO_PINGREP_SIZE)))
{
RAND_pseudo_bytes(mp->tail, 2);
}
else
return 0;
return 1;
}
unsigned int procmsg_PINGREP_client(unsigned char* buf, unsigned int size, void* ptr)
{
struct client *c = (struct client*)ptr;
PICA_debug3("PINGREP c2n");
c->disconnect_ticking = 0;
c->tmst = time(0);
return 1;
}
unsigned int procmsg_MULTILOGIN_client(unsigned char* buf, unsigned int size, void* ptr)
{
struct client *other, *me = (struct client*)ptr;
PICA_debug3("MULTILOGIN c2n");
//send to other instances connected to this node
other = client_tree_search(me->id);
while(other)
{
if (other != me)
{
struct PICA_proto_msg *mp;
if ((mp = client_wbuf_push(other, PICA_PROTO_MULTILOGIN, size)))
{
memcpy(mp->tail, buf + 2, size - 2);
}
}
other = other->next_multi;
}
//relay to other nodes
LISTMAP(nodelink, nodelink, mapfunc_sendmultilogin, buf);
return 1;
}
unsigned int procmsg_MULTILOGIN_node(unsigned char* buf, unsigned int size, void* ptr)
{
struct nodelink *n = (struct nodelink *)ptr;
struct client *c = client_list_head;
uint64_t timestamp;
void *sigdatas[4];
size_t sigdatalengths[4];
uint16_t payload_len = *(uint16_t*)(buf + 2);
int ret;
size_t siglen;
unsigned char *sig;
PICA_debug3("MULTILOGIN n2n");
if (payload_len <= sizeof timestamp + PICA_PROTO_NODELIST_ITEM_IPV4_SIZE)
return 0;
timestamp = *(uint64_t*)(buf + 4);
//TODO compare timestamps
sigdatalengths[0] = PICA_ID_SIZE;
//add timestamp to signature data
sigdatas[1] = buf + 4;
sigdatalengths[1] = sizeof(uint64_t);
//add sender node address to signature data
sigdatas[2] = buf + 4 + sizeof(uint64_t);
switch (buf[4 + sizeof(uint64_t)])
{
case PICA_PROTO_NEWNODE_IPV4:
sigdatalengths[2] = PICA_PROTO_NODELIST_ITEM_IPV4_SIZE;
break;
case PICA_PROTO_NEWNODE_IPV6:
sigdatalengths[2] = PICA_PROTO_NODELIST_ITEM_IPV6_SIZE;
break;
case PICA_PROTO_NEWNODE_DNS:
sigdatalengths[2] = 4 + buf[5 + sizeof(uint64_t)];
default:
PICA_warn("Received MULTILOGIN message with invalid node address");
return 1;
}
siglen = payload_len - sizeof(uint64_t) - sigdatalengths[2];
sig = buf + 4 + sizeof(uint64_t) + sigdatalengths[2];
sigdatas[3] = NULL;
sigdatalengths[3] = 0;
while(c)
{
//check signature against current client ID and data in MULTILOGIN packet
if (c->state == PICA_CLSTATE_CONNECTED)
{
EVP_PKEY *pubkey;
X509* x;
sigdatas[0] = c->id;
if ((x = SSL_get_peer_certificate(c->ssl_comm)) &&
(pubkey = X509_get_pubkey(x)))
{
ret = PICA_signverify(pubkey, sigdatas, sigdatalengths, sig, siglen);
EVP_PKEY_free(pubkey);
X509_free(x);
if (ret == 1)
{
PICA_debug3("found matching client connection for incoming n2n MULTILOGIN message");
break;
}
}
}
c = c->next;
}
if (!c)
{
PICA_debug3("no matching client found for receved n2n MULTILOGIN");
return 1;
}
while(c) //relay received MULTILOGIN to each client with same id
{
struct PICA_proto_msg *mp;
mp = client_wbuf_push(c, PICA_PROTO_MULTILOGIN, size);
if (mp)
{
memcpy(mp, buf, size);
}
c = c->next_multi;
}
return 1;
}
unsigned int procmsg_dummy_multi_c2n(unsigned char* buf, unsigned int size, void* ptr)
{
PICA_debug3("message %X from %p is handled by dummy procmsg", buf[0], ptr);
return 1;
}
unsigned int procmsg_PINGREQ_node(unsigned char* buf, unsigned int size, void* ptr)
{
struct nodelink *n = (struct nodelink *)ptr;
struct PICA_proto_msg *mp;
PICA_debug3("PINGREQ");
if ((mp = nodelink_wbuf_push(n, PICA_PROTO_PINGREP, PICA_PROTO_PINGREP_SIZE)))
{
RAND_pseudo_bytes(mp->tail, 2);
}
else
return 0;
return 1;
}
unsigned int procmsg_PINGREP_node(unsigned char* buf, unsigned int size, void* ptr)
{
struct nodelink *n = (struct nodelink *)ptr;
PICA_debug3("PINGREP n2n");
n->disconnect_ticking = 0;
n->tmst = time(0);
return 1;
}
unsigned int procmsg_NODERESP(unsigned char* buf, unsigned int size, void* ptr)
{
struct nodelink *n = (struct nodelink *)ptr;
struct PICA_proto_msg *mp;
PICA_debug1("received NODERESP");
switch(buf[0])
{
case PICA_PROTO_INITRESPOK:
if (buf[2] != 'O' || buf[3] != 'K')
return 0;
mp = nodelink_wbuf_push(n, PICA_PROTO_NEWNODE_IPV4, PICA_PROTO_NEWNODE_IPV4_SIZE);//IPv6
if (mp)
{
*((in_addr_t*)(mp->tail)) = inet_addr(nodecfg.announced_addr);
*((in_port_t*)(mp->tail + 4)) = (nodecfg.listen_port ? htons(atoi(nodecfg.listen_port)) : htons(PICA_COMM_PORT)); //CONF;
}
else
return 0;
mp = nodelink_wbuf_push(n, PICA_PROTO_NODELISTREQ, PICA_PROTO_NODELISTREQ_SIZE);
if (mp)
{
RAND_pseudo_bytes(mp->tail, 2);
}
else
return 0;
break;
case PICA_PROTO_VERDIFFER:
if (buf[2] > PICA_PROTO_VER_HIGH || (buf[3] > PICA_PROTO_VER_LOW && buf[2] == PICA_PROTO_VER_HIGH))
{
PICA_warn("make update of node software - peer node has newer protocol version");
}
else
{
//peer node has older software version
PICA_info("peer node has older protocol version");
}
return 0;
break;
}
return 1;
}
unsigned int procmsg_NEWNODE(unsigned char* buf, unsigned int size, void* ptr)
{
struct nodelink *n = (struct nodelink *)ptr;
struct PICA_nodeaddr na;
PICA_debug1("received NEWNODE");
switch (buf[0])
{
case PICA_PROTO_NEWNODE_IPV4:
PICA_debug1("NEWNODE_IPV4: ip %.16s port %hu\n", inet_ntoa(*(struct in_addr*)(buf + 2)), ntohs(*(in_port_t*)(buf + 6)));
{
struct PICA_nodeaddr_ipv4 na_ipv4;
na_ipv4.magick = PICA_PROTO_NEWNODE_IPV4;
na_ipv4.addr = *(in_addr_t*)(buf + 2);
na_ipv4.port = *(in_port_t*)(buf + 6);
if (nodelink_search_by_ipv4addr(na_ipv4.addr, na_ipv4.port)) //connection with this node is already established
{
PICA_debug1("disconnecting node %.16s:%hu - connection with this node is already established", inet_ntoa(*(struct in_addr*)&na_ipv4.addr), ntohs(na_ipv4.port));
return 0;
}
if (nodecfg.disable_reserved_addrs && PICA_is_reserved_addr_ipv4(na_ipv4.addr))
{
PICA_debug1("disconnecting node %.16s:%hu - private IP ranges are rejected by node configuration",
inet_ntoa(*(struct in_addr*)&na_ipv4.addr), ntohs(na_ipv4.port)
);
return 0;
}
sprintf(na.addr, "%.16s", inet_ntoa(*(struct in_addr*)&na_ipv4.addr));
na.port = ntohs(na_ipv4.port);
//int nodelink_attach_nodeaddr(struct nodelink *nl, unsigned int addr_type, void* nodeaddr, unsigned int nodeaddr_size)
nodelink_attach_nodeaddr(n, na_ipv4.magick, &na_ipv4, sizeof(struct PICA_nodeaddr_ipv4));
}
break;
case PICA_PROTO_NEWNODE_IPV6:
#warning "TO DO"
break;
case PICA_PROTO_NEWNODE_DNS:
#warning "TODO"
break;
}
PICA_nodeaddr_save(nodecfg.nodes_db_file, &na);//CONF filename
return 1;
}
unsigned int procmsg_NODELISTREQ(unsigned char* buf, unsigned int size, void* ptr)
{
struct PICA_proto_msg *mp;
struct nodelink *nlp;
unsigned int nl_size = 0;
struct nodelink *n = 0;
struct client *c = 0;
unsigned char *nl_buf = 0;
PICA_debug1("received NODELISTREQ");
switch(buf[0])
{
case PICA_PROTO_CLNODELISTREQ:
c = (struct client *)ptr;
break;
case PICA_PROTO_NODELISTREQ:
n = (struct nodelink *)ptr;
break;
}
nl_buf = (unsigned char*)malloc(65536);
if (!nl_buf)
return 0;//ERR
{
//TEMP FIXME CONF
//собственный адрес узла
nl_buf[nl_size] = PICA_PROTO_NEWNODE_IPV4;
*((in_addr_t*)(nl_buf + nl_size + 1)) = inet_addr(my_addr);
*((in_port_t*)(nl_buf + nl_size + 5)) = (nodecfg.listen_port ? htons(atoi(nodecfg.listen_port)) : htons(PICA_COMM_PORT)); //CONF
nl_size += PICA_PROTO_NODELIST_ITEM_IPV4_SIZE;
}
nlp = nodelink_list_head;
while(nlp && nl_size < 65532)//MAP
{
switch(nlp->addr_type)
{
case PICA_PROTO_NEWNODE_IPV4:
if ( PICA_PROTO_NODELISTREQ == buf[0] &&
((struct PICA_nodeaddr_ipv4*)nlp->node_addr)->addr == ((struct PICA_nodeaddr_ipv4*)n->node_addr)->addr &&
((struct PICA_nodeaddr_ipv4*)nlp->node_addr)->port == ((struct PICA_nodeaddr_ipv4*)n->node_addr)->port )
break;//exclude requester's address
nl_buf[nl_size] = PICA_PROTO_NEWNODE_IPV4;
*((in_addr_t*)(nl_buf + nl_size + 1)) = ((struct PICA_nodeaddr_ipv4*)nlp->node_addr)->addr;
*((in_port_t*)(nl_buf + nl_size + 5)) = ((struct PICA_nodeaddr_ipv4*)nlp->node_addr)->port;
nl_size += PICA_PROTO_NODELIST_ITEM_IPV4_SIZE;//CONF
break;
}
//#warning "set limit on list size, <65532 bytes!!!"
nlp = nlp->next;
}
switch(buf[0])
{
case PICA_PROTO_CLNODELISTREQ:
mp = client_wbuf_push( c, PICA_PROTO_CLNODELIST, nl_size + 4);
break;
case PICA_PROTO_NODELISTREQ:
mp = nodelink_wbuf_push( n, PICA_PROTO_NODELIST, nl_size + 4);
break;
}
if (mp)
{
*((uint16_t*)mp->tail) = nl_size;
memcpy(mp->tail + 2, nl_buf, nl_size);
}
free(nl_buf);
return 1;
}
unsigned int procmsg_NODELIST(unsigned char* buf, unsigned int size, void* ptr)
{
struct PICA_nodeaddr_ipv4 na_ipv4;
struct nodelink *n = (struct nodelink *)ptr;
unsigned int listsize = size - 2 - PICA_MSG_VARSIZE_INT16;