Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn> |
| 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 22 | |
| 23 | #include "sync-ccnx-wrapper.h" |
| 24 | #include <poll.h> |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 25 | |
| 26 | using namespace std; |
| 27 | using namespace boost; |
| 28 | |
| 29 | namespace Sync { |
| 30 | |
| 31 | CcnxWrapper::CcnxWrapper() |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 32 | : m_handle (0) |
| 33 | , m_keyStore (0) |
| 34 | , m_keyLoactor (0) |
| 35 | , m_running (true) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 36 | { |
| 37 | m_handle = ccn_create(); |
| 38 | ccn_connect(m_handle, NULL); |
| 39 | initKeyStore(); |
| 40 | createKeyLocator(); |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 41 | m_thread = thread (&CcnxWrapper::ccnLoop, this); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | CcnxWrapper::~CcnxWrapper() |
| 45 | { |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 46 | m_running = false; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 47 | m_thread.join(); |
| 48 | ccn_disconnect(m_handle); |
| 49 | ccn_destroy(&m_handle); |
| 50 | ccn_charbuf_destroy(&m_keyLoactor); |
| 51 | ccn_keystore_destroy(&m_keyStore); |
| 52 | } |
| 53 | |
| 54 | void CcnxWrapper::createKeyLocator() |
| 55 | { |
| 56 | int res; |
| 57 | |
| 58 | m_keyLoactor = ccn_charbuf_create(); |
| 59 | ccn_charbuf_append_tt(m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG); |
| 60 | ccn_charbuf_append_tt(m_keyLoactor, CCN_DTAG_Key, CCN_DTAG); |
| 61 | res = ccn_append_pubkey_blob(m_keyLoactor, ccn_keystore_public_key(m_keyStore)); |
| 62 | if (res >= 0) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 63 | { |
| 64 | ccn_charbuf_append_closer(m_keyLoactor); /* </Key> */ |
| 65 | ccn_charbuf_append_closer(m_keyLoactor); /* </KeyLocator> */ |
| 66 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | const ccn_pkey* CcnxWrapper::getPrivateKey() |
| 70 | { |
| 71 | return ccn_keystore_private_key(m_keyStore); |
| 72 | } |
| 73 | |
| 74 | const unsigned char* CcnxWrapper::getPublicKeyDigest() |
| 75 | { |
| 76 | return ccn_keystore_public_key_digest(m_keyStore); |
| 77 | } |
| 78 | |
| 79 | ssize_t CcnxWrapper::getPublicKeyDigestLength() |
| 80 | { |
| 81 | return ccn_keystore_public_key_digest_length(m_keyStore); |
| 82 | } |
| 83 | |
| 84 | void CcnxWrapper::initKeyStore() |
| 85 | { |
| 86 | ccn_charbuf *temp = ccn_charbuf_create(); |
| 87 | m_keyStore = ccn_keystore_create(); |
| 88 | ccn_charbuf_putf(temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME")); |
| 89 | ccn_keystore_init(m_keyStore, ccn_charbuf_as_string(temp), (char*)"Th1s1sn0t8g00dp8ssw0rd."); |
| 90 | ccn_charbuf_destroy(&temp); |
| 91 | } |
| 92 | |
| 93 | void CcnxWrapper::ccnLoop() |
| 94 | { |
| 95 | pollfd pfds[1]; |
| 96 | int res = ccn_run(m_handle, 0); |
| 97 | |
| 98 | pfds[0].fd = ccn_get_connection_fd(m_handle); |
| 99 | pfds[0].events = POLLIN; |
| 100 | |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 101 | while (m_running) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 103 | if (res >= 0) |
| 104 | { |
| 105 | int ret = poll(pfds, 1, 100); |
| 106 | if (ret >= 0) |
| 107 | { |
| 108 | recursive_mutex::scoped_lock lock(m_mutex); |
| 109 | res = ccn_run(m_handle, 0); |
| 110 | } |
| 111 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 112 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 115 | int CcnxWrapper::publishData(string name, string dataBuffer, int freshness) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 116 | { |
| 117 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 118 | ccn_charbuf *signed_info = ccn_charbuf_create(); |
| 119 | ccn_charbuf *content = ccn_charbuf_create(); |
| 120 | |
| 121 | ccn_name_from_uri(pname, name.c_str()); |
| 122 | ccn_signed_info_create(signed_info, |
| 123 | getPublicKeyDigest(), |
| 124 | getPublicKeyDigestLength(), |
| 125 | NULL, |
| 126 | CCN_CONTENT_DATA, |
| 127 | freshness, |
| 128 | NULL, |
| 129 | m_keyLoactor); |
| 130 | ccn_encode_ContentObject(content, pname, signed_info, |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 131 | dataBuffer.c_str(), dataBuffer.length(), |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 132 | NULL, getPrivateKey()); |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 133 | recursive_mutex::scoped_lock lock(m_mutex); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 134 | ccn_put(m_handle, content->buf, content->length); |
| 135 | |
| 136 | ccn_charbuf_destroy(&pname); |
| 137 | ccn_charbuf_destroy(&signed_info); |
| 138 | ccn_charbuf_destroy(&content); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static ccn_upcall_res incomingInterest( |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 143 | ccn_closure *selfp, |
| 144 | ccn_upcall_kind kind, |
| 145 | ccn_upcall_info *info) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 146 | { |
Chaoyi Bian | 49d4675 | 2012-03-07 10:35:21 -0800 | [diff] [blame] | 147 | function<void (string)> f = *(function<void (string)> *)selfp->data; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 148 | |
| 149 | switch (kind) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 150 | { |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 151 | case CCN_UPCALL_FINAL: |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 152 | delete selfp; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 153 | return CCN_UPCALL_RESULT_OK; |
| 154 | |
| 155 | case CCN_UPCALL_INTEREST: |
| 156 | break; |
| 157 | |
| 158 | default: |
| 159 | return CCN_UPCALL_RESULT_OK; |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 160 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 161 | |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 162 | string interest; |
Chaoyi Bian | 02dba3c | 2012-03-07 21:45:22 -0800 | [diff] [blame] | 163 | for (int i = 0; i < info->interest_comps->n - 1; i++) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 164 | { |
| 165 | char *comp; |
| 166 | size_t size; |
| 167 | interest += "/"; |
| 168 | ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size); |
| 169 | interest += comp; |
| 170 | } |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 171 | f(interest); |
| 172 | return CCN_UPCALL_RESULT_OK; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static ccn_upcall_res incomingData( |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 176 | ccn_closure *selfp, |
| 177 | ccn_upcall_kind kind, |
| 178 | ccn_upcall_info *info) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 179 | { |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame^] | 180 | function<void (string, string)> f = *(function<void (string, string)> *)selfp->data; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 181 | |
| 182 | switch (kind) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 183 | { |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 184 | case CCN_UPCALL_FINAL: |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 185 | delete selfp; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 186 | return CCN_UPCALL_RESULT_OK; |
| 187 | |
| 188 | case CCN_UPCALL_CONTENT: |
| 189 | break; |
| 190 | |
| 191 | default: |
| 192 | return CCN_UPCALL_RESULT_OK; |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 193 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 194 | |
| 195 | char *pcontent; |
| 196 | size_t len; |
| 197 | ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len); |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame^] | 198 | string name; |
| 199 | for (int i = 0; i < info->content_comps->n - 1; i++) |
| 200 | { |
| 201 | char *comp; |
| 202 | size_t size; |
| 203 | name += "/"; |
| 204 | ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size); |
| 205 | name += comp; |
| 206 | } |
| 207 | f(name, (string)pcontent); |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 208 | return CCN_UPCALL_RESULT_OK; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame^] | 211 | int CcnxWrapper::sendInterest(string strInterest, function<void (string, string)> dataCallback) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 212 | { |
| 213 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 214 | ccn_closure *dataClosure = new ccn_closure; |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame^] | 215 | function<void (string, string)> *f = new function<void (string, string)>(dataCallback); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 216 | |
| 217 | ccn_name_from_uri(pname, strInterest.c_str()); |
Chaoyi Bian | 49d4675 | 2012-03-07 10:35:21 -0800 | [diff] [blame] | 218 | dataClosure->data = f; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 219 | dataClosure->p = &incomingData; |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 220 | recursive_mutex::scoped_lock lock(m_mutex); |
| 221 | ccn_express_interest(m_handle, pname, dataClosure, NULL); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 222 | |
| 223 | ccn_charbuf_destroy(&pname); |
| 224 | } |
| 225 | |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 226 | int CcnxWrapper::setInterestFilter(string prefix, function<void (string)> interestCallback) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 227 | { |
| 228 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 229 | ccn_closure *interestClosure = new ccn_closure; |
Chaoyi Bian | 49d4675 | 2012-03-07 10:35:21 -0800 | [diff] [blame] | 230 | function<void (string)> *f = new function<void (string)>(interestCallback); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 231 | |
| 232 | ccn_name_from_uri(pname, prefix.c_str()); |
Chaoyi Bian | 49d4675 | 2012-03-07 10:35:21 -0800 | [diff] [blame] | 233 | interestClosure->data = f; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 234 | interestClosure->p = &incomingInterest; |
| 235 | ccn_set_interest_filter(m_handle, pname, interestClosure); |
| 236 | |
| 237 | ccn_charbuf_destroy(&pname); |
| 238 | } |
| 239 | |
| 240 | } |