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