List:General Discussion« Previous MessageNext Message »
From:Sasha Pachev Date:May 12 1999 9:16pm
Subject:Re: Packing IP addresses
View as plain text  
Matthias Pigulla wrote:
> 
> Hi folks,
> 
> I want to pack IP addresses for I have to store a lot of them in a
> database ;-)
> 
> Does anyone know a shorter solution than:
> 
> -MySQL---
> select
>  (substring_index(substring_index('255.255.255.255', '.', 1), '.', -1)
> << 24) +
>  (substring_index(substring_index('255.255.255.255', '.', 2), '.', -1)
> << 16) +
>  (substring_index(substring_index('255.255.255.255', '.', 3), '.', -1)
> << 8)  +
>  (substring_index(substring_index('255.255.255.255', '.', 4), '.', -1))
> as ipshort
> -MySQL---
> 
> Which would save all from 1.1.1.1 to 255.255.255.255 in an unsigned INT
> type field.
> 
> Matthias

Yes, if you are using C:

 struct in_addr ip;
 char* ip_str = "255.255.255.255";
 char query_buf[1024];

 inet_aton(ip_str, &ip);
 snprintf(query_buf, sizeof(query_buf), "insert into ip
values (%u)",
   ip.s_addr);

 In perl you can do a similiar piece using pack(),
cannot rembemer off the bat how do it. Do not know about
PHP.

The basic idea is to obtain the decimal value somehow in
the host language and then insert it using SQL.


-- 
Sasha Pachev
http://www.sashanet.com/ (home)
http://www.direct1.com/ (work)
Thread
Packing IP addressesMatthias Pigulla13 May
  • Re: Packing IP addressesUnknown Sender13 May
    • Re: Packing IP addressesDr Eberhard W Lisse13 May
      • Re: Packing IP addressesBen Kirkpatrick13 May
        • Re: Packing IP addressesUnknown Sender13 May
      • Re: Packing IP addressesunknown13 May
  • Re: Packing IP addressesSasha Pachev13 May
  • Re: Packing IP addressesSteve Lihn13 May