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