Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 8 | #include <stdexcept> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 9 | #include <ndn-cpp/common.hpp> |
| 10 | #include <ndn-cpp/interest.hpp> |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 11 | |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame^] | 12 | #include <cryptopp/osrng.h> |
| 13 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 14 | using namespace std; |
| 15 | |
| 16 | namespace ndn { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 17 | |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame^] | 18 | uint32_t |
| 19 | Interest::getNonce() const |
| 20 | { |
| 21 | static CryptoPP::AutoSeededRandomPool rng; |
| 22 | |
| 23 | if (nonce_ == 0) |
| 24 | nonce_ = rng.GenerateWord32(); |
| 25 | |
| 26 | return nonce_; |
| 27 | } |
| 28 | |
| 29 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 30 | bool |
| 31 | Interest::matchesName(const Name &name) const |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 32 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 33 | if (!name_.isPrefixOf(name)) |
| 34 | return false; |
| 35 | |
| 36 | if (minSuffixComponents_ >= 0 && |
| 37 | // Add 1 for the implicit digest. |
| 38 | !(name.size() + 1 - name_.size() >= minSuffixComponents_)) |
| 39 | return false; |
| 40 | |
| 41 | if (maxSuffixComponents_ >= 0 && |
| 42 | // Add 1 for the implicit digest. |
| 43 | !(name.size() + 1 - name_.size() <= maxSuffixComponents_)) |
| 44 | return false; |
| 45 | |
| 46 | if (!exclude_.empty() && name.size() > name_.size() && |
| 47 | exclude_.isExcluded(name[name_.size()])) |
| 48 | return false; |
| 49 | |
| 50 | return true; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 53 | std::ostream & |
| 54 | operator << (std::ostream &os, const Interest &interest) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 55 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 56 | os << interest.getName(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 58 | char delim = '?'; |
| 59 | |
| 60 | if (interest.getMinSuffixComponents() >= 0) { |
| 61 | os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| 62 | delim = '&'; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 63 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 64 | if (interest.getMaxSuffixComponents() >= 0) { |
| 65 | os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| 66 | delim = '&'; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 67 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 68 | if (interest.getChildSelector() >= 0) { |
| 69 | os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| 70 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 71 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 72 | if (interest.getMustBeFresh()) { |
| 73 | os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| 74 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 76 | if (interest.getScope() >= 0) { |
| 77 | os << delim << "ndn.Scope=" << interest.getScope(); |
| 78 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 79 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 80 | if (interest.getInterestLifetime() >= 0) { |
| 81 | os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime(); |
| 82 | delim = '&'; |
| 83 | } |
| 84 | |
| 85 | if (interest.getNonce() > 0) { |
| 86 | os << delim << "ndn.Nonce=" << interest.getNonce(); |
| 87 | delim = '&'; |
| 88 | } |
| 89 | if (!interest.getExclude().empty()) { |
| 90 | os << delim << "ndn.Exclude=" << interest.getExclude(); |
| 91 | delim = '&'; |
| 92 | } |
| 93 | |
| 94 | return os; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 97 | const Block& |
| 98 | Interest::wireEncode() const |
| 99 | { |
| 100 | if (wire_.hasWire()) |
| 101 | return wire_; |
| 102 | |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 103 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 104 | // Name |
| 105 | // Selectors? |
| 106 | // Nonce |
| 107 | // Scope? |
| 108 | // InterestLifetime? |
| 109 | |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 110 | wire_ = Block(Tlv::Interest); |
| 111 | wire_.push_back(getName().wireEncode()); |
| 112 | |
| 113 | // selectors |
| 114 | { |
| 115 | Block selectors(Tlv::Selectors); |
| 116 | |
| 117 | if (getMinSuffixComponents() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 118 | selectors.push_back |
| 119 | (nonNegativeIntegerBlock(Tlv::MinSuffixComponents, getMinSuffixComponents())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 120 | } |
| 121 | if (getMaxSuffixComponents() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 122 | selectors.push_back |
| 123 | (nonNegativeIntegerBlock(Tlv::MaxSuffixComponents, getMaxSuffixComponents())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 124 | } |
| 125 | if (!getExclude().empty()) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 126 | selectors.push_back |
| 127 | (getExclude().wireEncode()); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 128 | } |
| 129 | if (getChildSelector() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 130 | selectors.push_back |
| 131 | (nonNegativeIntegerBlock(Tlv::ChildSelector, getChildSelector())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 132 | } |
| 133 | if (getMustBeFresh()) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 134 | selectors.push_back |
| 135 | (booleanBlock(Tlv::MustBeFresh)); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame^] | 138 | if (!selectors.getAll().empty()) |
| 139 | { |
| 140 | selectors.encode(); |
| 141 | wire_.push_back(selectors); |
| 142 | } |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Nonce |
| 146 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 147 | wire_.push_back |
| 148 | (nonNegativeIntegerBlock(Tlv::Nonce, getNonce())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (getScope() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 152 | wire_.push_back |
| 153 | (nonNegativeIntegerBlock(Tlv::Scope, getScope())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 154 | } |
| 155 | if (getInterestLifetime() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 156 | wire_.push_back |
| 157 | (nonNegativeIntegerBlock(Tlv::InterestLifetime, getInterestLifetime())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | wire_.encode(); |
| 161 | return wire_; |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | Interest::wireDecode(const Block &wire) |
| 166 | { |
| 167 | wire_ = wire; |
| 168 | wire_.parse(); |
| 169 | |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 170 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 171 | // Name |
| 172 | // Selectors? |
| 173 | // Nonce |
| 174 | // Scope? |
| 175 | // InterestLifetime? |
| 176 | |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 177 | // Name |
| 178 | name_.wireDecode(wire_.get(Tlv::Name)); |
| 179 | |
| 180 | // Selectors |
| 181 | Block::element_iterator selectors = wire_.find(Tlv::Selectors); |
| 182 | if (selectors != wire_.getAll().end()) |
| 183 | { |
| 184 | selectors->parse(); |
| 185 | |
| 186 | // MinSuffixComponents |
| 187 | Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents); |
| 188 | if (val != selectors->getAll().end()) |
| 189 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 190 | minSuffixComponents_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // MaxSuffixComponents |
| 194 | val = selectors->find(Tlv::MaxSuffixComponents); |
| 195 | if (val != selectors->getAll().end()) |
| 196 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 197 | maxSuffixComponents_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Exclude |
| 201 | val = selectors->find(Tlv::Exclude); |
| 202 | if (val != selectors->getAll().end()) |
| 203 | { |
| 204 | exclude_.wireDecode(*val); |
| 205 | } |
| 206 | |
| 207 | // ChildSelector |
| 208 | val = selectors->find(Tlv::ChildSelector); |
| 209 | if (val != selectors->getAll().end()) |
| 210 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 211 | childSelector_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | //MustBeFresh aka AnswerOriginKind |
| 215 | val = selectors->find(Tlv::MustBeFresh); |
| 216 | if (val != selectors->getAll().end()) |
| 217 | { |
| 218 | mustBeFresh_ = true; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Nonce |
| 223 | Block::element_iterator val = wire_.find(Tlv::Nonce); |
| 224 | if (val != wire_.getAll().end()) |
| 225 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 226 | nonce_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Scope |
| 230 | val = wire_.find(Tlv::Scope); |
| 231 | if (val != wire_.getAll().end()) |
| 232 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 233 | scope_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // InterestLifetime |
| 237 | val = wire_.find(Tlv::InterestLifetime); |
| 238 | if (val != wire_.getAll().end()) |
| 239 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 240 | interestLifetime_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 245 | } |