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