Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include "common.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 14 | #include "ndnd-controller.hpp" |
| 15 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 16 | #include "../face.hpp" |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 17 | #include "../security/signature-sha256-with-rsa.hpp" |
| 18 | #include "../util/random.hpp" |
| 19 | |
| 20 | #include "ndnd-forwarding-entry.hpp" |
| 21 | #include "ndnd-face-instance.hpp" |
| 22 | #include "ndnd-status-response.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndnd { |
| 26 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 27 | Controller::Controller(Face& face) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 28 | : m_face(face) |
| 29 | , m_faceId(-1) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 34 | Controller::selfRegisterPrefix(const Name& prefixToRegister, |
| 35 | const SuccessCallback& onSuccess, |
| 36 | const FailCallback& onFail) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 37 | { |
| 38 | if (!m_ndndId.hasValue()) |
| 39 | { |
| 40 | if (m_filterRequests.empty()) |
| 41 | { |
| 42 | m_face.expressInterest(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 43 | bind(&Controller::onNdnidFetched, this, _1, _2), |
| 44 | bind(onFail, "NDNDID fetching timed out")); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 45 | } |
| 46 | m_filterRequests.push_back(FilterRequest(prefixToRegister, onSuccess, onFail)); |
| 47 | } |
| 48 | else |
| 49 | startPrefixAction(ForwardingEntry("selfreg", prefixToRegister), |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 50 | bind(&Controller::recordSelfRegisteredFaceId, this, _1, onSuccess), |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 51 | onFail); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | void |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 56 | Controller::selfDeregisterPrefix(const Name& prefixToRegister, |
| 57 | const SuccessCallback& onSuccess, |
| 58 | const FailCallback& onFail) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 59 | { |
| 60 | if (!m_ndndId.hasValue() || m_faceId == -1) |
| 61 | { |
| 62 | if (static_cast<bool>(onFail)) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 63 | onFail("NDNID is not available (must have been present after a successful registration operation)"); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 64 | return; |
| 65 | } |
| 66 | |
| 67 | startPrefixAction(ForwardingEntry("unreg", prefixToRegister, m_faceId), |
| 68 | bind(onSuccess), onFail); |
| 69 | } |
| 70 | |
| 71 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 72 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 73 | Controller::onNdnidFetched(const Interest& interest, Data& data) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 74 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 75 | if (data.getName().size() > interest.getName().size()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 76 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 77 | m_ndndId = data.getName()[interest.getName().size()]; |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 78 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 79 | if (m_ndndId.value_size() < 6) |
| 80 | { |
| 81 | for (FilterRequestList::iterator i = m_filterRequests.begin(); |
| 82 | i != m_filterRequests.end(); |
| 83 | ++i) |
| 84 | { |
| 85 | if (static_cast<bool>(i->m_onFailure)) |
| 86 | i->m_onFailure("Fetched unrecognized NDNID"); |
| 87 | } |
| 88 | |
| 89 | return; |
| 90 | } |
| 91 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 92 | for (FilterRequestList::iterator i = m_filterRequests.begin(); |
| 93 | i != m_filterRequests.end(); |
| 94 | ++i) |
| 95 | { |
| 96 | startPrefixAction(ForwardingEntry("selfreg", i->m_prefixToRegister), |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 97 | bind(&Controller::recordSelfRegisteredFaceId, this, _1, i->m_onSuccess), |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 98 | i->m_onFailure); |
| 99 | } |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | for (FilterRequestList::iterator i = m_filterRequests.begin(); |
| 104 | i != m_filterRequests.end(); |
| 105 | ++i) |
| 106 | { |
| 107 | if (static_cast<bool>(i->m_onFailure)) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 108 | i->m_onFailure("NDNID cannot be fetched"); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | m_filterRequests.clear(); |
| 112 | } |
| 113 | |
| 114 | void |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 115 | Controller::recordSelfRegisteredFaceId(const ForwardingEntry& entry, |
| 116 | const SuccessCallback& onSuccess) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 117 | { |
| 118 | m_faceId = entry.getFaceId(); |
| 119 | if (static_cast<bool>(onSuccess)) |
| 120 | onSuccess(); |
| 121 | } |
| 122 | |
| 123 | void |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 124 | Controller::startFaceAction(const FaceInstance& entry, |
| 125 | const FaceOperationSucceedCallback& onSuccess, |
| 126 | const FailCallback& onFail) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 127 | { |
| 128 | // Set the ForwardingEntry as the content of a Data packet and sign. |
| 129 | Data data; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 130 | data.setName(Name().appendVersion(random::generateWord32())); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 131 | data.setContent(entry.wireEncode()); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 133 | // Create an empty signature, since nobody going to verify it for now |
| 134 | // @todo In the future, we may require real signatures to do the registration |
| 135 | SignatureSha256WithRsa signature; |
| 136 | signature.setValue(Block(Tlv::SignatureValue)); |
| 137 | data.setSignature(signature); |
| 138 | |
| 139 | // Create an interest where the name has the encoded Data packet. |
| 140 | Name interestName; |
| 141 | interestName.append("ndnx"); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 142 | interestName.append(m_ndndId.value_begin()+6, m_ndndId.value_end()); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 143 | interestName.append(entry.getAction()); |
| 144 | interestName.append(data.wireEncode()); |
| 145 | |
| 146 | Interest interest(interestName); |
| 147 | interest.setScope(1); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 148 | interest.setInterestLifetime(time::seconds(1)); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 149 | interest.setMustBeFresh(true); |
| 150 | |
| 151 | m_face.expressInterest(interest, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 152 | bind(&Controller::processFaceActionResponse, this, _2, onSuccess, onFail), |
| 153 | bind(onFail, "Command Interest failed")); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 157 | Controller::startPrefixAction(const ForwardingEntry& entry, |
| 158 | const PrefixOperationSucceedCallback& onSuccess, |
| 159 | const FailCallback& onFail) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 160 | { |
| 161 | // Set the ForwardingEntry as the content of a Data packet and sign. |
| 162 | Data data; |
| 163 | data.setName(Name().appendVersion(random::generateWord32())); |
| 164 | data.setContent(entry.wireEncode()); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 165 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 166 | // Create an empty signature, since nobody going to verify it for now |
| 167 | // @todo In the future, we may require real signatures to do the registration |
| 168 | SignatureSha256WithRsa signature; |
| 169 | signature.setValue(Block(Tlv::SignatureValue)); |
| 170 | data.setSignature(signature); |
| 171 | |
| 172 | // Create an interest where the name has the encoded Data packet. |
| 173 | Name interestName; |
| 174 | interestName.append("ndnx"); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 175 | interestName.append(m_ndndId.value_begin() + 6, m_ndndId.value_end()); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 176 | interestName.append(entry.getAction()); |
| 177 | interestName.append(data.wireEncode()); |
| 178 | |
| 179 | Interest interest(interestName); |
| 180 | interest.setScope(1); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 181 | interest.setInterestLifetime(time::seconds(1)); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 182 | interest.setMustBeFresh(true); |
| 183 | |
| 184 | m_face.expressInterest(interest, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 185 | bind(&Controller::processPrefixActionResponse, this, _2, onSuccess, onFail), |
| 186 | bind(onFail, "Command Interest timed out")); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 190 | Controller::processFaceActionResponse(Data& data, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 191 | const FaceOperationSucceedCallback& onSuccess, |
| 192 | const FailCallback& onFail) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 193 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 194 | Block content = data.getContent(); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 195 | content.parse(); |
| 196 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 197 | if (content.elements().empty()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 198 | { |
| 199 | if (static_cast<bool>(onFail)) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 200 | onFail("Empty response"); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 204 | Block::element_const_iterator val = content.elements_begin(); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 205 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 206 | switch (val->type()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 207 | { |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 208 | case tlv::ndnd::FaceInstance: |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 209 | { |
| 210 | FaceInstance entry; |
| 211 | entry.wireDecode(*val); |
| 212 | |
| 213 | if (static_cast<bool>(onSuccess)) |
| 214 | onSuccess(entry); |
| 215 | return; |
| 216 | } |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 217 | case tlv::ndnd::StatusResponse: |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 218 | { |
| 219 | StatusResponse resp; |
| 220 | resp.wireDecode(*val); |
| 221 | |
| 222 | if (static_cast<bool>(onFail)) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 223 | onFail(resp.getInfo()); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | default: |
| 227 | { |
| 228 | if (static_cast<bool>(onFail)) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 229 | onFail("Invalid response"); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 236 | Controller::processPrefixActionResponse(Data& data, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 237 | const PrefixOperationSucceedCallback& onSuccess, |
| 238 | const FailCallback& onFail) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 239 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 240 | Block content = data.getContent(); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 241 | content.parse(); |
| 242 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 243 | if (content.elements().empty()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 244 | { |
| 245 | if (static_cast<bool>(onFail)) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 246 | onFail("Empty response"); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 250 | Block::element_const_iterator val = content.elements_begin(); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 251 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 252 | switch (val->type()) |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 253 | { |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 254 | case tlv::ndnd::ForwardingEntry: |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 255 | { |
| 256 | ForwardingEntry entry; |
| 257 | entry.wireDecode(*val); |
| 258 | |
| 259 | if (static_cast<bool>(onSuccess)) |
| 260 | onSuccess(entry); |
| 261 | return; |
| 262 | } |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 263 | case tlv::ndnd::StatusResponse: |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 264 | { |
| 265 | StatusResponse resp; |
| 266 | resp.wireDecode(*val); |
| 267 | |
| 268 | // std::cerr << "StatusReponse: " << resp << std::endl; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 269 | |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 270 | if (static_cast<bool>(onFail)) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 271 | onFail(resp.getInfo()); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | default: |
| 275 | { |
| 276 | if (static_cast<bool>(onFail)) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 277 | onFail("Invalid response"); |
Alexander Afanasyev | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | } // namespace ndnd |
| 284 | } // namespace ndn |