net_chksum.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /****************************************************************************
  2. * net/utils/net_chksum.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #ifdef CONFIG_NET
  25. #include "utils/utils.h"
  26. /****************************************************************************
  27. * Public Functions
  28. ****************************************************************************/
  29. /****************************************************************************
  30. * Name: chksum
  31. *
  32. * Description:
  33. * Calculate the raw change sum over the memory region described by
  34. * data and len.
  35. *
  36. * Input Parameters:
  37. * sum - Partial calculations carried over from a previous call to
  38. * chksum(). This should be zero on the first time that check
  39. * sum is called.
  40. * data - Beginning of the data to include in the checksum.
  41. * len - Length of the data to include in the checksum.
  42. *
  43. * Returned Value:
  44. * The updated checksum value.
  45. *
  46. ****************************************************************************/
  47. #ifndef CONFIG_NET_ARCH_CHKSUM
  48. uint16_t chksum(uint16_t sum, FAR const uint8_t *data, uint16_t len)
  49. {
  50. FAR const uint8_t *dataptr;
  51. FAR const uint8_t *last_byte;
  52. uint16_t t;
  53. dataptr = data;
  54. last_byte = data + len - 1;
  55. while (dataptr < last_byte)
  56. {
  57. /* At least two more bytes */
  58. t = ((uint16_t)dataptr[0] << 8) + dataptr[1];
  59. sum += t;
  60. if (sum < t)
  61. {
  62. sum++; /* carry */
  63. }
  64. dataptr += 2;
  65. }
  66. if (dataptr == last_byte)
  67. {
  68. t = (dataptr[0] << 8) + 0;
  69. sum += t;
  70. if (sum < t)
  71. {
  72. sum++; /* carry */
  73. }
  74. }
  75. /* Return sum in host byte order. */
  76. return sum;
  77. }
  78. #endif /* CONFIG_NET_ARCH_CHKSUM */
  79. /****************************************************************************
  80. * Name: net_chksum
  81. *
  82. * Description:
  83. * Calculate the Internet checksum over a buffer.
  84. *
  85. * The Internet checksum is the one's complement of the one's complement
  86. * sum of all 16-bit words in the buffer.
  87. *
  88. * See RFC1071.
  89. *
  90. * If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be
  91. * provided by architecture-specific logic.
  92. *
  93. * Input Parameters:
  94. *
  95. * buf - A pointer to the buffer over which the checksum is to be computed.
  96. *
  97. * len - The length of the buffer over which the checksum is to be
  98. * computed.
  99. *
  100. * Returned Value:
  101. * The Internet checksum of the buffer.
  102. *
  103. ****************************************************************************/
  104. #ifndef CONFIG_NET_ARCH_CHKSUM
  105. uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
  106. {
  107. return htons(chksum(0, (uint8_t *)data, len));
  108. }
  109. #endif /* CONFIG_NET_ARCH_CHKSUM */
  110. #endif /* CONFIG_NET */