• FauxPseudo @lemmy.world
    link
    fedilink
    arrow-up
    249
    arrow-down
    4
    ·
    1 year ago

    Ok. This covers every ipv6 and ipv4 address.

    “^\s*((([0-9A-Fa-f]1,4}:){7}([0-9A-Fa-f]{1,4}:))|(([0-9A-Fa-f]{1,4:)6}(:[0-9A-Fa-f]{1,4}((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3)|:))|(([0-9A-Fa-f]1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2}):((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3)|:))|(([0-9A-Fa-f]1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})((:[0-9A-Fa-f]{1,4)?:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]))3})):))|(([0-9A-Fa-f]{1,4:)3}(((:[0-9A-Fa-f]{1,4}){1,4})((:[0-9A-Fa-f]{1,4)0,2}:((25[0-5]2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3))|:))|(([0-9A-Fa-f]1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})((:[0-9A-Fa-f]{1,4)0,3}:((25[0-5]2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3))|:))|(([0-9A-Fa-f]1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})((:[0-9A-Fa-f]{1,4)0,4}:((25[0-5]2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3))|:))|(:(((:[0-9A-Fa-f]1,4}){1,7})((:[0-9A-Fa-f]{1,4)0,5}:((25[0-5]2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3))|:)))(%.+)?\s*$”

    • Danny M@lemmy.escapebigtech.info
      link
      fedilink
      arrow-up
      69
      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@sh.itjust.works
          link
          fedilink
          arrow-up
          1
          ·
          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
          0
          ·
          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@sh.itjust.works
            link
            fedilink
            arrow-up
            1
            ·
            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.

      • Centillionaire@kbin.social
        link
        fedilink
        arrow-up
        66
        arrow-down
        1
        ·
        1 year ago

        That would allow for like, 2 trillion devices? Feels like a bandaid, my dude. Next you’re gonna suggest a giant ice cube in the ocean once a year to stop global warming.

        • stoy@lemmy.zip
          link
          fedilink
          arrow-up
          11
          ·
          1 year ago

          So add two more octets:

          Moat companies will still just use something like 10.0.13.37.0.1

              • dohpaz42@lemmy.world
                link
                fedilink
                English
                arrow-up
                14
                arrow-down
                1
                ·
                1 year ago

                My dude, you used the 10.xx private IP as an example. Why wouldn’t they assume you were referring to internal networks?

                • stoy@lemmy.zip
                  link
                  fedilink
                  arrow-up
                  1
                  arrow-down
                  1
                  ·
                  1 year ago

                  I thought it was pretty clear with me adding 13.37 that I was making a joke, the earlier post spoke about how just adding one octet would still be too few addresses, so I joked about adding one more octet.

                  • dohpaz42@lemmy.world
                    link
                    fedilink
                    English
                    arrow-up
                    2
                    ·
                    1 year ago

                    I’m only pointing out why the other poster would make the assumption you were referring to an internal network. Do with it what you will.

          • intensely_human@lemm.ee
            link
            fedilink
            arrow-up
            5
            ·
            1 year ago

            Hurricanes cannot cross the equator. The equator is an imaginary line, and hence has zero mass. We can end every hurricane using zero point zero energy (0.0).

        • alienzx@feddit.nl
          link
          fedilink
          English
          arrow-up
          1
          arrow-down
          1
          ·
          edit-2
          1 year ago

          You could follow this logic and add 2 alphanumeric digits before 4 numeric octets. E.g. xf.192.168.1.1

          This would at least keep it looking like an IP and not a Mac address. Another advantage would be graceful ipv4 handling with a reserved range starting with “ip” like ip.10.10.10.1

      • Nalivai@discuss.tchncs.de
        link
        fedilink
        arrow-up
        15
        ·
        1 year ago

        Oh yeah, great, let’s change the fundamental protocol on which all the networks in the world are based. Now two third of the devices in the world crashed because you tried to ping 192.168.0.0.1

      • Patches@sh.itjust.works
        link
        fedilink
        arrow-up
        11
        ·
        edit-2
        1 year ago

        Made that joke in an interview once.

        They didn’t think it was funny. They truly thought Regex was the solution to, but never the cause of, all problems.

        They wanted to make a Regex to verify every single address in the world. Dodged a bullet

        • rob64@startrek.website
          link
          fedilink
          arrow-up
          5
          ·
          edit-2
          1 year ago

          Holy hell yeah you did. How would you go about doing that in a single expression? A bunch of back references to figure out the country? What if that’s not included? Oy.

          • Patches@sh.itjust.works
            link
            fedilink
            arrow-up
            4
            ·
            edit-2
            1 year ago

            You wouldn’t. It’s not possible. Which is what I told them.

            And why would you want to? Legally if you change the given address, and it fails to get delivered - that is on you. Not them.

            Some countries have addresses that are literally ‘Last house on the left by the Big Tree. Bumban(Neighborhood). NN (Country)’. Any US Centric validation would fail this but I assure you - mail gets delivered just fine.