Permalink
Browse files

Lock lookup methods to be thread safe; update fips codes

  • Loading branch information...
1 parent e475dc3 commit 84032f9b6dc71650e52020ab91f326d0f518dacd @borisz borisz committed Jul 23, 2009
Showing with 58 additions and 33 deletions.
  1. +2 −0 ChangeLog
  2. +17 −11 LookupService.cs
  3. +39 −22 RegionName.cs
View
2 ChangeLog
@@ -1,3 +1,5 @@
+ - Sync RegionName.cs with more recent FIPS codes ( 20090723 ). ( Boris Zentner )
+ - Guard most functions in lookupService by either lock or [MethodImpl(MethodImplOptions.Synchronized)] ( Kfir Harel )
1.11 May 16th, 2009
- Fix IndexOutOfRangeException error when a lookup end in the last valid
netblock using GEOIP_MEMORY_CACHE mode ( Boris Zentner )
View
28 LookupService.cs
@@ -22,6 +22,8 @@
using System;
using System.IO;
using System.Net;
+using System.Runtime.CompilerServices;
+
public class LookupService{
private FileStream file = null;
private DatabaseInfo databaseInfo = null;
@@ -137,7 +139,7 @@ public class LookupService{
this.file = new FileStream(databaseFile, FileMode.Open, FileAccess.Read);
dboptions = options;
init();
- } catch(System.SystemException e) {
+ } catch(System.SystemException) {
Console.Write("cannot open file " + databaseFile + "\n");
}
}
@@ -217,7 +219,7 @@ public class LookupService{
file.Close();
file = null;
}
- catch (Exception e) { }
+ catch (Exception) { }
}
public Country getCountry(IPAddress ipAddress) {
return getCountry(bytestoLong(ipAddress.GetAddressBytes()));
@@ -291,11 +293,10 @@ public class LookupService{
}
try {
// Synchronize since we're accessing the database file.
- //synchronized (this) {
+ lock (this) {
bool hasStructureInfo = false;
byte [] delim = new byte[3];
// Advance to part of file where database info is stored.
- //file.Seek(file.length() - 3);
file.Seek(-3,SeekOrigin.End);
for (int i=0; i<STRUCTURE_INFO_MAX_SIZE; i++) {
file.Read(delim,0,3);
@@ -305,12 +306,10 @@ public class LookupService{
}
}
if (hasStructureInfo) {
- //file.Seek(file.getFilePointer() - 3);
file.Seek(-3,SeekOrigin.Current);
}
else {
// No structure info, must be pre Sep 2002 database, go back to end.
- //file.Seek(file.length() - 3);
file.Seek(-3,SeekOrigin.End);
}
// Find the database info string.
@@ -327,10 +326,9 @@ public class LookupService{
this.databaseInfo = new DatabaseInfo(new String(dbInfo2));
return databaseInfo;
}
- //file.Seek(file.getFilePointer() -4);
file.Seek(-4,SeekOrigin.Current);
}
- //}
+ }
}
catch (Exception e) {
Console.Write(e.Message);
@@ -353,6 +351,8 @@ public class LookupService{
return getRegion(bytestoLong(addr.GetAddressBytes()));
}
+
+ [MethodImpl(MethodImplOptions.Synchronized)]
public Region getRegion(long ipnum){
Region record = new Region();
int seek_region = 0;
@@ -412,6 +412,8 @@ public class LookupService{
return getLocation(bytestoLong(addr.GetAddressBytes()));
}
+
+ [MethodImpl(MethodImplOptions.Synchronized)]
public Location getLocation(long ipnum){
int record_pointer;
byte[] record_buf = new byte[FULL_RECORD_LENGTH];
@@ -496,7 +498,7 @@ public class LookupService{
}
}
}
- catch (IOException e) {
+ catch (IOException) {
Console.Write("IO Exception while seting up segments");
}
return record;
@@ -516,6 +518,8 @@ public class LookupService{
}
return getOrg(bytestoLong(addr.GetAddressBytes()));
}
+
+ [MethodImpl(MethodImplOptions.Synchronized)]
public String getOrg(long ipnum){
int Seek_org;
int record_pointer;
@@ -545,11 +549,13 @@ public class LookupService{
org_buf = new String(buf2,0,str_length);
return org_buf;
}
- catch (IOException e) {
+ catch (IOException) {
Console.Write("IO Exception");
return null;
}
}
+
+ [MethodImpl(MethodImplOptions.Synchronized)]
private int SeekCountry(long ipAddress){
byte [] buf = new byte[2 * MAX_RECORD_LENGTH];
int [] x = new int[2];
@@ -565,7 +571,7 @@ public class LookupService{
file.Read(buf,0,2 * MAX_RECORD_LENGTH);
}
}
- catch (IOException e) {
+ catch (IOException) {
Console.Write("IO Exception");
}
for (int i = 0; i<2; i++) {
View
61 RegionName.cs
@@ -2047,6 +2047,7 @@ public static class RegionName
cc_reg_IS.Add( "23", "Rangarvallasysla" );
cc_reg_IS.Add( "28", "Skagafjardarsysla" );
cc_reg_IS.Add( "29", "Snafellsnes- og Hnappadalssysla" );
+ cc_reg_IS.Add( "30", "Strandasysla" );
cc_reg_IS.Add( "31", "Sudur-Mulasysla" );
cc_reg_IS.Add( "32", "Sudur-Tingeyjarsysla" );
cc_reg_IS.Add( "34", "Vestur-Bardastrandarsysla" );
@@ -2316,6 +2317,7 @@ public static class RegionName
GEOIP_REGION_NAME.Add( "LA", cc_reg_LA );
Hashtable cc_reg_LB = new Hashtable();
cc_reg_LB.Add( "01", "Beqaa" );
+ cc_reg_LB.Add( "02", "Al Janub" );
cc_reg_LB.Add( "03", "Liban-Nord" );
cc_reg_LB.Add( "04", "Beyrouth" );
cc_reg_LB.Add( "05", "Mont-Liban" );
@@ -2391,14 +2393,21 @@ public static class RegionName
Hashtable cc_reg_LR = new Hashtable();
cc_reg_LR.Add( "01", "Bong" );
cc_reg_LR.Add( "04", "Grand Cape Mount" );
+ cc_reg_LR.Add( "05", "Lofa" );
cc_reg_LR.Add( "06", "Maryland" );
cc_reg_LR.Add( "07", "Monrovia" );
cc_reg_LR.Add( "09", "Nimba" );
cc_reg_LR.Add( "10", "Sino" );
cc_reg_LR.Add( "11", "Grand Bassa" );
+ cc_reg_LR.Add( "12", "Grand Cape Mount" );
+ cc_reg_LR.Add( "13", "Maryland" );
cc_reg_LR.Add( "14", "Montserrado" );
+ cc_reg_LR.Add( "17", "Margibi" );
+ cc_reg_LR.Add( "18", "River Cess" );
cc_reg_LR.Add( "19", "Grand Gedeh" );
cc_reg_LR.Add( "20", "Lofa" );
+ cc_reg_LR.Add( "21", "Gbarpolu" );
+ cc_reg_LR.Add( "22", "River Gee" );
GEOIP_REGION_NAME.Add( "LR", cc_reg_LR );
Hashtable cc_reg_LS = new Hashtable();
cc_reg_LS.Add( "10", "Berea" );
@@ -2993,8 +3002,10 @@ public static class RegionName
cc_reg_NG.Add( "05", "Lagos" );
cc_reg_NG.Add( "10", "Rivers" );
cc_reg_NG.Add( "11", "Federal Capital Territory" );
+ cc_reg_NG.Add( "12", "Gongola" );
cc_reg_NG.Add( "16", "Ogun" );
cc_reg_NG.Add( "17", "Ondo" );
+ cc_reg_NG.Add( "18", "Oyo" );
cc_reg_NG.Add( "21", "Akwa Ibom" );
cc_reg_NG.Add( "22", "Cross River" );
cc_reg_NG.Add( "23", "Kaduna" );
@@ -3123,6 +3134,7 @@ public static class RegionName
GEOIP_REGION_NAME.Add( "NR", cc_reg_NR );
Hashtable cc_reg_NZ = new Hashtable();
cc_reg_NZ.Add( "10", "Chatham Islands" );
+ cc_reg_NZ.Add( "85", "Waikato" );
cc_reg_NZ.Add( "E7", "Auckland" );
cc_reg_NZ.Add( "E8", "Bay of Plenty" );
cc_reg_NZ.Add( "E9", "Canterbury" );
@@ -3255,6 +3267,7 @@ public static class RegionName
cc_reg_PH.Add( "42", "Misamis Occidental" );
cc_reg_PH.Add( "43", "Misamis Oriental" );
cc_reg_PH.Add( "44", "Mountain" );
+ cc_reg_PH.Add( "45", "Negros Occidental" );
cc_reg_PH.Add( "46", "Negros Oriental" );
cc_reg_PH.Add( "47", "Nueva Ecija" );
cc_reg_PH.Add( "48", "Nueva Vizcaya" );
@@ -3628,6 +3641,7 @@ public static class RegionName
Hashtable cc_reg_RW = new Hashtable();
cc_reg_RW.Add( "01", "Butare" );
cc_reg_RW.Add( "06", "Gitarama" );
+ cc_reg_RW.Add( "07", "Kibungo" );
cc_reg_RW.Add( "09", "Kigali" );
cc_reg_RW.Add( "11", "Est" );
cc_reg_RW.Add( "12", "Kigali" );
@@ -3909,6 +3923,7 @@ public static class RegionName
cc_reg_SN.Add( "04", "Saint-Louis" );
cc_reg_SN.Add( "05", "Tambacounda" );
cc_reg_SN.Add( "07", "Thies" );
+ cc_reg_SN.Add( "08", "Louga" );
cc_reg_SN.Add( "09", "Fatick" );
cc_reg_SN.Add( "10", "Kaolack" );
cc_reg_SN.Add( "11", "Kolda" );
@@ -4096,6 +4111,8 @@ public static class RegionName
cc_reg_TH.Add( "76", "Udon Thani" );
cc_reg_TH.Add( "77", "Amnat Charoen" );
cc_reg_TH.Add( "78", "Mukdahan" );
+ cc_reg_TH.Add( "79", "Nong Bua Lamphu" );
+ cc_reg_TH.Add( "80", "Sa Kaeo" );
GEOIP_REGION_NAME.Add( "TH", cc_reg_TH );
Hashtable cc_reg_TJ = new Hashtable();
cc_reg_TJ.Add( "01", "Kuhistoni Badakhshon" );
@@ -4110,32 +4127,29 @@ public static class RegionName
cc_reg_TM.Add( "05", "Mary" );
GEOIP_REGION_NAME.Add( "TM", cc_reg_TM );
Hashtable cc_reg_TN = new Hashtable();
- cc_reg_TN.Add( "02", "Al Qasrayn" );
- cc_reg_TN.Add( "03", "Al Qayrawan" );
- cc_reg_TN.Add( "06", "Jundubah" );
- cc_reg_TN.Add( "10", "Qafsah" );
- cc_reg_TN.Add( "14", "Kef" );
- cc_reg_TN.Add( "15", "Al Mahdiyah" );
+ cc_reg_TN.Add( "02", "Kasserine" );
+ cc_reg_TN.Add( "03", "Kairouan" );
+ cc_reg_TN.Add( "06", "Jendouba" );
+ cc_reg_TN.Add( "14", "El Kef" );
+ cc_reg_TN.Add( "15", "Al Mahdia" );
cc_reg_TN.Add( "16", "Al Munastir" );
cc_reg_TN.Add( "17", "Bajah" );
- cc_reg_TN.Add( "18", "Banzart" );
- cc_reg_TN.Add( "19", "Nabul" );
- cc_reg_TN.Add( "22", "Silyanah" );
- cc_reg_TN.Add( "23", "Susah" );
- cc_reg_TN.Add( "26", "Chaiyaphum Province" );
- cc_reg_TN.Add( "27", "Bin" );
+ cc_reg_TN.Add( "18", "Bizerte" );
+ cc_reg_TN.Add( "19", "Nabeul" );
+ cc_reg_TN.Add( "22", "Siliana" );
+ cc_reg_TN.Add( "23", "Sousse" );
+ cc_reg_TN.Add( "26", "Ariana" );
+ cc_reg_TN.Add( "27", "Ben Arous" );
cc_reg_TN.Add( "28", "Madanin" );
- cc_reg_TN.Add( "29", "Qabis" );
- cc_reg_TN.Add( "30", "Qafsah" );
- cc_reg_TN.Add( "31", "Qibili" );
- cc_reg_TN.Add( "32", "Safaqis" );
- cc_reg_TN.Add( "33", "Sidi Bu Zayd" );
- cc_reg_TN.Add( "34", "Tatawin" );
- cc_reg_TN.Add( "35", "Tawzar" );
+ cc_reg_TN.Add( "29", "Gabes" );
+ cc_reg_TN.Add( "30", "Gafsa" );
+ cc_reg_TN.Add( "31", "Kebili" );
+ cc_reg_TN.Add( "32", "Sfax" );
+ cc_reg_TN.Add( "33", "Sidi Bou Zid" );
+ cc_reg_TN.Add( "34", "Tataouine" );
+ cc_reg_TN.Add( "35", "Tozeur" );
cc_reg_TN.Add( "36", "Tunis" );
- cc_reg_TN.Add( "37", "Zaghwan" );
- cc_reg_TN.Add( "38", "Ariana" );
- cc_reg_TN.Add( "39", "Manouba" );
+ cc_reg_TN.Add( "37", "Zaghouan" );
GEOIP_REGION_NAME.Add( "TN", cc_reg_TN );
Hashtable cc_reg_TO = new Hashtable();
cc_reg_TO.Add( "01", "Ha" );
@@ -4490,12 +4504,14 @@ public static class RegionName
cc_reg_VN.Add( "03", "Ben Tre" );
cc_reg_VN.Add( "04", "Binh Tri Thien" );
cc_reg_VN.Add( "05", "Cao Bang" );
+ cc_reg_VN.Add( "06", "Cuu Long" );
cc_reg_VN.Add( "07", "Dac Lac" );
cc_reg_VN.Add( "09", "Dong Thap" );
cc_reg_VN.Add( "11", "Ha Bac" );
cc_reg_VN.Add( "12", "Hai Hung" );
cc_reg_VN.Add( "13", "Hai Phong" );
cc_reg_VN.Add( "14", "Ha Nam Ninh" );
+ cc_reg_VN.Add( "15", "Ha Noi" );
cc_reg_VN.Add( "16", "Ha Son Binh" );
cc_reg_VN.Add( "17", "Ha Tuyen" );
cc_reg_VN.Add( "19", "Hoang Lien Son" );
@@ -4606,6 +4622,7 @@ public static class RegionName
cc_reg_YE.Add( "03", "Al Mahrah" );
cc_reg_YE.Add( "04", "Hadramawt" );
cc_reg_YE.Add( "05", "Shabwah" );
+ cc_reg_YE.Add( "06", "Al Ghaydah" );
cc_reg_YE.Add( "08", "Al Hudaydah" );
cc_reg_YE.Add( "10", "Al Mahwit" );
cc_reg_YE.Add( "11", "Dhamar" );

0 comments on commit 84032f9

Please sign in to comment.