• Danny M@lemmy.escapebigtech.info
    link
    fedilink
    arrow-up
    85
    arrow-down
    2
    ·
    edit-2
    1 year ago

    Please don’t. Use regex to find something that looks like an IP then build a real parser. This is madness, its’s extremely hard to read and a mistake is almost impossible to spot. Not to mention that it’s slow.

    Just parse [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3} using regex (for v4) and then have some code check that all the octets are valid (and store the IP as a u32).

      • azertyfun
        link
        fedilink
        arrow-up
        6
        ·
        1 year ago

        Fuck that, if for whatever reason I’m writing an IP validator by hand I’m disallowing leading zeros. Parsers are very inconsistent, some will parse 010 as 10, others as 0o10 == 8 (you can try that right now with a POSIX ping). Talk about a footgun.

      • Danny M@lemmy.escapebigtech.info
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        1 year ago

        Definitely, tho if you store it as a u32 that is fixed magically. Because 1.2.3.4 and 1.02.003.04 both map to the same number.

        What I mean by storing it as a u32 is to convert it to a number, similar to how the IP gets sent over the wire, so for v4:

        octet[3] | octet[2] << 8 | octet[1] << 16 | octet[0] << 24

        or in more human terms:

        (fourth octet) + (third octet * 256) + (second octet * 256^2) + (first octet * 256^3)
        
        • p1mrx
          link
          fedilink
          arrow-up
          2
          ·
          1 year ago

          Because 1.2.3.4 and 1.02.003.04 both map to the same number.

          But 10.20.30.40 and 010.020.030.040 map to different numbers. It’s often best to reject IPv4 addresses with leading zeroes to avoid the decimal vs. octal ambiguity.

            • p1mrx
              link
              fedilink
              arrow-up
              2
              ·
              1 year ago

              It’s not about how people write them, it’s how parsers parse them. IPv4 has been around since 1982, and most parsers interpret leading zeros as octal.

        • Emma_Gold_Man@lemmy.dbzer0.com
          link
          fedilink
          arrow-up
          2
          ·
          1 year ago

          True enough for database or dictionary storage, but a lot of times things get implemented in arrays where you still wind up with two copies of the same uint32.