MySQL & IP Address Storage
While designing a schema for a event system here at my current job, the task of storing efficiently IPv4 addresses in a MySQL database came up. IPv4 = 4 bytes, okay no problem, did some research, and didn't get anything on an easy way to do this. So after some work, I present the following:
CREATE FUNCTION ipv4BinaryToString (ipbinary BINARY(4))
RETURNS VARCHAR(32) DETERMINISTIC
BEGIN
DECLARE ipstring VARCHAR(32) DEFAULT '';
DECLARE oct_count SMALLINT UNSIGNED DEFAULT 2;
SET ipstring=CONCAT(ipstring, CAST(CONV(HEX(SUBSTRING(ipbinary,1,1)),16,10) AS CHAR) );
WHILE oct_count < 5 DO
SET ipstring=CONCAT_WS('.',ipstring,CAST(CONV(HEX(SUBSTRING(ipbinary,oct_count,1)),16,10) AS CHAR));
SET oct_count=oct_count+1;
END WHILE;
RETURN ipstring;
END
CREATE FUNCTION ipv4StringToBinary (ipstring VARCHAR(32))
RETURNS VARBINARY(32) DETERMINISTIC
BEGIN
DECLARE ipbinary VARBINARY(32) DEFAULT "";
DECLARE oct_count SMALLINT UNSIGNED DEFAULT 1;
WHILE oct_count < 5 DO
IF LENGTH(ipstring) <= 3 THEN SET ipbinary=CONCAT(ipbinary,unhex(hex(CAST(ipstring AS UNSIGNED)))); END IF;
SET ipbinary=CONCAT(ipbinary,CAST(unhex(hex( CAST(SUBSTRING(ipstring,1,LOCATE('.',ipstring,1)-1) AS UNSIGNED) )) AS BINARY));
SET ipstring=SUBSTRING(ipstring,LOCATE('.',ipstring,1)+1);
SET oct_count=oct_count+1;
END WHILE;
RETURN ipbinary;
END
This may not be the best solution, but work well. returns successfully a binary representation and standard octet string of addresses.
While designing a schema for a event system here at my current job, the task of storing efficiently IPv4 addresses in a MySQL database came up. IPv4 = 4 bytes, okay no problem, did some research, and didn't get anything on an easy way to do this. So after some work, I present the following:
CREATE FUNCTION ipv4BinaryToString (ipbinary BINARY(4))
RETURNS VARCHAR(32) DETERMINISTIC
BEGIN
DECLARE ipstring VARCHAR(32) DEFAULT '';
DECLARE oct_count SMALLINT UNSIGNED DEFAULT 2;
SET ipstring=CONCAT(ipstring, CAST(CONV(HEX(SUBSTRING(ipbinary,1,1)),16,10) AS CHAR) );
WHILE oct_count < 5 DO
SET ipstring=CONCAT_WS('.',ipstring,CAST(CONV(HEX(SUBSTRING(ipbinary,oct_count,1)),16,10) AS CHAR));
SET oct_count=oct_count+1;
END WHILE;
RETURN ipstring;
END
CREATE FUNCTION ipv4StringToBinary (ipstring VARCHAR(32))
RETURNS VARBINARY(32) DETERMINISTIC
BEGIN
DECLARE ipbinary VARBINARY(32) DEFAULT "";
DECLARE oct_count SMALLINT UNSIGNED DEFAULT 1;
WHILE oct_count < 5 DO
IF LENGTH(ipstring) <= 3 THEN SET ipbinary=CONCAT(ipbinary,unhex(hex(CAST(ipstring AS UNSIGNED)))); END IF;
SET ipbinary=CONCAT(ipbinary,CAST(unhex(hex( CAST(SUBSTRING(ipstring,1,LOCATE('.',ipstring,1)-1) AS UNSIGNED) )) AS BINARY));
SET ipstring=SUBSTRING(ipstring,LOCATE('.',ipstring,1)+1);
SET oct_count=oct_count+1;
END WHILE;
RETURN ipbinary;
END
This may not be the best solution, but work well. returns successfully a binary representation and standard octet string of addresses.
Labels: mysql, open source projects, server projects
0 Comments:
Post a Comment
<< Home