{"id":374,"date":"2009-04-04T09:04:15","date_gmt":"2009-04-04T13:04:15","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/zeroday\/?p=374"},"modified":"2009-04-04T09:05:36","modified_gmt":"2009-04-04T13:05:36","slug":"storing-ip-addresses-in-mysql-with-rubyrails","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/zeroday\/2009\/04\/04\/storing-ip-addresses-in-mysql-with-rubyrails\/","title":{"rendered":"Storing IP Addresses in MySQL with ruby\/rails"},"content":{"rendered":"<p>A recent project has me thinking about storing of IP addresses in mysql.  The natural tendency is to store it as text.  My first attempt stored the address as char(16) with a normal index to help speed searches against it.  After some reading about high performance MySQL techniques I was reminded that IP addresses in dotted quad form are the least efficient.  Instead of storing as a string of characters I could instead convert the dotted quad into a 32 bit integer.  <\/p>\n<p>The magic of converting it is pretty easy to find online however if you are using ruby simply install the IPAddr gem.  <\/p>\n<blockquote><p>\n&gt;&gt; ip = IPAddr.new(&#8216;255.255.255.255&#8217;)<br \/>\n=&gt; #<br \/>\n&gt;&gt; puts ip.to_i<br \/>\n4294967295<br \/>\n=&gt; nil\n<\/p><\/blockquote>\n<p>Reversing the process isn&#8217;t quite as easy and the documentation fails to mention this possibility.  A little digging online will unearth this additional parameter that is needed:<\/p>\n<blockquote><p>\n&gt;&gt; ipnum = 4294967295<br \/>\n=&gt; 4294967295<br \/>\n&gt;&gt; ip = IPAddr.new(ipnum, Socket::AF_INET).to_s<br \/>\n=&gt; &#8220;255.255.255.255&#8221;\n<\/p><\/blockquote>\n<p>When I first tried to store this in MySQL I ran into another problem.  In my haste I created the column ip_num as an int(11).  The code I ran didn&#8217;t raise an exception and converted all the ip addresses in the database.  However when I viewed the results a large number of ip addresses came back as 127.255.255.255.  This ip address converts to 2147483647 as an integer.  <\/p>\n<p>If this number looks familiar it is because it is exactly half of the value of 255.255.255.255.  It is also the limit of a <i>signed<\/i> integer.<br \/>\n<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/numeric-type-overview.html\"> &#8220;The signed range is -2147483648 to 2147483647&#8221;<\/a><\/p>\n<p>Ensure that you create an unsigned int column for ip addresses to hold the max value of 4294967295.<br \/>\nThe unsigned range is 0 to 4294967295.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A recent project has me thinking about storing of IP addresses in mysql. The natural tendency is to store it as text. My first attempt stored the address as char(16) with a normal index to help speed searches against it. After some reading about high performance MySQL techniques I was reminded that IP addresses in [&hellip;]<\/p>\n","protected":false},"author":214,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[272],"tags":[],"class_list":["post-374","post","type-post","status-publish","format-standard","hentry","category-digital-warfare"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/zeroday\/wp-json\/wp\/v2\/posts\/374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/zeroday\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/zeroday\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/zeroday\/wp-json\/wp\/v2\/users\/214"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/zeroday\/wp-json\/wp\/v2\/comments?post=374"}],"version-history":[{"count":0,"href":"https:\/\/archive.blogs.harvard.edu\/zeroday\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/zeroday\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/zeroday\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/zeroday\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}