blob: 8ab7e819f22b57767e0d42969bee49a35aad6b96 [file] [log] [blame]
Yingdi Yu9236c432013-10-18 11:29:25 -07001/* -*- 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 Yufa4ce792014-02-06 18:09:22 -080011#ifndef CHRONOS_CONTACT_MANAGER_H
12#define CHRONOS_CONTACT_MANAGER_H
Yingdi Yu9236c432013-10-18 11:29:25 -070013
Yingdi Yu4685b1b2013-10-18 17:05:02 -070014#include <QObject>
Yingdi Yu9236c432013-10-18 11:29:25 -070015
Yingdi Yu4685b1b2013-10-18 17:05:02 -070016#ifndef Q_MOC_RUN
17#include "contact-storage.h"
Yingdi Yu4685b1b2013-10-18 17:05:02 -070018#include "endorse-certificate.h"
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070019#include "profile.h"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080020#include "endorse-info.pb.h"
21#include "endorse-collection.pb.h"
Yingdi Yufa0b6a02014-04-30 14:26:42 -070022#include <ndn-cxx/face.hpp>
23#include <ndn-cxx/security/key-chain.hpp>
24#include <ndn-cxx/security/validator.hpp>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080025#include <boost/thread/locks.hpp>
26#include <boost/thread/recursive_mutex.hpp>
Yingdi Yu4685b1b2013-10-18 17:05:02 -070027#endif
28
Yingdi Yufa4ce792014-02-06 18:09:22 -080029namespace chronos{
30
Yingdi Yu348f5ea2014-03-01 14:47:25 -080031typedef ndn::function<void(const ndn::Interest&)> TimeoutNotify;
32typedef std::vector<ndn::shared_ptr<Contact> > ContactList;
Yingdi Yu4685b1b2013-10-18 17:05:02 -070033
34class ContactManager : public QObject
Yingdi Yu9236c432013-10-18 11:29:25 -070035{
Yingdi Yu4685b1b2013-10-18 17:05:02 -070036 Q_OBJECT
37
Yingdi Yu9236c432013-10-18 11:29:25 -070038public:
Yingdi Yufa4ce792014-02-06 18:09:22 -080039 ContactManager(ndn::shared_ptr<ndn::Face> m_face,
Yingdi Yu76dd8002013-12-24 11:16:32 +080040 QObject* parent = 0);
Yingdi Yu9236c432013-10-18 11:29:25 -070041
42 ~ContactManager();
43
Yingdi Yu348f5ea2014-03-01 14:47:25 -080044 ndn::shared_ptr<Contact>
45 getContact(const ndn::Name& identity)
46 {
47 return m_contactStorage->getContact(identity);
48 }
Yingdi Yu4685b1b2013-10-18 17:05:02 -070049
50 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080051 getContactList(ContactList& contactList)
52 {
53 contactList.clear();
54 contactList.insert(contactList.end(), m_contactList.begin(), m_contactList.end());
55 }
Yingdi Yu06f572b2014-03-11 15:46:10 -070056private:
57 ndn::shared_ptr<ndn::IdentityCertificate>
58 loadTrustAnchor();
59
Yingdi Yu348f5ea2014-03-01 14:47:25 -080060 void
61 initializeSecurity();
Yingdi Yub2e747d2013-11-05 23:06:43 -080062
63 void
64 fetchCollectEndorse(const ndn::Name& identity);
65
66 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080067 fetchEndorseCertificateInternal(const ndn::Name& identity, int certIndex);
Yingdi Yuae8217c2013-11-09 00:03:26 -080068
69 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080070 prepareEndorseInfo(const ndn::Name& identity);
71
72 // PROFILE: self-endorse-certificate
Yingdi Yufa0b6a02014-04-30 14:26:42 -070073 void
74 onDnsSelfEndorseCertValidated(const ndn::shared_ptr<const ndn::Data>& selfEndorseCertificate,
Yingdi Yu348f5ea2014-03-01 14:47:25 -080075 const ndn::Name& identity);
Yingdi Yu4685b1b2013-10-18 17:05:02 -070076
Yingdi Yub2e747d2013-11-05 23:06:43 -080077 void
Yingdi Yufa0b6a02014-04-30 14:26:42 -070078 onDnsSelfEndorseCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& selfEndorseCertificate,
Yingdi Yu5a5ff202014-03-17 10:03:02 -070079 const std::string& failInfo,
Yingdi Yu348f5ea2014-03-01 14:47:25 -080080 const ndn::Name& identity);
Yingdi Yu9236c432013-10-18 11:29:25 -070081
Yingdi Yub2e747d2013-11-05 23:06:43 -080082 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080083 onDnsSelfEndorseCertTimeoutNotify(const ndn::Interest& interest,
84 const ndn::Name& identity);
Yingdi Yufa0b6a02014-04-30 14:26:42 -070085
Yingdi Yu348f5ea2014-03-01 14:47:25 -080086 // ENDORSED: endorse-collection
87 void
Yingdi Yufa0b6a02014-04-30 14:26:42 -070088 onDnsCollectEndorseValidated(const ndn::shared_ptr<const ndn::Data>& data,
Yingdi Yu348f5ea2014-03-01 14:47:25 -080089 const ndn::Name& identity);
Yingdi Yub2e747d2013-11-05 23:06:43 -080090
Yingdi Yu76dd8002013-12-24 11:16:32 +080091 void
Yingdi Yu5a5ff202014-03-17 10:03:02 -070092 onDnsCollectEndorseValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
93 const std::string& failInfo,
Yingdi Yu348f5ea2014-03-01 14:47:25 -080094 const ndn::Name& identity);
Yingdi Yuae8217c2013-11-09 00:03:26 -080095
96 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080097 onDnsCollectEndorseTimeoutNotify(const ndn::Interest& interest,
Yingdi Yufa0b6a02014-04-30 14:26:42 -070098 const ndn::Name& identity);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080099
100 // PROFILE-CERT: endorse-certificate
101 void
102 onEndorseCertificateInternal(const ndn::Interest& interest,
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700103 ndn::Data& data,
104 const ndn::Name& identity,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800105 int certIndex,
106 std::string hash);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800107
Yingdi Yu72232692013-11-12 17:50:21 -0800108 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800109 onEndorseCertificateInternalTimeout(const ndn::Interest& interest,
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700110 const ndn::Name& identity,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800111 int certIndex);
112
113 // Collect endorsement
114 void
115 collectEndorsement();
116
117 void
118 onDnsEndorseeValidated(const ndn::shared_ptr<const ndn::Data>& data);
119
120 void
Yingdi Yu5a5ff202014-03-17 10:03:02 -0700121 onDnsEndorseeValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
122 const std::string& failInfo);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800123
124 void
125 onDnsEndorseeTimeoutNotify(const ndn::Interest& interest);
126
127 void
128 decreaseCollectStatus();
129
130 void
131 publishCollectEndorsedDataInDNS();
132
133 // Identity certificate
134 void
135 onIdentityCertValidated(const ndn::shared_ptr<const ndn::Data>& data);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700136
Yingdi Yu76dd8002013-12-24 11:16:32 +0800137 void
Yingdi Yu5a5ff202014-03-17 10:03:02 -0700138 onIdentityCertValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
139 const std::string& failInfo);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800140
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800141 void
142 onIdentityCertTimeoutNotify(const ndn::Interest& interest);
143
144 void
145 decreaseIdCertCount();
146
147 // Publish self-endorse certificate
Yingdi Yufa4ce792014-02-06 18:09:22 -0800148 ndn::shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800149 getSignedSelfEndorseCertificate(const Profile& profile);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800150
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700151 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800152 publishSelfEndorseCertificateInDNS(const EndorseCertificate& selfEndorseCertificate);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700153
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800154 // Publish endorse certificate
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700155 ndn::shared_ptr<EndorseCertificate>
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800156 generateEndorseCertificate(const ndn::Name& identity);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800157
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800158 void
159 publishEndorseCertificateInDNS(const EndorseCertificate& endorseCertificate);
160
161 // Communication
162 void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800163 sendInterest(const ndn::Interest& interest,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800164 const ndn::OnDataValidated& onValidated,
165 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800166 const TimeoutNotify& timeoutNotify,
Yingdi Yu68de61d2014-01-16 19:13:03 -0800167 int retry = 1);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800168
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800169 void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700170 onTargetData(const ndn::Interest& interest,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800171 const ndn::Data& data,
172 const ndn::OnDataValidated& onValidated,
173 const ndn::OnDataValidationFailed& onValidationFailed);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800174
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800175 void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700176 onTargetTimeout(const ndn::Interest& interest,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800177 int retry,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800178 const ndn::OnDataValidated& onValidated,
179 const ndn::OnDataValidationFailed& onValidationFailed,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800180 const TimeoutNotify& timeoutNotify);
181
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800182 // DNS listener
Yingdi Yub2e747d2013-11-05 23:06:43 -0800183 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800184 onDnsInterest(const ndn::Name& prefix, const ndn::Interest& interest);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700185
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800186 void
187 onDnsRegisterFailed(const ndn::Name& prefix, const std::string& failInfo);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800188
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700189signals:
Yingdi Yu8e135832013-11-09 20:12:31 -0800190 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800191 contactEndorseInfoReady(const chronos::EndorseInfo& endorseInfo);
192
193 void
194 contactInfoFetchFailed(const QString& identity);
195
196 void
197 idCertNameListReady(const QStringList& certNameList);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700198
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700199 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800200 nameListReady(const QStringList& certNameList);
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700201
Yingdi Yub2e747d2013-11-05 23:06:43 -0800202 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800203 idCertReady(const ndn::IdentityCertificate& idCert);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800204
205 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800206 contactAliasListReady(const QStringList& aliasList);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800207
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800208 void
209 contactIdListReady(const QStringList& idList);
210
211 void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700212 contactInfoReady(const QString& identity,
213 const QString& name,
214 const QString& institute,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800215 bool isIntro);
216
217 void
218 warning(const QString& msg);
219
220public slots:
221 void
222 onIdentityUpdated(const QString& identity);
223
224 void
225 onFetchContactInfo(const QString& identity);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700226
Yingdi Yuae8217c2013-11-09 00:03:26 -0800227 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800228 onAddFetchedContact(const QString& identity);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800229
230 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800231 onUpdateProfile();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800232
Yingdi Yuae8217c2013-11-09 00:03:26 -0800233 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800234 onRefreshBrowseContact();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800235
Yingdi Yu72232692013-11-12 17:50:21 -0800236 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800237 onFetchIdCert(const QString& certName);
Yingdi Yu72232692013-11-12 17:50:21 -0800238
239 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800240 onAddFetchedContactIdCert(const QString& identity);
Yingdi Yu72232692013-11-12 17:50:21 -0800241
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800242 void
243 onWaitForContactList();
244
245 void
246 onWaitForContactInfo(const QString& identity);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700247
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800248 void
249 onRemoveContact(const QString& identity);
250
251 void
252 onUpdateAlias(const QString& identity, const QString& alias);
253
254 void
255 onUpdateIsIntroducer(const QString& identity, bool isIntro);
256
257 void
258 onUpdateEndorseCertificate(const QString& identity);
259
Yingdi Yu9236c432013-10-18 11:29:25 -0700260private:
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800261
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800262 class FetchedInfo {
263 public:
264 ndn::shared_ptr<EndorseCertificate> m_selfEndorseCert;
265 ndn::shared_ptr<EndorseCollection> m_endorseCollection;
266 std::vector<ndn::shared_ptr<EndorseCertificate> > m_endorseCertList;
267 ndn::shared_ptr<EndorseInfo> m_endorseInfo;
268 };
269
270 typedef std::map<ndn::Name, FetchedInfo> BufferedContacts;
271 typedef std::map<ndn::Name, ndn::shared_ptr<ndn::IdentityCertificate> > BufferedIdCerts;
272
273 typedef boost::recursive_mutex RecLock;
274 typedef boost::unique_lock<RecLock> UniqueRecLock;
275
276 // Conf
Yingdi Yufa4ce792014-02-06 18:09:22 -0800277 ndn::shared_ptr<ContactStorage> m_contactStorage;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800278 ndn::shared_ptr<ndn::Validator> m_validator;
279 ndn::shared_ptr<ndn::Face> m_face;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800280 ndn::KeyChain m_keyChain;
281 ndn::Name m_identity;
282 ContactList m_contactList;
283
284 // Buffer
285 BufferedContacts m_bufferedContacts;
286 BufferedIdCerts m_bufferedIdCerts;
287
288 // Tmp Dns
289 const ndn::RegisteredPrefixId* m_dnsListenerId;
290
291 RecLock m_collectCountMutex;
292 int m_collectCount;
293
294 RecLock m_idCertCountMutex;
295 int m_idCertCount;
Yingdi Yu9236c432013-10-18 11:29:25 -0700296};
297
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800298} // namespace chronos
Yingdi Yufa4ce792014-02-06 18:09:22 -0800299
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800300#endif //CHRONOS_CONTACT_MANAGER_H