[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r2720: Try to use client geo ip file. if not found use local. Signi (vidalia/branches/exit-country/src/vidalia/config)



Author: cviecco
Date: 2008-06-13 14:43:26 -0400 (Fri, 13 Jun 2008)
New Revision: 2720

Modified:
   vidalia/branches/exit-country/src/vidalia/config/filegeoipresolver.cpp
   vidalia/branches/exit-country/src/vidalia/config/filegeoipresolver.h
   vidalia/branches/exit-country/src/vidalia/config/networkoutpage.cpp
Log:
Try to use client geo ip file. if not found use local. Significant improvements in speed for geoip file loading.


Modified: vidalia/branches/exit-country/src/vidalia/config/filegeoipresolver.cpp
===================================================================
--- vidalia/branches/exit-country/src/vidalia/config/filegeoipresolver.cpp	2008-06-13 14:12:51 UTC (rev 2719)
+++ vidalia/branches/exit-country/src/vidalia/config/filegeoipresolver.cpp	2008-06-13 18:43:26 UTC (rev 2720)
@@ -15,61 +15,139 @@
 */
 
 //#include <torsocket.h>
-//#include <vidalia.h>
+#include <vidalia.h>
 #include "filegeoipresolver.h"
 #include "config.h"
 
+#include <time.h>
 
+#define GEOIP_FILENAME  (Vidalia::defaultDataDirectory() + "/geoip.data")
+
 /** Default constructor. */
 FileGeoIpResolver::FileGeoIpResolver()
 {
+  time_t start,end;
   ready=false;
-
-  QFile file(":/geoip/geoip.data");
-  QStringList list;  
   FileGeoIpResolverGeoLocation newlocation;
   FileGeoIpResolverIpRange newrange;
-    
-  fprintf(stderr,"starting filegeoip!\n");
  
-  if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
-      return;
+
+  start=time(NULL);
+
+  //start by attempting to read the user geoip file
+  FILE* file2;
+  file2=fopen(GEOIP_FILENAME.toAscii(),"r");
+  char tmpline[256];
+  char tmpline2[256];
+  char tmpline3[256];
+  int block_count=0;  
+  char *tok;
+
+  if(NULL!=file2){
+     //fprintf(stderr,"fopen, open!\n");
+     while (fgets(tmpline, 255, file2) != NULL) {
+          memcpy(tmpline2,tmpline,256);
+
+          if(3==sscanf(tmpline,"%u,%u,%s",&newrange.low_net,&newrange.high_net,tmpline3)){
+                if(0==block_count){
+                  ipRange.reserve(location.size()*location.size()/3);
+                }
+                block_count++;
+               //token = strtok(line, search);
+               //fprintf(stderr,"%d,%d,%s\n",newrange.low_net,newrange.high_net,tmpline2);   
+               //newrange.location=tmpline3;
+               QString newline(tmpline3);
+               //QString line2(tmpline2);
+               //QStringList list2(line2.split(","));
+                    //newrange.low_net=list2.at(0).toUInt();
+                    //newrange.high_net=list2.at(1).toUInt();
+                    //newrange.location=list2.at(2);
+                    newrange.location=newline;
+              
+ 
+               ipRange.append(newrange); 
+          }
+          else{
+                QString line2(tmpline2);
+                QStringList list2(line2.split(","));
+                switch(list2.size()){
+                      //beware of fallthrough
+                      default:
+                         newlocation.extra_info=list2.at(4);
+                      case 4: //is location;
+                         newlocation.country_short=list2.at(1);
+                         newlocation.latitude=list2.at(2).toFloat();
+                         newlocation.longitude=list2.at(3).toFloat();
+                         //fprintf(stderr,"%s",tmpline2);
+                         location.insert(list2.at(0),newlocation);
+                         break;
+                      case 0:
+                      case 1:
+                      case 2:
+                      case 3:
+                         break;
+
+                }
+          }
+          //QString line2(tmpline);
+          //QStringList list2(line2.split(','));
+
+      } //closes while
+      fclose(file2);
   }
+  else{
+    //user geoip file not found or cannot be opened
+    //use the default, built in file!
+     //QFile file(":geoip.data");
+     QFile file(":/geoip/geoip.data");
+     QString line;
+     QStringList list;
+     QTextStream in(&file);
+     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
+       fprintf (stderr,"cannot open geoip data file!\n");
+       return;
+     }
+     fprintf(stderr,"using default geoip file!\n");
+     while (!in.atEnd()) {
+         line = in.readLine();
+         list=line.split(",");
+         //list=in.readLine().split(',');
 
-  QTextStream in(&file);
-  while (!in.atEnd()) {
-         QString line = in.readLine();
-         //now validate... we are not validating at the moment, need to fix
-         list=line.split(",");
          switch (list.size()){
              case 0:
              case 1:
              case 2:
                     //invalid options
                     break;
+
              case 3:  //is range
                     newrange.low_net=list.at(0).toUInt();
                     newrange.high_net=list.at(1).toUInt();
                     newrange.location=list.at(2);
                     ipRange.append(newrange);
                     break;
-       
-             //now some magic, beware of breakless change
+
+             //now some magic, beware of fallthrough
              default:
                     newlocation.extra_info=list.at(4);
              case 4: //is location;
                     newlocation.country_short=list.at(1);
                     newlocation.latitude=list.at(2).toFloat();
-                    newlocation.longitude=list.at(3).toFloat(); 
+                    newlocation.longitude=list.at(3).toFloat();
                     location.insert(list.at(0),newlocation);
                     break;
+
          }
-         //process_line(line);
+
+     }//closes while
+
+
   }
-
+  
+ 
+  end=time(NULL);
   ready=true;
-  fprintf(stderr,"location_size=%d\n",location.size());
- 
+   
 }
 
 /** Sets the address and port of Tor, through which GeoIP requests will be
@@ -169,8 +247,9 @@
 
 QString 
 FileGeoIpResolver::get_country(QHostAddress ip){
+  QString unknown("??");
+  //return unknown;  
   GeoIp resolv=search(ip);
-  QString unknown("??");
    
   if (resolv.isEmpty() || resolv.isUnknown())
     return unknown;
@@ -220,9 +299,10 @@
         break;
      }
   }
-  if(-1==found)
+  if(-1==found){
+    //fprintf(stderr,"not found!\n");
     return invalid;
-
+  }
   
 
   //now get location

Modified: vidalia/branches/exit-country/src/vidalia/config/filegeoipresolver.h
===================================================================
--- vidalia/branches/exit-country/src/vidalia/config/filegeoipresolver.h	2008-06-13 14:12:51 UTC (rev 2719)
+++ vidalia/branches/exit-country/src/vidalia/config/filegeoipresolver.h	2008-06-13 18:43:26 UTC (rev 2720)
@@ -29,7 +29,7 @@
 #include <QMutex>
 #include <QMutexLocker>
 #include <QVector>
-
+#include <QVarLengthArray>
 #include <geoip.h>
 
 //#include "geoipcache.h"

Modified: vidalia/branches/exit-country/src/vidalia/config/networkoutpage.cpp
===================================================================
--- vidalia/branches/exit-country/src/vidalia/config/networkoutpage.cpp	2008-06-13 14:12:51 UTC (rev 2719)
+++ vidalia/branches/exit-country/src/vidalia/config/networkoutpage.cpp	2008-06-13 18:43:26 UTC (rev 2720)
@@ -160,6 +160,7 @@
   QString uniqueRouterName;
   quint32 torVersion = Vidalia::torControl()->getTorVersion();
 
+/*
   if (torVersion < 0x020100) {
     //ui.grpBridgeSettings->setEnabled(false);
     //ui.lblNoBridgeSupport->setVisible(true);
@@ -168,7 +169,8 @@
     ui.pButtonApplyCountry->setEnabled(false);
     return;
   }
- 
+*/ 
+
   /*Fill up the internal structure*/
   foreach( RouterStatus router, networkStatus){
      //fprintf(stderr,"*");