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(); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 48 | ccn_disconnect (m_handle); |
| 49 | ccn_destroy (&m_handle); |
| 50 | ccn_charbuf_destroy (&m_keyLoactor); |
| 51 | ccn_keystore_destroy (&m_keyStore); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 54 | /// @cond include_hidden |
| 55 | |
| 56 | void |
| 57 | CcnxWrapper::createKeyLocator () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 58 | { |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 59 | m_keyLoactor = ccn_charbuf_create(); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 60 | ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG); |
| 61 | ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG); |
| 62 | int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore)); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 63 | if (res >= 0) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 64 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 65 | ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */ |
| 66 | ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */ |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 67 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 70 | const ccn_pkey* |
| 71 | CcnxWrapper::getPrivateKey () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 72 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 73 | return ccn_keystore_private_key (m_keyStore); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 76 | const unsigned char* |
| 77 | CcnxWrapper::getPublicKeyDigest () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 78 | { |
| 79 | return ccn_keystore_public_key_digest(m_keyStore); |
| 80 | } |
| 81 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 82 | ssize_t |
| 83 | CcnxWrapper::getPublicKeyDigestLength () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 84 | { |
| 85 | return ccn_keystore_public_key_digest_length(m_keyStore); |
| 86 | } |
| 87 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 88 | void |
| 89 | CcnxWrapper::initKeyStore () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 90 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 91 | ccn_charbuf *temp = ccn_charbuf_create (); |
| 92 | m_keyStore = ccn_keystore_create (); |
| 93 | ccn_charbuf_putf (temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME")); |
| 94 | ccn_keystore_init (m_keyStore, ccn_charbuf_as_string(temp), (char*)"Th1s1sn0t8g00dp8ssw0rd."); |
| 95 | ccn_charbuf_destroy (&temp); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 98 | void |
| 99 | CcnxWrapper::ccnLoop () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 100 | { |
| 101 | pollfd pfds[1]; |
| 102 | int res = ccn_run(m_handle, 0); |
| 103 | |
| 104 | pfds[0].fd = ccn_get_connection_fd(m_handle); |
| 105 | pfds[0].events = POLLIN; |
| 106 | |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 107 | while (m_running) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 108 | { |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 109 | if (res >= 0) |
| 110 | { |
| 111 | int ret = poll(pfds, 1, 100); |
| 112 | if (ret >= 0) |
| 113 | { |
| 114 | recursive_mutex::scoped_lock lock(m_mutex); |
| 115 | res = ccn_run(m_handle, 0); |
| 116 | } |
| 117 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 118 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 121 | /// @endcond |
| 122 | |
| 123 | int |
| 124 | CcnxWrapper::publishData (const string &name, const string &dataBuffer, int freshness) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 125 | { |
| 126 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 127 | ccn_charbuf *signed_info = ccn_charbuf_create(); |
| 128 | ccn_charbuf *content = ccn_charbuf_create(); |
| 129 | |
| 130 | ccn_name_from_uri(pname, name.c_str()); |
| 131 | ccn_signed_info_create(signed_info, |
| 132 | getPublicKeyDigest(), |
| 133 | getPublicKeyDigestLength(), |
| 134 | NULL, |
| 135 | CCN_CONTENT_DATA, |
| 136 | freshness, |
| 137 | NULL, |
| 138 | m_keyLoactor); |
| 139 | ccn_encode_ContentObject(content, pname, signed_info, |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 140 | dataBuffer.c_str(), dataBuffer.length (), |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 141 | NULL, getPrivateKey()); |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 142 | recursive_mutex::scoped_lock lock(m_mutex); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 143 | ccn_put(m_handle, content->buf, content->length); |
| 144 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 145 | ccn_charbuf_destroy (&pname); |
| 146 | ccn_charbuf_destroy (&signed_info); |
| 147 | ccn_charbuf_destroy (&content); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 151 | static ccn_upcall_res |
| 152 | incomingInterest(ccn_closure *selfp, |
| 153 | ccn_upcall_kind kind, |
| 154 | ccn_upcall_info *info) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 155 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 156 | CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 157 | |
| 158 | switch (kind) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 159 | { |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 160 | case CCN_UPCALL_FINAL: |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 161 | delete f; |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 162 | delete selfp; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 163 | return CCN_UPCALL_RESULT_OK; |
| 164 | |
| 165 | case CCN_UPCALL_INTEREST: |
| 166 | break; |
| 167 | |
| 168 | default: |
| 169 | return CCN_UPCALL_RESULT_OK; |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 170 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 171 | |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 172 | string interest; |
Chaoyi Bian | 02dba3c | 2012-03-07 21:45:22 -0800 | [diff] [blame] | 173 | for (int i = 0; i < info->interest_comps->n - 1; i++) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 174 | { |
| 175 | char *comp; |
| 176 | size_t size; |
| 177 | interest += "/"; |
| 178 | ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size); |
| 179 | interest += comp; |
| 180 | } |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 181 | (*f) (interest); |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 182 | return CCN_UPCALL_RESULT_OK; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 185 | static ccn_upcall_res |
| 186 | incomingData(ccn_closure *selfp, |
| 187 | ccn_upcall_kind kind, |
| 188 | ccn_upcall_info *info) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 189 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 190 | CcnxWrapper::DataCallback *f = static_cast<CcnxWrapper::DataCallback*> (selfp->data); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 191 | |
| 192 | switch (kind) |
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 | case CCN_UPCALL_FINAL: |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 195 | delete f; // this never called in unit tests... |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 196 | delete selfp; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 197 | return CCN_UPCALL_RESULT_OK; |
| 198 | |
| 199 | case CCN_UPCALL_CONTENT: |
| 200 | break; |
| 201 | |
| 202 | default: |
| 203 | return CCN_UPCALL_RESULT_OK; |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 204 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 205 | |
| 206 | char *pcontent; |
| 207 | size_t len; |
| 208 | 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] | 209 | string name; |
| 210 | for (int i = 0; i < info->content_comps->n - 1; i++) |
| 211 | { |
| 212 | char *comp; |
| 213 | size_t size; |
| 214 | name += "/"; |
| 215 | ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size); |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 216 | name += comp; // this will also crash if name doesn't have \0 ending |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 217 | } |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 218 | // this will crash when content doesn't have \0 ending |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 219 | (*f) (name, (string)pcontent); |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 220 | return CCN_UPCALL_RESULT_OK; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 223 | int CcnxWrapper::sendInterest (const string &strInterest, const DataCallback &dataCallback) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 224 | { |
| 225 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 226 | ccn_closure *dataClosure = new ccn_closure; |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 227 | |
| 228 | ccn_name_from_uri (pname, strInterest.c_str()); |
| 229 | dataClosure->data = new DataCallback (dataCallback); // should be removed when closure is removed |
| 230 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 231 | dataClosure->p = &incomingData; |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 232 | recursive_mutex::scoped_lock lock(m_mutex); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 233 | ccn_express_interest (m_handle, pname, dataClosure, NULL); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 234 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 235 | ccn_charbuf_destroy (&pname); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 238 | int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 239 | { |
| 240 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 241 | ccn_closure *interestClosure = new ccn_closure; |
| 242 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 243 | ccn_name_from_uri (pname, prefix.c_str()); |
| 244 | interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 245 | interestClosure->p = &incomingInterest; |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 246 | ccn_set_interest_filter (m_handle, pname, interestClosure); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 247 | |
| 248 | ccn_charbuf_destroy(&pname); |
| 249 | } |
| 250 | |
| 251 | } |