blob: 747f13a21bd53b4ba50e3787df610bea358f51d2 [file] [log] [blame]
Shock Jiang6cce21a2014-09-07 10:14:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yumin Xia918343d2017-03-17 19:04:55 -07003 * Copyright (c) 2014-2017, Regents of the University of California.
Shock Jiang6cce21a2014-09-07 10:14:12 -07004 *
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 Afanasyevaa46c272016-03-09 12:54:12 -080021
Shock Jiang6cce21a2014-09-07 10:14:12 -070022#include <ndn-cxx/data.hpp>
23#include <ndn-cxx/interest.hpp>
24
25namespace ndn {
26namespace ndns {
27namespace label {
28
29inline size_t
30calculateSkip(const Name& name,
Yumin Xia6343c5b2016-10-20 15:45:50 -070031 const Name& zone)
Shock Jiang6cce21a2014-09-07 10:14:12 -070032{
33 size_t skip = 0;
34
Yumin Xia6343c5b2016-10-20 15:45:50 -070035 skip = zone.size();
36 BOOST_ASSERT(name.size() > skip);
37 BOOST_ASSERT(name.getPrefix(zone.size()) == zone);
Shock Jiang6cce21a2014-09-07 10:14:12 -070038
Yumin Xia918343d2017-03-17 19:04:55 -070039 BOOST_ASSERT(name.get(skip) == NDNS_ITERATIVE_QUERY);
Shock Jiang6cce21a2014-09-07 10:14:12 -070040
41 ++skip;
42 return skip;
43}
44
45bool
46matchName(const Interest& interest,
Yumin Xia6343c5b2016-10-20 15:45:50 -070047 const Name& zone,
Shock Jiang6cce21a2014-09-07 10:14:12 -070048 MatchResult& result)
49{
Yumin Xia6343c5b2016-10-20 15:45:50 -070050 // zoneName / <Update>|rrLabel / UPDATE|rrType / [VERSION]
Shock Jiang6cce21a2014-09-07 10:14:12 -070051
52 const Name& name = interest.getName();
Yumin Xia6343c5b2016-10-20 15:45:50 -070053 size_t skip = calculateSkip(name, zone);
Shock Jiang6cce21a2014-09-07 10:14:12 -070054
55 if (name.size() - skip < 1)
56 return false;
57
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080058 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 Jiang6cce21a2014-09-07 10:14:12 -070069
70 return true;
71}
72
73bool
74matchName(const Data& data,
Yumin Xia6343c5b2016-10-20 15:45:50 -070075 const Name& zone,
Shock Jiang6cce21a2014-09-07 10:14:12 -070076 MatchResult& result)
77{
Yumin Xia6343c5b2016-10-20 15:45:50 -070078 // zoneName / <Update>|rrLabel / UPDATE|rrType
Shock Jiang6cce21a2014-09-07 10:14:12 -070079
80 const Name& name = data.getName();
Yumin Xia6343c5b2016-10-20 15:45:50 -070081 size_t skip = calculateSkip(name, zone);
Shock Jiang6cce21a2014-09-07 10:14:12 -070082
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