» »

[C#] Funkcija za testiranje, če je IP slovenski

[C#] Funkcija za testiranje, če je IP slovenski

MaCoFaCo ::

Rabil sem en programček za testiranje če je nek IP slovenski, zato sem napisal tool, ki mi zgenerira C# funkcijo za to. Koda ni optimalna, pa tudi ni nujno da je 100% OK. Kolikor sem gledal pa mi je pravilno vračala vrednosti. Mogoče bo še kdo to rabil, zato objavljam tu.

using System;
namespace Utility
{

    public class IPToCountry
    {
        //Auto generated function by macofaco's tool
        public static bool IsFromSlovenia(string ip)
        {
            byte[] bip = FromString(ip);
            byte[] lip = null;
            byte[] uip = null;


            lip = FromNumber(1348460544);
            uip = FromNumber(1348464639);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1358356480);
            uip = FromNumber(1358360575);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1360125952);
            uip = FromNumber(1360130047);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1360551936);
            uip = FromNumber(1360556031);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1364897792);
            uip = FromNumber(1364901887);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1385496576);
            uip = FromNumber(1385504767);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1388322816);
            uip = FromNumber(1388331007);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1389772800);
            uip = FromNumber(1389789183);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1410654208);
            uip = FromNumber(1410662399);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1411973120);
            uip = FromNumber(1412005887);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1412726784);
            uip = FromNumber(1412743167);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1426046976);
            uip = FromNumber(1426063359);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1426718720);
            uip = FromNumber(1426735103);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1446641664);
            uip = FromNumber(1446674431);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1446838272);
            uip = FromNumber(1446871039);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1467449344);
            uip = FromNumber(1467457535);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1489502208);
            uip = FromNumber(1489534975);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1500192768);
            uip = FromNumber(1500194815);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1502478336);
            uip = FromNumber(1502609407);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(1507065856);
            uip = FromNumber(1507131391);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3238065152);
            uip = FromNumber(3238065407);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3238133760);
            uip = FromNumber(3238199295);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239079168);
            uip = FromNumber(3239079423);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239174912);
            uip = FromNumber(3239175167);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239304192);
            uip = FromNumber(3239305215);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239452416);
            uip = FromNumber(3239452671);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239465216);
            uip = FromNumber(3239465471);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239522560);
            uip = FromNumber(3239522815);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239525888);
            uip = FromNumber(3239526143);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239639040);
            uip = FromNumber(3239640063);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239713280);
            uip = FromNumber(3239713535);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239875072);
            uip = FromNumber(3239875327);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3239979776);
            uip = FromNumber(3239980031);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3240698624);
            uip = FromNumber(3240698879);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3240698880);
            uip = FromNumber(3240699135);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3240712448);
            uip = FromNumber(3240712703);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3241117696);
            uip = FromNumber(3241117951);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3243048960);
            uip = FromNumber(3243114495);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3244277760);
            uip = FromNumber(3244294143);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3245177856);
            uip = FromNumber(3245178111);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3245204224);
            uip = FromNumber(3245204479);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3245215232);
            uip = FromNumber(3245215743);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3245248768);
            uip = FromNumber(3245249023);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3245326336);
            uip = FromNumber(3245326847);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3245333504);
            uip = FromNumber(3245334527);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3247048192);
            uip = FromNumber(3247048703);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3247048960);
            uip = FromNumber(3247049215);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3247054848);
            uip = FromNumber(3247063039);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3248785664);
            uip = FromNumber(3248786943);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3249716224);
            uip = FromNumber(3249716479);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3250429952);
            uip = FromNumber(3250438143);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3251186944);
            uip = FromNumber(3251187199);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3251201280);
            uip = FromNumber(3251201535);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3251203328);
            uip = FromNumber(3251203583);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3251215104);
            uip = FromNumber(3251215231);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3251217664);
            uip = FromNumber(3251217919);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3251229696);
            uip = FromNumber(3251230719);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3252514816);
            uip = FromNumber(3252515071);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3252515584);
            uip = FromNumber(3252515839);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3253963776);
            uip = FromNumber(3253964287);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3255233792);
            uip = FromNumber(3255234047);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3255235072);
            uip = FromNumber(3255235327);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3257357568);
            uip = FromNumber(3257357823);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3258099200);
            uip = FromNumber(3258099455);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3262434304);
            uip = FromNumber(3262434815);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3263087872);
            uip = FromNumber(3263088127);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3264376064);
            uip = FromNumber(3264376319);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3264741376);
            uip = FromNumber(3264749567);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3264846720);
            uip = FromNumber(3264846847);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3265617920);
            uip = FromNumber(3265626111);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3266342912);
            uip = FromNumber(3266343167);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3268739840);
            uip = FromNumber(3268740095);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3268745728);
            uip = FromNumber(3268745983);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3270648832);
            uip = FromNumber(3270649087);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3271098368);
            uip = FromNumber(3271163903);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3273882112);
            uip = FromNumber(3273882367);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3274687744);
            uip = FromNumber(3274687999);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3274691328);
            uip = FromNumber(3274691583);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3274691840);
            uip = FromNumber(3274692095);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3274694656);
            uip = FromNumber(3274694911);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3274695168);
            uip = FromNumber(3274695423);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3276103680);
            uip = FromNumber(3276104703);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3276857600);
            uip = FromNumber(3276857855);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3277176832);
            uip = FromNumber(3277177087);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3277823488);
            uip = FromNumber(3277823743);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3277827328);
            uip = FromNumber(3277827353);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3281346048);
            uip = FromNumber(3281346303);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3282743552);
            uip = FromNumber(3282743807);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3283252736);
            uip = FromNumber(3283253247);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3284041984);
            uip = FromNumber(3284042239);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3284043776);
            uip = FromNumber(3284044031);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3284049408);
            uip = FromNumber(3284049663);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3285368832);
            uip = FromNumber(3285385215);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3286927616);
            uip = FromNumber(3286927871);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3287640576);
            uip = FromNumber(3287641087);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3287666688);
            uip = FromNumber(3287666943);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3287668736);
            uip = FromNumber(3287668991);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3287677440);
            uip = FromNumber(3287677695);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3287678976);
            uip = FromNumber(3287687167);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3287827456);
            uip = FromNumber(3287827711);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3287955968);
            uip = FromNumber(3287956223);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3287990272);
            uip = FromNumber(3287998463);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3557679104);
            uip = FromNumber(3557687295);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3557957632);
            uip = FromNumber(3557965823);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3558752256);
            uip = FromNumber(3558760447);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3561512960);
            uip = FromNumber(3561521151);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3562921984);
            uip = FromNumber(3562930175);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3563552768);
            uip = FromNumber(3563560959);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3564519424);
            uip = FromNumber(3564527615);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3572203520);
            uip = FromNumber(3572236287);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3582935040);
            uip = FromNumber(3582943231);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3583893504);
            uip = FromNumber(3583901695);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3584098304);
            uip = FromNumber(3584106495);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3584876544);
            uip = FromNumber(3584884735);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3588603904);
            uip = FromNumber(3588620287);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3589931008);
            uip = FromNumber(3589947391);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3590144000);
            uip = FromNumber(3590160383);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3645390848);
            uip = FromNumber(3645394943);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            lip = FromNumber(3645394944);
            uip = FromNumber(3645399039);
            if (IsGreater(bip, lip) && IsLess(bip, uip)) return true;


            return false;
        }

        private static byte[] FromString(string ip)
        {
            string[] splits = ip.Split('.');

            byte[] res = new byte[4];
            res[0] = Byte.Parse(splits[0]);
            res[1] = Byte.Parse(splits[1]);
            res[2] = Byte.Parse(splits[2]);
            res[3] = Byte.Parse(splits[3]);

            return res;
        }

        private static byte[] FromNumber(long ip)
        {
            byte[] res = new byte[4];
            res[3] = (byte)(ip & 0xFF);
            res[2] = (byte)((ip & 0xFF00) >> 8);
            res[1] = (byte)((ip & 0xFF0000) >> 16);
            res[0] = (byte)((ip & 0xFF000000) >> 24);

            return res;
        }

        private static bool IsGreater(byte[] x, byte[] y)
        {
            for (short i = 0; i < 4; i++)
            {
                if (x[i] < y[i])
                {
                    return false;
                }
            }

            return true;
        }

        private static bool IsLess(byte[] x, byte[] y)
        {
            for (short i = 0; i < 4; i++)
            {
                if (x[i] > y[i])
                {
                    return false;
                }
            }

            return true;
        }
    }
}
  • spremenilo: snow ()

Gundolf ::

Samo eno provokativno vprašanje: a je bilo res lažje copy-pastat vse te številke in if-e kot jih pa v eno tabelo dat in eno zanko čez narest? :)

MaCoFaCo ::

Tisto se potem počasneje izvaja, ker imaš zanko več :P

MaCoFaCo ::

Evo še kompaktnejše oblike.

using System;
namespace Utility
{

    //Auto generated file by macofaco's tool
    public class IPToCountry
    {
        private static byte[] data = new byte[] {
            80,95,224,0,80,95,239,255,
            80,246,224,0,80,246,239,255,
            81,17,224,0,81,17,239,255,
            81,24,96,0,81,24,111,255,
            81,90,176,0,81,90,191,255,
            82,149,0,0,82,149,31,255,
            82,192,32,0,82,192,63,255,
            82,214,64,0,82,214,127,255,
            84,20,224,0,84,20,255,255,
            84,41,0,0,84,41,127,255,
            84,52,128,0,84,52,191,255,
            84,255,192,0,84,255,255,255,
            85,10,0,0,85,10,63,255,
            86,58,0,0,86,58,127,255,
            86,61,0,0,86,61,127,255,
            87,119,128,0,87,119,159,255,
            88,200,0,0,88,200,127,255,
            89,107,32,0,89,107,39,255,
            89,142,0,0,89,143,255,255,
            89,212,0,0,89,212,255,255,
            193,0,244,0,193,0,244,255,
            193,2,0,0,193,2,255,255,
            193,16,109,0,193,16,109,255,
            193,17,227,0,193,17,227,255,
            193,19,220,0,193,19,223,255,
            193,22,31,0,193,22,31,255,
            193,22,81,0,193,22,81,255,
            193,23,49,0,193,23,49,255,
            193,23,62,0,193,23,62,255,
            193,24,248,0,193,24,251,255,
            193,26,26,0,193,26,26,255,
            193,28,146,0,193,28,146,255,
            193,30,43,0,193,30,43,255,
            193,41,35,0,193,41,35,255,
            193,41,36,0,193,41,36,255,
            193,41,89,0,193,41,89,255,
            193,47,136,0,193,47,136,255,
            193,77,0,0,193,77,255,255,
            193,95,192,0,193,95,255,255,
            193,109,124,0,193,109,124,255,
            193,109,227,0,193,109,227,255,
            193,110,14,0,193,110,15,255,
            193,110,145,0,193,110,145,255,
            193,111,192,0,193,111,193,255,
            193,111,220,0,193,111,223,255,
            193,138,6,0,193,138,7,255,
            193,138,9,0,193,138,9,255,
            193,138,32,0,193,138,63,255,
            193,164,137,0,193,164,141,255,
            193,178,188,0,193,178,188,255,
            193,189,160,0,193,189,191,255,
            193,201,45,0,193,201,45,255,
            193,201,101,0,193,201,101,255,
            193,201,109,0,193,201,109,255,
            193,201,155,0,193,201,155,127,
            193,201,165,0,193,201,165,255,
            193,201,212,0,193,201,215,255,
            193,221,112,0,193,221,112,255,
            193,221,115,0,193,221,115,255,
            193,243,140,0,193,243,141,255,
            194,6,237,0,194,6,237,255,
            194,6,242,0,194,6,242,255,
            194,39,85,0,194,39,85,255,
            194,50,166,0,194,50,166,255,
            194,116,204,0,194,116,205,255,
            194,126,197,0,194,126,197,255,
            194,146,109,0,194,146,109,255,
            194,152,0,0,194,152,31,255,
            194,153,155,128,194,153,155,255,
            194,165,96,0,194,165,127,255,
            194,176,112,0,194,176,112,255,
            194,213,3,0,194,213,3,255,
            194,213,26,0,194,213,26,255,
            194,242,36,0,194,242,36,255,
            194,249,0,0,194,249,255,255,
            195,35,122,0,195,35,122,255,
            195,47,197,0,195,47,197,255,
            195,47,211,0,195,47,211,255,
            195,47,213,0,195,47,213,255,
            195,47,224,0,195,47,224,255,
            195,47,226,0,195,47,226,255,
            195,69,96,0,195,69,99,255,
            195,80,225,0,195,80,225,255,
            195,85,192,0,195,85,192,255,
            195,95,158,0,195,95,158,255,
            195,95,173,0,195,95,173,25,
            195,149,94,0,195,149,94,255,
            195,170,177,0,195,170,177,255,
            195,178,118,0,195,178,119,255,
            195,190,129,0,195,190,129,255,
            195,190,136,0,195,190,136,255,
            195,190,158,0,195,190,158,255,
            195,210,192,0,195,210,255,255,
            195,234,137,0,195,234,137,255,
            195,245,106,0,195,245,107,255,
            195,245,208,0,195,245,208,255,
            195,245,216,0,195,245,216,255,
            195,245,250,0,195,245,250,255,
            195,246,0,0,195,246,31,255,
            195,248,68,0,195,248,68,255,
            195,250,58,0,195,250,58,255,
            195,250,192,0,195,250,223,255,
            212,13,224,0,212,13,255,255,
            212,18,32,0,212,18,63,255,
            212,30,64,0,212,30,95,255,
            212,72,96,0,212,72,127,255,
            212,93,224,0,212,93,255,255,
            212,103,128,0,212,103,159,255,
            212,118,64,0,212,118,95,255,
            212,235,128,0,212,235,255,255,
            213,143,64,0,213,143,95,255,
            213,157,224,0,213,157,255,255,
            213,161,0,0,213,161,31,255,
            213,172,224,0,213,172,255,255,
            213,229,192,0,213,229,255,255,
            213,250,0,0,213,250,63,255,
            213,253,64,0,213,253,127,255,
            217,72,64,0,217,72,79,255,
            217,72,80,0,217,72,95,255};

        public static bool IsFromSlovenia(string ip)
        {
            byte[] test = FromString(ip);
            for (int i = 0; i < data.Length; i += 8)
            {
                if (IsGreater(test, i, data) && IsLess(test, i + 4, data)) return true;
            }
            return false;
        }

        private static byte[] FromString(string ip)
        {
            string[] splits = ip.Split('.');

            byte[] res = new byte[4];
            res[0] = Byte.Parse(splits[0]);
            res[1] = Byte.Parse(splits[1]);
            res[2] = Byte.Parse(splits[2]);
            res[3] = Byte.Parse(splits[3]);

            return res;
        }

        private static bool IsGreater(byte[] ip, int i, byte[] data)
        {
            for (int j = 0; j < 4; j++)
            {
                if (ip[j] < data[i + j])
                {
                    return false;
                }
            }

            return true;
        }

        private static bool IsLess(byte[] ip, int i, byte[] data)
        {
            for (int j = 0; j < 4; j++)
            {
                if (ip[j] > data[i + j])
                {
                    return false;
                }
            }

            return true;
        }
    }
}


Raba:
bool result = Utility.IPToCountry.IsFromSlovenia("195.246.20.14"); //true
result = Utility.IPToCountry.IsFromSlovenia("66.249.85.99"); //false

kekz ::

In kakšna je definicija za sloveniski IP?
Ali mora biti uporabnik iz Slovenije ali ISP ...?
Npr. mi smo priključeni na provajderja EuropeOnline. Ali imamo slovenski IP?
Ali imajo slovenske domene obi.si, bauhaus.si, baumax.si slovenske IP-je?

Jaz namreč ponavadi ne testiram IP-jev, ampak imena domen. Upoštevam tudi (znane) izjeme (npr. kje.sm)

Zgodovina sprememb…

  • spremenilo: kekz ()

MaCoFaCo ::

Jaz sem pod "slovenski IP" misli tiste IPje, ki jih dodelijo slovenski ponudniki interneta (Arnes, SiOL, T-2, ...). Domena .si še ne pomeni, da je to slovenski IP (obi.si ne spada pod slovenski IP range).

b ::

Verjetno bi bilo fajn, ce bi zadeva brala IP range iz config datoteke. Po moznosti take, ki razume obe notaciji - IP range, kot imas ti (xx.xx.xx.xx - yy.yy.yy.yy) in zraven se netmask (xx.xx.xx.xx/nn). Potem bi dejansko ne rabil skompajlat ob vsakem update-u :)

Od kje si pa dobil podatke? RIPE je dokaj avtoritativen source (ne da se mi iskat natancnega URL-ja za vse IP-je dodeljene ISP-jem in ostalim uporabnikom v Sloveniji, ampak obstaja).

MTm2H37rqt7B ::

Kaj pa ce bi vkomponiral whois client, in bi sparsal kater ISP je?

Recimo: whoisas IP, pogledas country field in ce je SI je slovenski ip... nic lazjega.

Zgodovina sprememb…

MaCoFaCo ::

@b
Podatke sem dobil tule: http://software77.net/cgi-bin/ip-countr...

@pux
V tem primeru si odvisen od zunanjega sistema (hitrost).

MTm2H37rqt7B ::

Jah, hitrost vs. enostavnsot in natancnost :)


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[javaScript] Preverjanje formata zapisa EMŠO

Oddelek: Programiranje
132921 (2541) win64
»

[NALOGA] Java: Rekurzija

Oddelek: Programiranje
5950 (764) Spura
»

Slovenija IP range

Oddelek: Omrežja in internet
154747 (4276) Soprano
»

Majčken web server + senzor tlaka

Oddelek: Elektrotehnika in elektronika
151894 (1770) Brane2
»

Telemach bo začel z štetjem prometa

Oddelek: Omrežja in internet
51277 (1061) miki133

Več podobnih tem