blob: ef6bbf1d8e5e0c12db064459f71634c70cf7832e [file] [log] [blame]
shockjiang99ad3892014-08-03 14:56:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
shockjiang85312022014-07-27 23:22:16 -07004 *
shockjiang99ad3892014-08-03 14:56:13 -07005 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
shockjiang85312022014-07-27 23:22:16 -07007 *
shockjiang99ad3892014-08-03 14:56:13 -07008 * 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/>.
shockjiang85312022014-07-27 23:22:16 -070018 */
19
shockjiang85312022014-07-27 23:22:16 -070020#include "app/name-dig.hpp"
21#include "boost/program_options.hpp"
22#include "boost/filesystem.hpp"
23
24using namespace ndn;
25using namespace ndn::ndns;
26using namespace std;
27
shockjiang85312022014-07-27 23:22:16 -070028
29int main(int argc, char * argv[])
30{
31 //char *programName, char *dstLabel, char *resolverName
32 //const char *programName = argv[0];
33 std::string appName = boost::filesystem::basename(argv[0]);
34
35 string dstLabel;
36 string resolver;
37 int waitingSeconds=10;
38 string rrType = "TXT";
39 int tryMax = 1;
40 try{
41
42 namespace po = boost::program_options;
43 po::variables_map vm;
44
shockjiang85312022014-07-27 23:22:16 -070045
46 po::options_description generic("Generic Options");
47 generic.add_options()
48 ("help,h", "print help message")
49 ;
50
51
52 po::options_description config("Configuration");
53 config.add_options()
54 ("waiting,w", po::value<int>(&waitingSeconds), "set waiting seconds for every Interest. default: 10")
shockjiang99ad3892014-08-03 14:56:13 -070055 ("rrtype,t", po::value<std::string>(&rrType), "set request RR Type. default: TXT")
shockjiang85312022014-07-27 23:22:16 -070056 ("trynum,n", po::value<int>(&tryMax), "set maximal Interest Tried Number. default: 2")
57 ;
58
59
60 po::options_description hidden("Hidden Options");
61 hidden.add_options()
shockjiang99ad3892014-08-03 14:56:13 -070062 ("name", po::value<string>(&dstLabel), "name to be resolved")
shockjiang85312022014-07-27 23:22:16 -070063 ("resolver", po::value<string>(&resolver), "routable prefix of resolver")
64 ;
65
66 po::positional_options_description postion;
67 postion.add("name", 1);
68 postion.add("resolver", 1);
69
70
71
72 po::options_description cmdline_options;
73 cmdline_options.add(generic).add(config).add(hidden);
74
75 po::options_description config_file_options;
76 config_file_options.add(config).add(hidden);
77
78 po::options_description visible("Allowed options");
79 visible.add(generic).add(config);
80
81
82 po::parsed_options parsed = po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run();
83
84
85 po::store(parsed, vm);
86 po::notify(vm);
87
88 if (vm.count("help"))
89 {
shockjiang99ad3892014-08-03 14:56:13 -070090 cout<<"E.g: dig /name/to/be/resolved /routable/prefix/of/resolver"<<endl;
shockjiang85312022014-07-27 23:22:16 -070091 cout<<visible<<endl;
92 return 0;
93 }
94
shockjiang99ad3892014-08-03 14:56:13 -070095 cout<<"name="<<dstLabel<<" resolver="<<resolver<<" waiting="<<waitingSeconds<<"s RRType="<<rrType<<" tryMax="<<tryMax<<endl;
shockjiang85312022014-07-27 23:22:16 -070096 }
97 catch(const std::exception& ex)
98 {
99 cout << "Parameter Error: " << ex.what() << endl;
shockjiang99ad3892014-08-03 14:56:13 -0700100 return 0;
shockjiang85312022014-07-27 23:22:16 -0700101 }
102 catch(...)
103 {
104 cout << "Parameter Unknown error" << endl;
shockjiang99ad3892014-08-03 14:56:13 -0700105 return 0;
shockjiang85312022014-07-27 23:22:16 -0700106 }
107
108
109 NameDig dig(argv[0], argv[1]);
110
111 dig.setResolverName(Name(resolver));
112
113 dig.setInterestTriedMax(tryMax);
114
115 dig.setRrType(RR::toRRType(rrType));
116
117 dig.run();
118
119 if (dig.hasError())
120 {
121 cout<<"\n\n"<<dig.getProgramName()<<" cannot find any records for Name"
122 <<dig.getDstLabel().toUri()<<" from resolver "
123 <<resolver<<". Due to:"<<std::endl;
124 cout<<"Error: "<<dig.getErr()<<endl;
125 } else
126 {
shockjiang99ad3892014-08-03 14:56:13 -0700127 Name re = dig.getResponse().getQueryName();
shockjiang85312022014-07-27 23:22:16 -0700128 if (dig.getRrs().size() == 0)
129 {
130 cout<<"Dig found no record(s) for Name "
131 <<dig.getDstLabel().toUri()<<std::endl;
shockjiang99ad3892014-08-03 14:56:13 -0700132 cout<<"Final Response Name: "<<re.toUri()<<std::endl;
shockjiang85312022014-07-27 23:22:16 -0700133 }
134 else {
135 cout<<"Success to the dig "<<dig.getDstLabel().toUri()<<" by Resolver "
136 <<resolver<<endl;
137
138 vector<RR> rrs = dig.getRrs();
139 vector<RR>::const_iterator iter = rrs.begin();
140 while (iter != rrs.end())
141 {
142 cout<<" "<<*iter<<"\n";
143 iter ++;
144 }
shockjiang99ad3892014-08-03 14:56:13 -0700145 cout<<"Final Response Name: "<<re.toUri()<<std::endl;
shockjiang85312022014-07-27 23:22:16 -0700146 }
147 }
148
149}
150