-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathccnd_internal_client.c
1636 lines (1562 loc) · 56.7 KB
/
ccnd_internal_client.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
/**
* @file ccnd_internal_client.c
*
* Internal client of ccnd, handles requests for
* inspecting and controlling operation of the ccnd;
* requests and responses themselves use ccn protocols.
*
* Part of ccnd - the CCNx Daemon.
*
* Copyright (C) 2009-2013 Palo Alto Research Center, Inc.
*
* This work is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
* This work 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, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <ccn/ccn.h>
#include <ccn/charbuf.h>
#include <ccn/ccn_private.h>
#include <ccn/hashtb.h>
#include <ccn/keystore.h>
#include <ccn/schedule.h>
#include <ccn/sockaddrutil.h>
#include <ccn/uri.h>
#include "ccnd_private.h"
#if 0
#define GOT_HERE ccnd_msg(ccnd, "at ccnd_internal_client.c:%d", __LINE__);
#else
#define GOT_HERE
#endif
#define CCND_NOTICE_NAME "notice.txt"
#ifndef CCND_TEST_100137
#define CCND_TEST_100137 0
#endif
#ifndef CCND_PING
/* The ping responder is deprecated, but enable it by default for now */
#define CCND_PING 1
#endif
static void ccnd_start_notice(struct ccnd_handle *ccnd);
static void adjacency_timed_reset(struct ccnd_handle *ccnd, unsigned faceid);
/**
* Creates a key object using the service discovery name profile.
*/
static struct ccn_charbuf *
ccnd_init_service_ccnb(struct ccnd_handle *ccnd, const char *baseuri, int freshness)
{
struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
struct ccn *h = ccnd->internal_client;
struct ccn_charbuf *name = ccn_charbuf_create();
struct ccn_charbuf *pubid = ccn_charbuf_create();
struct ccn_charbuf *pubkey = ccn_charbuf_create();
struct ccn_charbuf *keyid = ccn_charbuf_create();
struct ccn_charbuf *cob = ccn_charbuf_create();
int res;
res = ccn_get_public_key(h, NULL, pubid, pubkey);
if (res < 0) abort();
ccn_name_from_uri(name, baseuri);
ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1);
ccn_charbuf_append_string(keyid, ".M.K");
ccn_charbuf_append_value(keyid, 0, 1);
ccn_charbuf_append_charbuf(keyid, pubid);
ccn_name_append(name, keyid->buf, keyid->length);
ccn_create_version(h, name, 0, ccnd->starttime, ccnd->starttime_usec * 1000);
sp.template_ccnb = ccn_charbuf_create();
ccnb_element_begin(sp.template_ccnb, CCN_DTAG_SignedInfo);
ccnb_element_begin(sp.template_ccnb, CCN_DTAG_KeyLocator);
ccnb_element_begin(sp.template_ccnb, CCN_DTAG_KeyName);
ccn_charbuf_append_charbuf(sp.template_ccnb, name);
ccnb_element_end(sp.template_ccnb);
ccnb_element_end(sp.template_ccnb);
ccnb_element_end(sp.template_ccnb);
sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
ccn_name_from_uri(name, "%00");
sp.sp_flags |= CCN_SP_FINAL_BLOCK;
sp.type = CCN_CONTENT_KEY;
sp.freshness = freshness;
res = ccn_sign_content(h, cob, name, &sp, pubkey->buf, pubkey->length);
if (res != 0) abort();
ccn_charbuf_destroy(&name);
ccn_charbuf_destroy(&pubid);
ccn_charbuf_destroy(&pubkey);
ccn_charbuf_destroy(&keyid);
ccn_charbuf_destroy(&sp.template_ccnb);
return(cob);
}
/* These are used in face->adjstate to track our state */
#define ADJ_SOL_SENT (1U << 0)
#define ADJ_SOL_RECV (1U << 1)
#define ADJ_OFR_SENT (1U << 2)
#define ADJ_OFR_RECV (1U << 3)
#define ADJ_CRQ_SENT (1U << 4)
#define ADJ_CRQ_RECV (1U << 5)
#define ADJ_DAT_SENT (1U << 6)
#define ADJ_DAT_RECV (1U << 7)
#define ADJ_TIMEDWAIT (1U << 8)
#define ADJ_PINGING (1U << 9)
#define ADJ_RETRYING (1U << 10)
#define ADJ_ACTIVE (1U << 11)
/**
* Update face->adjstate by setting / clearing the indicated bits.
*
* If a bit is in both masks, it is set.
* @returns the old values, or -1 for an error.
*/
int
adjstate_change_db(struct ccnd_handle *ccnd, struct face *face,
int set, int clear, int line)
{
int new;
int old;
if (face == NULL)
return(-1);
old = face->adjstate;
new = (old & ~clear) | set;
if (new != old) {
face->adjstate = new;
if (ccnd->debug & (2 | 4)) {
/* display the bits in face->adjstate */
char f[] = "sSoOcCdDTPRA\0";
int i;
for (i = 0; f[i] != 0; i++)
if (((new >> i) & 1) == 0)
f[i] = '.';
ccnd_msg(ccnd, "ic.%d adjstate %u %s %#x", line,
face->faceid, f, face->flags);
}
}
return(old);
}
#define adjstate_change(h, f, s, c) adjstate_change_db(h, f, s, c, __LINE__)
/**
* Append the URI representation of the adjacency prefix for face to the
* charbuf cb.
* @returns 0 for success, -1 for error.
*/
static int
append_adjacency_uri(struct ccnd_handle *ccnd,
struct ccn_charbuf *cb, struct face *face)
{
struct ccn_charbuf *name = NULL;
struct ccn_charbuf *comp = NULL;
int res;
if (face->guid == NULL)
return(-1);
comp = ccn_charbuf_create();
name = ccn_charbuf_create();
ccn_name_from_uri(name, "ccnx:/%C1.M.FACE");
ccn_charbuf_append_value(comp, CCN_MARKER_CONTROL, 1);
ccn_charbuf_append_string(comp, ".M.G");
ccn_charbuf_append_value(comp, 0, 1);
ccnd_append_face_guid(ccnd, comp, face);
ccn_name_append(name, comp->buf, comp->length);
res = ccn_uri_append(cb, name->buf, name->length, 1);
ccn_charbuf_destroy(&comp);
ccn_charbuf_destroy(&name);
return(res < 0 ? -1 : 0);
}
#define ADJ_REFRESH_SEC 120
#define ADJ_MICROS (ADJ_REFRESH_SEC * 1000000)
/**
* Scheduled event to refresh adjacency
*/
static int
adjacency_do_refresh(struct ccn_schedule *sched,
void *clienth,
struct ccn_scheduled_event *ev,
int flags)
{
struct ccnd_handle *ccnd = clienth;
struct face *face = NULL;
unsigned both;
face = ccnd_face_from_faceid(ccnd, ev->evint);
if (face == NULL)
return(0);
if ((flags & CCN_SCHEDULE_CANCEL) != 0) {
adjstate_change(ccnd, face, 0, ADJ_ACTIVE);
return(0);
}
both = ADJ_DAT_RECV | ADJ_DAT_SENT;
if ((face->adjstate & both) == both) {
ccnd_adjacency_offer_or_commit_req(ccnd, face);
if ((face->adjstate & ADJ_PINGING) != 0)
return((ADJ_MICROS + nrand48(ccnd->seed) % ADJ_MICROS) / 2);
}
adjstate_change(ccnd, face, 0, ADJ_ACTIVE);
return(0);
}
/**
* Register the adjacency prefix with the given forwarding flags.
*/
static void
ccnd_register_adjacency(struct ccnd_handle *ccnd, struct face *face,
unsigned forwarding_flags)
{
struct ccn_charbuf *uri = NULL;
unsigned both;
int res;
int adj = 0;
int lifetime = 0;
if ((forwarding_flags & CCN_FORW_ACTIVE) != 0) {
adj = CCN_FACE_ADJ;
lifetime = ADJ_REFRESH_SEC; /* seconds */
}
both = ADJ_DAT_RECV | ADJ_DAT_SENT;
if ((face->adjstate & both) != both)
return;
uri = ccn_charbuf_create();
res = append_adjacency_uri(ccnd, uri, face);
if (res >= 0)
res = ccnd_reg_uri(ccnd, ccn_charbuf_as_string(uri), face->faceid,
forwarding_flags, lifetime);
if (res >= 0) {
if ((face->flags & CCN_FACE_ADJ) != adj) {
face->flags ^= CCN_FACE_ADJ;
ccnd_face_status_change(ccnd, face->faceid);
}
if (lifetime != 0 && (face->adjstate & ADJ_ACTIVE) == 0) {
ccn_schedule_event(ccnd->sched, lifetime * 1000000,
adjacency_do_refresh, NULL, face->faceid);
adjstate_change(ccnd, face, ADJ_ACTIVE, 0);
}
}
ccn_charbuf_destroy(&uri);
}
/**
* Scheduled event for getting rid of an old guid cob.
*/
static int
ccnd_flush_guid_cob(struct ccn_schedule *sched,
void *clienth,
struct ccn_scheduled_event *ev,
int flags)
{
struct ccnd_handle *ccnd = clienth;
struct face *face = NULL;
if ((flags & CCN_SCHEDULE_CANCEL) != 0)
return(0);
face = ccnd_face_from_faceid(ccnd, ev->evint);
if (face != NULL)
ccn_charbuf_destroy(&face->guid_cob);
return(0);
}
/**
* Create the adjacency content object for our endpoint of the face.
*/
static void
ccnd_init_face_guid_cob(struct ccnd_handle *ccnd, struct face *face)
{
struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
struct ccn *h = ccnd->internal_client;
struct ccn_charbuf *name = NULL;
struct ccn_charbuf *payload = NULL;
struct ccn_charbuf *comp = NULL;
struct ccn_charbuf *cob = NULL;
int res;
int seconds = 60; /* freshness in the object */
int nfresh = 20; /* flush it after this many freshness periods */
if (face->guid == NULL || face->guid_cob != NULL)
return;
if ((face->adjstate & (ADJ_OFR_SENT | ADJ_OFR_RECV)) == 0)
return;
name = ccn_charbuf_create();
payload = ccn_charbuf_create();
comp = ccn_charbuf_create();
cob = ccn_charbuf_create();
ccn_name_from_uri(name, "ccnx:/%C1.M.FACE");
/* %C1.G.%00<guid> */
ccn_charbuf_reset(comp);
ccn_charbuf_append_value(comp, CCN_MARKER_CONTROL, 1);
ccn_charbuf_append_string(comp, ".M.G");
ccn_charbuf_append_value(comp, 0, 1);
ccnd_append_face_guid(ccnd, comp, face);
ccn_name_append(name, comp->buf, comp->length);
ccn_name_from_uri(name, "%C1.M.NODE");
/* %C1.K.%00<ccndid> */
ccn_charbuf_reset(comp);
ccn_charbuf_append_value(comp, CCN_MARKER_CONTROL, 1);
ccn_charbuf_append_string(comp, ".M.K");
ccn_charbuf_append_value(comp, 0, 1);
ccn_charbuf_append(comp, ccnd->ccnd_id, sizeof(ccnd->ccnd_id));
ccn_name_append(name, comp->buf, comp->length);
ccn_charbuf_reset(comp);
ccn_charbuf_putf(comp, "face~%u", face->faceid);
ccn_name_append(name, comp->buf, comp->length);
ccn_create_version(h, name, CCN_V_NOW, 0, 0);
ccn_name_from_uri(name, "%00");
sp.sp_flags |= CCN_SP_FINAL_BLOCK;
sp.freshness = seconds;
res = ccn_sign_content(h, cob, name, &sp, payload->buf, payload->length);
if (res != 0)
ccn_charbuf_destroy(&cob);
ccn_charbuf_destroy(&name);
ccn_charbuf_destroy(&payload);
ccn_charbuf_destroy(&comp);
ccn_charbuf_destroy(&sp.template_ccnb);
face->guid_cob = cob;
if (cob != NULL)
ccn_schedule_event(ccnd->sched, nfresh * seconds * 1000000 - 800000,
ccnd_flush_guid_cob, NULL, face->faceid);
}
/**
* Isolate the lower and upper bounds for the guid component from Exclude
*
* This is used as part of the adjacency protocol.
*/
static int
extract_bounds(const unsigned char *ccnb, struct ccn_parsed_interest *pi,
const unsigned char **plo, const unsigned char **phi)
{
struct ccn_buf_decoder decoder;
struct ccn_buf_decoder *d = NULL;
int res = -1;
int x;
int y;
int z;
size_t sz;
/* We are interested only in the Exclude element. */
ccnb = ccnb + pi->offset[CCN_PI_B_Exclude];
sz = pi->offset[CCN_PI_E_Exclude] - pi->offset[CCN_PI_B_Exclude];
if (sz != 0) {
d = ccn_buf_decoder_start(&decoder, ccnb, sz);
if (ccn_buf_match_dtag(d, CCN_DTAG_Exclude)) {
ccn_buf_advance(d);
if (ccn_buf_match_dtag(d, CCN_DTAG_Any)) {
ccn_buf_advance(d);
ccn_buf_check_close(d);
}
else return(-1);
x = d->decoder.token_index;
ccn_parse_required_tagged_BLOB(d, CCN_DTAG_Component, 8, 70);
y = d->decoder.token_index;
ccn_parse_required_tagged_BLOB(d, CCN_DTAG_Component, 8, 70);
z = d->decoder.token_index;
if (ccn_buf_match_dtag(d, CCN_DTAG_Any)) {
ccn_buf_advance(d);
ccn_buf_check_close(d);
}
else return(-1);
ccn_buf_check_close(d);
if (d->decoder.state < 0)
return (-1);
if (y - x != z - y)
return(-1);
res = ccn_ref_tagged_BLOB(CCN_DTAG_Component, ccnb, x, y, plo, &sz);
if (res < 0) return(-1);
res = ccn_ref_tagged_BLOB(CCN_DTAG_Component, ccnb, y, z, phi, &sz);
if (res < 0) return(-1);
return(sz);
}
}
return(-1);
}
/**
* Handle the data that comes back in response to interest sent by
* send_adjacency_solicit.
*
* We don't actually need to do much here, since the protocol is actually
* looking for an interest from the other side.
*/
static enum ccn_upcall_res
solicit_response(struct ccn_closure *selfp,
enum ccn_upcall_kind kind,
struct ccn_upcall_info *info)
{
struct face *face = NULL;
struct ccnd_handle *ccnd = selfp->data;
switch (kind) {
case CCN_UPCALL_FINAL:
free(selfp);
return(CCN_UPCALL_RESULT_OK);
default:
face = ccnd_face_from_faceid(ccnd, selfp->intdata);
if (face == NULL)
return(CCN_UPCALL_RESULT_ERR);
if ((face->adjstate & (ADJ_SOL_SENT)) != 0)
adjacency_timed_reset(ccnd, face->faceid);
return(CCN_UPCALL_RESULT_ERR);
}
}
/**
* Send an adjacency solitiation interest, to elicit an offer from the
* other side.
*/
static int
send_adjacency_solicit(struct ccnd_handle *ccnd, struct face *face)
{
struct ccn_charbuf *name;
struct ccn_charbuf *c;
struct ccn_charbuf *g;
struct ccn_charbuf *templ;
struct ccn_closure *action = NULL;
int i;
int ans = -1;
if (face == NULL || face->guid != NULL || face->adjstate != 0)
return(-1);
/* Need to poke the client library here so that it gets the curren time */
ccn_process_scheduled_operations(ccnd->internal_client);
name = ccn_charbuf_create();
c = ccn_charbuf_create();
g = ccn_charbuf_create();
templ = ccn_charbuf_create();
/* Construct a proposed partial guid, without marker bytes */
ccn_charbuf_reset(g);
ccn_charbuf_append_value(g, 0, 1); /* 1 reserved byte of zero */
/* The first half is chosen by our side */
for (i = 0; i < 6; i++)
ccn_charbuf_append_value(g, nrand48(ccnd->seed) & 0xff, 1);
/* The second half will be chosen by the other side */
for (i = 0; i < 6; i++)
ccn_charbuf_append_value(g, 0, 1);
/* Construct the interest */
ccn_charbuf_reset(templ);
ccnb_element_begin(templ, CCN_DTAG_Interest);
ccn_name_from_uri(name, "ccnx:/%C1.M.FACE");
ccn_charbuf_append_charbuf(templ, name);
/* This interest excludes all but a range of possible guid components */
ccnb_element_begin(templ, CCN_DTAG_Exclude);
ccnb_tagged_putf(templ, CCN_DTAG_Any, "");
ccn_charbuf_reset(c);
ccn_charbuf_append_string(c, "\xC1.M.G");
ccn_charbuf_append_value(c, 0, 1);
ccn_charbuf_append(c, g->buf, g->length);
ccnb_append_tagged_blob(templ, CCN_DTAG_Component, c->buf, c->length);
ccn_charbuf_reset(c);
ccn_charbuf_append_string(c, "\xC1.M.G");
ccn_charbuf_append_value(c, 0, 1);
ccn_charbuf_append(c, g->buf, g->length - 6);
for (i = 0; i < 6; i++)
ccn_charbuf_append_value(c, 0xff, 1);
ccnb_append_tagged_blob(templ, CCN_DTAG_Component, c->buf, c->length);
ccnb_tagged_putf(templ, CCN_DTAG_Any, "");
ccnb_element_end(templ); /* Exclude */
/* We don't want to get confused by cached content */
ccnb_tagged_putf(templ, CCN_DTAG_AnswerOriginKind, "%d", 0);
/* Only talk to direct peers */
ccnb_tagged_putf(templ, CCN_DTAG_Scope, "2");
/* Bypass the FIB - send to just the face we want */
ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face->faceid);
ccnb_element_end(templ); /* Interest */
action = calloc(1, sizeof(*action));
if (action != NULL) {
action->p = &solicit_response;
action->intdata = face->faceid;
action->data = ccnd;
ans = ccn_express_interest(ccnd->internal_client, name, action, templ);
/* Use the guid slot to hold our proposal */
if (ans >= 0)
ans = ccnd_set_face_guid(ccnd, face, g->buf, g->length);
if (ans >= 0) {
adjstate_change(ccnd, face, ADJ_SOL_SENT, 0);
ccnd_internal_client_has_somthing_to_say(ccnd);
}
ans = (ans < 0) ? -1 : 0;
}
ccn_charbuf_destroy(&name);
ccn_charbuf_destroy(&c);
ccn_charbuf_destroy(&g);
ccn_charbuf_destroy(&templ);
return(ans);
}
/**
* Scheduled event to call send_adjacency_solicit.
*/
static int
ccnd_do_solicit(struct ccn_schedule *sched,
void *clienth,
struct ccn_scheduled_event *ev,
int flags)
{
struct ccnd_handle *ccnd = clienth;
struct face *face = NULL;
unsigned faceid;
unsigned check, want;
if ((flags & CCN_SCHEDULE_CANCEL) != 0)
return(0);
faceid = ev->evint;
face = ccnd_face_from_faceid(ccnd, faceid);
if (face == NULL)
return(0);
check = CCN_FACE_CONNECTING | CCN_FACE_UNDECIDED | CCN_FACE_NOSEND |
CCN_FACE_GG | CCN_FACE_MCAST | CCN_FACE_PASSIVE | CCN_FACE_NORECV |
CCN_FACE_BC | CCN_FACE_ADJ;
want = 0;
if (face->adjstate == 0 && (face->flags & check) == want)
send_adjacency_solicit(ccnd, face);
return(0);
}
/**
* Answer an adjacency guid request from any face, based on the guid
* in the name.
*
* @returns CCN_UPCALL_RESULT_INTEREST_CONSUMED if an answer was sent,
* otherwise -1.
*/
static int
ccnd_answer_by_guid(struct ccnd_handle *ccnd, struct ccn_upcall_info *info)
{
struct face *face = NULL;
unsigned char mb[6] = "\xC1.M.G\x00";
const unsigned char *p = NULL;
size_t size = 0;
unsigned faceid;
int res;
res = ccn_name_comp_get(info->interest_ccnb, info->interest_comps, 1,
&p, &size);
if (res < 0)
return(-1);
if (size < sizeof(mb))
return(-1);
if (memcmp(p, mb, sizeof(mb)) != 0)
return(-1);
faceid = ccnd_faceid_from_guid(ccnd, p + sizeof(mb), size - sizeof(mb));
if (faceid == CCN_NOFACEID)
return(-1);
face = ccnd_face_from_faceid(ccnd, faceid);
if (face == NULL)
return(-1);
if ((face->flags & CCN_FACE_ADJ) == 0)
return(-1);
if (face->guid_cob == NULL)
ccnd_init_face_guid_cob(ccnd, face);
if (face->guid_cob == NULL)
return(-1);
res = -1;
if (ccn_content_matches_interest(face->guid_cob->buf,
face->guid_cob->length,
1,
NULL,
info->interest_ccnb,
info->pi->offset[CCN_PI_E],
info->pi
)) {
ccn_put(info->h, face->guid_cob->buf, face->guid_cob->length);
res = CCN_UPCALL_RESULT_INTEREST_CONSUMED;
}
return(res);
}
/**
* Handle the data coming back from an adjacency offer or commit request.
*/
static enum ccn_upcall_res
incoming_adjacency(struct ccn_closure *selfp,
enum ccn_upcall_kind kind,
struct ccn_upcall_info *info)
{
struct face *face = NULL;
struct ccnd_handle *ccnd = selfp->data;
switch (kind) {
case CCN_UPCALL_FINAL:
free(selfp);
return(CCN_UPCALL_RESULT_OK);
case CCN_UPCALL_CONTENT:
face = ccnd_face_from_faceid(ccnd, selfp->intdata);
if (face == NULL)
return(CCN_UPCALL_RESULT_ERR);
if ((face->adjstate & (ADJ_TIMEDWAIT)) != 0)
return(CCN_UPCALL_RESULT_ERR);
/* XXX - this should scrutinize the data to make sure it is OK */
if ((face->adjstate & (ADJ_OFR_SENT | ADJ_CRQ_SENT)) != 0)
adjstate_change(ccnd, face, ADJ_DAT_RECV, 0);
adjstate_change(ccnd, face, 0, ADJ_PINGING | ADJ_RETRYING);
if ((face->adjstate & (ADJ_CRQ_RECV)) != 0 &&
(face->adjstate & (ADJ_DAT_SENT)) == 0 &&
face->guid_cob != 0) {
ccn_put(info->h, face->guid_cob->buf,
face->guid_cob->length);
adjstate_change(ccnd, face, ADJ_DAT_SENT, 0);
if ((face->adjstate & (ADJ_DAT_RECV)) == 0)
ccnd_adjacency_offer_or_commit_req(ccnd, face);
}
ccnd_register_adjacency(ccnd, face,
CCN_FORW_CHILD_INHERIT | CCN_FORW_ACTIVE);
return(CCN_UPCALL_RESULT_OK);
case CCN_UPCALL_INTEREST_TIMED_OUT:
face = ccnd_face_from_faceid(ccnd, selfp->intdata);
if (face == NULL)
return(CCN_UPCALL_RESULT_ERR);
if ((face->adjstate & (ADJ_RETRYING | ADJ_TIMEDWAIT)) == 0) {
/* Retry one time */
adjstate_change(ccnd, face, ADJ_RETRYING, 0);
return(CCN_UPCALL_RESULT_REEXPRESS);
}
adjacency_timed_reset(ccnd, face->faceid);
return(CCN_UPCALL_RESULT_OK);
default:
face = ccnd_face_from_faceid(ccnd, selfp->intdata);
if (face != NULL)
adjacency_timed_reset(ccnd, face->faceid);
return(CCN_UPCALL_RESULT_ERR);
}
}
/**
* Express an interest to pull adjacency information from the other side
*/
void
ccnd_adjacency_offer_or_commit_req(struct ccnd_handle *ccnd, struct face *face)
{
struct ccn_charbuf *name;
struct ccn_charbuf *c;
struct ccn_charbuf *templ;
struct ccn_closure *action = NULL;
if (face == NULL || face->guid == NULL)
return;
if ((face->adjstate & (ADJ_SOL_SENT | ADJ_TIMEDWAIT)) != 0)
return;
if ((face->adjstate & (ADJ_PINGING)) != 0)
return;
/* Need to poke the client library here so that it gets the current time */
ccn_process_scheduled_operations(ccnd->internal_client);
name = ccn_charbuf_create();
c = ccn_charbuf_create();
templ = ccn_charbuf_create();
ccn_name_from_uri(name, "ccnx:/%C1.M.FACE");
ccn_charbuf_reset(c);
ccn_charbuf_append_string(c, "\xC1.M.G");
ccn_charbuf_append_value(c, 0, 1);
ccnd_append_face_guid(ccnd, c, face);
ccn_name_append(name, c->buf, c->length);
ccn_name_from_uri(name, "%C1.M.NODE");
ccn_charbuf_reset(templ);
ccnb_element_begin(templ, CCN_DTAG_Interest);
ccn_charbuf_append_charbuf(templ, name);
ccnb_element_begin(templ, CCN_DTAG_Exclude);
ccn_charbuf_reset(c);
ccn_charbuf_append_string(c, "\xC1.M.K");
ccn_charbuf_append_value(c, 0, 1);
ccn_charbuf_append(c, ccnd->ccnd_id, sizeof(ccnd->ccnd_id));
ccnb_append_tagged_blob(templ, CCN_DTAG_Component, c->buf, c->length);
ccnb_element_end(templ); /* Exclude */
ccnb_tagged_putf(templ, CCN_DTAG_AnswerOriginKind, "%d", 0);
ccnb_tagged_putf(templ, CCN_DTAG_Scope, "2");
ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face->faceid);
ccnb_element_end(templ); /* Interest */
action = calloc(1, sizeof(*action));
if (action != NULL) {
action->p = &incoming_adjacency;
action->intdata = face->faceid;
action->data = ccnd;
adjstate_change(ccnd, face, ADJ_PINGING, ADJ_RETRYING);
ccn_express_interest(ccnd->internal_client, name, action, templ);
if ((face->adjstate & ADJ_OFR_RECV) != 0)
adjstate_change(ccnd, face, ADJ_CRQ_SENT, 0);
else
adjstate_change(ccnd, face, ADJ_OFR_SENT, 0);
}
ccn_charbuf_destroy(&name);
ccn_charbuf_destroy(&c);
ccn_charbuf_destroy(&templ);
}
/**
* Determine whether an offer matches up with our solicitation.
*/
static void
check_offer_matches_my_solicit(struct ccnd_handle *ccnd, struct face *face,
struct ccn_upcall_info *info)
{
const unsigned char *p = NULL;
size_t size = 0;
int res;
const char *mg = "\xC1.M.G";
const char *mn = "\xC1.M.NODE";
if (info->pi->prefix_comps != 3)
return;
if ((face->adjstate & ADJ_SOL_SENT) == 0)
return;
if (face->guid == NULL)
return;
res = ccn_name_comp_get(info->interest_ccnb, info->interest_comps, 2,
&p, &size);
if (res < 0)
return;
if (size != strlen(mn) || 0 != memcmp(p, mn, size))
return;
res = ccn_name_comp_get(info->interest_ccnb, info->interest_comps, 1,
&p, &size);
if (res < 0)
return;
res = strlen(mg) + 1;
if (size != res + face->guid[0] || face->guid[0] <= 6)
return;
if (0 != memcmp(p, mg, res))
return;
if (0 != memcmp(p + res, face->guid + 1, face->guid[0] - 6))
return;
ccnd_forget_face_guid(ccnd, face);
ccnd_set_face_guid(ccnd, face, p + res, size - res);
adjstate_change(ccnd, face, ADJ_OFR_RECV, ADJ_SOL_SENT);
}
/**
* Schedule negotiation of a link guid if appropriate
*/
static void
schedule_adjacency_negotiation(struct ccnd_handle *ccnd, unsigned faceid)
{
struct face *face = ccnd_face_from_faceid(ccnd, faceid);
unsigned check, want;
int delay;
if (face == NULL)
return;
check = CCN_FACE_CONNECTING | CCN_FACE_UNDECIDED | CCN_FACE_NOSEND |
CCN_FACE_GG | CCN_FACE_MCAST | CCN_FACE_PASSIVE | CCN_FACE_NORECV |
CCN_FACE_BC | CCN_FACE_ADJ;
want = 0;
if (ccnd->sched != NULL && (face->flags & check) == want) {
/* If face creation was initiated remotely, dally a bit longer. */
delay = 2000 + nrand48(ccnd->seed) % 131072U;
if ((face->flags & CCN_FACE_PERMANENT) == 0)
delay += 200000;
ccn_schedule_event(ccnd->sched, delay, ccnd_do_solicit, NULL, faceid);
}
}
/**
* Scheduled event for recovering from a broken adjacency negotiation
*/
static int
adjacency_do_reset(struct ccn_schedule *sched,
void *clienth,
struct ccn_scheduled_event *ev,
int flags)
{
struct ccnd_handle *ccnd = clienth;
struct face *face = NULL;
if ((flags & CCN_SCHEDULE_CANCEL) != 0)
return(0);
face = ccnd_face_from_faceid(ccnd, ev->evint);
if (face == NULL)
return(0);
if ((face->adjstate & ADJ_TIMEDWAIT) == 0)
return(0);
if (face->adjstate != ADJ_TIMEDWAIT) {
adjstate_change(ccnd, face, ADJ_TIMEDWAIT, ~ADJ_ACTIVE);
ccnd_forget_face_guid(ccnd, face);
return(666666);
}
adjstate_change(ccnd, face, 0, ~0);
schedule_adjacency_negotiation(ccnd, face->faceid);
return(0);
}
/**
* Schedule recovery from a broken adjacency negotiation
*/
static void
adjacency_timed_reset(struct ccnd_handle *ccnd, unsigned faceid)
{
struct face *face = ccnd_face_from_faceid(ccnd, faceid);
if (face == NULL || ccnd->sched == NULL)
return;
if ((face->flags & CCN_FACE_ADJ) != 0) {
ccnd_face_status_change(ccnd, faceid);
face->flags &= ~CCN_FACE_ADJ;
}
adjstate_change(ccnd, face, ADJ_TIMEDWAIT, ~ADJ_ACTIVE);
ccnd_forget_face_guid(ccnd, face);
ccn_schedule_event(ccnd->sched, 9000000 + nrand48(ccnd->seed) % 8000000U,
adjacency_do_reset, NULL, faceid);
}
static int
clean_guest(struct ccn_schedule *sched,
void *clienth,
struct ccn_scheduled_event *ev,
int flags)
{
struct ccnd_handle *ccnd = clienth;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = ⅇ
unsigned faceid;
int res;
if ((flags & CCN_SCHEDULE_CANCEL) != 0)
return(0);
faceid = ev->evint;
hashtb_start(ccnd->guest_tab, e);
res = hashtb_seek(e, &faceid, sizeof(unsigned), 0);
if (res < 0)
return(-1);
hashtb_delete(e);
hashtb_end(e);
return(0);
}
static enum ccn_upcall_res
ccnd_req_guest(struct ccn_closure *selfp,
enum ccn_upcall_kind kind,
struct ccn_upcall_info *info)
{
struct ccnd_handle *ccnd = selfp->data;
struct hashtb_enumerator ee;
struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
struct hashtb_enumerator *e = ⅇ
const char *guest_uri = NULL;
struct ccn_charbuf *name = NULL;
struct ccn_charbuf *uri = NULL;
struct face *reqface = NULL;
struct guest_entry *g = NULL;
const unsigned char *p = NULL;
size_t size = 0;
size_t start = 0;
size_t end = 0;
int res;
guest_uri = getenv("CCND_PREFIX");
if (guest_uri == NULL || guest_uri[0] == 0)
return(CCN_UPCALL_RESULT_ERR);
reqface = ccnd_face_from_faceid(ccnd, ccnd->interest_faceid);
if (reqface == NULL)
return(CCN_UPCALL_RESULT_ERR);
if ((reqface->flags & CCN_FACE_GG) != 0)
return(CCN_UPCALL_RESULT_ERR);
name = ccn_charbuf_create();
if (name == NULL)
return(CCN_UPCALL_RESULT_ERR);
res = ccn_name_from_uri(name, guest_uri);
if (res < 0) {
ccn_charbuf_destroy(&name);
return(CCN_UPCALL_RESULT_ERR);
}
hashtb_start(ccnd->guest_tab, e);
res = hashtb_seek(e, &reqface->faceid, sizeof(unsigned), 0);
if (res < 0) {
ccn_charbuf_destroy(&name);
return(CCN_UPCALL_RESULT_ERR);
}
g = e->data;
hashtb_end(e);
if (g->cob != NULL) {
if (ccn_content_matches_interest(g->cob->buf,
g->cob->length,
1,
NULL,
info->interest_ccnb,
info->pi->offset[CCN_PI_E],
info->pi)) {
ccn_put(info->h, g->cob->buf, g->cob->length);
ccn_charbuf_destroy(&name);
return(CCN_UPCALL_RESULT_INTEREST_CONSUMED);
}
/* We have a cob cached; no new one until the old one expires */
ccn_charbuf_destroy(&name);
return(CCN_UPCALL_RESULT_ERR);
}
if (info->interest_comps->n != 4) {
ccn_charbuf_destroy(&name);
return(CCN_UPCALL_RESULT_ERR);
}
res = ccn_name_comp_get(info->interest_ccnb, info->interest_comps, 2,
&p, &size);
if (res < 0) {
ccn_charbuf_destroy(&name);
return(CCN_UPCALL_RESULT_ERR);
}
ccn_name_append(name, p, size);
uri = ccn_charbuf_create();
ccn_uri_append(uri, name->buf, name->length, 1);
ccnd_reg_uri(ccnd, ccn_charbuf_as_string(uri), reqface->faceid,
CCN_FORW_CHILD_INHERIT | CCN_FORW_ACTIVE,
0x7FFFFFFF);
g->cob = ccn_charbuf_create();
ccn_charbuf_reset(name);
start = info->pi->offset[CCN_PI_B_Name];
end = info->interest_comps->buf[info->pi->prefix_comps];
ccn_charbuf_append(name, info->interest_ccnb + start, end - start);
ccnb_element_end(name);
ccn_create_version(info->h, name, CCN_V_NOW, 0, 0);
ccn_name_from_uri(name, "%00");
sp.sp_flags = CCN_SP_FINAL_BLOCK;
sp.freshness = 5;
res = ccn_sign_content(info->h, g->cob, name, &sp, uri->buf, uri->length);
if (res < 0) {
ccn_charbuf_destroy(&name);
ccn_charbuf_destroy(&g->cob);
ccn_charbuf_destroy(&uri);
return(CCN_UPCALL_RESULT_ERR);
}
ccn_schedule_event(ccnd->sched, sp.freshness * 1000000,
clean_guest, NULL, reqface->faceid);
if (g->cob != NULL &&
ccn_content_matches_interest(g->cob->buf,
g->cob->length,
1,
NULL,
info->interest_ccnb,
info->pi->offset[CCN_PI_E],
info->pi)) {
ccn_put(info->h, g->cob->buf, g->cob->length);
ccn_charbuf_destroy(&name);
ccn_charbuf_destroy(&uri);
return(CCN_UPCALL_RESULT_INTEREST_CONSUMED);
}
return(CCN_UPCALL_RESULT_OK);
}
/**
* Local interpretation of selfp->intdata
*/
#define MORECOMPS_MASK 0x007F
#define MUST_VERIFY 0x0080
#define MUST_VERIFY1 (MUST_VERIFY + 1)
#define OPER_MASK 0xFF00
#define OP_PING 0x0000
#define OP_NEWFACE 0x0200
#define OP_DESTROYFACE 0x0300
#define OP_PREFIXREG 0x0400
#define OP_SELFREG 0x0500
#define OP_UNREG 0x0600
#define OP_NOTICE 0x0700
#define OP_SERVICE 0x0800
#define OP_ADJACENCY 0x0900
#define OP_GUEST 0x0A00
#define OP_SETSTRATEGY 0x0B00
#define OP_GETSTRATEGY 0x0C00
#define OP_REMSTRATEGY 0x0D00
/**
* Common interest handler for ccnd_internal_client
*/
static enum ccn_upcall_res
ccnd_answer_req(struct ccn_closure *selfp,
enum ccn_upcall_kind kind,
struct ccn_upcall_info *info)
{
struct ccn_charbuf *msg = NULL;
struct ccn_charbuf *name = NULL;
struct ccn_charbuf *keylocator = NULL;
struct ccn_charbuf *signed_info = NULL;
struct ccn_charbuf *reply_body = NULL;
struct ccnd_handle *ccnd = NULL;
int res = 0;
int start = 0;
int end = 0;
int morecomps = 0;
const unsigned char *final_comp = NULL;
size_t final_size = 0;
struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
struct face *face = NULL;
const char *v = NULL;
switch (kind) {
case CCN_UPCALL_FINAL:
free(selfp);
return(CCN_UPCALL_RESULT_OK);
case CCN_UPCALL_INTEREST:
break;