If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > IP address storage

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-25-08, 09:25
kalmon kalmon is offline
Registered User
 
Join Date: Aug 2007
Posts: 12
IP address storage

Is there a commonly accepted best practice for storing IP addresses in a database? I've seen a 4 column of bytes method, a single column INT method, and a single column CHAR(15) method.

I know it is possible to do any of those and that there are pros and cons to them but which one is best? Also what is everyone going to do when IPv6 starts gaining more momentum?
Reply With Quote
  #2 (permalink)  
Old 03-25-08, 09:33
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
what do you want to store the information for?
if its a passive audit log, ie you just need to know what IP address was used then storing as an single number may make sense

if you need to manipulate theat data then Id suggest storing as a string

I don't think it actaully matters but your solution will need to handle IPv6 when it truly conmes into force..
Reply With Quote
  #3 (permalink)  
Old 03-25-08, 09:46
kalmon kalmon is offline
Registered User
 
Join Date: Aug 2007
Posts: 12
I am storing the IP addresses of systems that access a database. It needs the ability to be searched and and also converted to a human readable format. I was leaning towards have 4 fields IPoct1, IPoct2, IPoct3, IPoct4 all of tinyINT unsigned / Byte type. This way I can search by network and it retains human readability. But I wasn't sure if this was a proper way to store data (4 fields that technically hold data that 1 could).

-Brian
Reply With Quote
  #4 (permalink)  
Old 03-25-08, 10:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by kalmon
I know it is possible to do any of those and that there are pros and cons to them but which one is best?
the method where the pros and cons more closely fit your particular needs

for searching, the INTEGER method is the fastest, because you always search top-down, i.e. you always start with the first octet, then the second, etc.

you never search for all IPs where the 3rd octet is 155 -- that'd be silly ™

the top-down search translates very easily into integer ranges

regarding "proper way to store data (4 fields that technically hold data that 1 could)" this depends completely on what kind of data it is

4 fields instead of 1 is much better if, for instance, you're talking about street, city, state, country
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-25-08, 10:15
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by kalmon
I know it is possible to do any of those and that there are pros and cons to them but which one is best?
the method where the pros and cons more closely fit your particular needs

for searching, the INTEGER method is the fastest, because you always search top-down, i.e. you always start with the first octet, then the second, etc.

you never search for all IPs where the 3rd octet is 155 -- that'd be silly ™

the top-down search translates very easily into integer ranges

regarding "proper way to store data (4 fields that technically hold data that 1 could)" this depends completely on what kind of data it is

4 fields instead of 1 is much better if, for instance, you're talking about street, city, state, country
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 03-25-08, 15:45
rvt rvt is offline
Registered User
 
Join Date: Mar 2008
Posts: 14
Check if your DB vendor supports a native IP format, if so use it because it's properly optimized for doing queries against it. PG for example has one : http://www.postgresql.org/docs/8.3/i...net-types.html

Otherwise use 4 fields of one byte, I believe hostip.org does the same.

Ries
Reply With Quote
  #7 (permalink)  
Old 03-25-08, 16:23
MCrowley MCrowley is offline
Wage drone 24601
 
Join Date: Jan 2003
Location: Massachusetts
Posts: 4,899
I could see searching on just the third octet. Around here, we have set up the network addresses to be on the 10.0.0.0 network, which is a 'non-network" according to the powers that be. As such, the third octet came out to be the location for us. My building has 10.0.101.0 and 10.0.102.0 for clients. Easy enough to trace.
Reply With Quote
  #8 (permalink)  
Old 03-26-08, 08:42
kalmon kalmon is offline
Registered User
 
Join Date: Aug 2007
Posts: 12
Thanks for all the replies. I am using Mysql 5.1 and I decided to go with 4 tinyint fields, although I did manage to get a working conversion to unsigned Int and back to dotted format.

-Brian
Reply With Quote
  #9 (permalink)  
Old 03-26-08, 08:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
a working conversion? any chance you could share it?

also, did you see the INET_ATON() and INET_NTOA() functions?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 03-27-08, 16:34
kalmon kalmon is offline
Registered User
 
Join Date: Aug 2007
Posts: 12
I'm off work until Monday so I dont have the exact code, but to sum it up:

IPasINT = octet[X](X=0to3) * 256 ^ X(0to3)
This creates an unsigned integer. It is important to note that this in not the same format that Windows uses, although if read in binary it would be... Windows uses a signed int therefor 128.0.0.0 and greater would be negative numbers as signed integers.

Those NTOA and ATON functions are expecting the signed int I believe. I do know they just convert the order between network order and host order.

*edited for correct math*
The conversion to signed int went something like:
Code:
If IPasINT > 4294967296 Then IPasSINT = IPasINT - 4294967296
If IPasINT <= 4294967296 THEN IPasSINT = IPasINT
To revert it to a human readable format from an unsigned int was a little trickier. The correct way is to do modulus division but I did it like this:
Code:
Loop X from 3 to 0
  octet[X] = IPasINT / (256 ^ X) just ignore decimal part ALWAYS round down
  IPasINT = IPasINT - (octet[X] * (256 ^ X))
Loop ends

Last edited by kalmon; 03-31-08 at 12:15. Reason: Memory is not what it used to be and math was WAY off
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On