blob: 456a19dd92a554682f4bd3c8c4ec7cace65c7bb3 [file] [log] [blame]
Yingdi Yu0b0a7362014-08-05 16:31:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Yingdi Yu0b0a7362014-08-05 16:31:30 -07004 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08009 * Qiuhan Ding <qiuhanding@cs.ucla.edu>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070010 */
11
Yingdi Yueb692ac2015-02-10 18:46:18 -080012#ifndef CHRONOCHAT_CONTACT_MANAGER_HPP
13#define CHRONOCHAT_CONTACT_MANAGER_HPP
Yingdi Yu0b0a7362014-08-05 16:31:30 -070014
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 Ding0cfc1512015-02-17 17:44:11 -080022#include "endorse-info.hpp"
23#include "endorse-collection.hpp"
Yingdi Yu0b0a7362014-08-05 16:31:30 -070024#include <ndn-cxx/security/key-chain.hpp>
Varun Patil3d850902020-11-23 12:19:14 +053025#include <ndn-cxx/security/validator-config.hpp>
26#include <ndn-cxx/face.hpp>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070027#include <boost/thread/locks.hpp>
28#include <boost/thread/recursive_mutex.hpp>
29#endif
30
Yingdi Yueb692ac2015-02-10 18:46:18 -080031namespace chronochat {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070032
33typedef function<void(const Interest&)> TimeoutNotify;
34typedef std::vector<shared_ptr<Contact> > ContactList;
35
36class ContactManager : public QObject
37{
38 Q_OBJECT
39
40public:
Yingdi Yu2c9e7712014-10-20 11:55:05 -070041 ContactManager(ndn::Face& m_face, QObject* parent = 0);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070042
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 }
57private:
Yingdi Yu0b0a7362014-08-05 16:31:30 -070058 void
59 initializeSecurity();
60
61 void
62 fetchCollectEndorse(const Name& identity);
63
64 void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080065 fetchEndorseCertificateInternal(const Name& identity, size_t certIndex);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070066
67 void
68 prepareEndorseInfo(const Name& identity);
69
70 // PROFILE: self-endorse-certificate
71 void
Varun Patil3d850902020-11-23 12:19:14 +053072 onDnsSelfEndorseCertValidated(const Data& selfEndorseCertificate,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070073 const Name& identity);
74
75 void
Varun Patil3d850902020-11-23 12:19:14 +053076 onDnsSelfEndorseCertValidationFailed(const Data& selfEndorseCertificate,
77 const ndn::security::ValidationError& error,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070078 const Name& identity);
79
80 void
81 onDnsSelfEndorseCertTimeoutNotify(const Interest& interest, const Name& identity);
82
83 // ENDORSED: endorse-collection
84 void
Varun Patil3d850902020-11-23 12:19:14 +053085 onDnsCollectEndorseValidated(const Data& data, const Name& identity);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070086
87 void
Varun Patil3d850902020-11-23 12:19:14 +053088 onDnsCollectEndorseValidationFailed(const Data& data,
89 const ndn::security::ValidationError& error,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070090 const Name& identity);
91
92 void
93 onDnsCollectEndorseTimeoutNotify(const Interest& interest,
94 const Name& identity);
95
96 // PROFILE-CERT: endorse-certificate
97 void
Varun Patil3d850902020-11-23 12:19:14 +053098 onEndorseCertificateInternal(const Interest& interest, const Data& data,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080099 const Name& identity, size_t certIndex,
100 std::string hash);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700101
102 void
103 onEndorseCertificateInternalTimeout(const Interest& interest,
104 const Name& identity,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800105 size_t certIndex);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700106
107 // Collect endorsement
108 void
109 collectEndorsement();
110
111 void
Varun Patil3d850902020-11-23 12:19:14 +0530112 onDnsEndorseeValidated(const Data& data);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700113
114 void
Varun Patil3d850902020-11-23 12:19:14 +0530115 onDnsEndorseeValidationFailed(const Data& data,
116 const ndn::security::ValidationError& error);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700117
118 void
119 onDnsEndorseeTimeoutNotify(const Interest& interest);
120
121 void
122 decreaseCollectStatus();
123
124 void
125 publishCollectEndorsedDataInDNS();
126
127 // Identity certificate
128 void
Varun Patil3d850902020-11-23 12:19:14 +0530129 onIdentityCertValidated(const Data& data);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700130
131 void
Varun Patil3d850902020-11-23 12:19:14 +0530132 onIdentityCertValidationFailed(const Data& data,
133 const ndn::security::ValidationError& error);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700134
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 Patil3d850902020-11-23 12:19:14 +0530158 const ndn::security::DataValidationSuccessCallback& onValidated,
159 const ndn::security::DataValidationFailureCallback& onValidationFailed,
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700160 const TimeoutNotify& timeoutNotify,
161 int retry = 1);
162
163 void
164 onTargetData(const Interest& interest,
165 const Data& data,
Varun Patil3d850902020-11-23 12:19:14 +0530166 const ndn::security::DataValidationSuccessCallback& onValidated,
167 const ndn::security::DataValidationFailureCallback& onValidationFailed);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700168
169 void
170 onTargetTimeout(const Interest& interest,
171 int retry,
Varun Patil3d850902020-11-23 12:19:14 +0530172 const ndn::security::DataValidationSuccessCallback& onValidated,
173 const ndn::security::DataValidationFailureCallback& onValidationFailed,
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700174 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 Patil3d850902020-11-23 12:19:14 +0530183 void
184 onKeyInterest(const Name& prefix, const Interest& interest);
185
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700186signals:
187 void
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800188 contactEndorseInfoReady(const EndorseInfo& endorseInfo);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700189
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 Patil3d850902020-11-23 12:19:14 +0530200 idCertReady(const ndn::security::Certificate& idCert);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700201
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
217public 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
257private:
258
Varun Patila24bd3e2020-11-24 10:08:33 +0530259 class FetchedInfo
260 {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700261 public:
262 shared_ptr<EndorseCertificate> m_selfEndorseCert;
263 shared_ptr<EndorseCollection> m_endorseCollection;
264 std::vector<shared_ptr<EndorseCertificate> > m_endorseCertList;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800265 shared_ptr<EndorseInfo> m_endorseInfo;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700266 };
267
268 typedef std::map<Name, FetchedInfo> BufferedContacts;
Varun Patil3d850902020-11-23 12:19:14 +0530269 typedef std::map<Name, shared_ptr<ndn::security::Certificate> > BufferedIdCerts;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700270
271 typedef boost::recursive_mutex RecLock;
272 typedef boost::unique_lock<RecLock> UniqueRecLock;
273
274 // Conf
275 shared_ptr<ContactStorage> m_contactStorage;
Varun Patil3d850902020-11-23 12:19:14 +0530276 shared_ptr<ndn::security::ValidatorConfig> m_validator;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700277 ndn::Face& m_face;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700278 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 Patila24bd3e2020-11-24 10:08:33 +0530287 ndn::ScopedRegisteredPrefixHandle m_dnsListenerHandle;
288 ndn::ScopedRegisteredPrefixHandle m_keyListenerHandle;
289 ndn::ScopedRegisteredPrefixHandle m_profileCertListenerHandle;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700290
291 RecLock m_collectCountMutex;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800292 size_t m_collectCount;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700293
294 RecLock m_idCertCountMutex;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800295 size_t m_idCertCount;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700296};
297
Yingdi Yueb692ac2015-02-10 18:46:18 -0800298} // namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700299
Yingdi Yueb692ac2015-02-10 18:46:18 -0800300#endif // CHRONOCHAT_CONTACT_MANAGER_HPP