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