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 | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, 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 | |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 39 | BOOST_ASSERT(name.get(skip) == NDNS_ITERATIVE_QUERY || |
| 40 | name.get(skip) == NDNS_CERT_QUERY); |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 41 | |
| 42 | ++skip; |
| 43 | return skip; |
| 44 | } |
| 45 | |
| 46 | bool |
| 47 | matchName(const Interest& interest, |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame^] | 48 | const Name& zone, |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 49 | MatchResult& result) |
| 50 | { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame^] | 51 | // zoneName / <Update>|rrLabel / UPDATE|rrType / [VERSION] |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 52 | |
| 53 | const Name& name = interest.getName(); |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame^] | 54 | size_t skip = calculateSkip(name, zone); |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 55 | |
| 56 | if (name.size() - skip < 1) |
| 57 | return false; |
| 58 | |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 59 | size_t offset = 1; |
| 60 | if (name.get(-offset).isVersion()) { |
| 61 | result.version = name.get(-offset); |
| 62 | ++offset; |
| 63 | } |
| 64 | else { |
| 65 | result.version = name::Component(); |
| 66 | } |
| 67 | |
| 68 | result.rrType = name.get(-offset); |
| 69 | 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] | 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | bool |
| 75 | matchName(const Data& data, |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame^] | 76 | const Name& zone, |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 77 | MatchResult& result) |
| 78 | { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame^] | 79 | // zoneName / <Update>|rrLabel / UPDATE|rrType |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 80 | |
| 81 | const Name& name = data.getName(); |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame^] | 82 | size_t skip = calculateSkip(name, zone); |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 83 | |
| 84 | if (name.size() - skip < 2) |
| 85 | return false; |
| 86 | |
| 87 | result.version = name.get(-1); |
| 88 | result.rrType = name.get(-2); |
| 89 | result.rrLabel = name.getSubName(skip, std::max<size_t>(0, name.size() - skip - 2)); |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | } // namespace label |
| 95 | } // namespace ndns |
| 96 | } // namespace ndn |