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 | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 33 | SyncSocket::SyncSocket (const Name& syncPrefix, |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 34 | const ndn::Name& dataPrefix, |
| 35 | uint64_t dataSession, |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame^] | 36 | shared_ptr<Face> face, |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 37 | const IdentityCertificate& myCertificate, |
| 38 | shared_ptr<SecRuleRelative> dataRule, |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 39 | NewDataCallback dataCallback, |
| 40 | RemoveCallback rmCallback ) |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 41 | : m_dataPrefix(dataPrefix) |
| 42 | , m_dataSession(dataSession) |
| 43 | , m_newDataCallback(dataCallback) |
| 44 | , m_myCertificate(myCertificate) |
Yingdi Yu | 6e235db | 2013-12-27 08:40:53 +0800 | [diff] [blame] | 45 | , m_face(face) |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 46 | , m_ioService(face->ioService()) |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame^] | 47 | { |
| 48 | if(static_cast<bool>(dataRule)) |
| 49 | { |
| 50 | m_withSecurity = true; |
| 51 | m_syncValidator = shared_ptr<Validator>(new SyncValidator(syncPrefix, |
| 52 | m_myCertificate, |
| 53 | m_face, |
| 54 | bind(&SyncSocket::publishData, this, _1, _2, _3, true), |
| 55 | dataRule)); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | m_withSecurity = false; |
| 60 | m_syncValidator = shared_ptr<Validator>(new ValidatorNull()); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | m_syncLogic = shared_ptr<SyncLogic>(new SyncLogic(syncPrefix, |
| 65 | myCertificate, |
| 66 | m_syncValidator, |
| 67 | m_face, |
| 68 | bind(&SyncSocket::passCallback, this, _1), |
| 69 | rmCallback)); |
| 70 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 71 | |
| 72 | SyncSocket::~SyncSocket() |
Yingdi Yu | 479e117 | 2013-11-06 16:36:19 -0800 | [diff] [blame] | 73 | { |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 76 | void |
| 77 | 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] | 78 | { |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 79 | shared_ptr<Data> data = make_shared<Data>(); |
| 80 | data->setContent(reinterpret_cast<const uint8_t*>(buf), len); |
| 81 | data->setFreshnessPeriod(1000*freshness); |
| 82 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 83 | m_ioService->post(bind(&SyncSocket::publishDataInternal, this, |
| 84 | data, m_dataPrefix, m_dataSession, isCert)); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 88 | SyncSocket::publishDataInternal(shared_ptr<Data> data, const Name &prefix, uint64_t session, bool isCert) |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 89 | { |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 90 | uint64_t sequence = getNextSeq(prefix, session); |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 91 | Name dataName = prefix; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 92 | dataName.append(boost::lexical_cast<string>(session)).append(boost::lexical_cast<string>(sequence)); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 93 | if(isCert) |
| 94 | dataName.append("INTRO-CERT"); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 95 | data->setName(dataName); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 96 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 97 | m_keyChain.sign(*data, m_myCertificate.getName()); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 98 | m_face->put(*data); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 99 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 100 | SeqNo s(session, sequence + 1); |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 101 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 102 | m_sequenceLog[prefix] = s; |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame^] | 103 | m_syncLogic->addLocalNames (prefix, session, sequence); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 107 | 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] | 108 | { |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 109 | Name interestName = prefix; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 110 | 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] | 111 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 112 | ndn::Interest interest(interestName); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 113 | interest.setMustBeFresh(true); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 114 | |
| 115 | const OnDataValidated& onValidated = bind(&SyncSocket::onDataValidated, this, _1, interestName.size(), dataCallback); |
| 116 | const OnDataValidationFailed& onValidationFailed = bind(&SyncSocket::onDataValidationFailed, this, _1, _2); |
| 117 | |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 118 | m_face->expressInterest(interest, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 119 | bind(&SyncSocket::onData, this, _1, _2, onValidated, onValidationFailed), |
| 120 | bind(&SyncSocket::onDataTimeout, this, _1, retry, onValidated, onValidationFailed)); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 121 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 125 | SyncSocket::onData(const ndn::Interest& interest, Data& data, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 126 | const OnDataValidated& onValidated, |
| 127 | const OnDataValidationFailed& onValidationFailed) |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 128 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 129 | m_syncValidator->validate(data, onValidated, onValidationFailed); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 133 | SyncSocket::onDataTimeout(const ndn::Interest& interest, |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 134 | int retry, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 135 | const OnDataValidated& onValidated, |
| 136 | const OnDataValidationFailed& onValidationFailed) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 137 | { |
| 138 | if(retry > 0) |
| 139 | { |
Yingdi Yu | 51c8025 | 2014-02-10 19:32:05 -0800 | [diff] [blame] | 140 | m_face->expressInterest(interest, |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 141 | bind(&SyncSocket::onData, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 142 | this, |
| 143 | _1, |
| 144 | _2, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 145 | onValidated, |
| 146 | onValidationFailed), |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 147 | bind(&SyncSocket::onDataTimeout, |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 148 | this, |
| 149 | _1, |
| 150 | retry - 1, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 151 | onValidated, |
| 152 | onValidationFailed)); |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 153 | |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 154 | } |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 155 | else |
Yingdi Yu | 6d638f0 | 2014-01-24 11:01:21 -0800 | [diff] [blame] | 156 | _LOG_DEBUG("interest eventually time out!"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 160 | SyncSocket::onDataValidated(const shared_ptr<const Data>& data, |
| 161 | size_t interestNameSize, |
| 162 | const OnDataValidated& onValidated) |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 163 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 164 | _LOG_DEBUG("--------------------" << data->getName()); |
| 165 | if(data->getName().size() > interestNameSize |
| 166 | && data->getName().get(interestNameSize).toEscapedString() == "INTRO-CERT") |
| 167 | { |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame^] | 168 | if(!m_withSecurity) |
| 169 | return; |
| 170 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 171 | Data rawData; |
| 172 | rawData.wireDecode(data->getContent().blockFromValue()); |
| 173 | IntroCertificate introCert(rawData); |
Yingdi Yu | aed0f3e | 2014-02-28 14:54:16 -0800 | [diff] [blame^] | 174 | dynamic_pointer_cast<SyncValidator>(m_syncValidator)->addParticipant(introCert); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 175 | } |
| 176 | else |
| 177 | { |
| 178 | onValidated(data); |
| 179 | } |
Yingdi Yu | 46c9f1a | 2013-12-18 15:15:46 +0800 | [diff] [blame] | 180 | } |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 181 | |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 182 | void |
| 183 | SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data, |
| 184 | const std::string& failureInfo) |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 185 | { |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 186 | _LOG_DEBUG("data cannot be verified!: " << failureInfo); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | }//Sync |