Advertisement
tolikpunkoff

GetIPInfo SxGeoSharp function

Sep 30th, 2018
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. public Dictionary<string, object> GetIPInfo(string IP, SxGeoInfoType InfoType)
  2.         {
  3.             Dictionary<string, object> data_city = new Dictionary<string, object>();
  4.             Dictionary<string, object> data_country = new Dictionary<string, object>();
  5.             Dictionary<string, object> data_region = new Dictionary<string, object>();
  6.             if (!IsOpen)
  7.             {
  8.                 ErrorMessage = "Database not open.";
  9.                 return null;
  10.             }
  11.             if (!IPConverter.IsIP(IP)) //проверяем IP ли это)
  12.             {
  13.                 ErrorMessage = IP+" is not valid IP address.";
  14.                 return null;
  15.             }
  16.  
  17.             //получаем ID IP-адреса
  18.             uint ID = SearchID(IP);
  19.             if (ID == 0) //не нашли
  20.             {
  21.                 ErrorMessage = "Not found.";
  22.                 return null;
  23.             }
  24.             //создаем переменные для хранения ответа
  25.             IPInfo = new Dictionary<string, object>();
  26.             IPInfoTypes = new Dictionary<string, Type>();
  27.             //добавляем сам адрес
  28.             IPInfo.Add("ip", IP);
  29.             IPInfoTypes.Add("ip", typeof(string));
  30.  
  31.             if (Header.IdLen == 1) //БД SxGeo, ничего кроме ISO-кода вывести не сможем
  32.             {
  33.                 IPInfo.Add("country_iso", IdToIso(ID));
  34.                 IPInfoTypes.Add("country_iso", typeof(string));
  35.                 return IPInfo;
  36.             }
  37.  
  38.             //БД SxGeoCountry, можем вывести много чего
  39.  
  40.             SxGeoUnpack Unpacker = null;
  41.             byte[] buf = null;
  42.  
  43.             //если найденный 'ID' < размера справочника городов
  44.             //город не найден - только страна
  45.             if (ID < Header.CountrySize)
  46.             {
  47.                 Unpacker = new SxGeoUnpack(Header.pack_country, Header.DBEncoding);
  48.                 buf = ReadDBDirs(Header.countries_begin, ID, Header.MaxCountry,cities_db);
  49.                 data_country = Unpacker.Unpack(buf);
  50.                 AddData(data_country, Unpacker.GetRecordTypes(), "country_");
  51.                 return IPInfo;
  52.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement