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 2022/11/02 06:27:41 UTC
[GitHub] [incubator-nuttx] wengzhe opened a new pull request, #7506: net/utils: add net_chksum_adjust defined by RFC3022
wengzhe opened a new pull request, #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506
## Summary
net_chksum_adjust is used for fast checksum adjustment after modifying some fields in a network packet. Will be used in NAT.
## Impact
N/A
## Testing
pass CI
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] wengzhe commented on pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
Posted by GitBox <gi...@apache.org>.
wengzhe commented on PR #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506#issuecomment-1299780642
> Maybe can use but shift instead on multiply/divide 256?
@pkarashchenko I'm not sure if it will be optimized by compiler, and this version is **exactly** the same as [RFC 3022, Section 4.2, Page 9](https://www.rfc-editor.org/rfc/rfc3022#section-4.2) at this point for convenience.
It seems that developers who read this code may worry about the details (and sometimes so do I), I'd like to change data pointer to `uint16_t *` and use `HTONS`/`NTOHS` macros instead. Let me update it.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] pkarashchenko commented on a diff in pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on code in PR #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506#discussion_r1011650221
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,69 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, as described in RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint16_t *chksum,
+ FAR const uint16_t *optr, ssize_t olen,
+ FAR const uint16_t *nptr, ssize_t nlen)
+{
+#ifdef CONFIG_ENDIAN_BIG
+#warning "Not verified on big-endian yet."
Review Comment:
```suggestion
# warning "Not verified on big-endian yet."
```
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] wengzhe commented on a diff in pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
Posted by GitBox <gi...@apache.org>.
wengzhe commented on code in PR #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506#discussion_r1011870239
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,69 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, as described in RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint16_t *chksum,
+ FAR const uint16_t *optr, ssize_t olen,
+ FAR const uint16_t *nptr, ssize_t nlen)
+{
+#ifdef CONFIG_ENDIAN_BIG
+#warning "Not verified on big-endian yet."
Review Comment:
Done.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] pkarashchenko commented on pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on PR #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506#issuecomment-1299854601
Let's not change pointers and use it as is
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] wengzhe commented on pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
Posted by GitBox <gi...@apache.org>.
wengzhe commented on PR #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506#issuecomment-1299872806
> Let's not change pointers and use it as is
Since the algorithm is applicable only for even offsets and even lengths, I think there's no difference even if we use `uint16_t *` instead (because data will always be processed per 2 bytes) ? And I think use `uint16_t *` might have stronger indication that we are aligned to 2 bytes (just like `net_chksum`)
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] pkarashchenko commented on a diff in pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on code in PR #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506#discussion_r1011299526
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
+ x = ~x & 0xffff;
+ while (olen)
Review Comment:
```suggestion
while (olen > 0)
```
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
+ x = ~x & 0xffff;
+ while (olen)
+ {
+ oldval = optr[0] * 256 + optr[1];
+ optr += 2;
+ x -= oldval & 0xffff;
+ if (x <= 0)
+ {
+ x--;
+ x &= 0xffff;
+ }
+
+ olen -= 2;
+ }
+ while (nlen)
Review Comment:
```suggestion
while (nlen > 0)
```
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
Review Comment:
Let's add `#error` in case trying to compile if for the big endian
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
Review Comment:
```suggestion
x = (int32_t)chksum[0] * 256 + chksum[1];
```
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
+ x = ~x & 0xffff;
+ while (olen)
+ {
+ oldval = optr[0] * 256 + optr[1];
Review Comment:
```suggestion
oldval = (int32_t)optr[0] * 256 + optr[1];
```
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
+ x = ~x & 0xffff;
+ while (olen)
+ {
+ oldval = optr[0] * 256 + optr[1];
+ optr += 2;
+ x -= oldval & 0xffff;
+ if (x <= 0)
+ {
+ x--;
+ x &= 0xffff;
+ }
+
+ olen -= 2;
+ }
+ while (nlen)
+ {
+ newval = nptr[0] * 256 + nptr[1];
+ nptr += 2;
+ x += newval & 0xffff;
+ if (x & 0x10000)
Review Comment:
```suggestion
if (x & 0x10000 != 0)
```
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
+ x = ~x & 0xffff;
+ while (olen)
+ {
+ oldval = optr[0] * 256 + optr[1];
+ optr += 2;
+ x -= oldval & 0xffff;
+ if (x <= 0)
+ {
+ x--;
+ x &= 0xffff;
+ }
+
+ olen -= 2;
+ }
+ while (nlen)
+ {
+ newval = nptr[0] * 256 + nptr[1];
Review Comment:
```suggestion
newval = (int32_t)nptr[0] * 256 + nptr[1];
```
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] wengzhe commented on a diff in pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
Posted by GitBox <gi...@apache.org>.
wengzhe commented on code in PR #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506#discussion_r1011362949
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
+ x = ~x & 0xffff;
+ while (olen)
+ {
+ oldval = optr[0] * 256 + optr[1];
+ optr += 2;
+ x -= oldval & 0xffff;
+ if (x <= 0)
+ {
+ x--;
+ x &= 0xffff;
+ }
+
+ olen -= 2;
+ }
+ while (nlen)
Review Comment:
Done.
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
Review Comment:
`NTOHS` is used instead.
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
+ x = ~x & 0xffff;
+ while (olen)
+ {
+ oldval = optr[0] * 256 + optr[1];
+ optr += 2;
+ x -= oldval & 0xffff;
+ if (x <= 0)
+ {
+ x--;
+ x &= 0xffff;
+ }
+
+ olen -= 2;
+ }
+ while (nlen)
+ {
+ newval = nptr[0] * 256 + nptr[1];
Review Comment:
`NTOHS` is used instead.
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
+ x = ~x & 0xffff;
+ while (olen)
Review Comment:
Done.
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
+ x = ~x & 0xffff;
+ while (olen)
+ {
+ oldval = optr[0] * 256 + optr[1];
Review Comment:
`NTOHS` is used instead.
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
+
+ int32_t x;
+ int32_t oldval;
+ int32_t newval;
+
+ x = chksum[0] * 256 + chksum[1];
+ x = ~x & 0xffff;
+ while (olen)
+ {
+ oldval = optr[0] * 256 + optr[1];
+ optr += 2;
+ x -= oldval & 0xffff;
+ if (x <= 0)
+ {
+ x--;
+ x &= 0xffff;
+ }
+
+ olen -= 2;
+ }
+ while (nlen)
+ {
+ newval = nptr[0] * 256 + nptr[1];
+ nptr += 2;
+ x += newval & 0xffff;
+ if (x & 0x10000)
Review Comment:
Done.
##########
net/utils/net_chksum.c:
##########
@@ -123,4 +123,66 @@ uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
}
#endif /* CONFIG_NET_ARCH_CHKSUM */
+/****************************************************************************
+ * Name: net_chksum_adjust
+ *
+ * Description:
+ * Adjusts the checksum of a packet without having to completely
+ * recalculate it, exactly the same as RFC 3022, Section 4.2, Page 9.
+ *
+ * Input Parameters:
+ * chksum - points to the chksum in the packet
+ * optr - points to the old data in the packet
+ * olen - length of old data
+ * nptr - points to the new data in the packet
+ * nlen - length of new data
+ *
+ * Limitations:
+ * The algorithm is applicable only for even offsets and even lengths.
+ ****************************************************************************/
+
+void net_chksum_adjust(FAR uint8_t *chksum,
+ FAR const uint8_t *optr, size_t olen,
+ FAR const uint8_t *nptr, size_t nlen)
+{
+ /* TODO: Verify this works for little- and big-endian. */
Review Comment:
`NTOHS` is used, so make it as a warning.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] wengzhe commented on pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
Posted by GitBox <gi...@apache.org>.
wengzhe commented on PR #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506#issuecomment-1300127922
> Ok. If w are sure that pointers are properly aligned, we can used htons/ntohs
Yeah, checksum in network packets (and the source data to calculate it) will be surely aligned to 16bits. (Otherwise the function `net_chksum` will also get into same problem.)
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] pkarashchenko merged pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
Posted by GitBox <gi...@apache.org>.
pkarashchenko merged PR #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-nuttx] pkarashchenko commented on pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
Posted by GitBox <gi...@apache.org>.
pkarashchenko commented on PR #7506:
URL: https://github.com/apache/incubator-nuttx/pull/7506#issuecomment-1300064434
Ok. If w are sure that pointers are properly aligned, we can used htons/ntohs
--
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.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org