tcp_seqno.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /****************************************************************************
  2. * net/tcp/tcp_seqno.c
  3. *
  4. * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Large parts of this file were leveraged from uIP logic:
  8. *
  9. * Copyright (c) 2001-2003, Adam Dunkels.
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. The name of the author may not be used to endorse or promote
  22. * products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  27. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  29. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  31. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  33. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ****************************************************************************/
  38. /****************************************************************************
  39. * Included Files
  40. ****************************************************************************/
  41. #include <nuttx/config.h>
  42. #if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
  43. #include <stdint.h>
  44. #include <debug.h>
  45. #include <nuttx/net/netconfig.h>
  46. #include <nuttx/net/netdev.h>
  47. #include "devif/devif.h"
  48. /****************************************************************************
  49. * Private Data
  50. ****************************************************************************/
  51. /* g_tcpsequence is used to generate initial TCP sequence numbers */
  52. static uint32_t g_tcpsequence;
  53. /****************************************************************************
  54. * Public Functions
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: tcp_setsequence
  58. *
  59. * Description:
  60. * Set the TCP/IP sequence number
  61. *
  62. * Assumptions:
  63. * This function must be called with the network locked if seqno refers
  64. * to a shared, global resource.
  65. *
  66. ****************************************************************************/
  67. void tcp_setsequence(FAR uint8_t *seqno, uint32_t value)
  68. {
  69. /* Copy the sequence number in network (big-endian) order */
  70. *seqno++ = value >> 24;
  71. *seqno++ = (value >> 16) & 0xff;
  72. *seqno++ = (value >> 8) & 0xff;
  73. *seqno = value & 0xff;
  74. }
  75. /****************************************************************************
  76. * Name: tcp_getsequence
  77. *
  78. * Description:
  79. * Get the TCP/IP sequence number
  80. *
  81. * Assumptions:
  82. * This function must be called with the network locked if seqno refers
  83. * to a shared, global resource.
  84. *
  85. ****************************************************************************/
  86. uint32_t tcp_getsequence(FAR uint8_t *seqno)
  87. {
  88. uint32_t value;
  89. /* Combine the sequence number from network (big-endian) order */
  90. value = (uint32_t)seqno[0] << 24 |
  91. (uint32_t)seqno[1] << 16 |
  92. (uint32_t)seqno[2] << 8 |
  93. (uint32_t)seqno[3];
  94. return value;
  95. }
  96. /****************************************************************************
  97. * Name: tcp_addsequence
  98. *
  99. * Description:
  100. * Add the length to get the next TCP sequence number.
  101. *
  102. * Assumptions:
  103. * This function must be called with the network locked if seqno refers
  104. * to a shared, global resource.
  105. *
  106. ****************************************************************************/
  107. uint32_t tcp_addsequence(FAR uint8_t *seqno, uint16_t len)
  108. {
  109. return tcp_getsequence(seqno) + (uint32_t)len;
  110. }
  111. /****************************************************************************
  112. * Name: tcp_initsequence
  113. *
  114. * Description:
  115. * Set the (initial) the TCP/IP sequence number when a TCP connection is
  116. * established.
  117. *
  118. * Assumptions:
  119. * This function must be called with the network locked if seqno refers
  120. * to a shared, global resource.
  121. *
  122. ****************************************************************************/
  123. void tcp_initsequence(FAR uint8_t *seqno)
  124. {
  125. tcp_setsequence(seqno, g_tcpsequence);
  126. }
  127. /****************************************************************************
  128. * Name: tcp_nextsequence
  129. *
  130. * Description:
  131. * Increment the TCP/IP sequence number
  132. *
  133. * Assumptions:
  134. * This function must be called with the network locked.
  135. *
  136. ****************************************************************************/
  137. void tcp_nextsequence(void)
  138. {
  139. g_tcpsequence++;
  140. }
  141. #endif /* CONFIG_NET && CONFIG_NET_TCP */