-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNdnHandler.cpp
91 lines (82 loc) · 2.71 KB
/
NdnHandler.cpp
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
#include "NdnHandler.h"
NdnHandler::NdnHandler() {
initKeystore();
createKeylocator();
h = ccn_create();
if (h == NULL || ccn_connect(h, NULL) == -1) {
fprintf(stderr, "Failed to connect to ccnd.\n");
abort();
}
}
NdnHandler::~NdnHandler() {
if (h != NULL) {
ccn_disconnect(h);
// destory will can crash at
// ccn_destroy -> ccn_destroy_interest-> ccn_replace_handler
// ccn_destroy(&h);
}
if (keystore != NULL) {
ccn_keystore_destroy(&keystore);
}
if (keylocator != NULL) {
ccn_charbuf_destroy(&keylocator);
}
}
void NdnHandler::initKeystore() {
keystore = ccn_keystore_create();
struct ccn_charbuf *temp = ccn_charbuf_create();
ccn_charbuf_putf(temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME"));
int res = ccn_keystore_init(keystore, ccn_charbuf_as_string(temp), (char *)"Th1s1sn0t8g00dp8ssw0rd.");
if (res != 0) {
fprintf(stderr, "Failed to initialized keystore %s\n", ccn_charbuf_as_string(temp));
abort();
}
ccn_charbuf_destroy(&temp);
}
void NdnHandler::createKeylocator() {
keylocator = ccn_charbuf_create();
ccn_charbuf_append_tt(keylocator, CCN_DTAG_KeyLocator, CCN_DTAG);
ccn_charbuf_append_tt(keylocator, CCN_DTAG_Key, CCN_DTAG);
int res = ccn_append_pubkey_blob(keylocator, ccn_keystore_public_key(keystore));
if (res < 0) {
ccn_charbuf_destroy(&keylocator);
}else {
ccn_charbuf_append_closer(keylocator);
ccn_charbuf_append_closer(keylocator);
}
}
ssize_t NdnHandler::getPublicKeyDigestLength() {
return ccn_keystore_public_key_digest_length(keystore);
}
const unsigned char *NdnHandler::getPublicKeyDigest() {
return ccn_keystore_public_key_digest(keystore);
}
const struct ccn_pkey *NdnHandler::getPrivateKey() {
return ccn_keystore_private_key(keystore);
}
/*
* Comparison operator for sorting the excl list with qsort.
* For convenience, the items in the excl array are
* charbufs containing ccnb-encoded Names of one component each.
* (This is not the most efficient representation.)
*/
int /* for qsort */
NdnHandler::nameCompare(const void *a, const void *b)
{
const struct ccn_charbuf *aa = *(const struct ccn_charbuf **)a;
const struct ccn_charbuf *bb = *(const struct ccn_charbuf **)b;
int ans = ccn_compare_names(aa->buf, aa->length, bb->buf, bb->length);
if (ans == 0)
fprintf(stderr, "wassat? %d\n", __LINE__);
return (ans);
}
void NdnHandler::excludeAll(struct ccn_charbuf *c)
{
unsigned char bf_all[9] = { 3, 1, 'A', 0, 0, 0, 0, 0, 0xFF };
const struct ccn_bloom_wire *b = ccn_bloom_validate_wire(bf_all, sizeof(bf_all));
if (b == NULL) abort();
ccn_charbuf_append_tt(c, CCN_DTAG_Bloom, CCN_DTAG);
ccn_charbuf_append_tt(c, sizeof(bf_all), CCN_BLOB);
ccn_charbuf_append(c, bf_all, sizeof(bf_all));
ccn_charbuf_append_closer(c);
}