Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/web/index.html> |
| 20 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 21 | * @author Chaoyi Bian <bcy@pku.edu.cn> |
| 22 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include "sync-socket.h" |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 26 | #include "sync-logging.h" |
| 27 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 28 | using namespace ndn; |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 29 | |
Yingdi Yu | 06a678a | 2014-08-01 17:07:08 -0700 | [diff] [blame] | 30 | INIT_LOGGER ("SyncSocket") |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 31 | |
| 32 | namespace Sync { |
| 33 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 34 | using ndn::shared_ptr; |
| 35 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 36 | static const uint8_t ROUTING_PREFIX_SEPARATOR[2] = {0xF0, 0x2E}; |
| 37 | |
| 38 | const Name SyncSocket::EMPTY_NAME = Name(); |
| 39 | |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 40 | SyncSocket::SyncSocket (const Name& syncPrefix, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 41 | const Name& dataPrefix, |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 42 | uint64_t dataSession, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 43 | bool withRoutingPrefix, |
Yingdi Yu | a873d64 | 2014-04-15 21:31:33 -0700 | [diff] [blame] | 44 | const Name& routingPrefix, |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 45 | shared_ptr<Face> face, |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 46 | const IdentityCertificate& myCertificate, |
| 47 | shared_ptr<SecRuleRelative> dataRule, |
Yingdi Yu | a873d64 | 2014-04-15 21:31:33 -0700 | [diff] [blame] | 48 | NewDataCallback dataCallback, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 49 | RemoveCallback rmCallback ) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 50 | : m_dataPrefix(dataPrefix) |
| 51 | , m_dataSession(dataSession) |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 52 | , m_withRoutingPrefix(false) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 53 | , m_newDataCallback(dataCallback) |
| 54 | , m_myCertificate(myCertificate) |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 55 | , m_face(face) |
Alexander Afanasyev | 7fe5983 | 2014-07-02 12:17:46 -0700 | [diff] [blame] | 56 | , m_ioService(face->getIoService()) |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 57 | { |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 58 | if(withRoutingPrefix && !routingPrefix.isPrefixOf(m_dataPrefix)) |
| 59 | { |
| 60 | m_withRoutingPrefix = true; |
| 61 | m_routableDataPrefix.append(routingPrefix).append(ROUTING_PREFIX_SEPARATOR, 2).append(m_dataPrefix); |
| 62 | } |
| 63 | |
| 64 | |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 65 | if(static_cast<bool>(dataRule)) |
| 66 | { |
| 67 | m_withSecurity = true; |
Yingdi Yu | a873d64 | 2014-04-15 21:31:33 -0700 | [diff] [blame] | 68 | m_syncValidator = shared_ptr<Validator>(new SyncValidator(syncPrefix, |
| 69 | m_myCertificate, |
| 70 | *m_face, |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 71 | bind(&SyncSocket::publishData, this, _1, _2, _3, true), |
| 72 | dataRule)); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | m_withSecurity = false; |
| 77 | m_syncValidator = shared_ptr<Validator>(new ValidatorNull()); |
| 78 | } |
| 79 | |
Yingdi Yu | a873d64 | 2014-04-15 21:31:33 -0700 | [diff] [blame] | 80 | |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 81 | m_syncLogic = shared_ptr<SyncLogic>(new SyncLogic(syncPrefix, |
| 82 | myCertificate, |
| 83 | m_syncValidator, |
| 84 | m_face, |
| 85 | bind(&SyncSocket::passCallback, this, _1), |
| 86 | rmCallback)); |
| 87 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 88 | |
| 89 | SyncSocket::~SyncSocket() |
Yingdi Yu | 479e117 | 2013-11-06 16:36:19 -0800 | [diff] [blame] | 90 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 91 | } |
| 92 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 93 | void |
| 94 | SyncSocket::publishData(const uint8_t* buf, size_t len, int freshness, bool isCert) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 95 | { |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 96 | shared_ptr<Data> data = make_shared<Data>(); |
| 97 | data->setContent(reinterpret_cast<const uint8_t*>(buf), len); |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 98 | data->setFreshnessPeriod(time::milliseconds(1000*freshness)); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 7fe5983 | 2014-07-02 12:17:46 -0700 | [diff] [blame] | 100 | m_ioService.post(bind(&SyncSocket::publishDataInternal, this, |
| 101 | data, isCert)); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 105 | SyncSocket::publishDataInternal(shared_ptr<Data> data, bool isCert) |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 106 | { |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 107 | Name dataPrefix = (m_withRoutingPrefix ? m_routableDataPrefix : m_dataPrefix); |
| 108 | |
| 109 | uint64_t sequence = getNextSeq(); |
| 110 | |
| 111 | Name dataName; |
| 112 | dataName.append(m_dataPrefix) |
Yingdi Yu | 68bc51a | 2014-03-25 16:02:12 -0700 | [diff] [blame] | 113 | .append(boost::lexical_cast<std::string>(m_dataSession)) |
| 114 | .append(boost::lexical_cast<std::string>(sequence)); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 115 | if(isCert) |
| 116 | dataName.append("INTRO-CERT"); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 117 | data->setName(dataName); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 118 | m_keyChain.sign(*data, m_myCertificate.getName()); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 119 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 120 | if(m_withRoutingPrefix) |
| 121 | { |
| 122 | Name wrappedName; |
| 123 | wrappedName.append(m_routableDataPrefix) |
Yingdi Yu | 68bc51a | 2014-03-25 16:02:12 -0700 | [diff] [blame] | 124 | .append(boost::lexical_cast<std::string>(m_dataSession)) |
| 125 | .append(boost::lexical_cast<std::string>(sequence)); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 126 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 127 | Data wrappedData(wrappedName); |
| 128 | wrappedData.setContent(data->wireEncode()); |
| 129 | m_keyChain.sign(wrappedData, m_myCertificate.getName()); |
| 130 | |
| 131 | m_face->put(wrappedData); |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | m_face->put(*data); |
| 136 | } |
| 137 | |
| 138 | SeqNo s(m_dataSession, sequence + 1); |
| 139 | m_sequenceLog[dataPrefix] = s; |
| 140 | m_syncLogic->addLocalNames (dataPrefix, m_dataSession, sequence); // If DNS works, we should use pure m_dataprefix rather than the one with routing prefix. |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Yingdi Yu | a873d64 | 2014-04-15 21:31:33 -0700 | [diff] [blame] | 143 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 144 | SyncSocket::fetchData(const Name& prefix, const SeqNo& seq, const OnDataValidated& dataCallback, int retry) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 145 | { |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 146 | Name interestName = prefix; |
Yingdi Yu | 68bc51a | 2014-03-25 16:02:12 -0700 | [diff] [blame] | 147 | interestName.append(boost::lexical_cast<std::string>(seq.getSession())).append(boost::lexical_cast<std::string>(seq.getSeq())); |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 148 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 149 | ndn::Interest interest(interestName); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 150 | interest.setMustBeFresh(true); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 151 | |
Yingdi Yu | a873d64 | 2014-04-15 21:31:33 -0700 | [diff] [blame] | 152 | m_face->expressInterest(interest, |
| 153 | bind(&SyncSocket::onData, this, _1, _2, dataCallback), |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 154 | bind(&SyncSocket::onDataTimeout, this, _1, retry, dataCallback)); |
| 155 | |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | SyncSocket::onData(const ndn::Interest& interest, Data& data, const OnDataValidated& dataCallback) |
| 160 | { |
| 161 | bool encaped = false; |
| 162 | |
| 163 | Name interestName = interest.getName(); |
| 164 | Name::const_iterator it = interestName.begin(); |
| 165 | Name::const_iterator end = interestName.end(); |
| 166 | |
| 167 | size_t offset = interestName.size(); |
| 168 | for(; it != end; it++) |
| 169 | { |
| 170 | offset--; |
Alexander Afanasyev | 7fe5983 | 2014-07-02 12:17:46 -0700 | [diff] [blame] | 171 | if(it->toUri() == "%F0.") |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 172 | { |
| 173 | encaped = true; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if(!encaped) |
| 179 | offset = interestName.size(); |
| 180 | |
| 181 | const OnDataValidated& onValidated = bind(&SyncSocket::onDataValidated, this, _1, offset, dataCallback); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 182 | const OnDataValidationFailed& onValidationFailed = bind(&SyncSocket::onDataValidationFailed, this, _1, _2); |
| 183 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 184 | if(encaped) |
| 185 | { |
| 186 | shared_ptr<Data> innerData = make_shared<Data>(); |
| 187 | innerData->wireDecode(data.getContent().blockFromValue()); |
| 188 | m_syncValidator->validate(*innerData, onValidated, onValidationFailed); |
| 189 | } |
| 190 | else |
| 191 | m_syncValidator->validate(data, onValidated, onValidationFailed); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 195 | SyncSocket::onDataTimeout(const ndn::Interest& interest, int retry, const OnDataValidated& dataCallback) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 196 | { |
| 197 | if(retry > 0) |
| 198 | { |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 199 | m_face->expressInterest(interest, |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 200 | bind(&SyncSocket::onData, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 201 | this, |
| 202 | _1, |
| 203 | _2, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 204 | dataCallback), |
Yingdi Yu | a873d64 | 2014-04-15 21:31:33 -0700 | [diff] [blame] | 205 | bind(&SyncSocket::onDataTimeout, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 206 | this, |
| 207 | _1, |
| 208 | retry - 1, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame] | 209 | dataCallback)); |
Yingdi Yu | a873d64 | 2014-04-15 21:31:33 -0700 | [diff] [blame] | 210 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 211 | } |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 212 | else |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 213 | _LOG_DEBUG("interest eventually time out!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 217 | SyncSocket::onDataValidated(const shared_ptr<const Data>& data, |
| 218 | size_t interestNameSize, |
| 219 | const OnDataValidated& onValidated) |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 220 | { |
Yingdi Yu | a873d64 | 2014-04-15 21:31:33 -0700 | [diff] [blame] | 221 | if(data->getName().size() > interestNameSize |
Alexander Afanasyev | 7fe5983 | 2014-07-02 12:17:46 -0700 | [diff] [blame] | 222 | && data->getName().get(interestNameSize).toUri() == "INTRO-CERT") |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 223 | { |
Yingdi Yu | a873d64 | 2014-04-15 21:31:33 -0700 | [diff] [blame] | 224 | if(!m_withSecurity) |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 225 | return; |
| 226 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 227 | Data rawData; |
| 228 | rawData.wireDecode(data->getContent().blockFromValue()); |
| 229 | IntroCertificate introCert(rawData); |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 230 | dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introCert); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 231 | } |
| 232 | else |
| 233 | { |
| 234 | onValidated(data); |
| 235 | } |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 236 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 237 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 238 | void |
| 239 | SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data, |
| 240 | const std::string& failureInfo) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 241 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 242 | _LOG_DEBUG("data cannot be verified!: " << failureInfo); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | }//Sync |