Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 11 | #if __clang__ |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 12 | #pragma clang diagnostic ignored "-Wtautological-compare" |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 13 | #endif |
| 14 | |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 15 | #include "contact-manager.h" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 16 | #include <QStringList> |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 17 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 18 | #ifndef Q_MOC_RUN |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 19 | #include <ndn-cpp-dev/util/crypto.hpp> |
| 20 | #include <ndn-cpp-dev/util/io.hpp> |
| 21 | #include <ndn-cpp-dev/security/sec-rule-relative.hpp> |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 22 | #include <ndn-cpp-dev/security/validator-regex.hpp> |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 23 | #include <cryptopp/base64.h> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 24 | #include <cryptopp/files.h> |
| 25 | #include <cryptopp/sha.h> |
| 26 | #include <boost/asio.hpp> |
| 27 | #include <boost/tokenizer.hpp> |
| 28 | #include <boost/filesystem.hpp> |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 29 | #include "logging.h" |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 30 | #endif |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 31 | |
| 32 | using namespace ndn; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 33 | using namespace std; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 34 | namespace fs = boost::filesystem; |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 35 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 36 | INIT_LOGGER("chronos.ContactManager"); |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 37 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 38 | namespace chronos{ |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 39 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 40 | static const uint8_t DNS_RP_SEPARATOR[2] = {0xF0, 0x2E}; // %F0. |
| 41 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 42 | ContactManager::ContactManager(shared_ptr<Face> face, |
| 43 | QObject* parent) |
| 44 | : QObject(parent) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 45 | , m_face(face) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 46 | , m_dnsListenerId(0) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 47 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 48 | initializeSecurity(); |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | ContactManager::~ContactManager() |
Yingdi Yu | 6eabbd7 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 52 | {} |
Yingdi Yu | 9236c43 | 2013-10-18 11:29:25 -0700 | [diff] [blame] | 53 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 54 | // private methods |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 55 | void |
| 56 | ContactManager::initializeSecurity() |
| 57 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 58 | fs::path anchorPath = fs::path(getenv("HOME")) / ".chronos" / "anchor.cert"; |
| 59 | shared_ptr<IdentityCertificate> anchor = io::load<IdentityCertificate>(anchorPath.c_str()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 60 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 61 | shared_ptr<ValidatorRegex> validator = make_shared<ValidatorRegex>(m_face); |
| 62 | validator->addDataVerificationRule(make_shared<SecRuleRelative>("^([^<DNS>]*)<DNS><ENDORSED>", |
| 63 | "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>$", |
| 64 | "==", "\\1", "\\1\\2", true)); |
| 65 | validator->addDataVerificationRule(make_shared<SecRuleRelative>("^([^<DNS>]*)<DNS><><ENDORSEE>", |
| 66 | "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>$", |
| 67 | "==", "\\1", "\\1\\2", true)); |
| 68 | validator->addDataVerificationRule(make_shared<SecRuleRelative>("^([^<DNS>]*)<DNS><PROFILE>", |
| 69 | "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>$", |
| 70 | "==", "\\1", "\\1\\2", true)); |
| 71 | validator->addDataVerificationRule(make_shared<SecRuleRelative>("^([^<PROFILE-CERT>]*)<PROFILE-CERT>", |
| 72 | "^([^<KEY>]*)<KEY>(<>*<ksk-.*>)<ID-CERT>$", |
| 73 | "==", "\\1", "\\1\\2", true)); |
| 74 | validator->addDataVerificationRule(make_shared<SecRuleRelative>("^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>", |
| 75 | "^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>$", |
| 76 | ">", "\\1\\2", "\\1", true)); |
| 77 | validator->addDataVerificationRule(make_shared<SecRuleRelative>("^([^<KEY>]*)<KEY><dsk-.*><ID-CERT>", |
| 78 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
| 79 | "==", "\\1", "\\1\\2", true)); |
| 80 | validator->addDataVerificationRule(make_shared<SecRuleRelative>("^(<>*)$", |
| 81 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
| 82 | ">", "\\1", "\\1\\2", true)); |
| 83 | validator->addTrustAnchor(anchor); |
| 84 | m_validator = validator; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 88 | ContactManager::fetchCollectEndorse(const Name& identity) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 89 | { |
| 90 | Name interestName = identity; |
| 91 | interestName.append("DNS").append("ENDORSED"); |
| 92 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 93 | Interest interest(interestName); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 94 | interest.setInterestLifetime(1000); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 95 | interest.setMustBeFresh(true); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 96 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 97 | OnDataValidated onValidated = bind(&ContactManager::onDnsCollectEndorseValidated, this, _1, identity); |
| 98 | OnDataValidationFailed onValidationFailed = bind(&ContactManager::onDnsCollectEndorseValidationFailed, this, _1, identity); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 99 | TimeoutNotify timeoutNotify = bind(&ContactManager::onDnsCollectEndorseTimeoutNotify, this, _1, identity); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 100 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 101 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 105 | ContactManager::fetchEndorseCertificateInternal(const Name& identity, int certIndex) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 106 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 107 | shared_ptr<EndorseCollection> endorseCollection = m_bufferedContacts[identity].m_endorseCollection; |
| 108 | |
| 109 | if(certIndex >= endorseCollection->endorsement_size()) |
| 110 | prepareEndorseInfo(identity); |
| 111 | |
| 112 | Name interestName(endorseCollection->endorsement(certIndex).certname()); |
| 113 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 114 | Interest interest(interestName); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 115 | interest.setInterestLifetime(1000); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 116 | interest.setMustBeFresh(true); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 117 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 118 | m_face->expressInterest(interest, |
| 119 | bind(&ContactManager::onEndorseCertificateInternal, |
| 120 | this, _1, _2, identity, certIndex, |
| 121 | endorseCollection->endorsement(certIndex).hash()), |
| 122 | bind(&ContactManager::onEndorseCertificateInternalTimeout, |
| 123 | this, _1, identity, certIndex)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 124 | } |
| 125 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 126 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 127 | ContactManager::prepareEndorseInfo(const Name& identity) |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 128 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 129 | // _LOG_DEBUG("prepareEndorseInfo"); |
| 130 | const Profile& profile = m_bufferedContacts[identity].m_selfEndorseCert->getProfile(); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 131 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 132 | shared_ptr<EndorseInfo> endorseInfo = make_shared<EndorseInfo>(); |
| 133 | m_bufferedContacts[identity].m_endorseInfo = endorseInfo; |
| 134 | |
| 135 | Profile::const_iterator pIt = profile.begin(); |
| 136 | Profile::const_iterator pEnd = profile.end(); |
| 137 | |
| 138 | map<string, int> endorseCount; |
| 139 | for(; pIt != pEnd; pIt++) |
| 140 | { |
| 141 | // _LOG_DEBUG("prepareEndorseInfo: profile[" << pIt->first << "]: " << pIt->second); |
| 142 | endorseCount[pIt->first] = 0; |
| 143 | } |
| 144 | |
| 145 | int endorseCertCount = 0; |
| 146 | |
| 147 | vector<shared_ptr<EndorseCertificate> >::const_iterator cIt = m_bufferedContacts[identity].m_endorseCertList.begin(); |
| 148 | vector<shared_ptr<EndorseCertificate> >::const_iterator cEnd = m_bufferedContacts[identity].m_endorseCertList.end(); |
| 149 | |
| 150 | for(; cIt != cEnd; cIt++, endorseCertCount++) |
| 151 | { |
| 152 | shared_ptr<Contact> contact = getContact((*cIt)->getSigner().getPrefix(-1)); |
| 153 | if(!static_cast<bool>(contact)) |
| 154 | continue; |
| 155 | |
| 156 | if(!contact->isIntroducer() |
| 157 | || !contact->canBeTrustedFor(profile.getIdentityName())) |
| 158 | continue; |
| 159 | |
| 160 | if(!Validator::verifySignature(**cIt, contact->getPublicKey())) |
| 161 | continue; |
| 162 | |
| 163 | const Profile& tmpProfile = (*cIt)->getProfile(); |
| 164 | if(tmpProfile != profile) |
| 165 | continue; |
| 166 | |
| 167 | const vector<string>& endorseList = (*cIt)->getEndorseList(); |
| 168 | vector<string>::const_iterator eIt = endorseList.begin(); |
| 169 | for(; eIt != endorseList.end(); eIt++) |
| 170 | endorseCount[*eIt] += 1; |
| 171 | } |
| 172 | |
| 173 | pIt = profile.begin(); |
| 174 | pEnd = profile.end(); |
| 175 | for(; pIt != pEnd; pIt++) |
| 176 | { |
| 177 | EndorseInfo::Endorsement* endorsement = endorseInfo->add_endorsement(); |
| 178 | endorsement->set_type(pIt->first); |
| 179 | endorsement->set_value(pIt->second); |
| 180 | stringstream ss; |
| 181 | ss << endorseCount[pIt->first] << "/" << endorseCertCount; |
| 182 | endorsement->set_endorse(ss.str()); |
| 183 | } |
| 184 | |
| 185 | emit contactEndorseInfoReady (*endorseInfo); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | ContactManager::onDnsSelfEndorseCertValidated(const shared_ptr<const Data>& data, |
| 190 | const Name& identity) |
| 191 | { |
| 192 | try |
| 193 | { |
| 194 | Data plainData; |
| 195 | plainData.wireDecode(data->getContent().blockFromValue()); |
| 196 | shared_ptr<EndorseCertificate> selfEndorseCertificate = make_shared<EndorseCertificate>(boost::cref(plainData)); |
| 197 | if(Validator::verifySignature(plainData, selfEndorseCertificate->getPublicKeyInfo())) |
| 198 | { |
| 199 | m_bufferedContacts[identity].m_selfEndorseCert = selfEndorseCertificate; |
| 200 | fetchCollectEndorse(identity); |
| 201 | } |
| 202 | else |
| 203 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri())); |
| 204 | } |
| 205 | catch(Block::Error& e) |
| 206 | { |
| 207 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri())); |
| 208 | } |
| 209 | catch(Data::Error& e) |
| 210 | { |
| 211 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri())); |
| 212 | } |
| 213 | catch(EndorseCertificate::Error& e) |
| 214 | { |
| 215 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri())); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | ContactManager::onDnsSelfEndorseCertValidationFailed(const shared_ptr<const Data>& data, |
| 221 | const Name& identity) |
| 222 | { |
| 223 | // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert, |
| 224 | // but let's stay with failure for now. |
| 225 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri())); |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | ContactManager::onDnsSelfEndorseCertTimeoutNotify(const Interest& interest, |
| 230 | const Name& identity) |
| 231 | { |
| 232 | // If we cannot validate the Self-Endorse-Certificate, we may retry or fetch id-cert, |
| 233 | // but let's stay with failure for now. |
| 234 | emit contactInfoFetchFailed(QString::fromStdString(identity.toUri())); |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | ContactManager::onDnsCollectEndorseValidated(const shared_ptr<const Data>& data, |
| 239 | const Name& identity) |
| 240 | { |
| 241 | shared_ptr<EndorseCollection> endorseCollection = make_shared<EndorseCollection>(); |
| 242 | if(!endorseCollection->ParseFromArray(data->getContent().value(), data->getContent().value_size())) |
| 243 | { |
| 244 | m_bufferedContacts[identity].m_endorseCollection = endorseCollection; |
| 245 | fetchEndorseCertificateInternal(identity, 0); |
| 246 | } |
| 247 | else |
| 248 | prepareEndorseInfo(identity); |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | ContactManager::onDnsCollectEndorseValidationFailed(const shared_ptr<const Data>& data, |
| 253 | const Name& identity) |
| 254 | { |
| 255 | prepareEndorseInfo(identity); |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | ContactManager::onDnsCollectEndorseTimeoutNotify(const Interest& interest, |
| 260 | const Name& identity) |
| 261 | { |
| 262 | // _LOG_DEBUG("onDnsCollectEndorseTimeoutNotify: " << interest.getName()); |
| 263 | prepareEndorseInfo(identity); |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | ContactManager::onEndorseCertificateInternal(const Interest& interest, |
| 268 | Data& data, |
| 269 | const Name& identity, |
| 270 | int certIndex, |
| 271 | string hash) |
| 272 | { |
| 273 | stringstream ss; |
| 274 | { |
| 275 | using namespace CryptoPP; |
| 276 | |
| 277 | SHA256 hash; |
| 278 | StringSource(data.wireEncode().wire(), data.wireEncode().size(), true, |
| 279 | new HashFilter(hash, new FileSink(ss))); |
| 280 | } |
| 281 | |
| 282 | if(ss.str() == hash) |
| 283 | { |
| 284 | shared_ptr<EndorseCertificate> endorseCertificate = make_shared<EndorseCertificate>(boost::cref(data)); |
| 285 | m_bufferedContacts[identity].m_endorseCertList.push_back(endorseCertificate); |
| 286 | } |
| 287 | |
| 288 | fetchEndorseCertificateInternal(identity, certIndex+1); |
| 289 | } |
| 290 | |
| 291 | void |
| 292 | ContactManager::onEndorseCertificateInternalTimeout(const Interest& interest, |
| 293 | const Name& identity, |
| 294 | int certIndex) |
| 295 | { |
| 296 | fetchEndorseCertificateInternal(identity, certIndex+1); |
| 297 | } |
| 298 | |
| 299 | void |
| 300 | ContactManager::collectEndorsement() |
| 301 | { |
| 302 | { |
| 303 | boost::recursive_mutex::scoped_lock lock(m_collectCountMutex); |
| 304 | m_collectCount = m_contactList.size(); |
| 305 | |
| 306 | ContactList::iterator it = m_contactList.begin(); |
| 307 | ContactList::iterator end = m_contactList.end(); |
| 308 | |
| 309 | for(; it != end ; it++) |
| 310 | { |
| 311 | Name interestName = (*it)->getNameSpace(); |
| 312 | interestName.append("DNS").append(m_identity.wireEncode()).append("ENDORSEE"); |
| 313 | |
| 314 | Interest interest(interestName); |
| 315 | interest.setInterestLifetime(1000); |
| 316 | |
| 317 | OnDataValidated onValidated = bind(&ContactManager::onDnsEndorseeValidated, this, _1); |
| 318 | OnDataValidationFailed onValidationFailed = bind(&ContactManager::onDnsEndorseeValidationFailed, this, _1); |
| 319 | TimeoutNotify timeoutNotify = bind(&ContactManager::onDnsEndorseeTimeoutNotify, this, _1); |
| 320 | |
| 321 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0); |
| 322 | } |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 327 | ContactManager::onDnsEndorseeValidated(const shared_ptr<const Data>& data) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 328 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 329 | Data endorseData; |
| 330 | endorseData.wireDecode(data->getContent().blockFromValue()); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 331 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 332 | EndorseCertificate endorseCertificate(endorseData); |
| 333 | m_contactStorage->updateCollectEndorse(endorseCertificate); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 334 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 335 | decreaseCollectStatus(); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 336 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 337 | |
| 338 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 339 | ContactManager::onDnsEndorseeValidationFailed(const shared_ptr<const Data>& data) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 340 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 341 | decreaseCollectStatus(); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 344 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 345 | ContactManager::onDnsEndorseeTimeoutNotify(const Interest& interest) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 346 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 347 | decreaseCollectStatus(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 350 | void |
| 351 | ContactManager::decreaseCollectStatus() |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 352 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 353 | int count; |
| 354 | { |
| 355 | boost::recursive_mutex::scoped_lock lock(m_collectCountMutex); |
| 356 | m_collectCount--; |
| 357 | count = m_collectCount; |
| 358 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 359 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 360 | if(count == 0) |
| 361 | publishCollectEndorsedDataInDNS(); |
| 362 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 363 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 364 | void |
| 365 | ContactManager::publishCollectEndorsedDataInDNS() |
| 366 | { |
| 367 | Name dnsName = m_identity; |
| 368 | dnsName.append("DNS").append("ENDORSED").appendVersion(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 369 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 370 | Data data; |
| 371 | data.setName(dnsName); |
| 372 | |
| 373 | EndorseCollection endorseCollection; |
| 374 | m_contactStorage->getCollectEndorse(endorseCollection); |
| 375 | |
| 376 | OBufferStream os; |
| 377 | endorseCollection.SerializeToOstream(&os); |
| 378 | |
| 379 | data.setContent(os.buf()); |
| 380 | m_keyChain.signByIdentity(data, m_identity); |
| 381 | |
| 382 | m_contactStorage->updateDnsOthersEndorse(data); |
| 383 | m_face->put(data); |
| 384 | } |
| 385 | |
| 386 | void |
| 387 | ContactManager::onIdentityCertValidated(const shared_ptr<const Data>& data) |
| 388 | { |
| 389 | shared_ptr<IdentityCertificate> cert = make_shared<IdentityCertificate>(boost::cref(*data)); |
| 390 | m_bufferedIdCerts[cert->getName()] = cert; |
| 391 | decreaseIdCertCount(); |
| 392 | } |
| 393 | |
| 394 | void |
| 395 | ContactManager::onIdentityCertValidationFailed(const shared_ptr<const Data>& data) |
| 396 | { |
| 397 | _LOG_DEBUG("ContactManager::onIdentityCertValidationFailed " << data->getName()); |
| 398 | decreaseIdCertCount(); |
| 399 | } |
| 400 | |
| 401 | void |
| 402 | ContactManager::onIdentityCertTimeoutNotify(const Interest& interest) |
| 403 | { |
| 404 | _LOG_DEBUG("ContactManager::onIdentityCertTimeoutNotify: " << interest.getName()); |
| 405 | decreaseIdCertCount(); |
| 406 | } |
| 407 | |
| 408 | void |
| 409 | ContactManager::decreaseIdCertCount() |
| 410 | { |
| 411 | int count; |
| 412 | { |
| 413 | boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex); |
| 414 | m_idCertCount--; |
| 415 | count = m_idCertCount; |
| 416 | } |
| 417 | |
| 418 | if(count == 0) |
| 419 | { |
| 420 | QStringList certNameList; |
| 421 | QStringList nameList; |
| 422 | |
| 423 | BufferedIdCerts::const_iterator it = m_bufferedIdCerts.begin(); |
| 424 | BufferedIdCerts::const_iterator end = m_bufferedIdCerts.end(); |
| 425 | for(; it != end; it++) |
| 426 | { |
| 427 | certNameList << QString::fromStdString(it->second->getName().toUri()); |
| 428 | Profile profile(*(it->second)); |
| 429 | nameList << QString::fromStdString(profile.get("name")); |
| 430 | } |
| 431 | |
| 432 | emit idCertNameListReady(certNameList); |
| 433 | emit nameListReady(nameList); |
| 434 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 437 | shared_ptr<EndorseCertificate> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 438 | ContactManager::getSignedSelfEndorseCertificate(const Profile& profile) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 439 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 440 | Name certificateName = m_keyChain.getDefaultCertificateNameForIdentity(m_identity); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 441 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 442 | shared_ptr<IdentityCertificate> signingCert = m_keyChain.getCertificate(certificateName); |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 443 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 444 | vector<string> endorseList; |
| 445 | Profile::const_iterator it = profile.begin(); |
| 446 | for(; it != profile.end(); it++) |
| 447 | endorseList.push_back(it->first); |
| 448 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 449 | shared_ptr<EndorseCertificate> selfEndorseCertificate = |
| 450 | shared_ptr<EndorseCertificate>(new EndorseCertificate(*signingCert, profile, endorseList)); |
| 451 | |
| 452 | m_keyChain.sign(*selfEndorseCertificate, certificateName); |
| 453 | |
| 454 | return selfEndorseCertificate; |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 457 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 458 | ContactManager::publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 459 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 460 | Name dnsName = m_identity; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 461 | dnsName.append("DNS").append("PROFILE").appendVersion(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 462 | |
| 463 | Data data; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 464 | data.setName(dnsName); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 465 | data.setContent(selfEndorseCertificate.wireEncode()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 466 | data.setFreshnessPeriod(1000); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 467 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 468 | m_keyChain.signByIdentity(data, m_identity); |
| 469 | |
| 470 | m_contactStorage->updateDnsSelfProfileData(data); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 471 | m_face->put(data); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 474 | shared_ptr<EndorseCertificate> |
| 475 | ContactManager::generateEndorseCertificate(const Name& identity) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 476 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 477 | shared_ptr<Contact> contact = getContact(identity); |
| 478 | if(!static_cast<bool>(contact)) |
| 479 | return shared_ptr<EndorseCertificate>(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 480 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 481 | Name signerKeyName = m_keyChain.getDefaultKeyNameForIdentity(m_identity); |
| 482 | |
| 483 | vector<string> endorseList; |
| 484 | m_contactStorage->getEndorseList(identity, endorseList); |
| 485 | |
| 486 | shared_ptr<EndorseCertificate> cert = |
| 487 | shared_ptr<EndorseCertificate>(new EndorseCertificate(contact->getPublicKeyName(), |
| 488 | contact->getPublicKey(), |
| 489 | contact->getNotBefore(), |
| 490 | contact->getNotAfter(), |
| 491 | signerKeyName, |
| 492 | contact->getProfile(), |
| 493 | endorseList)); |
| 494 | m_keyChain.signByIdentity(*cert, m_identity); |
| 495 | return cert; |
| 496 | |
| 497 | } |
| 498 | |
| 499 | void |
| 500 | ContactManager::publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate) |
| 501 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 502 | Name endorsee = endorseCertificate.getPublicKeyName().getPrefix(-1); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 503 | Name dnsName = m_identity; |
| 504 | dnsName.append("DNS") |
| 505 | .append(endorsee.wireEncode()) |
| 506 | .append("ENDORSEE") |
| 507 | .appendVersion(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 508 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 509 | Data data; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 510 | data.setName(dnsName); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 511 | data.setContent(endorseCertificate.wireEncode()); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 512 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 513 | m_keyChain.signByIdentity(data, m_identity); |
| 514 | |
| 515 | m_contactStorage->updateDnsEndorseOthers(data, dnsName.get(-3).toEscapedString()); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 516 | m_face->put(data); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 520 | ContactManager::sendInterest(const Interest& interest, |
| 521 | const OnDataValidated& onValidated, |
| 522 | const OnDataValidationFailed& onValidationFailed, |
| 523 | const TimeoutNotify& timeoutNotify, |
| 524 | int retry /* = 1 */) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 525 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 526 | m_face->expressInterest(interest, |
| 527 | bind(&ContactManager::onTargetData, |
| 528 | this, _1, _2, onValidated, onValidationFailed), |
| 529 | bind(&ContactManager::onTargetTimeout, |
| 530 | this, _1, retry, onValidated, onValidationFailed, timeoutNotify)); |
| 531 | } |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 532 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 533 | void |
| 534 | ContactManager::onTargetData(const Interest& interest, |
| 535 | const Data& data, |
| 536 | const OnDataValidated& onValidated, |
| 537 | const OnDataValidationFailed& onValidationFailed) |
| 538 | { |
| 539 | // _LOG_DEBUG("On receiving data: " << data.getName()); |
| 540 | m_validator->validate(data, onValidated, onValidationFailed); |
| 541 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 542 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 543 | void |
| 544 | ContactManager::onTargetTimeout(const Interest& interest, |
| 545 | int retry, |
| 546 | const OnDataValidated& onValidated, |
| 547 | const OnDataValidationFailed& onValidationFailed, |
| 548 | const TimeoutNotify& timeoutNotify) |
| 549 | { |
| 550 | // _LOG_DEBUG("On interest timeout: " << interest.getName()); |
| 551 | if(retry > 0) |
| 552 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1); |
| 553 | else |
| 554 | timeoutNotify(interest); |
| 555 | } |
| 556 | |
| 557 | void |
| 558 | ContactManager::onDnsInterest(const Name& prefix, const Interest& interest) |
| 559 | { |
| 560 | const Name& interestName = interest.getName(); |
| 561 | shared_ptr<Data> data; |
| 562 | |
| 563 | if(interestName.size() <= prefix.size()) |
| 564 | return; |
| 565 | |
| 566 | if(interestName.size() == (prefix.size()+1)) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 567 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 568 | data = m_contactStorage->getDnsData("N/A", interestName.get(prefix.size()).toEscapedString()); |
| 569 | if(static_cast<bool>(data)) |
| 570 | m_face->put(*data); |
| 571 | return; |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 572 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 573 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 574 | if(interestName.size() == (prefix.size()+2)) |
| 575 | { |
| 576 | data = m_contactStorage->getDnsData(interestName.get(prefix.size()).toEscapedString(), |
| 577 | interestName.get(prefix.size()+1).toEscapedString()); |
| 578 | if(static_cast<bool>(data)) |
| 579 | m_face->put(*data); |
| 580 | return; |
| 581 | } |
| 582 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 583 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 584 | void |
| 585 | ContactManager::onDnsRegisterFailed(const Name& prefix, const string& failInfo) |
| 586 | { |
| 587 | emit warning(QString(failInfo.c_str())); |
| 588 | } |
| 589 | |
| 590 | |
| 591 | // public slots |
| 592 | void |
| 593 | ContactManager::onIdentityUpdated(const QString& identity) |
| 594 | { |
| 595 | m_identity = Name(identity.toStdString()); |
| 596 | |
| 597 | m_contactStorage = make_shared<ContactStorage>(m_identity); |
| 598 | |
| 599 | if(m_dnsListenerId) |
| 600 | m_face->unsetInterestFilter(m_dnsListenerId); |
| 601 | |
| 602 | Name dnsPrefix; |
| 603 | dnsPrefix.append(m_identity).append("DNS"); |
| 604 | m_dnsListenerId = m_face->setInterestFilter(dnsPrefix, |
| 605 | bind(&ContactManager::onDnsInterest, this, _1, _2), |
| 606 | bind(&ContactManager::onDnsRegisterFailed, this, _1, _2)); |
| 607 | |
| 608 | m_contactList.clear(); |
| 609 | m_contactStorage->getAllContacts(m_contactList); |
| 610 | |
| 611 | m_bufferedContacts.clear(); |
| 612 | |
| 613 | collectEndorsement(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 616 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 617 | ContactManager::onFetchContactInfo(const QString& identity) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 618 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 619 | // try to fetch self-endorse-certificate via DNS PROFILE first. |
| 620 | Name identityName(identity.toStdString()); |
| 621 | Name interestName; |
| 622 | interestName.append(identityName).append("DNS").append("PROFILE"); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 623 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 624 | // _LOG_DEBUG("onFetchContactInfo " << identity.toStdString() << " profile: " << interestName); |
| 625 | |
| 626 | Interest interest(interestName); |
| 627 | interest.setInterestLifetime(1000); |
| 628 | interest.setMustBeFresh(true); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 629 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 630 | OnDataValidated onValidated = bind(&ContactManager::onDnsSelfEndorseCertValidated, this, _1, identityName); |
| 631 | OnDataValidationFailed onValidationFailed = bind(&ContactManager::onDnsSelfEndorseCertValidationFailed, this, _1, identityName); |
| 632 | TimeoutNotify timeoutNotify = bind(&ContactManager::onDnsSelfEndorseCertTimeoutNotify, this, _1, identityName); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 633 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 634 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0); |
| 635 | } |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 636 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 637 | void |
| 638 | ContactManager::onAddFetchedContact(const QString& identity) |
| 639 | { |
| 640 | // _LOG_DEBUG("onAddFetchedContact"); |
| 641 | |
| 642 | Name identityName(identity.toStdString()); |
| 643 | |
| 644 | BufferedContacts::const_iterator it = m_bufferedContacts.find(identityName); |
| 645 | if(it != m_bufferedContacts.end()) |
| 646 | { |
| 647 | Contact contact(*(it->second.m_selfEndorseCert)); |
| 648 | // _LOG_DEBUG("onAddFetchedContact: contact ready"); |
| 649 | try |
| 650 | { |
| 651 | m_contactStorage->addContact(contact); |
| 652 | m_bufferedContacts.erase(identityName); |
| 653 | |
| 654 | m_contactList.clear(); |
| 655 | m_contactStorage->getAllContacts(m_contactList); |
| 656 | |
| 657 | onWaitForContactList(); |
| 658 | } |
| 659 | catch(ContactStorage::Error& e) |
| 660 | { |
| 661 | emit warning(QString::fromStdString(e.what())); |
| 662 | } |
| 663 | } |
| 664 | else |
| 665 | { |
| 666 | emit warning(QString("Failure: no information of %1").arg(identity)); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | void |
| 671 | ContactManager::onUpdateProfile() |
| 672 | { |
| 673 | // Get current profile; |
| 674 | shared_ptr<Profile> newProfile = m_contactStorage->getSelfProfile(); |
| 675 | if(!static_cast<bool>(newProfile)) |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 676 | return; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 677 | |
| 678 | _LOG_DEBUG("ContactManager::onUpdateProfile: getProfile"); |
| 679 | |
| 680 | shared_ptr<EndorseCertificate> newEndorseCertificate = getSignedSelfEndorseCertificate(*newProfile); |
| 681 | |
| 682 | m_contactStorage->addSelfEndorseCertificate(*newEndorseCertificate); |
| 683 | |
| 684 | publishSelfEndorseCertificateInDNS(*newEndorseCertificate); |
| 685 | } |
| 686 | |
| 687 | void |
| 688 | ContactManager::onRefreshBrowseContact() |
| 689 | { |
| 690 | return; // the website info is not available. |
| 691 | |
| 692 | std::vector<std::string> bufferedIdCertNames; |
| 693 | try |
| 694 | { |
| 695 | using namespace boost::asio::ip; |
| 696 | tcp::iostream request_stream; |
| 697 | request_stream.expires_from_now(boost::posix_time::milliseconds(5000)); |
| 698 | request_stream.connect("ndncert.named-data.net","80"); |
| 699 | if(!request_stream) |
| 700 | { |
| 701 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #1")); |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | request_stream << "GET /cert/list/ HTTP/1.0\r\n"; |
| 706 | request_stream << "Host: ndncert.named-data.net\r\n\r\n"; |
| 707 | request_stream.flush(); |
| 708 | |
| 709 | string line1; |
| 710 | std::getline(request_stream,line1); |
| 711 | if (!request_stream) |
| 712 | { |
| 713 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #2")); |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | std::stringstream response_stream(line1); |
| 718 | std::string http_version; |
| 719 | response_stream >> http_version; |
| 720 | unsigned int status_code; |
| 721 | response_stream >> status_code; |
| 722 | std::string status_message; |
| 723 | std::getline(response_stream,status_message); |
| 724 | |
| 725 | if (!response_stream||http_version.substr(0,5)!="HTTP/") |
| 726 | { |
| 727 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #3")); |
| 728 | return; |
| 729 | } |
| 730 | if (status_code!=200) |
| 731 | { |
| 732 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #4")); |
| 733 | return; |
| 734 | } |
| 735 | vector<string> headers; |
| 736 | std::string header; |
| 737 | while (std::getline(request_stream, header) && header != "\r") |
| 738 | headers.push_back(header); |
| 739 | |
| 740 | std::istreambuf_iterator<char> stream_iter (request_stream); |
| 741 | std::istreambuf_iterator<char> end_of_stream; |
| 742 | |
| 743 | typedef boost::tokenizer< boost::escaped_list_separator<char>, std::istreambuf_iterator<char> > tokenizer_t; |
| 744 | tokenizer_t certItems (stream_iter, end_of_stream,boost::escaped_list_separator<char>('\\', '\n', '"')); |
| 745 | |
| 746 | for (tokenizer_t::iterator it = certItems.begin(); it != certItems.end (); it++) |
| 747 | if (!it->empty()) |
| 748 | bufferedIdCertNames.push_back(*it); |
| 749 | } |
| 750 | catch(std::exception &e) |
| 751 | { |
| 752 | emit warning(QString::fromStdString("Fail to fetch certificate directory! #N")); |
| 753 | } |
| 754 | |
| 755 | { |
| 756 | boost::recursive_mutex::scoped_lock lock(m_idCertCountMutex); |
| 757 | m_idCertCount = bufferedIdCertNames.size(); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 758 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 759 | m_bufferedIdCerts.clear(); |
| 760 | |
| 761 | std::vector<std::string>::const_iterator it = bufferedIdCertNames.begin(); |
| 762 | std::vector<std::string>::const_iterator end = bufferedIdCertNames.end(); |
| 763 | for(; it != end; it++) |
| 764 | { |
| 765 | Name certName(*it); |
| 766 | |
| 767 | Interest interest(certName); |
| 768 | interest.setInterestLifetime(1000); |
| 769 | interest.setMustBeFresh(true); |
| 770 | |
| 771 | OnDataValidated onValidated = bind(&ContactManager::onIdentityCertValidated, this, _1); |
| 772 | OnDataValidationFailed onValidationFailed = bind(&ContactManager::onIdentityCertValidationFailed, this, _1); |
| 773 | TimeoutNotify timeoutNotify = bind(&ContactManager::onIdentityCertTimeoutNotify, this, _1); |
| 774 | |
| 775 | sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0); |
| 776 | } |
| 777 | |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 780 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 781 | ContactManager::onFetchIdCert(const QString& qCertName) |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 782 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 783 | Name certName(qCertName.toStdString()); |
| 784 | if(m_bufferedIdCerts.find(certName) != m_bufferedIdCerts.end()) |
| 785 | { |
| 786 | emit idCertReady(*m_bufferedIdCerts[certName]); |
| 787 | } |
Yingdi Yu | 7223269 | 2013-11-12 17:50:21 -0800 | [diff] [blame] | 788 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 789 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 790 | void |
| 791 | ContactManager::onAddFetchedContactIdCert(const QString& qCertName) |
| 792 | { |
| 793 | Name certName(qCertName.toStdString()); |
| 794 | Name identity = IdentityCertificate::certificateNameToPublicKeyName(certName).getPrefix(-1); |
| 795 | |
| 796 | BufferedIdCerts::const_iterator it = m_bufferedIdCerts.find(certName); |
| 797 | if(it != m_bufferedIdCerts.end()) |
| 798 | { |
| 799 | Contact contact(*it->second); |
| 800 | try |
| 801 | { |
| 802 | m_contactStorage->addContact(contact); |
| 803 | m_bufferedIdCerts.erase(certName); |
| 804 | |
| 805 | m_contactList.clear(); |
| 806 | m_contactStorage->getAllContacts(m_contactList); |
| 807 | |
| 808 | onWaitForContactList(); |
| 809 | } |
| 810 | catch(ContactStorage::Error& e) |
| 811 | { |
| 812 | emit warning(QString::fromStdString(e.what())); |
| 813 | } |
| 814 | } |
| 815 | else |
| 816 | emit warning(QString("Failure: no information of %1").arg(QString::fromStdString(identity.toUri()))); |
| 817 | } |
| 818 | |
| 819 | void |
| 820 | ContactManager::onWaitForContactList() |
| 821 | { |
| 822 | ContactList::const_iterator it = m_contactList.begin(); |
| 823 | ContactList::const_iterator end = m_contactList.end(); |
| 824 | |
| 825 | QStringList aliasList; |
| 826 | QStringList idList; |
| 827 | for(; it != end; it++) |
| 828 | { |
| 829 | aliasList << QString((*it)->getAlias().c_str()); |
| 830 | idList << QString((*it)->getNameSpace().toUri().c_str()); |
| 831 | } |
| 832 | |
| 833 | emit contactAliasListReady(aliasList); |
| 834 | emit contactIdListReady(idList); |
| 835 | } |
| 836 | |
| 837 | void |
| 838 | ContactManager::onWaitForContactInfo(const QString& identity) |
| 839 | { |
| 840 | ContactList::const_iterator it = m_contactList.begin(); |
| 841 | ContactList::const_iterator end = m_contactList.end(); |
| 842 | |
| 843 | for(; it != end; it++) |
| 844 | if((*it)->getNameSpace().toUri() == identity.toStdString()) |
| 845 | emit contactInfoReady(QString((*it)->getNameSpace().toUri().c_str()), |
| 846 | QString((*it)->getName().c_str()), |
| 847 | QString((*it)->getInstitution().c_str()), |
| 848 | (*it)->isIntroducer()); |
| 849 | } |
| 850 | |
| 851 | void |
| 852 | ContactManager::onRemoveContact(const QString& identity) |
| 853 | { |
| 854 | m_contactStorage->removeContact(Name(identity.toStdString())); |
| 855 | m_contactList.clear(); |
| 856 | m_contactStorage->getAllContacts(m_contactList); |
| 857 | |
| 858 | onWaitForContactList(); |
| 859 | } |
| 860 | |
| 861 | void |
| 862 | ContactManager::onUpdateAlias(const QString& identity, const QString& alias) |
| 863 | { |
| 864 | m_contactStorage->updateAlias(Name(identity.toStdString()), alias.toStdString()); |
| 865 | m_contactList.clear(); |
| 866 | m_contactStorage->getAllContacts(m_contactList); |
| 867 | |
| 868 | onWaitForContactList(); |
| 869 | } |
| 870 | |
| 871 | void |
| 872 | ContactManager::onUpdateIsIntroducer(const QString& identity, bool isIntroducer) |
| 873 | { |
| 874 | m_contactStorage->updateIsIntroducer(Name(identity.toStdString()), isIntroducer); |
| 875 | } |
| 876 | |
| 877 | void |
| 878 | ContactManager::onUpdateEndorseCertificate(const QString& identity) |
| 879 | { |
| 880 | Name identityName(identity.toStdString()); |
| 881 | shared_ptr<EndorseCertificate> newEndorseCertificate = generateEndorseCertificate(identityName); |
| 882 | |
| 883 | if(!static_cast<bool>(newEndorseCertificate)) |
| 884 | return; |
| 885 | |
| 886 | m_contactStorage->addEndorseCertificate(*newEndorseCertificate, identityName); |
| 887 | |
| 888 | publishEndorseCertificateInDNS(*newEndorseCertificate); |
| 889 | } |
| 890 | |
| 891 | } // namespace chronos |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 892 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 893 | |
| 894 | #if WAF |
| 895 | #include "contact-manager.moc" |
| 896 | #include "contact-manager.cpp.moc" |
| 897 | #endif |