tcpin.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH
  3. * Copyright (c) 1993 by Digital Equipment Corporation
  4. * Copyright (c) 1983, 1993 by The Regents of the University of California
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file net/tcpin.c
  38. * \brief TCP input functions.
  39. *
  40. * \verbatim
  41. * $Id: tcpin.c 5505 2014-01-01 11:15:16Z mifi $
  42. * \endverbatim
  43. */
  44. #include <cfg/os.h>
  45. #include <sys/thread.h>
  46. #include <netinet/ip.h>
  47. #include <sys/socket.h>
  48. #include <netinet/tcp.h>
  49. #ifdef NUTDEBUG
  50. #include <net/netdebug.h>
  51. #endif
  52. /*!
  53. * \addtogroup xgTCP
  54. */
  55. /*@{*/
  56. /*!
  57. * \brief Process incoming TCP segments from IP layer.
  58. *
  59. * \warning The caller must take care not to pass broadcast
  60. * or multicast segments.
  61. *
  62. * \note This routine is called by the IP layer on incoming
  63. * TCP segments. Applications typically do not call
  64. * this function.
  65. *
  66. */
  67. int NutTcpInput(NUTDEVICE * dev, NETBUF * nb)
  68. {
  69. TCPHDR *th = (TCPHDR *) nb->nb_tp.vp;
  70. (void)dev;
  71. /* Process unicasts only. */
  72. if (th && (nb->nb_flags & NBAF_UNICAST) != 0) {
  73. uint_fast8_t hdrlen = th->th_off * 4;
  74. /* Check the header length. */
  75. if (hdrlen >= sizeof(TCPHDR) && hdrlen <= nb->nb_tp.sz) {
  76. nb->nb_ap.sz = nb->nb_tp.sz - hdrlen;
  77. if (nb->nb_ap.sz) {
  78. nb->nb_ap.vp = ((uint32_t *) th) + th->th_off;
  79. }
  80. nb->nb_tp.sz = hdrlen;
  81. /*
  82. ** According to RFC1122 we MUST check the checksum
  83. ** on incoming segments. Anyway, we rely on lower
  84. ** level checksums to save processing resources.
  85. **
  86. ** However, we may combine checksum calculation
  87. ** with moving data to the receiver buffer.
  88. **/
  89. NutTcpStateMachine(nb);
  90. return 0;
  91. }
  92. }
  93. /* Silently discard this segment. */
  94. NutNetBufFree(nb);
  95. return 0;
  96. }
  97. /*@}*/