1: 
   2: 
   3: /**********************
   4: *                     *
   5: *  COMPILER INCLUDES  *
   6: *                     *
   7: **********************/
   8: 
   9: 
  10: #include <memory> 
  11: 
  12: 
  13: /*********************
  14: *                    *
  15: *  PROJECT INCLUDES  *
  16: *                    *
  17: *********************/
  18: 
  19: 
  20: #include "mind.h" 
  21: #include "dnsconfig.h" 
  22: #include "mindlog.h" 
  23: #include "rrcache.h" 
  24: #include "wwwi/configfile.h" 
  25: 
  26: 
  27: using std::auto_ptr;
  28: using std::vector;
  29: using WWWI::ConfigFile;
  30: 
  31: 
  32: /**************************
  33: *                         *
  34: *  DNSCONFIG CONSTRUCTOR  *
  35: *                         *
  36: **************************/
  37: 
  38: 
  39: DNSConfig::DNSConfig() {
  40:   m_bDaemon = true;
  41:   m_bHaveRoot = false;
  42:   m_usUDPClientPort = 53;
  43:   m_ipUDPClientInterface = INADDR_ANY;
  44: }
  45: 
  46: 
  47: /*********************************
  48: *                                *
  49: *  METHOD ADDDEFAULTROOTSERVERS  *
  50: *                                *
  51: *********************************/
  52: 
  53: 
  54: void DNSConfig::AddDefaultRootServers() {
  55:   RRCachePtr cap = GetSingleton<RRCache>();
  56: #ifdef USE_ICANN 
  57:   if (m_bHaveRoot==true) 
  58:     throw ConfigEx("Configuration Error: two sets of root servers");
  59:   cap->AddRootServer("a.root-servers.net","198.41.0.4");
  60:   cap->AddRootServer("b.root-servers.net","128.9.0.107");
  61:   cap->AddRootServer("c.root-servers.net","192.33.4.12");
  62:   cap->AddRootServer("d.root-servers.net","128.8.10.90");
  63:   cap->AddRootServer("e.root-servers.net","192.203.230.10");      
  64:   cap->AddRootServer("f.root-servers.net","192.5.5.241");
  65:   cap->AddRootServer("g.root-servers.net","192.112.36.4");
  66:   cap->AddRootServer("h.root-servers.net","128.63.2.53");
  67:   cap->AddRootServer("i.root-servers.net","192.36.148.17");
  68:   m_bHaveRoot = true;
  69: #endif 
  70: #ifdef USE_OPENNIC 
  71:   if (m_bHaveRoot==true) 
  72:     throw ConfigEx("Configuration Error: two sets of root servers");
  73:   cap->AddRootServer("ns0.opennic.glue.","131.161.247.226");
  74:   cap->AddRootServer("ns1.opennic.glue.","157.238.46.24");
  75:   cap->AddRootServer("ns10.opennic.glue.","216.178.136.117");
  76:   cap->AddRootServer("ns11.opennic.glue.","216.178.136.116");
  77:   cap->AddRootServer("ns2.opennic.glue.","209.104.33.250");
  78:   cap->AddRootServer("ns3.opennic.glue.","209.104.63.249");
  79:   cap->AddRootServer("ns4.opennic.glue.","130.94.168.216");
  80:   cap->AddRootServer("ns5.opennic.glue.","131.161.247.228");
  81:   cap->AddRootServer("ns6.opennic.glue.","199.175.137.211");
  82:   cap->AddRootServer("ns7.opennic.glue.","207.6.128.246");
  83:   cap->AddRootServer("ns8.opennic.glue.","167.216.255.199");
  84:   m_bHaveRoot = true;
  85: #endif 
  86: }
  87: 
  88: 
  89: /*********************
  90: *                    *
  91: *  METHOD CONFIGURE  *
  92: *                    *
  93: *********************/
  94: 
  95: 
  96: void DNSConfig::Configure(int i_iArgs, char **i_cppArgs) {
  97:   RRCachePtr cap;
  98:   MINDLogPtr lgp;
  99:   auto_ptr<char> strFileName;
 100:   bool bHaveConfigFile = false;
 101:   int i;
 102: 
 103:   lgp = GetSingleton<MINDLog>();
 104:   lgp->SetPriority(LY_INFO);
 105: //  lgp->SetPriority(LF_RESOLVER,LY_DEBUG);
 106: //  lgp->SetPriority(LF_CACHE,LY_DEBUG);
 107: //  lgp->SetPriority(LF_CEXPIRE,LY_ERROR);
 108: //  lgp->SetPriority(LF_OBJNEWDEL,LY_DEBUG);
 109: //  lgp->SetPriority(LF_THREADS,LY_EXTRA_DEBUG);
 110: //  lgp->SetPriority(LF_MISC,LY_DEBUG);
 111: //  lgp->SetPriority(LF_UDP,LY_DEBUG);
 112: 
 113:   cap = GetSingleton<RRCache>();
 114: 
 115:   for(i=1;i<i_iArgs;i++) {
 116:     if ((i_cppArgs[i][0]!='-') || (i_cppArgs[i][1]==0))
 117:       throw ConfigEx(FOUT("Bad command line parameter: ",i_cppArgs[i]));
 118:     switch(i_cppArgs[i][1]) {
 119:     case 'd': m_bDaemon = false; break;
 120:     case 'c':
 121:       if (strlen(i_cppArgs[i])>2) 
 122:         strFileName = StringDuplicate(i_cppArgs[i]+2);
 123:       else if ((i+1)<i_iArgs)
 124:         strFileName = StringDuplicate(i_cppArgs[++i]);
 125:       else
 126:         throw ConfigEx("Bad option usage: -l requires a filename");
 127:       bHaveConfigFile = true;
 128:       break;
 129:     case 'l':
 130:       char *strLocalHost;
 131:       if (strlen(i_cppArgs[i])>2) 
 132:         strLocalHost = i_cppArgs[i]+2;
 133:       else if ((i+1)<i_iArgs)
 134:         strLocalHost = i_cppArgs[++i];
 135:       else
 136:         throw ConfigEx("Bad option usage: -l requires a hostname");
 137:       cap->AddRecord(strLocalHost,TY_A,CL_IN,1000000,"127.0.0.1");
 138:       cap->AddRecord("1.0.0.127.in-addr.arpa",TY_PTR,CL_IN,1000000,strLocalHost);
 139:       break;
 140:     default:
 141:       throw ConfigEx(FOUT("Bad option: ",i_cppArgs[i]));
 142:     }
 143:   }
 144:   
 145:   if (bHaveConfigFile==true) {
 146:     ConfigFile cf(strFileName.get());
 147:     cf.Load();
 148:     if (cf.Test("UDPClientInterface")==true) {
 149:       char strUDPClientInterface[32];
 150:       cf.Get(strUDPClientInterface,32,"UDPClientInterface");
 151:       m_ipUDPClientInterface = NTOH(inet_addr(strUDPClientInterface));
 152:       if (m_ipUDPClientInterface==(IPAddress)-1) 
 153:         throw ConfigEx(FOUT("Configuration Error: UDPClientInterface value is invalid: ",strUDPClientInterface));
 154:     }
 155:     if (cf.Test("UDPClientPort")==true) 
 156:       cf.Get(m_usUDPClientPort,"UDPClientPort");
 157:     if (cf.Test("RootServer")==true) {
 158:       vector<string> xtrv = cf.GetAll("RootServer");
 159:       vector<string>::iterator xtri; 
 160:       string xtrIP;
 161:       size_t sz;
 162:       for(xtri=xtrv.begin(); xtri!=xtrv.end(); ++xtri) {
 163:         sz = (*xtri).find_first_of(':');
 164:         if (sz==string::npos) 
 165:           throw ConfigEx(FOUT("Configuration Error: line ",(*xtri)," is not in the correct format."));
 166:         xtrIP = (*xtri).substr(sz+1);
 167:         (*xtri).erase(sz);
 168:         cap->AddRootServer((*xtri).c_str(),xtrIP.c_str());
 169:       }
 170:       m_bHaveRoot = true;
 171:     } 
 172:     if (cf.Test("LocalHost")==true) {
 173:       char *strLocalHost;
 174:       cf.Get(strLocalHost,"LocalHost");
 175:       cap->AddRecord(strLocalHost,TY_A,CL_IN,1000000,"127.0.0.1");
 176:       cap->AddRecord("1.0.0.127.in-addr.arpa",TY_PTR,CL_IN,1000000,strLocalHost);
 177:       delete[] strLocalHost;
 178:     }
 179:   }
 180:     
 181:   if (m_bHaveRoot==false) {
 182:     this->AddDefaultRootServers();
 183:     if (m_bHaveRoot==false)  
 184:       throw ConfigEx("Configuration Error: no root servers configured!");
 185:   }
 186: 
 187:   if (m_bDaemon==true) 
 188:     lgp->SetMethod(LM_SYSLOG);
 189: 
 190:     
 191: }
 192: 
 193: 
 194: