Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 3 | * Copyright (c) 2020, Regents of the University of California |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 4 | * Yingdi Yu |
| 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 | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 12 | #ifndef CHRONOCHAT_CONTACT_MANAGER_HPP |
| 13 | #define CHRONOCHAT_CONTACT_MANAGER_HPP |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 14 | |
| 15 | #include <QObject> |
| 16 | |
| 17 | #ifndef Q_MOC_RUN |
| 18 | #include "common.hpp" |
| 19 | #include "contact-storage.hpp" |
| 20 | #include "endorse-certificate.hpp" |
| 21 | #include "profile.hpp" |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 22 | #include "endorse-info.hpp" |
| 23 | #include "endorse-collection.hpp" |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 24 | #include <ndn-cxx/security/key-chain.hpp> |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 25 | #include <ndn-cxx/security/validator-config.hpp> |
| 26 | #include <ndn-cxx/face.hpp> |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 27 | #include <boost/thread/locks.hpp> |
| 28 | #include <boost/thread/recursive_mutex.hpp> |
| 29 | #endif |
| 30 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 31 | namespace chronochat { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 32 | |
| 33 | typedef function<void(const Interest&)> TimeoutNotify; |
| 34 | typedef std::vector<shared_ptr<Contact> > ContactList; |
| 35 | |
| 36 | class ContactManager : public QObject |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | |
| 40 | public: |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 41 | ContactManager(ndn::Face& m_face, QObject* parent = 0); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 42 | |
| 43 | ~ContactManager(); |
| 44 | |
| 45 | shared_ptr<Contact> |
| 46 | getContact(const Name& identity) |
| 47 | { |
| 48 | return m_contactStorage->getContact(identity); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | getContactList(ContactList& contactList) |
| 53 | { |
| 54 | contactList.clear(); |
| 55 | contactList.insert(contactList.end(), m_contactList.begin(), m_contactList.end()); |
| 56 | } |
| 57 | private: |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 58 | void |
| 59 | initializeSecurity(); |
| 60 | |
| 61 | void |
| 62 | fetchCollectEndorse(const Name& identity); |
| 63 | |
| 64 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 65 | fetchEndorseCertificateInternal(const Name& identity, size_t certIndex); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 66 | |
| 67 | void |
| 68 | prepareEndorseInfo(const Name& identity); |
| 69 | |
| 70 | // PROFILE: self-endorse-certificate |
| 71 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 72 | onDnsSelfEndorseCertValidated(const Data& selfEndorseCertificate, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 73 | const Name& identity); |
| 74 | |
| 75 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 76 | onDnsSelfEndorseCertValidationFailed(const Data& selfEndorseCertificate, |
| 77 | const ndn::security::ValidationError& error, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 78 | const Name& identity); |
| 79 | |
| 80 | void |
| 81 | onDnsSelfEndorseCertTimeoutNotify(const Interest& interest, const Name& identity); |
| 82 | |
| 83 | // ENDORSED: endorse-collection |
| 84 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 85 | onDnsCollectEndorseValidated(const Data& data, const Name& identity); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 86 | |
| 87 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 88 | onDnsCollectEndorseValidationFailed(const Data& data, |
| 89 | const ndn::security::ValidationError& error, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 90 | const Name& identity); |
| 91 | |
| 92 | void |
| 93 | onDnsCollectEndorseTimeoutNotify(const Interest& interest, |
| 94 | const Name& identity); |
| 95 | |
| 96 | // PROFILE-CERT: endorse-certificate |
| 97 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 98 | onEndorseCertificateInternal(const Interest& interest, const Data& data, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 99 | const Name& identity, size_t certIndex, |
| 100 | std::string hash); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 101 | |
| 102 | void |
| 103 | onEndorseCertificateInternalTimeout(const Interest& interest, |
| 104 | const Name& identity, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 105 | size_t certIndex); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 106 | |
| 107 | // Collect endorsement |
| 108 | void |
| 109 | collectEndorsement(); |
| 110 | |
| 111 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 112 | onDnsEndorseeValidated(const Data& data); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 113 | |
| 114 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 115 | onDnsEndorseeValidationFailed(const Data& data, |
| 116 | const ndn::security::ValidationError& error); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 117 | |
| 118 | void |
| 119 | onDnsEndorseeTimeoutNotify(const Interest& interest); |
| 120 | |
| 121 | void |
| 122 | decreaseCollectStatus(); |
| 123 | |
| 124 | void |
| 125 | publishCollectEndorsedDataInDNS(); |
| 126 | |
| 127 | // Identity certificate |
| 128 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 129 | onIdentityCertValidated(const Data& data); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 130 | |
| 131 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 132 | onIdentityCertValidationFailed(const Data& data, |
| 133 | const ndn::security::ValidationError& error); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 134 | |
| 135 | void |
| 136 | onIdentityCertTimeoutNotify(const Interest& interest); |
| 137 | |
| 138 | void |
| 139 | decreaseIdCertCount(); |
| 140 | |
| 141 | // Publish self-endorse certificate |
| 142 | shared_ptr<EndorseCertificate> |
| 143 | getSignedSelfEndorseCertificate(const Profile& profile); |
| 144 | |
| 145 | void |
| 146 | publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate); |
| 147 | |
| 148 | // Publish endorse certificate |
| 149 | shared_ptr<EndorseCertificate> |
| 150 | generateEndorseCertificate(const Name& identity); |
| 151 | |
| 152 | void |
| 153 | publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate); |
| 154 | |
| 155 | // Communication |
| 156 | void |
| 157 | sendInterest(const Interest& interest, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 158 | const ndn::security::DataValidationSuccessCallback& onValidated, |
| 159 | const ndn::security::DataValidationFailureCallback& onValidationFailed, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 160 | const TimeoutNotify& timeoutNotify, |
| 161 | int retry = 1); |
| 162 | |
| 163 | void |
| 164 | onTargetData(const Interest& interest, |
| 165 | const Data& data, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 166 | const ndn::security::DataValidationSuccessCallback& onValidated, |
| 167 | const ndn::security::DataValidationFailureCallback& onValidationFailed); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 168 | |
| 169 | void |
| 170 | onTargetTimeout(const Interest& interest, |
| 171 | int retry, |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 172 | const ndn::security::DataValidationSuccessCallback& onValidated, |
| 173 | const ndn::security::DataValidationFailureCallback& onValidationFailed, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 174 | const TimeoutNotify& timeoutNotify); |
| 175 | |
| 176 | // DNS listener |
| 177 | void |
| 178 | onDnsInterest(const Name& prefix, const Interest& interest); |
| 179 | |
| 180 | void |
| 181 | onDnsRegisterFailed(const Name& prefix, const std::string& failInfo); |
| 182 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 183 | void |
| 184 | onKeyInterest(const Name& prefix, const Interest& interest); |
| 185 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 186 | signals: |
| 187 | void |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 188 | contactEndorseInfoReady(const EndorseInfo& endorseInfo); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 189 | |
| 190 | void |
| 191 | contactInfoFetchFailed(const QString& identity); |
| 192 | |
| 193 | void |
| 194 | idCertNameListReady(const QStringList& certNameList); |
| 195 | |
| 196 | void |
| 197 | nameListReady(const QStringList& certNameList); |
| 198 | |
| 199 | void |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 200 | idCertReady(const ndn::security::Certificate& idCert); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 201 | |
| 202 | void |
| 203 | contactAliasListReady(const QStringList& aliasList); |
| 204 | |
| 205 | void |
| 206 | contactIdListReady(const QStringList& idList); |
| 207 | |
| 208 | void |
| 209 | contactInfoReady(const QString& identity, |
| 210 | const QString& name, |
| 211 | const QString& institute, |
| 212 | bool isIntro); |
| 213 | |
| 214 | void |
| 215 | warning(const QString& msg); |
| 216 | |
| 217 | public slots: |
| 218 | void |
| 219 | onIdentityUpdated(const QString& identity); |
| 220 | |
| 221 | void |
| 222 | onFetchContactInfo(const QString& identity); |
| 223 | |
| 224 | void |
| 225 | onAddFetchedContact(const QString& identity); |
| 226 | |
| 227 | void |
| 228 | onUpdateProfile(); |
| 229 | |
| 230 | void |
| 231 | onRefreshBrowseContact(); |
| 232 | |
| 233 | void |
| 234 | onFetchIdCert(const QString& certName); |
| 235 | |
| 236 | void |
| 237 | onAddFetchedContactIdCert(const QString& identity); |
| 238 | |
| 239 | void |
| 240 | onWaitForContactList(); |
| 241 | |
| 242 | void |
| 243 | onWaitForContactInfo(const QString& identity); |
| 244 | |
| 245 | void |
| 246 | onRemoveContact(const QString& identity); |
| 247 | |
| 248 | void |
| 249 | onUpdateAlias(const QString& identity, const QString& alias); |
| 250 | |
| 251 | void |
| 252 | onUpdateIsIntroducer(const QString& identity, bool isIntro); |
| 253 | |
| 254 | void |
| 255 | onUpdateEndorseCertificate(const QString& identity); |
| 256 | |
| 257 | private: |
| 258 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 259 | class FetchedInfo |
| 260 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 261 | public: |
| 262 | shared_ptr<EndorseCertificate> m_selfEndorseCert; |
| 263 | shared_ptr<EndorseCollection> m_endorseCollection; |
| 264 | std::vector<shared_ptr<EndorseCertificate> > m_endorseCertList; |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 265 | shared_ptr<EndorseInfo> m_endorseInfo; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | typedef std::map<Name, FetchedInfo> BufferedContacts; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 269 | typedef std::map<Name, shared_ptr<ndn::security::Certificate> > BufferedIdCerts; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 270 | |
| 271 | typedef boost::recursive_mutex RecLock; |
| 272 | typedef boost::unique_lock<RecLock> UniqueRecLock; |
| 273 | |
| 274 | // Conf |
| 275 | shared_ptr<ContactStorage> m_contactStorage; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 276 | shared_ptr<ndn::security::ValidatorConfig> m_validator; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 277 | ndn::Face& m_face; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 278 | ndn::KeyChain m_keyChain; |
| 279 | Name m_identity; |
| 280 | ContactList m_contactList; |
| 281 | |
| 282 | // Buffer |
| 283 | BufferedContacts m_bufferedContacts; |
| 284 | BufferedIdCerts m_bufferedIdCerts; |
| 285 | |
| 286 | // Tmp Dns |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 287 | ndn::ScopedRegisteredPrefixHandle m_dnsListenerHandle; |
| 288 | ndn::ScopedRegisteredPrefixHandle m_keyListenerHandle; |
| 289 | ndn::ScopedRegisteredPrefixHandle m_profileCertListenerHandle; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 290 | |
| 291 | RecLock m_collectCountMutex; |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 292 | size_t m_collectCount; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 293 | |
| 294 | RecLock m_idCertCountMutex; |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 295 | size_t m_idCertCount; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 298 | } // namespace chronochat |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 299 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 300 | #endif // CHRONOCHAT_CONTACT_MANAGER_HPP |