C code for validating ip address

Rated 4.34/5 based on 531 customer reviews

Basically, the input can be either in IPAddressv4 or IPAddressv4:portformat.

Currently I have the following code to validate the first format. Also, please suggest a mechanism tovalidate the second format(address:port) also.

0.0.0.0:1,000 ...it won't if the thousands seperator is '.' (which it is in germany for instance) ... The truley strict versions I've seen/written don't use scanf().

As may be seen in the reference, to check a numeric representation, there may be 1 to 4 parts, and in each case different limitations on the parts apply.So, at the very least, the reformatted "0.0.0.3" should be returned to the user application so the user knows how their input was interpreted. The source code can be viewed from the repository, so that might provide some ideas for improvements, if you feel there are any.For instance, this here is typically it's own method, call this simply Now that you have this, if you ever want to extend this class to also look for if the IP is localhost or if it is a broadcast address, it is pretty easy to do this. You can also tell exactly which one of your validation rules was broken in the validation of the IP address string. I would suggest using what's available on the platform/framework you are developing.If there's nothing you consider good enough, then you might just want to add a compiled regex to a helper class. This library supports IPv4 and IPv6 transparently, so validating either works the same below, and it also supports CIDR prefix-length IPC4 addresses as well.

Leave a Reply