-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmld.c
1095 lines (889 loc) · 25.8 KB
/
dmld.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
/*
Copyright Jeroen Vreeken ([email protected]), 2015
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, either version 3 of the License, or
(at your option) any later version.
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 <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <dml/dml_server.h>
#include <dml/dml_connection.h>
#include <dml/dml_packet.h>
#include <dml/dml_route.h>
#include <dml/dml.h>
#include "dml_config.h"
#include <dml/dml_client.h>
#include <dml/dml_id.h>
#include "dmld_cache.h"
#include <dml/dml_log.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#define DMLD_DATA_KEEPALIVE 60
struct connection_update {
uint8_t id[DML_ID_SIZE];
uint8_t hops;
struct connection_update *next;
};
struct connection_data_client {
struct dml_connection *dc;
uint16_t packet_id;
struct connection_data_client *next;
};
struct connection_data {
uint8_t id[DML_ID_SIZE];
struct dml_connection *dc;
uint16_t packet_id;
time_t t_data;
struct connection_data_client *client_list;
struct connection_data *next;
};
struct connection_data *data_list;
int connection_data_remove(struct connection_data *data);
int connection_data_remove_client(struct connection_data *data, struct dml_connection *dc);
void connection_data_update(struct dml_connection *dc, bool old_valid);
struct connection {
struct dml_connection *dc;
uint32_t flags;
struct dml_client *client;
uint8_t update_id[DML_ID_SIZE];
struct connection_update *bad_list;
struct connection_update *good_list;
struct connection_update *req_description;
struct connection_update *req_certificate;
struct connection_update *req_header;
char *name;
int hops_offset;
struct connection *next;
};
struct connection *connection_list = NULL;
struct connection *connection_create(void)
{
struct connection *con;
con = calloc(1, sizeof(struct connection));
if (!con)
goto err_calloc;
con->name = strdup("unknown connection");
if (!con->name)
goto err_name;
con->next = connection_list;
connection_list = con;
return con;
err_name:
free(con);
err_calloc:
return NULL;
}
char *connection_name_get(struct connection *con)
{
return con->name;
}
int connection_name_set(struct connection *con, char *name)
{
char *tmp = strdup(name);
if (!tmp)
return -1;
int i;
for (i = 0; i < strlen(tmp); i++) {
if (tmp[i] == ',') {
tmp[i] = 0;
con->hops_offset = atoi(tmp+i+1);
dml_log(DML_LOG_DEBUG, "Connection to %s has hops_offset of %d", tmp, con->hops_offset);
}
}
free(con->name);
con->name = tmp;
return 0;
}
bool connection_valid(struct connection *con)
{
struct connection *entry;
for (entry = connection_list; entry; entry = entry->next) {
if (entry == con)
return true;
}
return false;
}
void connection_destroy(struct connection *con)
{
struct connection **entry;
for (entry = &connection_list; *entry; entry = &(*entry)->next) {
if (*entry == con) {
*entry = con->next;
break;
}
}
g_source_remove_by_user_data(con);
g_source_remove_by_user_data(con);
while (con->bad_list) {
struct connection_update *cu = con->bad_list;
con->bad_list = cu->next;
free(cu);
}
while (con->good_list) {
struct connection_update *cu = con->good_list;
con->good_list = cu->next;
free(cu);
}
while (con->req_description) {
struct connection_update *cu = con->req_description;
con->req_description = cu->next;
free(cu);
}
while (con->req_certificate) {
struct connection_update *cu = con->req_certificate;
con->req_certificate = cu->next;
free(cu);
}
while (con->req_header) {
struct connection_update *cu = con->req_header;
con->req_header = cu->next;
free(cu);
}
connection_data_update(con->dc, false);
struct connection_data *data, *dnext;
dml_log(DML_LOG_INFO, "remove client %s from data list", con->name);
for (data = data_list; data; data = dnext) {
dnext = data->next;
connection_data_remove_client(data, con->dc);
if (!data->client_list && data->dc != con->dc) {
dml_log(DML_LOG_INFO, "Sending disconnect request upstream");
dml_packet_send_req_disc(data->dc, data->id);
connection_data_remove(data);
}
}
free(con->name);
free(con);
}
struct connection_data *connection_data_create(void)
{
struct connection_data *entry;
entry = calloc(1, sizeof(struct connection_data));
if (!entry)
return NULL;
entry->next = data_list;
data_list = entry;
return entry;
}
struct connection_data *connection_data_by_connection(struct dml_connection *dc, uint16_t packet_id)
{
struct connection_data *entry;
for (entry = data_list; entry; entry = entry->next) {
if (entry->dc == dc && entry->packet_id == packet_id) {
return entry;
}
}
return NULL;
}
struct connection_data *connection_data_by_id(uint8_t id[DML_ID_SIZE])
{
struct connection_data *entry;
for (entry = data_list; entry; entry = entry->next) {
if (!memcmp(entry->id, id, DML_ID_SIZE)) {
return entry;
}
}
return NULL;
}
int connection_data_add_client(struct connection_data *data, struct dml_connection *dc, uint16_t packet_id)
{
struct connection_data_client *entry;
struct connection *con = dml_connection_arg_get(dc);
for (entry = data->client_list; entry; entry = entry->next) {
if (entry->dc == dc)
break;
}
if (!entry) {
entry = calloc(1, sizeof(struct connection_data_client));
if (!entry)
return -1;
entry->next = data->client_list;
data->client_list = entry;
}
char *idstr = dml_id_str(data->id);
dml_log(DML_LOG_INFO, "Add client to %s: %s %d", idstr, connection_name_get(con), packet_id);
free(idstr);
entry->packet_id = packet_id;
entry->dc = dc;
return 0;
}
int connection_data_remove_client(struct connection_data *data, struct dml_connection *dc)
{
struct connection_data_client **entry;
struct connection *con = dml_connection_arg_get(dc);
char *idstr = dml_id_str(data->id);
dml_log(DML_LOG_INFO, "Remove client from %s: %s", idstr, connection_name_get(con));
free(idstr);
for (entry = &data->client_list; *entry; entry = &(*entry)->next) {
struct connection_data_client *old = *entry;
if (old->dc != dc)
continue;
*entry = old->next;
free(old);
return 0;
}
return -1;
}
uint16_t connection_data_new_id(void)
{
static uint16_t id = DML_PACKET_DATA;
struct connection_data *entry;
id++;
do {
for (entry = data_list; entry; entry = entry->next) {
if (entry->packet_id == id) {
id++;
if (id < DML_PACKET_DATA)
id = DML_PACKET_DATA;
break;
}
}
if (!entry)
return id;
} while(1);
}
int connection_data_remove(struct connection_data *data)
{
struct connection_data **entry;
char *idstr = dml_id_str(data->id);
dml_log(DML_LOG_INFO, "Removing %s from data list", idstr);
free(idstr);
for (entry = &data_list; *entry; entry = &(*entry)->next) {
if (*entry == data) {
*entry = data->next;
break;
}
}
while (data->client_list) {
struct connection_data_client *dc = data->client_list;
dml_packet_send_disc(dc->dc, data->id, DML_PACKET_DISC_UNROUTABLE);
data->client_list = dc->next;
free(dc);
}
free(data);
return 0;
}
gboolean connection_data_keepalive(void *arg)
{
time_t now = time(NULL);
struct connection_data *entry;
for (entry = data_list; entry; entry = entry->next) {
if (now > entry->t_data + DMLD_DATA_KEEPALIVE) {
char *idstr = dml_id_str(entry->id);
struct connection *con = dml_connection_arg_get(entry->dc);
dml_log(DML_LOG_INFO, "No data for a while, sending keepalive connect %s to %s",
idstr, connection_name_get(con));
free(idstr);
dml_packet_send_connect(entry->dc, entry->id, entry->packet_id);
entry->t_data = now;
}
}
g_timeout_add_seconds(DMLD_DATA_KEEPALIVE, connection_data_keepalive, connection_data_keepalive);
return G_SOURCE_REMOVE;
}
/* A connection has been updated, recheck */
void connection_data_update(struct dml_connection *dc, bool old_valid)
{
struct connection_data *data, *dnext;
for (data = data_list; data; data = dnext) {
dnext = data->next;
/* Is it using the connection? */
if (data->dc != dc)
continue;
struct dml_connection *dc_r = dml_route_connection_get(data->id);
if (dc_r) {
if (dc_r != dc) {
data->dc = dc_r;
dml_packet_send_connect(dc_r, data->id, data->packet_id);
if (old_valid)
dml_packet_send_req_disc(dc, data->id);
}
continue;
}
connection_data_remove(data);
}
}
void update_clear(struct connection *con)
{
memset(con->update_id, 0, DML_ID_SIZE);
}
gboolean update(void *arg)
{
struct connection *con = arg;
// printf("update\n");
if (!connection_valid(con)) {
dml_log(DML_LOG_DEBUG, "Update called on invalid connection");
goto invalid;
}
while (dml_connection_send_empty(con->dc)) {
struct connection_update *up;
up = con->bad_list;
if (up)
con->bad_list = up->next;
else {
up = con->good_list;
if (up)
con->good_list = up->next;
}
if (!up)
break;
char *idstr = dml_id_str(up->id);
int hops = up->hops + con->hops_offset;
if (hops > 255)
hops = 255;
dml_log(DML_LOG_INFO, "Send update %s (%d hops)", idstr, hops);
free(idstr);
dml_packet_send_route(con->dc, up->id, hops);
free(up);
}
if (!dml_connection_send_empty(con->dc)) {
if (con->bad_list || con->good_list)
dml_log(DML_LOG_WARNING, "Send not empty, but update waiting");
}
// printf("wait a little %p\n", con);
g_timeout_add_seconds(1, update, con);
invalid:
return G_SOURCE_REMOVE;
}
gboolean update_all(void *arg)
{
struct connection *con = arg;
if (!connection_valid(con)) {
dml_log(DML_LOG_DEBUG, "Update called on invalid connection");
goto invalid;
}
dml_route_sort_lock_dec();
// printf("g\n");
while (dml_connection_send_empty(con->dc)) {
uint8_t hops;
struct dml_connection *dc;
int r;
r = dml_route_iterate(con->update_id, &hops, &dc);
// printf("r: %d\n", r);
if (r) {
// dml_log(DML_LOG_INFO, "switch to regular updates %p", con);
dml_packet_send_update(con->dc, DML_PACKET_UPDATE_INITIAL_DONE);
g_timeout_add_seconds(1, update, con);
return G_SOURCE_REMOVE;
}
/* no update to the originating node */
if (dc == con->dc)
continue;
int new_hops = hops + 1 + con->hops_offset;
if (new_hops > 255)
new_hops = 255;
dml_packet_send_route(con->dc, con->update_id, new_hops);
}
// printf("wait a little %p\n", con);
dml_route_sort_lock_inc();
g_timeout_add_seconds(1, update_all, con);
invalid:
return G_SOURCE_REMOVE;
}
void connection_update(uint8_t id[DML_ID_SIZE], uint8_t hops, struct dml_connection *dc, bool bad, uint8_t alt_hops)
{
struct connection *con;
// printf("got update\n");
if (hops == 255 && alt_hops == 255) {
// remove from the cache (might come back with new header)
dmld_cache_delete(id);
}
for (con = connection_list; con; con = con->next) {
struct connection_update *up, **upp;
uint8_t up_hops = con->dc == dc ? alt_hops : hops;
up_hops = up_hops == 255 ? 255 : up_hops + 1;
for (up = con->bad_list; up; up = up->next) {
if (!memcmp(up->id, id, DML_ID_SIZE)) {
up->hops = up_hops;
dml_log(DML_LOG_INFO, "Already on bad list");
break;
}
}
if (up)
continue;
for (upp = &con->good_list; *upp; upp = &(*upp)->next) {
if (!memcmp((*upp)->id, id, DML_ID_SIZE)) {
up = *upp;
*upp = up->next;
dml_log(DML_LOG_INFO, "Already on good list");
break;
}
}
if (!up) {
up = malloc(sizeof(struct connection_update));
memcpy(up->id, id, DML_ID_SIZE);
}
up->hops = up_hops;
if (bad) {
up->next = con->bad_list;
con->bad_list = up;
dml_log(DML_LOG_INFO, "On bad list");
/* It is bad, so we want updates a bit faster */
g_source_remove_by_user_data(con);
g_timeout_add(100, update, con);
} else {
up->next = con->good_list;
con->good_list = up;
dml_log(DML_LOG_INFO, "On good list");
}
}
struct connection_data *cdat = connection_data_by_id(id);
if (cdat) {
if (dc) {
if (cdat->dc != dc) {
dml_packet_send_req_disc(cdat->dc, cdat->id);
cdat->dc = dc;
dml_packet_send_connect(dc, cdat->id, cdat->packet_id);
}
} else {
connection_data_remove(cdat);
}
}
}
int list_add(struct connection_update **list, uint8_t id[DML_ID_SIZE])
{
struct connection_update **entry, *new_id;
for (entry = list; *entry; entry = &(*entry)->next) {
if (!memcmp(id, (*entry)->id, DML_ID_SIZE)) {
return 0;
}
}
new_id = malloc(sizeof(struct connection_update));
if (!new_id)
return -1;
memcpy(new_id->id, id, DML_ID_SIZE);
new_id->next = *list;
*list = new_id;
return 0;
}
bool list_check_remove(struct connection_update **list, uint8_t id[DML_ID_SIZE])
{
struct connection_update **entry;
for (entry = list; *entry; entry = &(*entry)->next) {
if (!memcmp(id, (*entry)->id, DML_ID_SIZE)) {
struct connection_update *remove = *entry;
*entry = (*entry)->next;
free(remove);
return true;
}
}
return false;
}
void rx_packet(struct dml_connection *dc, void *arg,
uint16_t id, uint16_t len, uint8_t *data)
{
struct connection *con = arg;
dml_log(DML_LOG_DEBUG, "packet: %d", id);
switch (id) {
case DML_PACKET_HELLO:
dml_packet_parse_hello(data, len, &con->flags, NULL);
if (con->flags & DML_PACKET_HELLO_UPDATES) {
update_clear(con);
dml_route_sort_lock_inc();
update_all(con);
}
break;
case DML_PACKET_ROUTE: {
uint8_t id[DML_ID_SIZE];
uint8_t hops;
dml_packet_parse_route(data, len, id, &hops);
int new_hops = hops + con->hops_offset;
if (new_hops > 255)
new_hops = 255;
dml_route_update(id, new_hops, dc);
break;
}
case DML_PACKET_REQ_DESCRIPTION: {
uint8_t id[DML_ID_SIZE];
struct dml_connection *dc_r;
dml_packet_parse_req_description(data, len, id);
void *description;
size_t description_size;
if (dmld_cache_search_description(id, &description, &description_size)) {
dml_connection_send(dc, description, DML_PACKET_DESCRIPTION, description_size);
dml_log(DML_LOG_INFO, "Use cached description");
break;
}
dc_r = dml_route_connection_get(id);
if (dc_r) {
dml_log(DML_LOG_INFO, "Request description");
dml_packet_send_req_description(dc_r, id);
} else {
dml_log(DML_LOG_WARNING, "Description requested but id is not routable");
}
list_add(&con->req_description, id);
break;
}
case DML_PACKET_DESCRIPTION: {
uint8_t desc_id[DML_ID_SIZE];
uint8_t version;
uint32_t bps;
char *mime, *name, *alias, *description;
if (dml_packet_parse_description(data, len,
desc_id, &version, &bps, &mime, &name, &alias, &description))
break;
dmld_cache_insert_description(desc_id, data, len);
dml_log(DML_LOG_INFO, "Got description for %s", name);
struct connection *con;
for (con = connection_list; con; con = con->next) {
if (list_check_remove(&con->req_description, desc_id)) {
dml_log(DML_LOG_INFO, "Send description");
dml_connection_send(con->dc, data, id, len);
}
}
free(description);
free(alias);
free(name);
free(mime);
break;
}
case DML_PACKET_REQ_CERTIFICATE: {
uint8_t id[DML_ID_SIZE];
struct dml_connection *dc_r;
dml_packet_parse_req_certificate(data, len, id);
void *certificate;
size_t certificate_size;
if (dmld_cache_search_certificate(id, &certificate, &certificate_size)) {
dml_packet_send_certificate(dc, id, certificate, certificate_size);
dml_log(DML_LOG_INFO, "Use cached certificate");
break;
}
dc_r = dml_route_connection_get(id);
if (dc_r) {
dml_packet_send_req_certificate(dc_r, id);
}
list_add(&con->req_certificate, id);
break;
}
case DML_PACKET_CERTIFICATE: {
uint8_t id[DML_ID_SIZE];
void *certificate_data;
size_t certificate_len;
if (dml_packet_parse_certificate(data, len, id,
&certificate_data, &certificate_len))
break;
struct connection *con;
dmld_cache_insert_certificate(id, certificate_data, certificate_len);
for (con = connection_list; con; con = con->next) {
if (list_check_remove(&con->req_certificate, id)) {
dml_packet_send_certificate(con->dc, id,
certificate_data, certificate_len);
}
}
free(certificate_data);
break;
}
case DML_PACKET_REQ_HEADER: {
uint8_t id[DML_ID_SIZE];
struct dml_connection *dc_r;
dml_packet_parse_req_header(data, len, id);
void *header;
size_t header_size;
uint8_t header_sig[DML_SIG_SIZE];
if (dmld_cache_search_header(id, header_sig, &header, &header_size)) {
dml_packet_send_header(dc, id, header_sig, header, header_size);
dml_log(DML_LOG_INFO, "Use cached header");
break;
}
dc_r = dml_route_connection_get(id);
char *idstr = dml_id_str(id);
dml_log(DML_LOG_INFO, "Request header for %s: %p", idstr, dc_r);
free(idstr);
if (dc_r) {
dml_packet_send_req_header(dc_r, id);
}
list_add(&con->req_header, id);
break;
}
case DML_PACKET_HEADER: {
uint8_t id[DML_ID_SIZE];
uint8_t sig[DML_SIG_SIZE];
void *header_data;
size_t header_len;
if (dml_packet_parse_header(data, len, id, sig,
&header_data, &header_len))
break;
char *idstr = dml_id_str(id);
dml_log(DML_LOG_INFO, "Got header for %s", idstr);
free(idstr);
struct connection *con;
for (con = connection_list; con; con = con->next) {
if (list_check_remove(&con->req_header, id)) {
dml_packet_send_header(con->dc, id,
sig, header_data, header_len);
}
}
dmld_cache_insert_header(id, sig, header_data, header_len);
free(header_data);
break;
}
case DML_PACKET_CONNECT: {
uint8_t id[DML_ID_SIZE];
uint16_t packet_id;
if (dml_packet_parse_connect(data, len, id, &packet_id))
break;
struct connection_data *cdat = connection_data_by_id(id);
if (!cdat) {
struct dml_connection *dc_r = dml_route_connection_get(id);
dml_log(DML_LOG_INFO, "No data for this id yet");
if (!dc_r) {
dml_packet_send_disc(dc, id, DML_PACKET_DISC_UNROUTABLE);
break;
}
cdat = connection_data_create();
cdat->packet_id = connection_data_new_id();
cdat->dc = dc_r;
memcpy(cdat->id, id, DML_ID_SIZE);
dml_log(DML_LOG_INFO, "Sending connect");
dml_packet_send_connect(dc_r, id, cdat->packet_id);
}
connection_data_add_client(cdat, dc, packet_id);
break;
}
case DML_PACKET_DISC: {
uint8_t id[DML_ID_SIZE];
uint8_t reason;
if (dml_packet_parse_disc(data, len, id, &reason))
break;
struct connection_data *cdat = connection_data_by_id(id);
if (!cdat || cdat->dc != dc)
break;
if (reason & DML_PACKET_DISC_UNROUTABLE) {
struct dml_connection *dc_r = dml_route_connection_get(id);
if (dc_r && dc_r != dc) {
cdat->dc = dc_r;
dml_packet_send_connect(dc_r, id, cdat->packet_id);
break;
}
}
connection_data_remove(cdat);
break;
}
case DML_PACKET_REQ_DISC: {
uint8_t id[DML_ID_SIZE];
if (dml_packet_parse_req_disc(data, len, id))
break;
struct connection_data *cdat = connection_data_by_id(id);
if (!cdat)
break;
connection_data_remove_client(cdat, dc);
dml_packet_send_disc(dc, id, DML_PACKET_DISC_REQUESTED);
if (!cdat->client_list) {
dml_log(DML_LOG_INFO, "Sending disconnect request upstream");
dml_packet_send_req_disc(cdat->dc, cdat->id);
connection_data_remove(cdat);
}
break;
}
case DML_PACKET_REQ_REVERSE: {
uint8_t id[DML_ID_SIZE];
uint8_t rev_id[DML_ID_SIZE];
uint8_t action;
uint16_t status;
if (dml_packet_parse_req_reverse(data, len, id, rev_id, &action, &status))
break;
struct dml_connection *dc_r = dml_route_connection_get(id);
if (!dc_r)
break;
dml_log(DML_LOG_INFO, "Sending req_reverse: action=%d, status=%d", action, status);
dml_packet_send_req_reverse(dc_r, id, rev_id, action, status);
break;
}
default: {
/* Is it an unknown id or data? */
if (id < DML_PACKET_DATA)
break;
dml_log(DML_LOG_DEBUG, "Got data (%d)", len);
struct connection_data *cdat = connection_data_by_connection(dc, id);
if (!cdat)
break;
dml_log(DML_LOG_DEBUG, "Found connection");
cdat->t_data = time(NULL);
struct connection_data_client *cdatc;
for (cdatc = cdat->client_list; cdatc; cdatc = cdatc->next) {
dml_log(DML_LOG_DEBUG, "Sending to client as %d", cdatc->packet_id);
dml_connection_send_data(cdatc->dc, data, cdatc->packet_id, len);
}
break;
}
}
}
int server_connection_close(struct dml_connection *dc, void *arg)
{
struct connection *con = arg;
dml_log(DML_LOG_WARNING, "server close %p %s", dc, connection_name_get(con));
dml_route_remove(dc);
connection_destroy(con);
return dml_connection_destroy(dc);
}
void server_connection(void *arg, int fd)
{
struct dml_connection *dc;
struct connection *con;
char *name = NULL;
struct sockaddr addr;
socklen_t addrlen = sizeof(addr);
if (!getpeername(fd, &addr, &addrlen)) {
if (addr.sa_family == AF_INET) {
struct sockaddr_in addr_in;
addrlen = sizeof(addr_in);
if (!getpeername(fd, &addr_in, &addrlen)) {
char ip[INET_ADDRSTRLEN] = {0};
inet_ntop(AF_INET, &addr_in.sin_addr, ip, addrlen);
if (asprintf(&name, "server-IP4-%s:%d", ip, ntohs(addr_in.sin_port)) < 0)
return;
}
} else if (addr.sa_family == AF_INET6) {
struct sockaddr_in6 addr_in6;
addrlen = sizeof(addr_in6);
if (!getpeername(fd, &addr_in6, &addrlen)) {
char ip6[INET6_ADDRSTRLEN] = {0};
inet_ntop(AF_INET6, &addr_in6.sin6_addr, ip6, addrlen);
if (asprintf(&name, "server-IP6-%s:%d", ip6, ntohs(addr_in6.sin6_port)) < 0)
return;
}
}
}
if (!name) {
if (asprintf(&name, "server-fd:%d", fd) < 0)
return;
}
con = connection_create();
if (!con)
return;
connection_name_set(con, name);
free(name);
dc = dml_connection_create(fd, con, rx_packet, server_connection_close);
// printf("new server connection %p %p\n", con, dc);
con->dc = dc;
dml_packet_send_hello(dc, DML_PACKET_HELLO_UPDATES, "dmld " DML_VERSION);
}
gboolean client_reconnect(void *clientv)
{
struct dml_client *client = clientv;
int r;
if ((r = dml_client_connect(client))) {
if (r == -2) {
/* Address resolution ongoing... trye again a bit faster */
dml_log(DML_LOG_DEBUG, "Continue reconnect to %s later", dml_client_host_get(client));
g_timeout_add(100, client_reconnect, client);
} else {
dml_log(DML_LOG_ERROR, "Reconnect to DML server %s failed", dml_client_host_get(client));
g_timeout_add_seconds(2, client_reconnect, client);
}
}
return G_SOURCE_REMOVE;
}
int client_connection_close(struct dml_connection *dc, void *arg)
{
struct connection *con = arg;
dml_log(DML_LOG_WARNING, "client close %p %s", dc, connection_name_get(con));
struct dml_client *client = con->client;
g_timeout_add_seconds(1, client_reconnect, client);
dml_route_remove(dc);
connection_destroy(con);
return dml_connection_destroy(dc);
}
void client_connect(struct dml_client *client, void *arg)
{
struct dml_connection *dc;
struct connection *con;
char *name = arg;
dml_log(DML_LOG_INFO, "Connected to DML server %s", name);
con = connection_create();
if (!con)
return;
connection_name_set(con, name);
int fd = dml_client_fd_get(client);
dc = dml_connection_create(fd, con, rx_packet, client_connection_close);
con->dc = dc;
con->client = client;
dml_packet_send_hello(dc, DML_PACKET_HELLO_UPDATES, "dmld " DML_VERSION);
}