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 std; |
| 25 | using namespace ndn; |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 26 | |
| 27 | INIT_LOGGER ("SyncSocket"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 28 | |
| 29 | namespace Sync { |
| 30 | |
Alexander Afanasyev | 531803b | 2014-02-05 15:57:35 -0800 | [diff] [blame] | 31 | using ndn::shared_ptr; |
| 32 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 33 | static const uint8_t ROUTING_PREFIX_SEPARATOR[2] = {0xF0, 0x2E}; |
| 34 | |
| 35 | const Name SyncSocket::EMPTY_NAME = Name(); |
| 36 | |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 37 | SyncSocket::SyncSocket (const Name& syncPrefix, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 38 | const Name& dataPrefix, |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 39 | uint64_t dataSession, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 40 | bool withRoutingPrefix, |
| 41 | const Name& routingPrefix, |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 42 | shared_ptr<Face> face, |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 43 | const IdentityCertificate& myCertificate, |
| 44 | shared_ptr<SecRuleRelative> dataRule, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 45 | NewDataCallback dataCallback, |
| 46 | RemoveCallback rmCallback ) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 47 | : m_dataPrefix(dataPrefix) |
| 48 | , m_dataSession(dataSession) |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 49 | , m_withRoutingPrefix(false) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 50 | , m_newDataCallback(dataCallback) |
| 51 | , m_myCertificate(myCertificate) |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 52 | , m_face(face) |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 53 | , m_ioService(face->ioService()) |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 54 | { |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 55 | if(withRoutingPrefix && !routingPrefix.isPrefixOf(m_dataPrefix)) |
| 56 | { |
| 57 | m_withRoutingPrefix = true; |
| 58 | m_routableDataPrefix.append(routingPrefix).append(ROUTING_PREFIX_SEPARATOR, 2).append(m_dataPrefix); |
| 59 | } |
| 60 | |
| 61 | |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 62 | if(static_cast<bool>(dataRule)) |
| 63 | { |
| 64 | m_withSecurity = true; |
| 65 | m_syncValidator = shared_ptr<Validator>(new SyncValidator(syncPrefix, |
| 66 | m_myCertificate, |
| 67 | m_face, |
| 68 | bind(&SyncSocket::publishData, this, _1, _2, _3, true), |
| 69 | dataRule)); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | m_withSecurity = false; |
| 74 | m_syncValidator = shared_ptr<Validator>(new ValidatorNull()); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | m_syncLogic = shared_ptr<SyncLogic>(new SyncLogic(syncPrefix, |
| 79 | myCertificate, |
| 80 | m_syncValidator, |
| 81 | m_face, |
| 82 | bind(&SyncSocket::passCallback, this, _1), |
| 83 | rmCallback)); |
| 84 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 85 | |
| 86 | SyncSocket::~SyncSocket() |
Yingdi Yu | 479e117 | 2013-11-06 16:36:19 -0800 | [diff] [blame] | 87 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 90 | void |
| 91 | 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] | 92 | { |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 93 | shared_ptr<Data> data = make_shared<Data>(); |
| 94 | data->setContent(reinterpret_cast<const uint8_t*>(buf), len); |
| 95 | data->setFreshnessPeriod(1000*freshness); |
| 96 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 97 | m_ioService->post(bind(&SyncSocket::publishDataInternal, this, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 98 | data, isCert)); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 102 | SyncSocket::publishDataInternal(shared_ptr<Data> data, bool isCert) |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 103 | { |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 104 | Name dataPrefix = (m_withRoutingPrefix ? m_routableDataPrefix : m_dataPrefix); |
| 105 | |
| 106 | uint64_t sequence = getNextSeq(); |
| 107 | |
| 108 | Name dataName; |
| 109 | dataName.append(m_dataPrefix) |
| 110 | .append(boost::lexical_cast<string>(m_dataSession)) |
| 111 | .append(boost::lexical_cast<string>(sequence)); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 112 | if(isCert) |
| 113 | dataName.append("INTRO-CERT"); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 114 | data->setName(dataName); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 115 | m_keyChain.sign(*data, m_myCertificate.getName()); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 116 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 117 | if(m_withRoutingPrefix) |
| 118 | { |
| 119 | Name wrappedName; |
| 120 | wrappedName.append(m_routableDataPrefix) |
| 121 | .append(boost::lexical_cast<string>(m_dataSession)) |
| 122 | .append(boost::lexical_cast<string>(sequence)); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 123 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 124 | Data wrappedData(wrappedName); |
| 125 | wrappedData.setContent(data->wireEncode()); |
| 126 | m_keyChain.sign(wrappedData, m_myCertificate.getName()); |
| 127 | |
| 128 | m_face->put(wrappedData); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | m_face->put(*data); |
| 133 | } |
| 134 | |
| 135 | SeqNo s(m_dataSession, sequence + 1); |
| 136 | m_sequenceLog[dataPrefix] = s; |
| 137 | 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] | 138 | } |
| 139 | |
| 140 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 141 | 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] | 142 | { |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 143 | Name interestName = prefix; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 144 | interestName.append(boost::lexical_cast<string>(seq.getSession())).append(boost::lexical_cast<string>(seq.getSeq())); |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 145 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 146 | ndn::Interest interest(interestName); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 147 | interest.setMustBeFresh(true); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 148 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 149 | m_face->expressInterest(interest, |
| 150 | bind(&SyncSocket::onData, this, _1, _2, dataCallback), |
| 151 | bind(&SyncSocket::onDataTimeout, this, _1, retry, dataCallback)); |
| 152 | |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | SyncSocket::onData(const ndn::Interest& interest, Data& data, const OnDataValidated& dataCallback) |
| 157 | { |
| 158 | bool encaped = false; |
| 159 | |
| 160 | Name interestName = interest.getName(); |
| 161 | Name::const_iterator it = interestName.begin(); |
| 162 | Name::const_iterator end = interestName.end(); |
| 163 | |
| 164 | size_t offset = interestName.size(); |
| 165 | for(; it != end; it++) |
| 166 | { |
| 167 | offset--; |
| 168 | if(it->toEscapedString() == "%F0.") |
| 169 | { |
| 170 | encaped = true; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if(!encaped) |
| 176 | offset = interestName.size(); |
| 177 | |
| 178 | const OnDataValidated& onValidated = bind(&SyncSocket::onDataValidated, this, _1, offset, dataCallback); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 179 | const OnDataValidationFailed& onValidationFailed = bind(&SyncSocket::onDataValidationFailed, this, _1, _2); |
| 180 | |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 181 | if(encaped) |
| 182 | { |
| 183 | shared_ptr<Data> innerData = make_shared<Data>(); |
| 184 | innerData->wireDecode(data.getContent().blockFromValue()); |
| 185 | m_syncValidator->validate(*innerData, onValidated, onValidationFailed); |
| 186 | } |
| 187 | else |
| 188 | m_syncValidator->validate(data, onValidated, onValidationFailed); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 192 | SyncSocket::onDataTimeout(const ndn::Interest& interest, int retry, const OnDataValidated& dataCallback) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 193 | { |
| 194 | if(retry > 0) |
| 195 | { |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 196 | m_face->expressInterest(interest, |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 197 | bind(&SyncSocket::onData, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 198 | this, |
| 199 | _1, |
| 200 | _2, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 201 | dataCallback), |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 202 | bind(&SyncSocket::onDataTimeout, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 203 | this, |
| 204 | _1, |
| 205 | retry - 1, |
Yingdi Yu | d4ef5d3 | 2014-03-01 02:02:01 -0800 | [diff] [blame^] | 206 | dataCallback)); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 207 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 208 | } |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 209 | else |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 210 | _LOG_DEBUG("interest eventually time out!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 214 | SyncSocket::onDataValidated(const shared_ptr<const Data>& data, |
| 215 | size_t interestNameSize, |
| 216 | const OnDataValidated& onValidated) |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 217 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 218 | if(data->getName().size() > interestNameSize |
| 219 | && data->getName().get(interestNameSize).toEscapedString() == "INTRO-CERT") |
| 220 | { |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 221 | if(!m_withSecurity) |
| 222 | return; |
| 223 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 224 | Data rawData; |
| 225 | rawData.wireDecode(data->getContent().blockFromValue()); |
| 226 | IntroCertificate introCert(rawData); |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame] | 227 | dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introCert); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 228 | } |
| 229 | else |
| 230 | { |
| 231 | onValidated(data); |
| 232 | } |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 233 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 234 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 235 | void |
| 236 | SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data, |
| 237 | const std::string& failureInfo) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 238 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 239 | _LOG_DEBUG("data cannot be verified!: " << failureInfo); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | }//Sync |