00001
00007 #include <fstream>
00008 #include <stdlib.h>
00009 #include <boost/bind.hpp>
00010 #include <boost/thread/thread.hpp>
00011 #include <readline/readline.h>
00012 #include <readline/history.h>
00013 #include "include/Cli.h"
00014 #include "include/generic.h"
00015
00016 using namespace std;
00017 using namespace boost;
00018
00019
00020 Cli::Cli(Configuration cfg, NetSniff *netsnf)
00021 {
00022
00023 cout << endl << "VoIPHoney CLI interface ...." << endl << endl;
00024 prompt = "VoIPHoney@" + cfg.GetHostname() + ":CLI> ";
00025 hcmdpath = cfg.GetValue("helpcmdpath");
00026
00027 this->netsnf = netsnf;
00028 }
00029 bool Cli::SetDebugger(Debug *debugger)
00030 {
00031 this->dbg = debugger;
00032 if (this->dbg != NULL)
00033 return true;
00034 else
00035 return false;
00036 }
00037
00038 void Cli::MainLoop ()
00039 {
00040
00041 char *buf;
00042 string cmd;
00043 string validcommands = "debug test module help verbosity exit sniffer ";
00044
00045 if (chdir (hcmdpath.c_str()) == -1)
00046 {
00047 cerr << "Error loading commands help on " << hcmdpath.c_str() << endl;
00048 }
00049
00050 while((buf = readline(prompt.c_str()))!=NULL)
00051 {
00052 cmd = buf;
00053 if (cmd.length() > 0)
00054 {
00055
00056 unsigned int len = cmd.length();
00057 unsigned int last = cmd.find_last_of(" ");
00058
00059
00060 if (last == len - 1)
00061 cmd.erase(last);
00062
00063 string keyword, args;
00064 SplitStr(cmd, &keyword, &args);
00065
00066 if (validcommands.find(keyword + " ") != string::npos)
00067 ProcessCommand(keyword, args);
00068 else
00069 {
00070
00071 cout << " Unknown command: " << FORE_RED << cmd.c_str() << ENDCOLOR << endl << endl;
00072 cout << "Type help to display a command list" << endl;
00073 cout << "Or type help [" << FORE_YELLOW << "command" << ENDCOLOR << "] for help on a specific command" << endl;
00074
00075 cout << endl;
00076 }
00077
00078 if (strcmp(buf,"exit")==0 || strcmp(buf,"exit ")==0)
00079 break;
00080 if (buf[0]!=0)
00081 add_history(buf);
00082
00083 }
00084
00085 free(buf);
00086 }
00087
00088 }
00089
00090 void Cli::ProcessCommand(string cmd, string args)
00091 {
00092 if (cmd.compare("help") == 0)
00093 DisplayHelp(args);
00094 else if (cmd.compare("debug") == 0)
00095 DebugCmd(args);
00096 else if (cmd.compare("verbosity") == 0)
00097 VerbosityCmd(args);
00098 else if (cmd.compare("sniffer") == 0)
00099 SnifferCmd(args);
00100 }
00101
00102 void Cli::DebugCmd(string args)
00103 {
00104 if (args.compare("show") == 0)
00105 {
00106 cout << "Debugging is set to: [";
00107 if (dbg->isDebugging())
00108 cout << "yes";
00109 else
00110 cout << "no";
00111 cout << "]" << endl;
00112 }
00113 else
00114 {
00115 string keyword, carg;
00116 SplitStr(args, &keyword, &carg);
00117 if (keyword.compare("set") == 0)
00118 {
00119 if (carg.compare("yes") == 0)
00120 dbg->StartDebug();
00121 else if (carg.compare("no") == 0)
00122 dbg->StopDebug();
00123 else
00124 cout << "Unknow value " << carg << " for setting debug" << endl;
00125
00126 }
00127 else
00128 cout << "Sorry: debug " << args.c_str() << " not possible" << endl;
00129 }
00130 }
00131
00132 void Cli::VerbosityCmd(string args)
00133 {
00134 if (args.compare("show") == 0)
00135 {
00136 cout << "Verbosity is set to: [" << dbg->GetVerbosity().c_str();
00137 cout << "]" << endl;
00138 }
00139 else
00140 {
00141 string keyword, carg;
00142 SplitStr(args, &keyword, &carg);
00143 if (keyword.compare("set") == 0)
00144 {
00145 int verb = -1;
00146 istringstream buffer(carg);
00147 buffer >> verb;
00148 if (verb >=0 && verb <= 100)
00149 {
00150 dbg->SetVerbosity(carg);
00151 cout << "Verbosity is now " << carg.c_str() << endl;
00152 }
00153 else
00154 cout << "Verbosity value must be between 0 and 100" << endl;
00155
00156 }
00157 else
00158 cout << "Sorry: verbosity " << args.c_str() << " not possible" << endl;
00159 }
00160 }
00161
00162 void Cli::SnifferCmd(string args)
00163 {
00164 if (args.compare("show") == 0)
00165 {
00166 if (netsnf->isReady())
00167 {
00168 cout << "Sniffer " << FORE_GREEN << "READY " << ENDCOLOR;
00169 if (netsnf->isSniffing())
00170 cout << "AND " << FORE_GREEN << "SNIFFING" << ENDCOLOR;
00171 else
00172 cout << "AND " << FORE_RED "NOT SNIFFING" << ENDCOLOR;
00173 cout << endl << netsnf->GetFullStatus();
00174 }
00175 else
00176 cout << "Sniffer " << FORE_RED << "NOT READY" << ENDCOLOR;
00177
00178 cout << endl;
00179 }
00180 else if (args.compare("start") == 0)
00181 {
00182
00183 thread* sniffThread = new thread(bind(&network::NetSniff::startsniff,netsnf));
00184 if (sniffThread == NULL)
00185 dbg->Prnterr(CODEAT, "Error creating thread");
00186 }
00187 else if (args.compare("stop") == 0)
00188 {
00189 netsnf->stopsniff();
00190 }
00191 else
00192 cout << "Sorry: sniffer " << args.c_str() << " not possible" << endl;
00193 }
00194
00195 void Cli::SplitStr(string str, string *keyword, string *args)
00196 {
00197
00198 unsigned int first_white = str.find_first_of(" ");
00199 *keyword = str.substr(0,first_white);
00200 if (first_white > str.length())
00201 *args = "";
00202 else
00203 *args = str.substr(first_white + 1, str.length()-1);
00204 }
00205
00206 void Cli::DisplayHelp(string args)
00207 {
00208 int content_len;
00209 char * content;
00210 cout << endl << "-----------------------------------------------------------------------" << endl;
00211 if (args.length() > 1)
00212 cout << "Help on topic " << args.c_str();
00213 else
00214 {
00215 cout << "Main Help ";
00216 args = "help";
00217 }
00218 cout << endl << "-----------------------------------------------------------------------" << endl;
00219
00220
00221 ifstream is;
00222
00223 is.open (args.c_str(), ios::binary );
00224
00225 if (is.fail())
00226 {
00227 cout << "Error loading help for " << args.c_str() << endl;
00228 return;
00229 }
00230
00231
00232 is.seekg (0, ios::end);
00233 content_len = is.tellg();
00234 is.seekg (0, ios::beg);
00235
00236
00237 try {
00238 content = new char [content_len];
00239
00240 is.read (content, content_len);
00241 is.close();
00242
00243 cout.write(content,content_len);
00244 cout << endl << "-----------------------------------------------------------------------" << endl;
00245
00246
00247 delete[] content;
00248 }
00249 catch (std::bad_alloc&)
00250 {
00251 dbg->Prnterr(CODEAT, "Error allocating memory");
00252 }
00253
00254 }
00255
00256 Cli::~Cli()
00257 {
00258 dbg->Prntdbg(CODEAT, 20, "Finish CLI ...");
00259 }