uip_arch.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2001, Adam Dunkels.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote
  14. * products derived from this software without specific prior
  15. * written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  18. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  23. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * This file is part of the uIP TCP/IP stack.
  30. *
  31. * $Id: uip_arch.c,v 1.2.2.1 2003/10/04 22:54:17 adam Exp $
  32. *
  33. */
  34. #include "uip.h"
  35. #include "uip_arch.h"
  36. #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
  37. #define IP_PROTO_TCP 6
  38. /*-----------------------------------------------------------------------------------*/
  39. void uip_add32(u8_t *op32, u16_t op16)
  40. {
  41. uip_acc32[3] = op32[3] + (op16 & 0xff);
  42. uip_acc32[2] = op32[2] + (op16 >> 8);
  43. uip_acc32[1] = op32[1];
  44. uip_acc32[0] = op32[0];
  45. if(uip_acc32[2] < (op16 >> 8)) {
  46. ++uip_acc32[1];
  47. if(uip_acc32[1] == 0) {
  48. ++uip_acc32[0];
  49. }
  50. }
  51. if(uip_acc32[3] < (op16 & 0xff)) {
  52. ++uip_acc32[2];
  53. if(uip_acc32[2] == 0) {
  54. ++uip_acc32[1];
  55. if(uip_acc32[1] == 0) {
  56. ++uip_acc32[0];
  57. }
  58. }
  59. }
  60. }
  61. /*-----------------------------------------------------------------------------------*/
  62. u16_t uip_chksum(u16_t *sdata, u16_t len)
  63. {
  64. u16_t acc;
  65. for (acc = 0; len > 1; len -= 2) {
  66. u16_t u = ((unsigned char *)sdata)[0] + (((unsigned char *)sdata)[1] << 8);
  67. if ((acc += u) < u) {
  68. /* Overflow, so we add the carry to acc (i.e., increase by
  69. one). */
  70. ++acc;
  71. }
  72. ++sdata;
  73. }
  74. /* add up any odd byte */
  75. if(len == 1) {
  76. acc += htons(((u16_t)(*(u8_t *)sdata)) << 8);
  77. if(acc < htons(((u16_t)(*(u8_t *)sdata)) << 8)) {
  78. ++acc;
  79. }
  80. }
  81. return acc;
  82. }
  83. /*-----------------------------------------------------------------------------------*/
  84. u16_t uip_ipchksum(void)
  85. {
  86. return uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN], 20);
  87. }
  88. /*-----------------------------------------------------------------------------------*/
  89. u16_t uip_tcpchksum(void)
  90. {
  91. u16_t hsum, sum;
  92. /* Compute the checksum of the TCP header. */
  93. hsum = uip_chksum((u16_t *)&uip_buf[20 + UIP_LLH_LEN], 20);
  94. /* Compute the checksum of the data in the TCP packet and add it to
  95. the TCP header checksum. */
  96. sum = uip_chksum((u16_t *)uip_appdata,
  97. (u16_t)(((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - 40)));
  98. if((sum += hsum) < hsum) {
  99. ++sum;
  100. }
  101. if((sum += BUF->srcipaddr[0]) < BUF->srcipaddr[0]) {
  102. ++sum;
  103. }
  104. if((sum += BUF->srcipaddr[1]) < BUF->srcipaddr[1]) {
  105. ++sum;
  106. }
  107. if((sum += BUF->destipaddr[0]) < BUF->destipaddr[0]) {
  108. ++sum;
  109. }
  110. if((sum += BUF->destipaddr[1]) < BUF->destipaddr[1]) {
  111. ++sum;
  112. }
  113. if((sum += (u16_t)htons((u16_t)IP_PROTO_TCP)) < (u16_t)htons((u16_t)IP_PROTO_TCP)) {
  114. ++sum;
  115. }
  116. hsum = (u16_t)htons((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - 20);
  117. if((sum += hsum) < hsum) {
  118. ++sum;
  119. }
  120. return sum;
  121. }