udpout.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/udpout.c
  38. * \brief UDP output functions.
  39. *
  40. * \verbatim
  41. * $Id: udpout.c 3686 2011-12-04 14:20:38Z haraldkipp $
  42. * \endverbatim
  43. */
  44. #include <netinet/in.h>
  45. #include <netinet/ip.h>
  46. #include <netinet/udp.h>
  47. #include <netinet/ipcsum.h>
  48. #include <net/route.h>
  49. #include <sys/socket.h>
  50. /*!
  51. * \addtogroup xgUDP
  52. */
  53. /*@{*/
  54. /*!
  55. * \brief Send a UDP packet.
  56. *
  57. * \param sock Socket descriptor. This pointer must have been
  58. * retrieved by calling NutUdpCreateSocket().
  59. * \param daddr IP address of the remote host in network byte order.
  60. * \param port Remote port number in host byte order.
  61. * \param nb Network buffer structure containing the datagram.
  62. * This buffer will be released if the function returns
  63. * an error.
  64. *
  65. * \note Applications typically do not call this function but
  66. * use the UDP socket interface.
  67. *
  68. * \return 0 on success, -1 otherwise.
  69. */
  70. int NutUdpOutput(UDPSOCKET * sock, uint32_t daddr, uint16_t port, NETBUF * nb)
  71. {
  72. uint32_t saddr;
  73. uint32_t csum;
  74. UDPHDR *uh;
  75. NUTDEVICE *dev;
  76. IFNET *nif;
  77. if ((nb = NutNetBufAlloc(nb, NBAF_TRANSPORT, sizeof(UDPHDR))) == 0)
  78. return -1;
  79. uh = nb->nb_tp.vp;
  80. uh->uh_sport = sock->so_local_port;
  81. uh->uh_dport = htons(port);
  82. uh->uh_ulen = htons((uint16_t)(nb->nb_tp.sz + nb->nb_ap.sz));
  83. /*
  84. * Get local address for this destination.
  85. */
  86. if ((dev = NutIpRouteQuery(daddr, &saddr)) != 0) {
  87. nif = dev->dev_icb;
  88. saddr = nif->if_local_ip;
  89. } else
  90. saddr = 0;
  91. uh->uh_sum = 0;
  92. csum = NutIpPseudoChkSumPartial(saddr, daddr, IPPROTO_UDP, uh->uh_ulen);
  93. csum = NutIpChkSumPartial(csum, uh, sizeof(UDPHDR));
  94. uh->uh_sum = NutIpChkSum(csum, nb->nb_ap.vp, nb->nb_ap.sz);
  95. return NutIpOutput(IPPROTO_UDP, daddr, nb);
  96. }
  97. /*@}*/