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