You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uip_arch.c 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <stdint.h>
  2. #include <byteswap.h>
  3. #include "uip_arch.h"
  4. #include "uip.h"
  5. volatile u8_t uip_acc32[4];
  6. void uip_add32 ( u8_t *op32, u16_t op16 ) {
  7. * ( ( uint32_t * ) uip_acc32 ) =
  8. htonl ( ntohl ( *( ( uint32_t * ) op32 ) ) + op16 );
  9. }
  10. #define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
  11. #define IP_PROTO_TCP 6
  12. u16_t uip_chksum(u16_t *sdata, u16_t len) {
  13. u16_t acc;
  14. for(acc = 0; len > 1; len -= 2) {
  15. acc += *sdata;
  16. if(acc < *sdata) {
  17. /* Overflow, so we add the carry to acc (i.e., increase by
  18. one). */
  19. ++acc;
  20. }
  21. ++sdata;
  22. }
  23. /* add up any odd byte */
  24. if(len == 1) {
  25. acc += htons(((u16_t)(*(u8_t *)sdata)) << 8);
  26. if(acc < htons(((u16_t)(*(u8_t *)sdata)) << 8)) {
  27. ++acc;
  28. }
  29. }
  30. return acc;
  31. }
  32. u16_t uip_ipchksum(void) {
  33. return uip_chksum((u16_t *)&uip_buf[UIP_LLH_LEN], 20);
  34. }
  35. u16_t uip_tcpchksum(void) {
  36. u16_t hsum, sum;
  37. /* Compute the checksum of the TCP header. */
  38. hsum = uip_chksum((u16_t *)&uip_buf[20 + UIP_LLH_LEN], 20);
  39. /* Compute the checksum of the data in the TCP packet and add it to
  40. the TCP header checksum. */
  41. sum = uip_chksum((u16_t *)uip_appdata,
  42. (u16_t)(((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - 40)));
  43. if((sum += hsum) < hsum) {
  44. ++sum;
  45. }
  46. if((sum += BUF->srcipaddr[0]) < BUF->srcipaddr[0]) {
  47. ++sum;
  48. }
  49. if((sum += BUF->srcipaddr[1]) < BUF->srcipaddr[1]) {
  50. ++sum;
  51. }
  52. if((sum += BUF->destipaddr[0]) < BUF->destipaddr[0]) {
  53. ++sum;
  54. }
  55. if((sum += BUF->destipaddr[1]) < BUF->destipaddr[1]) {
  56. ++sum;
  57. }
  58. if((sum += (u16_t)htons((u16_t)IP_PROTO_TCP)) < (u16_t)htons((u16_t)IP_PROTO_TCP)) {
  59. ++sum;
  60. }
  61. hsum = (u16_t)htons((((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - 20);
  62. if((sum += hsum) < hsum) {
  63. ++sum;
  64. }
  65. return sum;
  66. }