IP-addresses stored as int results in overflow?

后端 未结 11 468
無奈伤痛
無奈伤痛 2021-02-02 12:27

I\'m writing a chat-server in node.js, and I want to store connected users IP-addresses in a mysql database as (unsigned) integers. I have written a javascript method to convert

11条回答
  •  执笔经年
    2021-02-02 13:17

    IP Addresses in the V4 space are unsigned 32 bit numbers, hence the IP address of FF.FF.FF.FF is 2^32 and cannot be greater then that number. Please see:

    This stack overflow article on the same subject

    To turn that number back into an IP address you must break the number down into its 4 parts since each byte is one octet of the address so convert the number to hex and then parse out each pair. You may or may not have to add a leading zero for the first octet.

    Additionally you may have to deal with byte order of the integer ( endien issues ) but since most systems are intel based these days you might not have to deal with that.

提交回复
热议问题