#include <memory>
#include "mind.h"
#include "dnsconfig.h"
#include "mindlog.h"
#include "rrcache.h"
#include "wwwi/configfile.h"
using std::auto_ptr;
using std::vector;
using WWWI::ConfigFile;
DNSConfig::DNSConfig() {
m_bDaemon = true;
m_bHaveRoot = false;
m_usUDPClientPort = 53;
m_ipUDPClientInterface = INADDR_ANY;
}
void DNSConfig::AddDefaultRootServers() {
RRCachePtr cap = GetSingleton<RRCache>();
#ifdef USE_ICANN
if (m_bHaveRoot==true)
throw ConfigEx("Configuration Error: two sets of root servers");
cap->AddRootServer("a.root-servers.net","198.41.0.4");
cap->AddRootServer("b.root-servers.net","128.9.0.107");
cap->AddRootServer("c.root-servers.net","192.33.4.12");
cap->AddRootServer("d.root-servers.net","128.8.10.90");
cap->AddRootServer("e.root-servers.net","192.203.230.10");
cap->AddRootServer("f.root-servers.net","192.5.5.241");
cap->AddRootServer("g.root-servers.net","192.112.36.4");
cap->AddRootServer("h.root-servers.net","128.63.2.53");
cap->AddRootServer("i.root-servers.net","192.36.148.17");
m_bHaveRoot = true;
#endif
#ifdef USE_OPENNIC
if (m_bHaveRoot==true)
throw ConfigEx("Configuration Error: two sets of root servers");
cap->AddRootServer("ns0.opennic.glue.","131.161.247.226");
cap->AddRootServer("ns1.opennic.glue.","157.238.46.24");
cap->AddRootServer("ns10.opennic.glue.","216.178.136.117");
cap->AddRootServer("ns11.opennic.glue.","216.178.136.116");
cap->AddRootServer("ns2.opennic.glue.","209.104.33.250");
cap->AddRootServer("ns3.opennic.glue.","209.104.63.249");
cap->AddRootServer("ns4.opennic.glue.","130.94.168.216");
cap->AddRootServer("ns5.opennic.glue.","131.161.247.228");
cap->AddRootServer("ns6.opennic.glue.","199.175.137.211");
cap->AddRootServer("ns7.opennic.glue.","207.6.128.246");
cap->AddRootServer("ns8.opennic.glue.","167.216.255.199");
m_bHaveRoot = true;
#endif
}
void DNSConfig::Configure(int i_iArgs, char **i_cppArgs) {
RRCachePtr cap;
MINDLogPtr lgp;
auto_ptr<char> strFileName;
bool bHaveConfigFile = false;
int i;
lgp = GetSingleton<MINDLog>();
lgp->SetPriority(LY_INFO);
cap = GetSingleton<RRCache>();
for(i=1;i<i_iArgs;i++) {
if ((i_cppArgs[i][0]!='-') || (i_cppArgs[i][1]==0))
throw ConfigEx(FOUT("Bad command line parameter: ",i_cppArgs[i]));
switch(i_cppArgs[i][1]) {
case 'd': m_bDaemon = false; break;
case 'c':
if (strlen(i_cppArgs[i])>2)
strFileName = StringDuplicate(i_cppArgs[i]+2);
else if ((i+1)<i_iArgs)
strFileName = StringDuplicate(i_cppArgs[++i]);
else
throw ConfigEx("Bad option usage: -l requires a filename");
bHaveConfigFile = true;
break;
case 'l':
char *strLocalHost;
if (strlen(i_cppArgs[i])>2)
strLocalHost = i_cppArgs[i]+2;
else if ((i+1)<i_iArgs)
strLocalHost = i_cppArgs[++i];
else
throw ConfigEx("Bad option usage: -l requires a hostname");
cap->AddRecord(strLocalHost,TY_A,CL_IN,1000000,"127.0.0.1");
cap->AddRecord("1.0.0.127.in-addr.arpa",TY_PTR,CL_IN,1000000,strLocalHost);
break;
default:
throw ConfigEx(FOUT("Bad option: ",i_cppArgs[i]));
}
}
if (bHaveConfigFile==true) {
ConfigFile cf(strFileName.get());
cf.Load();
if (cf.Test("UDPClientInterface")==true) {
char strUDPClientInterface[32];
cf.Get(strUDPClientInterface,32,"UDPClientInterface");
m_ipUDPClientInterface = NTOH(inet_addr(strUDPClientInterface));
if (m_ipUDPClientInterface==(IPAddress)-1)
throw ConfigEx(FOUT("Configuration Error: UDPClientInterface value is invalid: ",strUDPClientInterface));
}
if (cf.Test("UDPClientPort")==true)
cf.Get(m_usUDPClientPort,"UDPClientPort");
if (cf.Test("RootServer")==true) {
vector<string> xtrv = cf.GetAll("RootServer");
vector<string>::iterator xtri;
string xtrIP;
size_t sz;
for(xtri=xtrv.begin(); xtri!=xtrv.end(); ++xtri) {
sz = (*xtri).find_first_of(':');
if (sz==string::npos)
throw ConfigEx(FOUT("Configuration Error: line ",(*xtri)," is not in the correct format."));
xtrIP = (*xtri).substr(sz+1);
(*xtri).erase(sz);
cap->AddRootServer((*xtri).c_str(),xtrIP.c_str());
}
m_bHaveRoot = true;
}
if (cf.Test("LocalHost")==true) {
char *strLocalHost;
cf.Get(strLocalHost,"LocalHost");
cap->AddRecord(strLocalHost,TY_A,CL_IN,1000000,"127.0.0.1");
cap->AddRecord("1.0.0.127.in-addr.arpa",TY_PTR,CL_IN,1000000,strLocalHost);
delete[] strLocalHost;
}
}
if (m_bHaveRoot==false) {
this->AddDefaultRootServers();
if (m_bHaveRoot==false)
throw ConfigEx("Configuration Error: no root servers configured!");
}
if (m_bDaemon==true)
lgp->SetMethod(LM_SYSLOG);
}