blob: 6e6428d55e618ba2c2b68f17577ba391f21c5a86 [file] [log] [blame]
shockjianga5ae48c2014-07-27 23:21:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
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#include "name-server.hpp"
20
21
22namespace ndn{
23namespace ndns{
24NameServer::NameServer(const char *programName, const char *prefix, const char *nameZone)
25: NDNApp(programName, prefix)
26, m_zone(Name(nameZone))
27, m_zoneMgr(m_zone)
28{
29 //m_zoneMgr.lookupId();
30}//NameServer Construction
31
32
33void
34NameServer::onInterest(const Name &name, const Interest &interest)
35{
36
37 cout<<"[* -> *] receive Interest: "<<interest.getName().toUri()<<std::endl;
38 Query query;
39 if (!query.fromInterest(interest))
40 {
41 cout<<"can resolve the Query from Interest: "<<endl;
42 return;
43 }
44
45 /*
46 * query.getAuthorityZone is routable name, not the zone's service name
47 if (query.getAuthorityZone() != m_zoneMgr.getZone().getAuthorizedName())
48 {
49 cout<<"Query is intent to zone: "<<query.getAuthorityZone()
50 <<". This is "<<m_zoneMgr.getZone().getAuthorizedName()<<endl;
51 return;
52 }
53 */
54
55 Response response;
56 Name name2 = interest.getName();
57 name2.appendVersion();
58 response.setQueryName(name2);
59 RRMgr mgr(m_zone, query, response);
60
61
62 if (mgr.lookup()<0)
63 {
64 cout<<"[* !! *] lookup error, then exit: "<<mgr.getErr()<<endl;
65 return;
66 }
67
68 if (response.getRrs().size() >0)
69 {
70 response.setResponseType(Response::NDNS_Resp);
71 } else {
72
73 if (query.getRrType() == RR::NS) {
74 int count = mgr.count();
75 if (count < 0)
76 {
77 cout<<"[* !! *] lookup error, then exit: "<<mgr.getErr()<<endl;
78 return;
79 } else if (count > 0)
80 {
81 response.setResponseType(Response::NDNS_Auth);
82 } else{
83 response.setResponseType(Response::NDNS_Nack);
84 }
85 } else {
86 response.setResponseType(Response::NDNS_Nack);
87 }
88 }
89
90
91 Data data = response.toData();
92 data.setFreshnessPeriod(response.getFreshness());
93
94 m_keyChain.sign(data);
95 m_face.put(data);
96 cout<<"[* <- *] send response: "<<response<<": "<<data<<endl;
97}//onInterest
98
99
100void
101NameServer::run()
102{
103 //m_zoneMgr.lookupId();
104 if (m_zoneMgr.getZone().getId() == 0)
105 {
106 m_hasError = true;
107 m_error = "cannot get Zone.id from database for name="+m_zone.getAuthorizedName().toUri();
108 stop();
109 }
110
111 boost::asio::signal_set signalSet(m_ioService, SIGINT, SIGTERM);
112 signalSet.async_wait(boost::bind(&NDNApp::signalHandler, this));
113 // boost::bind(&NdnTlvPingServer::signalHandler, this)
114 Name name;
115 name.set(m_prefix);
116 name.append(Query::toString(Query::QUERY_DNS));
117
118 m_face.setInterestFilter(name,
119 bind(&NameServer::onInterest,
120 this, _1, _2),
121 bind(&NDNApp::onRegisterFailed,
122 this, _1,_2));
123
124 std::cout << "\n=== NDNS Server for Zone "
125 << m_zoneMgr.getZone().getAuthorizedName().toUri()
126 <<" with routable prefix "<< name.toUri()
127 << " starts===\n" << std::endl;
128
129 try {
130 m_face.processEvents();
131 }
132 catch (std::exception& e) {
133 m_hasError = true;
134 m_error = "ERROR: ";
135 m_error += e.what();
136 stop();
137 }
138
139}//run
140
141} //namespace ndns
142} /* namespace ndn */
143