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