You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2020/03/10 13:25:25 UTC

[GitHub] [incubator-nuttx] anpaza opened a new pull request #524: inetaddr() more compliant with standards (especially error handling).

anpaza opened a new pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524
 
 
   Standards-compliant inetaddr().

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] anpaza commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
anpaza commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390324008
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,65 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the rightmost
+ *           two bytes of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three bytes
+ *           of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored directly
+ *           into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
   unsigned int a, b, c, d;
-  uint32_t result;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if (a < 0x100 && b < 0x1000000)
+            result = (a << 24) | b;
 
 Review comment:
   Sorry, I got you wrong.
   Fixed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] patacongo commented on issue #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
patacongo commented on issue #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#issuecomment-597109921
 
 
   There were several successful build then the checks were cancelled.  I think that is good enough. Thanks for your patience.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390311224
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,65 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the rightmost
+ *           two bytes of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three bytes
+ *           of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored directly
+ *           into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
   unsigned int a, b, c, d;
-  uint32_t result;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if (a < 0x100 && b < 0x1000000)
+            result = (a << 24) | b;
+          break;
+        }
+
+      case 3:
+        {
+          if (a < 0x100 && b < 0x100 && c < 0x10000)
+            result = (a << 24) | (b << 16) | c;
 
 Review comment:
   The coding standard requires that all if expresseions be enclosed in braces

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390369662
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,75 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the
+ *           binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the
+ *           rightmost two bytes of the binary address. This notation is
+ *           suitable for specifying (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three
+ *           bytes of the binary address. This notation is suitable for
+ *           specifying (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored
+ *           directly into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
-  unsigned int a, b, c, d;
-  uint32_t result;
+  unsigned int a;
+  unsigned int b;
+  unsigned int c;
+  unsigned int d;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if ((a < 0x100) && (b < 0x1000000))
+            {
+              result = (a << 24) | b;
+            }
+          break;
+        }
+
+      case 3:
+        {
+          if ((a < 0x100) && (b < 0x100) && (c < 0x10000))
+            {
+              result = (a << 24) | (b << 16) | c;
 
 Review comment:
   @anpaza and @patacongo but should we call htos(c) here to get the network order? instead call HTONL in one step.
   these two is different:
   (a << 24) | (b << 16) | htos(c)
   htol((a << 24) | (b << 16) | c)
   which one is the right answer?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390369662
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,75 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the
+ *           binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the
+ *           rightmost two bytes of the binary address. This notation is
+ *           suitable for specifying (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three
+ *           bytes of the binary address. This notation is suitable for
+ *           specifying (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored
+ *           directly into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
-  unsigned int a, b, c, d;
-  uint32_t result;
+  unsigned int a;
+  unsigned int b;
+  unsigned int c;
+  unsigned int d;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if ((a < 0x100) && (b < 0x1000000))
+            {
+              result = (a << 24) | b;
+            }
+          break;
+        }
+
+      case 3:
+        {
+          if ((a < 0x100) && (b < 0x100) && (c < 0x10000))
+            {
+              result = (a << 24) | (b << 16) | c;
 
 Review comment:
   @anpaza and @patacongo but should we call htos(c) here to get the network order? case 1/2 has the similar issue.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390317817
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,65 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the rightmost
+ *           two bytes of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three bytes
+ *           of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored directly
+ *           into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
   unsigned int a, b, c, d;
-  uint32_t result;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if (a < 0x100 && b < 0x1000000)
+            result = (a << 24) | b;
 
 Review comment:
   Let me be more clear.  You do not have to add any parentheses.  That was all fine.  You need braces.  This should be like:
   
       if (a < 0x100 && b < 0x1000000)
         {
           result = (a << 24) | b;
         }
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390311324
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,65 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the rightmost
+ *           two bytes of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three bytes
+ *           of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored directly
+ *           into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
   unsigned int a, b, c, d;
-  uint32_t result;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if (a < 0x100 && b < 0x1000000)
+            result = (a << 24) | b;
+          break;
+        }
+
+      case 3:
+        {
+          if (a < 0x100 && b < 0x100 && c < 0x10000)
+            result = (a << 24) | (b << 16) | c;
+          break;
+        }
+
+      case 4:
+        {
+          if (a < 0x100 && b < 0x100 && c < 0x100 && d < 0x100)
+          result = (a << 24) | (b << 16) | (c << 8) | d;
 
 Review comment:
   The coding standard requires that all if expresseions be enclosed in braces

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390310512
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,65 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the rightmost
+ *           two bytes of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three bytes
+ *           of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored directly
+ *           into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
   unsigned int a, b, c, d;
-  uint32_t result;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if (a < 0x100 && b < 0x1000000)
+            result = (a << 24) | b;
 
 Review comment:
   The coding standard requires that all if expresseions be enclosed in braces.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] patacongo commented on issue #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
patacongo commented on issue #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#issuecomment-597103327
 
 
   Thanks... I will merge the change once all of the checks complete.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] patacongo merged pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
patacongo merged pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524
 
 
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390310512
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,65 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the rightmost
+ *           two bytes of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three bytes
+ *           of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored directly
+ *           into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
   unsigned int a, b, c, d;
-  uint32_t result;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if (a < 0x100 && b < 0x1000000)
+            result = (a << 24) | b;
 
 Review comment:
   The coding standard requires that all if expresseions be enclosed in braces.  https://cwiki.apache.org/confluence/display/NUTTX/Coding+Standard#ifthenelse
   
   nxstyle does not catch this.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
patacongo commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390318099
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,65 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the rightmost
+ *           two bytes of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three bytes
+ *           of the binary address. This notation is suitable for specifying
+ *           (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored directly
+ *           into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
   unsigned int a, b, c, d;
-  uint32_t result;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if (a < 0x100 && b < 0x1000000)
+            result = (a << 24) | b;
 
 Review comment:
   the same is true with all of the other comments that mark as resolved but not fixed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390369662
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,75 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the
+ *           binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the
+ *           rightmost two bytes of the binary address. This notation is
+ *           suitable for specifying (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three
+ *           bytes of the binary address. This notation is suitable for
+ *           specifying (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored
+ *           directly into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
-  unsigned int a, b, c, d;
-  uint32_t result;
+  unsigned int a;
+  unsigned int b;
+  unsigned int c;
+  unsigned int d;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if ((a < 0x100) && (b < 0x1000000))
+            {
+              result = (a << 24) | b;
+            }
+          break;
+        }
+
+      case 3:
+        {
+          if ((a < 0x100) && (b < 0x100) && (c < 0x10000))
+            {
+              result = (a << 24) | (b << 16) | c;
 
 Review comment:
   @anpaza and @patacongo but should we call htos(c) here to get the network order? instead call HTONL in one step.
   these two is different:
   (a << 24) | (b << 16) | htos(c)
   htol((a << 24) | (b << 16) | c)
   which one is the right answer?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #524: inetaddr() more compliant with standards (especially error handling).
URL: https://github.com/apache/incubator-nuttx/pull/524#discussion_r390369662
 
 

 ##########
 File path: libs/libc/net/lib_inetaddr.c
 ##########
 @@ -56,19 +56,75 @@
  *   standard IPv4 dotted decimal notation, to an integer value suitable for
  *   use as an Internet address.
  *
+ *   inet_aton() returns nonzero if the address is valid, zero if not.
+ *   The address supplied in cp can have one of the following forms:
+ *
+ *   a.b.c.d Each of the four numeric parts specifies a byte of the address;
+ *           the bytes are assigned in left-to-right order to produce the
+ *           binary address.
+ *
+ *   a.b.c   Parts a and b specify the first two bytes of the binary address.
+ *           Part c is interpreted as a 16-bit value that defines the
+ *           rightmost two bytes of the binary address. This notation is
+ *           suitable for specifying (outmoded) Class B network addresses.
+ *
+ *   a.b     Part a specifies the first byte of the binary address. Part b is
+ *           interpreted as a 24-bit value that defines the rightmost three
+ *           bytes of the binary address. This notation is suitable for
+ *           specifying (outmoded) Class A network addresses.
+ *
+ *   a       The value a is interpreted as a 32-bit value that is stored
+ *           directly into the binary address without any byte rearrangement.
+ *
+ * Returned Value:
+ *   If input string cannot be recognized as a valid IPv4 number, function
+ *   returns zero.
+ *
  ****************************************************************************/
 
 in_addr_t inet_addr(FAR const char *cp)
 {
-  unsigned int a, b, c, d;
-  uint32_t result;
+  unsigned int a;
+  unsigned int b;
+  unsigned int c;
+  unsigned int d;
+  uint32_t result = 0;
+
+  switch (sscanf(cp, "%u.%u.%u.%u", &a, &b, &c, &d))
+    {
+      case 1:
+        {
+          result = a;
+          break;
+        }
+
+      case 2:
+        {
+          if ((a < 0x100) && (b < 0x1000000))
+            {
+              result = (a << 24) | b;
+            }
+          break;
+        }
+
+      case 3:
+        {
+          if ((a < 0x100) && (b < 0x100) && (c < 0x10000))
+            {
+              result = (a << 24) | (b << 16) | c;
 
 Review comment:
   @anpaza and @patacongo but should we call htos(c) here to get the network order? instead call HTONL in one step.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services