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 08:49:15 UTC
[GitHub] [incubator-nuttx] wengzhe commented on a diff in pull request #7506: net/utils: add net_chksum_adjust defined by RFC3022
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