code for validating IPv4 address

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • qazmlp

    code for validating IPv4 address

    What is the best & fastest way of validating an IPv4 address?
    Basically, the input can be either in IPAddressv4 or IPAddressv4:por t
    format.

    Currently I have the following code to validate the first format. Does
    anybody have any comment on this? Also, please suggest a mechanism to
    validate the second format(address: port) also.

    Thanks!

    -----------------------------
    #define INVALID -1
    #define VALID 0

    int validateIP(char *ipadd)
    {
    unsigned b1, b2, b3, b4;
    unsigned char c;

    if (sscanf(ipadd, "%3u.%3u.%3u.%3 u%c", &b1, &b2, &b3, &b4, &c) != 4)
    return INVALID;

    if ((b1 | b2 | b3 | b4) > 255) return INVALID;
    if (strspn(ipadd, "0123456789 .") < strlen(ipadd)) return INVALID;
    return VALID;
    }
  • Burne C

    #2
    Re: code for validating IPv4 address


    "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
    news:db9bbf31.0 307270251.607a4 18c@posting.goo gle.com...[color=blue]
    > What is the best & fastest way of validating an IPv4 address?
    > Basically, the input can be either in IPAddressv4 or IPAddressv4:por t
    > format.
    >
    > Currently I have the following code to validate the first format. Does
    > anybody have any comment on this? Also, please suggest a mechanism to
    > validate the second format(address: port) also.
    >
    > Thanks!
    >
    > -----------------------------
    > #define INVALID -1
    > #define VALID 0
    >
    > int validateIP(char *ipadd)
    > {
    > unsigned b1, b2, b3, b4;[/color]

    I didn't see this kind of declaration before, you just used "unsigned" in here and no type
    specifier. However, I compiled your code with no error in my. Is it legal for using "unsigned" alone
    ?
    [color=blue]
    > unsigned char c;
    >
    > if (sscanf(ipadd, "%3u.%3u.%3u.%3 u%c", &b1, &b2, &b3, &b4, &c) != 4)
    > return INVALID;
    >
    > if ((b1 | b2 | b3 | b4) > 255) return INVALID;
    > if (strspn(ipadd, "0123456789 .") < strlen(ipadd)) return INVALID;
    > return VALID;
    > }[/color]

    --
    BC


    Comment

    • MG

      #3
      Re: code for validating IPv4 address


      "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
      news:db9bbf31.0 307270251.607a4 18c@posting.goo gle.com...[color=blue]
      > What is the best & fastest way of validating an IPv4 address?
      > Basically, the input can be either in IPAddressv4 or IPAddressv4:por t
      > format.
      >
      > Currently I have the following code to validate the first format. Does
      > anybody have any comment on this? Also, please suggest a mechanism to
      > validate the second format(address: port) also.
      >
      > Thanks!
      >
      > -----------------------------
      > #define INVALID -1
      > #define VALID 0
      >
      > int validateIP(char *ipadd)
      > {
      > unsigned b1, b2, b3, b4;
      > unsigned char c;
      >
      > if (sscanf(ipadd, "%3u.%3u.%3u.%3 u%c", &b1, &b2, &b3, &b4, &c) != 4)
      > return INVALID;
      >
      > if ((b1 | b2 | b3 | b4) > 255) return INVALID;
      > if (strspn(ipadd, "0123456789 .") < strlen(ipadd)) return INVALID;
      > return VALID;
      > }[/color]

      just an add on..
      depending upon the situation, you might require some add on checks of the
      reserved IP address...

      MG


      Comment

      • Rouben Rostamian

        #4
        Re: code for validating IPv4 address

        In article <db9bbf31.03072 70251.607a418c@ posting.google. com>,
        qazmlp <qazmlp1209@red iffmail.com> wrote:[color=blue]
        >What is the best & fastest way of validating an IPv4 address?
        >Basically, the input can be either in IPAddressv4 or IPAddressv4:por t
        >format.
        >
        >Currently I have the following code to validate the first format. Does
        >anybody have any comment on this? Also, please suggest a mechanism to
        >validate the second format(address: port) also.[/color]

        [code snipped]
        [color=blue]
        > if ((b1 | b2 | b3 | b4) > 255) return INVALID;[/color]


        That's not good. Surely you meant:

        if (b1 > 255 || b2 > 255 || b3 > 255 || b4 > 255 ) return INVALID;


        --
        Rouben Rostamian <rostamian@umbc .edu>

        Comment

        • Emmanuel Delahaye

          #5
          Re: code for validating IPv4 address

          In 'comp.lang.c', "Burne C" <nobody@notexis t.com> wrote:
          [color=blue][color=green]
          >> unsigned b1, b2, b3, b4;[/color]
          >
          > I didn't see this kind of declaration before, you just used "unsigned"
          > in here and no type specifier. However, I compiled your code with no
          > error in my. Is it legal for using "unsigned" alone ?[/color]

          Yes, 'unsigned' a short name for 'unsigned int', like 'long' is a shortname
          for 'long int' etc.

          --
          -ed- emdelYOURBRA@no os.fr [remove YOURBRA before answering me]
          The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
          <blank line>
          FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

          Comment

          • Cael

            #6
            Re: code for validating IPv4 address


            "Rouben Rostamian" <rouben@pc18.ma th.umbc.edu> wrote in message
            news:bg0ug1$621 $1@pc18.math.um bc.edu...[color=blue]
            > In article <db9bbf31.03072 70251.607a418c@ posting.google. com>,
            > qazmlp <qazmlp1209@red iffmail.com> wrote:[color=green]
            > >What is the best & fastest way of validating an IPv4 address?
            > >Basically, the input can be either in IPAddressv4 or IPAddressv4:por t
            > >format.
            > >
            > >Currently I have the following code to validate the first format. Does
            > >anybody have any comment on this? Also, please suggest a mechanism to
            > >validate the second format(address: port) also.[/color]
            >
            > [code snipped]
            >[color=green]
            > > if ((b1 | b2 | b3 | b4) > 255) return INVALID;[/color]
            >
            >
            > That's not good. Surely you meant:
            >
            > if (b1 > 255 || b2 > 255 || b3 > 255 || b4 > 255 ) return INVALID;
            >[/color]

            I think the statement (b1 | b2 | b3 | b4) > 255 is very effective, the
            result of bitwise OR (b1 | b2 | b3 | b4) is bigger than 255 if any of them
            is bigger than 255.

            [color=blue]
            >
            > --
            > Rouben Rostamian <rostamian@umbc .edu>[/color]

            --
            Cael



            Comment

            • qazmlp

              #7
              Re: code for validating IPv4 address

              qazmlp1209@redi ffmail.com (qazmlp) wrote in message news:<db9bbf31. 0307270251.607a 418c@posting.go ogle.com>...[color=blue]
              > What is the best & fastest way of validating an IPv4 address?
              > Basically, the input can be either in IPAddressv4 or IPAddressv4:por t
              > format.
              >
              > Currently I have the following code to validate the first format. Does
              > anybody have any comment on this? Also, please suggest a mechanism to
              > validate the second format(address: port) also.
              >
              > Thanks!
              >
              > -----------------------------
              > #define INVALID -1
              > #define VALID 0
              >
              > int validateIP(char *ipadd)
              > {
              > unsigned b1, b2, b3, b4;
              > unsigned char c;
              >
              > if (sscanf(ipadd, "%3u.%3u.%3u.%3 u%c", &b1, &b2, &b3, &b4, &c) != 4)
              > return INVALID;
              >
              > if ((b1 | b2 | b3 | b4) > 255) return INVALID;
              > if (strspn(ipadd, "0123456789 .") < strlen(ipadd)) return INVALID;
              > return VALID;
              > }[/color]

              I am expecting your suggestions for validating the 'address:port' format.

              Thanks!

              Comment

              • Dan Pop

                #8
                Re: code for validating IPv4 address

                In <db9bbf31.03072 70251.607a418c@ posting.google. com> qazmlp1209@redi ffmail.com (qazmlp) writes:
                [color=blue]
                >What is the best & fastest way of validating an IPv4 address?
                >Basically, the input can be either in IPAddressv4 or IPAddressv4:por t
                >format.
                >
                >Currently I have the following code to validate the first format. Does
                >anybody have any comment on this? Also, please suggest a mechanism to
                >validate the second format(address: port) also.
                >
                >#define INVALID -1
                >#define VALID 0
                >
                >int validateIP(char *ipadd)
                >{
                > unsigned b1, b2, b3, b4;
                > unsigned char c;
                >
                > if (sscanf(ipadd, "%3u.%3u.%3u.%3 u%c", &b1, &b2, &b3, &b4, &c) != 4)
                > return INVALID;
                >
                > if ((b1 | b2 | b3 | b4) > 255) return INVALID;
                > if (strspn(ipadd, "0123456789 .") < strlen(ipadd)) return INVALID;
                > return VALID;
                >}[/color]

                This looks exactly like code I've posted a couple of years ago. The more
                paranoid check at the end was added in response to some criticism from
                Tak-Shing :-)

                Extending it for IPAddressv4:por t shouldn't be that difficult to anyone
                who has understood how the current version works (which, of course,
                excludes qazmlp).

                #define PORTMAX 65535 /* or whatever appropriate */

                int validateIP(char *ipadd)
                {
                unsigned b1, b2, b3, b4, port = 0;
                unsigned char c;
                int rc;

                rc = sscanf(ipadd, "%3u.%3u.%3u.%3 u:%u%c",
                &b1, &b2, &b3, &b4, &port, &c);
                if (rc != 4 && rc != 5) return INVALID;
                if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
                if (strspn(ipadd, "0123456789 .:") < strlen(ipadd)) return INVALID;
                return VALID;
                }

                Trivia quiz: what is the purpose of the last check?

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • Dan Pop

                  #9
                  Re: code for validating IPv4 address

                  In <bg0l2f$7ev1@im sp212.netvigato r.com> "Burne C" <nobody@notexis t.com> writes:

                  [color=blue]
                  >I didn't see this kind of declaration before, you just used "unsigned" in here and no type
                  >specifier. However, I compiled your code with no error in my. Is it legal for using "unsigned" alone
                  >?[/color]

                  Here's the complete list of C99 type "aliases":

                  - short, signed short, short int, or signed short int
                  - unsigned short, or unsigned short int
                  - int, signed, or signed int
                  - unsigned, or unsigned int
                  - long, signed long, long int, or signed long int
                  - unsigned long, or unsigned long int
                  - long long, signed long long, long long int, or signed long long int
                  - unsigned long long, or unsigned long long int

                  Dan
                  --
                  Dan Pop
                  DESY Zeuthen, RZ group
                  Email: Dan.Pop@ifh.de

                  Comment

                  • Dan Pop

                    #10
                    Re: code for validating IPv4 address

                    In <3f253311.11430 993@news21.on.a ibn.com> Lew.Pitcher@td. com (Lew Pitcher) writes:
                    [color=blue]
                    >On 28 Jul 2003 13:49:08 GMT, Dan.Pop@cern.ch (Dan Pop) wrote:
                    >[snip][color=green]
                    >>
                    >> #define PORTMAX 65535 /* or whatever appropriate */
                    >>
                    >> int validateIP(char *ipadd)
                    >> {
                    >> unsigned b1, b2, b3, b4, port = 0;
                    >> unsigned char sep, c;
                    >> int rc;
                    >>
                    >> rc = sscanf(ipadd, "%3u.%3u.%3u.%3 u:%u%c",
                    >> &b1, &b2, &b3, &b4, &port, &c);
                    >> if (rc != 4 && rc != 5) return INVALID;
                    >> if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
                    >> if (strspn(ipadd, "0123456789 .:") < strlen(ipadd)) return INVALID;
                    >> return VALID;
                    >> }
                    >>
                    >>Trivia quiz: what is the purpose of the last check?[/color]
                    >
                    >Well, I won't profess to know the purpose of the last check, but it /will/
                    >reject an IP address string of "1.2.3.4=", where the prior tests would not.[/color]

                    The original version, as posted by the OP, would catch "1.2.3.4=" because
                    scanf would return 5 instead of 4. Yet, the test is still there.

                    This points out a bug in my version above: "1.2.3.4.5" will not be
                    identified as an invalid IP address. So, back to the drawing board:

                    rc = sscanf(ipadd, "%3u.%3u.%3u.%3 u%c%u%c",
                    &b1, &b2, &b3, &b4, &sep, &port, &c);
                    if (rc != 4 && rc != 6) return INVALID;
                    if (rc == 6 && sep != ':') return INVALID;
                    if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
                    if (strspn(ipadd, "0123456789 .:") < strlen(ipadd)) return INVALID;
                    return VALID;

                    Why is the last check still there?

                    Dan
                    --
                    Dan Pop
                    DESY Zeuthen, RZ group
                    Email: Dan.Pop@ifh.de

                    Comment

                    • James Antill

                      #11
                      Re: code for validating IPv4 address

                      On Mon, 28 Jul 2003 18:13:41 +0000, Dan Pop wrote:
                      [color=blue]
                      > The original version, as posted by the OP, would catch "1.2.3.4=" because
                      > scanf would return 5 instead of 4. Yet, the test is still there.
                      >
                      > This points out a bug in my version above: "1.2.3.4.5" will not be
                      > identified as an invalid IP address. So, back to the drawing board:
                      >
                      > rc = sscanf(ipadd, "%3u.%3u.%3u.%3 u%c%u%c",
                      > &b1, &b2, &b3, &b4, &sep, &port, &c);
                      > if (rc != 4 && rc != 6) return INVALID;
                      > if (rc == 6 && sep != ':') return INVALID;
                      > if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
                      > if (strspn(ipadd, "0123456789 .:") < strlen(ipadd)) return INVALID;
                      > return VALID;
                      >
                      > Why is the last check still there?[/color]

                      I'll assume because strtoul() doens't do any checking on input, which
                      makes "-0" valid as an unsidnged number.

                      You could also argue that because of locale's the above will catch...

                      0.0.0.0:1,000

                      ....but it won't if the thousands seperator is '.' (which it is in germany
                      for instance) ... so it doesn't catch that.

                      The truley strict versions I've seen/written don't use scanf().

                      --
                      James Antill -- james@and.org
                      Need an efficent and powerful string library for C?


                      Comment

                      • qazmlp

                        #12
                        Re: code for validating IPv4 address

                        Dan.Pop@cern.ch (Dan Pop) wrote in message news:<bg39kk$ru 0$2@sunnews.cer n.ch>...[color=blue]
                        > In <db9bbf31.03072 70251.607a418c@ posting.google. com> qazmlp1209@redi ffmail.com (qazmlp) writes:
                        >[color=green]
                        > >What is the best & fastest way of validating an IPv4 address?
                        > >Basically, the input can be either in IPAddressv4 or IPAddressv4:por t
                        > >format.
                        > >
                        > >Currently I have the following code to validate the first format. Does
                        > >anybody have any comment on this? Also, please suggest a mechanism to
                        > >validate the second format(address: port) also.
                        > >
                        > >#define INVALID -1
                        > >#define VALID 0
                        > >
                        > >int validateIP(char *ipadd)
                        > >{
                        > > unsigned b1, b2, b3, b4;
                        > > unsigned char c;
                        > >
                        > > if (sscanf(ipadd, "%3u.%3u.%3u.%3 u%c", &b1, &b2, &b3, &b4, &c) != 4)
                        > > return INVALID;
                        > >
                        > > if ((b1 | b2 | b3 | b4) > 255) return INVALID;
                        > > if (strspn(ipadd, "0123456789 .") < strlen(ipadd)) return INVALID;
                        > > return VALID;
                        > >}[/color]
                        >
                        > This looks exactly like code I've posted a couple of years ago. The more
                        > paranoid check at the end was added in response to some criticism from
                        > Tak-Shing :-)
                        >
                        > Extending it for IPAddressv4:por t shouldn't be that difficult to anyone
                        > who has understood how the current version works (which, of course,
                        > excludes qazmlp).
                        >
                        > #define PORTMAX 65535 /* or whatever appropriate */
                        >
                        > int validateIP(char *ipadd)
                        > {
                        > unsigned b1, b2, b3, b4, port = 0;
                        > unsigned char c;
                        > int rc;
                        >
                        > rc = sscanf(ipadd, "%3u.%3u.%3u.%3 u:%u%c",
                        > &b1, &b2, &b3, &b4, &port, &c);
                        > if (rc != 4 && rc != 5) return INVALID;
                        > if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
                        > if (strspn(ipadd, "0123456789 .:") < strlen(ipadd)) return INVALID;
                        > return VALID;
                        > }[/color]
                        As I mentioned in my original post, the same function should handle
                        the input which can either be in 'ipaddress' or 'ipaddress:port '. Is
                        this possible with a single sscanf? Or, should I do like this:
                        call strchr() to find ':'
                        If found, call the 2-nd version of validateIP() which handles
                        ipaddress:port' format.
                        else, call the 1st version of validateIP() which handles 'ipaddress'
                        format.
                        [color=blue]
                        >
                        > Trivia quiz: what is the purpose of the last check?
                        >
                        > Dan[/color]

                        Comment

                        • Dan Pop

                          #13
                          Re: code for validating IPv4 address

                          In <db9bbf31.03072 82025.24b20445@ posting.google. com> qazmlp1209@redi ffmail.com (qazmlp) writes:
                          [color=blue]
                          >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bg39kk$ru 0$2@sunnews.cer n.ch>...[color=green]
                          >>
                          >> Extending it for IPAddressv4:por t shouldn't be that difficult to anyone
                          >> who has understood how the current version works (which, of course,
                          >> excludes qazmlp).
                          >>
                          >> #define PORTMAX 65535 /* or whatever appropriate */
                          >>
                          >> int validateIP(char *ipadd)
                          >> {
                          >> unsigned b1, b2, b3, b4, port = 0;
                          >> unsigned char c;
                          >> int rc;
                          >>
                          >> rc = sscanf(ipadd, "%3u.%3u.%3u.%3 u:%u%c",
                          >> &b1, &b2, &b3, &b4, &port, &c);
                          >> if (rc != 4 && rc != 5) return INVALID;
                          >> if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
                          >> if (strspn(ipadd, "0123456789 .:") < strlen(ipadd)) return INVALID;
                          >> return VALID;
                          >> }[/color]
                          >As I mentioned in my original post, the same function should handle
                          >the input which can either be in 'ipaddress' or 'ipaddress:port '. Is
                          >this possible with a single sscanf?[/color]

                          If you were not the idiot you actually are, you'd have noticed that this
                          is exactly what the code I've posted is supposed to do.

                          Dan
                          --
                          Dan Pop
                          DESY Zeuthen, RZ group
                          Email: Dan.Pop@ifh.de

                          Comment

                          • Dan Pop

                            #14
                            Re: code for validating IPv4 address

                            In <pan.2003.07.28 .21.10.23.93056 6@and.org> "James Antill" <james-netnews@and.org > writes:
                            [color=blue]
                            >On Mon, 28 Jul 2003 18:13:41 +0000, Dan Pop wrote:
                            >[color=green]
                            >> The original version, as posted by the OP, would catch "1.2.3.4=" because
                            >> scanf would return 5 instead of 4. Yet, the test is still there.
                            >>
                            >> This points out a bug in my version above: "1.2.3.4.5" will not be
                            >> identified as an invalid IP address. So, back to the drawing board:
                            >>
                            >> rc = sscanf(ipadd, "%3u.%3u.%3u.%3 u%c%u%c",
                            >> &b1, &b2, &b3, &b4, &sep, &port, &c);
                            >> if (rc != 4 && rc != 6) return INVALID;
                            >> if (rc == 6 && sep != ':') return INVALID;
                            >> if ((b1 | b2 | b3 | b4) > 255 || port > PORTMAX) return INVALID;
                            >> if (strspn(ipadd, "0123456789 .:") < strlen(ipadd)) return INVALID;
                            >> return VALID;
                            >>
                            >> Why is the last check still there?[/color]
                            >
                            > I'll assume because strtoul() doens't do any checking on input, which
                            >makes "-0" valid as an unsidnged number.[/color]

                            Even simpler, +12 is a valid unsigned number. The other issue is embedded
                            white space: "+12. 34. 56. 78" would pass all the other tests, yet it
                            hardly looks like a valid IP address.
                            [color=blue]
                            > You could also argue that because of locale's the above will catch...[/color]

                            The code was meant to be used in the C locale...

                            Dan
                            --
                            Dan Pop
                            DESY Zeuthen, RZ group
                            Email: Dan.Pop@ifh.de

                            Comment

                            • Default User

                              #15
                              Re: code for validating IPv4 address



                              Dan Pop wrote:
                              [color=blue]
                              > If you were not the idiot you actually are, you'd have noticed that this
                              > is exactly what the code I've posted is supposed to do.[/color]


                              I still can't figure out if this guy (quasilump or whatever) is an
                              elaborate troll or the world's slowest-learning programmer. At least in
                              this thread he actually interacted a bit with the respondents. Usually
                              he declines to respond to any comments or questions.




                              Brian Rodenborn

                              Comment

                              Working...