🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: chile.comp.programacion
Article #3258

Re: base de datos ip de cada pais

#3258
From: Cristian Gutierrez
Date: Tue, 29 Mar 2005 06:45
57 lines
3903 bytes
Hoy en la ma~nana, pato_donald@bigfoot.com.nospam dijo:
> "Rick_oj" <rick_oj@[sin-esto]tutopia.com> balbuce� en news:d29ghc$ej1$1
> @news1.nivel5.cl:
>
>> por si ha alguien le interesa, o lo necesita, lo encontre hoy
>> 
>> http://tinyurl.com/7yg2t

(gran aporte, gracias!)

> �en qu� formato est�?
> �decimal?
>
> me sale: 33996344,"33996351","GB","GBR","UNITED KINGDOM"

Yup, es la representacion decimal de los numeros IPs de 32 bits. Tienes
que agrupar los 4 octetos por separado y convertirlos a decimal, para
obtener la tipica notacion "tres puntitos".

En general, para un entero N de 32 bits:

  n3p == 0xFF&(N>>24) + "." + 0xFF&(N>>16) + "." + 0xFF&(N>>8) + "." + 0xFF&(N)

Donde:  n3p  = notacion 3 puntitos
        0xFF = 255, es decir, 8 bits "prendidos" en 1
        &    = AND-logico, deja prendidos los bits que estan prendidos
               en los numeros de ambos lados.
        >>X  = operacion de "shift" a la derecha; se come X bits de la
               "derecha" del numero (equivalente a dividir por 2^X)

Por ejemplo:

33996344 == 00000010000001101011111000111000
            \______/\______/\______/\______/
              = 2     = 6    = 190    = 56

33996351 == 00000010000001101011111000111111
            \______/\______/\______/\______/
              = 2     = 6    = 190    = 63

Luego, esa linea corresponde a:

  "2.6.190.56","2.6.190.63","GB","GBR","UNITED KINGDOM"


Y si tienes Perl a mano, este one-liner hace todo el trabajo sucio:

  $ perl -nlpe '$n=0;s/(\d+)/join".",map{0xff&($1>>8*(3-$n++%4))}1..4/eg' \
    < ip-to-country.csv

Eso, si es que no me pifie en algun lado... :-P

-- 
Cristian Gutierrez			http://www.dcc.uchile.cl/~crgutier
crgutier[@]dcc.uchile.cl                        Jabber:crgutier@jabber.org

To segfault is human; to bluescreen moronic.
Technical Details
Message-ID: <87k6nreyr1.fsf@guti.no-ip.org>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!newspeer.monmouth.com!newsfeed.yul.equant.net!news-santiago!helcaraxe.dcc.uchile.cl!guti.no-ip.org!news
References: <d29ghc$ej1$1@news1.nivel5.cl> <Xns9628E712E583patodonaldID6986628@200.73.8.4>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1593cb,53e32211a6e3a92e
X-Google-Thread: fad43,53e32211a6e3a92e
X-Google-Attributes: gid1593cb,gidfad43,public
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!newspeer.monmouth.com!newsfeed.yul.equant.net!news-santiago!helcaraxe.dcc.uchile.cl!guti.no-ip.org!news
From: Cristian Gutierrez <crgutier@dcc.uchile.cl>
Newsgroups: chile.comp.programacion,chile.comp.www
Subject: Re: base de datos ip de cada pais
Date: Tue, 29 Mar 2005 02:45:38 -0400
Organization: DCC - Universidad de Chile
Lines: 57
Message-ID: <87k6nreyr1.fsf@guti.no-ip.org>
References: <d29ghc$ej1$1@news1.nivel5.cl> <Xns9628E712E583patodonaldID6986628@200.73.8.4>
NNTP-Posting-Host: anakena.dcc.uchile.cl
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: helcaraxe.dcc.uchile.cl 1112079612 19668 192.80.24.6 (29 Mar 2005 07:00:12 GMT)
X-Complaints-To: newsmaster@dcc.uchile.cl
NNTP-Posting-Date: Tue, 29 Mar 2005 07:00:12 +0000 (UTC)
Face: iVBORw0KGgoAAAANSUhEUgAAADwAAAA8AgMAAABHkjHhAAAADFBMVEUWFhZfX1+ioqL6+vr8
 dj+QAAAAAWJLR0QAiAUdSAAAAg5JREFUeNql0s9r02AYB/B4GIHuoAgVVmG9CVYwf4ILrht40Ynd
 dEXioWUsA3PaQQ8LKHpYLnbTThwiHbWYV9Sj9rL36NUObGkPUrVzuIs/6BvyluZ9ljcxaXI2lzwf
 eJOH9/k+AvhP/7j/Fv65OxMzmynhqO1zH/So+29NLeqvh6YU8WADmemIP4lxL8pxp6p3o2aZegZl
 R3YyaBJNaaGHKkqgxOXQNkKXzLQR+g9C6+b5kfcQevgmpYRuIrRZn5BCryO0US3ogW1BdV0J+3dz
 grpXmQXYfuf5nkLH5ksK/SIrnq/rg26ioTmdygtupwgWHSvoTnvF4CZl17dOYNoWy9yNPOzXfstg
 tYtXuR9o0JwlL+HgeWGBW8ZgGBbFnWUhw33Sbdz6TvHN1ey8a3YGIN+6T2C5PYFdD2sA6d5FAu9v
 LH3WBbAJBgmIDJ3ioooFoCzPNDq5in82TX7egsdXcE+0pZ3sEr+fxW7XoCvS3HRS4f7rrGHojRP0
 6A72TOtuoBrZnVvz5kGIzVejX0jpvi2bF0Q+iz1TWuXFwbOkP7/hrrdaH58u+HakX7wwSsF8/eDT
 wf45417kFwIPd7yIcWDS52UDApevuZ86p0PLK1tuZHpoBN8YLgOM9pswfft/vO9oMf9g6UbMYNij
 /jDYglZwHRCYIIgx56aO4earyPlD89R0Mvo/9kR9HfgIgorhXaQc58oAAAAASUVORK5CYII=
User-Agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.3 (gnu/linux)
Cancel-Lock: sha1:De9r0ftn3sfJZUqeDCSceNmKlik=