←back to thread

The IPv6 Transition

(www.potaroo.net)
215 points todsacerdoti | 2 comments | | HN request time: 0.017s | source
Show context
johnisgood ◴[] No.41902581[source]
This may be a random question, but do any of you have a working code (preferably C or Lua, without regexes but with regexes works too) for: 1) checking if a given string is a valid IPv6 address, and 2) checking if this IPv6 address is in the range? It should handle both IPv4 and IPv6. It must handle edge cases (number of chunks being 8, numbers must be less than 65535 if I am correct, then there is some stuff regarding ":::", etc.). There seemed to be too many edge cases, but maybe I was wrong.

The range check should be in the form of "isIPInRange(ip, cidr)", e.g. isIPInRange("192.168.0.255", "192.168.0.0/24").

It is trivial for IPv4, but not so trivial for IPv6.

If you are wondering why I am not asking LLM this, that is because when (at the time) I did attempt, it failed spectacularly, plus hey, it is HN, someone may find it useful and the more eyes the better anyways (to spot bugs, issues, what have you).

replies(1): >>41903606 #
1. alwayslikethis ◴[] No.41903606[source]
You probably just shouldn't try to do string matching here and just use one of the conversion function to get them to binary, apply the mask, and see if they match
replies(1): >>41908425 #
2. ianburrell ◴[] No.41908425[source]
IPv4 has problems with the non-standard formats that use octal or combine octets. Like 192.168.1 is 192.168.0.1, 127.0.0.1 is 127.1.