Validating ip address c
If I were a lecturer, I'd show your code to the students as an example why they should always use libraries, even doing something seemingly simple as validating the IPv4 address. Besides that, we are starting to make the migration to the IPv6 standard, which is harder to validate.
So, if you can rely on a well tested library for IP validation that does not bring an additional huge dependency to your code, use that instead.
Clarification is not invalidation — there is no intention in Rev 3 to change the question.
Correcting wrong assumptions happens all the time on Stack Exchange sites. Your code has three major errors that all the previous reviewers failed to spot.
Still, you're right that it's a good point to make. Allowing empty spaces after and before would be a much more user friendly option, but it is still rejected by the function. Besides, always consider possible future security concerns. A few additional comments to complement the other review: The first error message has "octect" but what was meant was undoubtedly "octet".
Because error strings are tied to enum values, it is a good thing to specify them in the actual enum construct.
as input is a benefit to the caller because it makes the validation less strict while still correct (so more user-friendly).
#define PORTMAX 65535 /* or whatever appropriate */ int validate IP(char *ipadd) As I mentioned in my original post, the same function should handle the input which can either be in 'ipaddress' or 'ipaddress:port'. Or, should I do like this: call strchr() to find ':' If found, call the 2-nd version of validate IP() which handles ipaddress:port' format.
else, call the 1st version of validate IP() which handles 'ipaddress' format.