Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "ndns-label.hpp" |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 21 | |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 22 | #include <ndn-cxx/data.hpp> |
| 23 | #include <ndn-cxx/interest.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndns { |
| 27 | namespace label { |
| 28 | |
| 29 | inline size_t |
| 30 | calculateSkip(const Name& name, |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 31 | const Name& zone) |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 32 | { |
| 33 | size_t skip = 0; |
| 34 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 35 | skip = zone.size(); |
| 36 | BOOST_ASSERT(name.size() > skip); |
| 37 | BOOST_ASSERT(name.getPrefix(zone.size()) == zone); |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 38 | |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 39 | BOOST_ASSERT(name.get(skip) == NDNS_ITERATIVE_QUERY); |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 40 | |
| 41 | ++skip; |
| 42 | return skip; |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | matchName(const Interest& interest, |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 47 | const Name& zone, |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 48 | MatchResult& result) |
| 49 | { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 50 | // zoneName / <Update>|rrLabel / UPDATE|rrType / [VERSION] |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 51 | |
| 52 | const Name& name = interest.getName(); |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 53 | size_t skip = calculateSkip(name, zone); |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 54 | |
| 55 | if (name.size() - skip < 1) |
| 56 | return false; |
| 57 | |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 58 | size_t offset = 1; |
| 59 | if (name.get(-offset).isVersion()) { |
| 60 | result.version = name.get(-offset); |
| 61 | ++offset; |
| 62 | } |
| 63 | else { |
| 64 | result.version = name::Component(); |
| 65 | } |
| 66 | |
| 67 | result.rrType = name.get(-offset); |
| 68 | result.rrLabel = name.getSubName(skip, std::max<size_t>(0, name.size() - skip - offset)); |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool |
| 74 | matchName(const Data& data, |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 75 | const Name& zone, |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 76 | MatchResult& result) |
| 77 | { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 78 | // zoneName / <Update>|rrLabel / UPDATE|rrType |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 79 | |
| 80 | const Name& name = data.getName(); |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 81 | size_t skip = calculateSkip(name, zone); |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 82 | |
| 83 | if (name.size() - skip < 2) |
| 84 | return false; |
| 85 | |
| 86 | result.version = name.get(-1); |
| 87 | result.rrType = name.get(-2); |
| 88 | result.rrLabel = name.getSubName(skip, std::max<size_t>(0, name.size() - skip - 2)); |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | } // namespace label |
| 94 | } // namespace ndns |
| 95 | } // namespace ndn |