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