Module talk:IPAddress

This is an old revision of this page, as edited by L235 (talk | contribs) at 09:23, 14 December 2020 (isIpOrRange: Replying to Johnuniq (using reply-link)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Latest comment: 4 years ago by L235 in topic isIpOrRange

isIp

Takes a single argument, a string, and returns:

  • "6" if it is a valid IPV6 address
  • "4" if it is a valid IPV6 address
  • empty string if it is neither.

When applied to {{PAGENAME}}, this helps determine whether a template is being placed on an anonymous user page/user talk page or not, as used in Template:AfC talk:

{{#if:{{IsIPAddress|{{PAGENAME}}}}|I'm on an anonymous IP user or user talk page|I'm on some other page}}

Note that extra whitespace is not permitted (e.g. "127.0.0.1 " with an extra space at the end is not a valid IP address).

isIpV6

Returns 1 if the given argument is a valid IPv6 address, else 0.

isIpV4

Returns 1 if the given argument is a valid IPv4 address, else 0.

More than 3 digits in a group

Is '00000000000005.10.10.10' a correct IPv4 address? --DixonD (talk) 07:53, 8 March 2013 (UTC)Reply

good question. it's not so much about "more than 3 digits" as it's about leading zeroes: is 12.02.4.4 legal? how about 12.00.4.4? what are the rules?
current code allows those, as well as 1.00000000100.2.2, i.e, any number can be preceded by any number of 0's. it's relatively easy to disallow. from Dot-decimal notation#Caveat it can be concluded that preceding with 0's is not a good idea, and can lead to confusion- this may be interpreted sometimes to mean "octal". should i change the code to disallow? should we add it to the tests? peace - קיפודנחש (aka kipod) (talk) 09:06, 8 March 2013 (UTC)Reply
Yes, I think it is better to disallow. I would change the regexp from "^(%d+)%.(%d+)%.(%d+)%.(%d+)$" to something like "^(%d{1,3})%.(%d{1,3})%.(%d{1,3})%.(%d{1,3})$". --DixonD (talk) 12:11, 8 March 2013 (UTC)Reply
technically, what you suggest is not viable, because lua's string comparison is not real regex, and specifically, there's no support for quantifiers in curlies. It would also still allow 000 or 020 etc., which should also be disallowed. However, as i mentioned, the solution is not difficult. --קיפודנחש (aka kipod) (talk) 14:11, 8 March 2013 (UTC)Reply
i did it the way i thought is right, i.e., disallow leading 0 (i.e., 0 is OK, but 02 or 00 is not). this cause one test to fail, for the string "00.00.00.00". not sure if this should fail (your answer above indicate you think it should), in which case we need to modify the test, or maybe it should pass, in which case we should modify the code. wither is easy. can someone point to an authoritative source that explains which of the following (if any) is legal?
00.00.00.00
000000012.13.14.15
012.13.14.15
01.02.03.04
if no authoritative source can be found, your opinion will be appreciated anyway. also, is there past behavior we should emulate, regardless of "what's right"? thanks. peace - קיפודנחש (aka kipod) (talk) 16:37, 8 March 2013 (UTC)Reply
Ping in Ubuntu treats numbers starting with 0 as octal numbers. 012.13.14.15 is legal (actually in decimal: 10.13.14.15), but 08.8.8.8 is not valid (8 is not octal number). --Harriv (talk) 19:14, 16 March 2013 (UTC)Reply

Requested move 13 July 2018

The following is a closed discussion of a requested move. Please do not modify it. Subsequent comments should be made in a new section on the talk page. Editors desiring to contest the closing decision should consider a move review. No further edits should be made to this section.

The result of the move request was: no consensus to move the module to the proposed title at this time, per the discussion below. Dekimasuよ! 02:26, 21 July 2018 (UTC)Reply


Module:IPAddressModule:Validate IP – Clearer module name; the current one is not meaningfully distinct from Module:IP {{3x|p}}ery (talk) 21:16, 13 July 2018 (UTC)Reply

  • Support: Module names should be even more accurately descriptive than template ones, since we do not need module names to be excessively concise for repeated typing in wikisource.  — SMcCandlish ¢ 😼  22:33, 13 July 2018 (UTC)Reply
  • Oppose The term "IP" has no immediately clear meaning and "Validate IP" might mean anything—"Address" is needed for context and preciseness. "Validate IP Address" would be silly. This is a make-work proposal with no benefit. Despite the MfD there is no reason to dictate what this module might do in the future by giving it a one-function-only title. Johnuniq (talk) 00:00, 14 July 2018 (UTC)Reply
    +1. also note all the interwiki. we do not yet have central (a la common) modules, so every interwiki is a direct copy. it is tangibly better to keep the same name used by all the other wikipedias that chose to copy this module. peace - קיפודנחש (aka kipod) (talk) 03:22, 16 July 2018 (UTC)Reply

The above discussion is preserved as an archive of a requested move. Please do not modify it. Subsequent comments should be made in a new section on this talk page or in a move review. No further edits should be made to this section.

Template ((IP-user other)) now uses Module:IPAddress

{{IP-user other}} had IPv4 and IPv6 detection using template code only, but Module:IPAddress does better detecting. So I made it so {{IP-user other}} now uses Module:IPAddress . So thanks for this nice module. (I am planning to use this stuff in some system messages later on.)

--David Göthberg (talk) 22:22, 27 December 2019 (UTC)Reply

isIpOrRange

@KevinL: Per the discussion at my talk (permalink), I have added a function you can use to Module:IPAddress/sandbox. Once you're happy with it, it can be moved to Module:IPAddress. I guess that is the best place. This module is full-protected which might suit your purposes.

The function returns 'ip' if the input is an IPv4 or IPv6 address, or 'range' if a CIDR IPv4 or IPv6 range. Otherwise it returns an empty string. That should make it easy for testing with #if. Some examples of how to call it follow.

  • {{#invoke:IPAddress/sandbox|isIpOrRange|1.2.3.4}} → ip
  • {{#invoke:IPAddress/sandbox|isIpOrRange| 1.2.3.4 }} → ip
  • {{#invoke:IPAddress/sandbox|isIpOrRange|1.2.3.0/24}} → range
  • {{#invoke:IPAddress/sandbox|isIpOrRange|2606:6000:610a:9000:9df6:cc8d:592b:1ae}} → ip
  • {{#invoke:IPAddress/sandbox|isIpOrRange|2606:6000:610a:9000::/64}} → range
  • {{#invoke:IPAddress/sandbox|isIpOrRange}}
  • {{#invoke:IPAddress/sandbox|isIpOrRange|}}
  • {{#invoke:IPAddress/sandbox|isIpOrRange|Jimbo}}

The second example shows that leading/trailing whitespace is ignored. If some other way of calling it is wanted, or some change, let me know. I'm posting here to alert others watching this page. Johnuniq (talk) 09:20, 14 December 2020 (UTC)Reply

@Johnuniq: This is perfect – thanks for your fast work here! Please go ahead and move this to the main module. I really appreciate it. Best, KevinL (aka L235 · t · c) 09:23, 14 December 2020 (UTC)Reply