udpin.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/udpin.c
  38. * \brief UDP input functions.
  39. *
  40. * \verbatim
  41. * $Id: udpin.c 5505 2014-01-01 11:15:16Z mifi $
  42. * \endverbatim
  43. */
  44. #include <sys/event.h>
  45. #include <netinet/udp.h>
  46. #include <sys/socket.h>
  47. #include <netinet/ip_icmp.h>
  48. #include <netinet/icmp.h>
  49. /*!
  50. * \addtogroup xgUDP
  51. */
  52. /*@{*/
  53. /*!
  54. * \brief Handle incoming UDP packets.
  55. *
  56. * \note This routine is called by the IP layer on
  57. * incoming UDP packets. Applications typically do
  58. * not call this function.
  59. *
  60. * \param nb Network buffer structure containing the UDP packet.
  61. */
  62. int NutUdpInput(NUTDEVICE * dev, NETBUF * nb)
  63. {
  64. UDPHDR *uh;
  65. UDPSOCKET *sock;
  66. (void)dev;
  67. uh = (UDPHDR *) nb->nb_tp.vp;
  68. /* Make sure that the datagram contains a full header. */
  69. if (uh == NULL || nb->nb_tp.sz < sizeof(UDPHDR)) {
  70. NutNetBufFree(nb);
  71. return 0;
  72. }
  73. nb->nb_ap.sz = nb->nb_tp.sz - sizeof(UDPHDR);
  74. nb->nb_tp.sz = sizeof(UDPHDR);
  75. if (nb->nb_ap.sz) {
  76. nb->nb_ap.vp = uh + 1;
  77. }
  78. /*
  79. * Find a port. If none exists and if this datagram hasn't been
  80. * broadcasted, return an ICMP unreachable.
  81. */
  82. if ((sock = NutUdpFindSocket(uh->uh_dport)) == 0) {
  83. if ((nb->nb_flags & NBAF_UNICAST) == 0 ||
  84. NutIcmpResponse(ICMP_UNREACH, ICMP_UNREACH_PORT, 0, nb) == 0) {
  85. NutNetBufFree(nb);
  86. }
  87. return 0;
  88. }
  89. /* if buffer size is defined, use packet queue */
  90. if (sock->so_rx_bsz) {
  91. /* New packet fits into the buffer? */
  92. if (sock->so_rx_cnt + nb->nb_ap.sz > sock->so_rx_bsz) {
  93. /* No, so discard it */
  94. NutNetBufFree(nb);
  95. return 0;
  96. } else {
  97. /* if a first packet is already in the queue, find the end
  98. * and add the new packet */
  99. if (sock->so_rx_nb) {
  100. NETBUF *snb;
  101. for (snb = sock->so_rx_nb; snb->nb_next != 0; snb = snb->nb_next);
  102. snb->nb_next = nb;
  103. } else
  104. sock->so_rx_nb = nb;
  105. /* increment input buffer count */
  106. sock->so_rx_cnt += nb->nb_ap.sz;
  107. };
  108. } else { /* no packet queue */
  109. /* if a packet is still buffered, discard it */
  110. if (sock->so_rx_nb) {
  111. NutNetBufFree(sock->so_rx_nb);
  112. }
  113. sock->so_rx_nb = nb;
  114. sock->so_rx_cnt = nb->nb_ap.sz; /* set input buffer count to size of new packet */
  115. };
  116. /* post the event only, if one thread is waiting */
  117. if (sock->so_rx_rdy)
  118. NutEventPost(&sock->so_rx_rdy);
  119. return 0;
  120. }
  121. /*@}*/