ethout.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/ethout.c
  38. * \brief Ethernet output functions.
  39. *
  40. * \verbatim
  41. * $Id: ethout.c 4407 2012-08-05 17:06:22Z haraldkipp $
  42. * \endverbatim
  43. */
  44. #include <string.h>
  45. #include <sys/types.h>
  46. #include <netinet/if_ether.h>
  47. #include <netinet/in.h>
  48. #include <net/ether.h>
  49. /*!
  50. * \addtogroup xgEthernet
  51. */
  52. /*@{*/
  53. /*!
  54. * \brief Send Ethernet frame.
  55. *
  56. * Send an Ethernet frame of a given type using the specified device.
  57. *
  58. * \param dev Identifies the device to use.
  59. * \param type Type of this frame, either ETHERTYPE_IP or ETHERTYPE_ARP.
  60. * \param ha MAC address of the destination, set null for broadcasts.
  61. * \param nb Network buffer structure containing the packet to be sent.
  62. * The structure must have been allocated by a previous
  63. * call NutNetBufAlloc() and will be freed if this function
  64. * returns with an error.
  65. *
  66. * \return 0 on success, -1 in case of any errors.
  67. */
  68. int NutEtherOutput(NUTDEVICE * dev, uint16_t type, uint8_t * ha, NETBUF * nb)
  69. {
  70. ETHERHDR *eh;
  71. IFNET *nif;
  72. if (NutNetBufAlloc(nb, NBAF_DATALINK, sizeof(ETHERHDR)) == 0)
  73. return -1;
  74. eh = (ETHERHDR *) nb->nb_dl.vp;
  75. nif = dev->dev_icb;
  76. memcpy(eh->ether_shost, nif->if_mac, 6);
  77. if (ha) {
  78. memcpy(eh->ether_dhost, ha, 6);
  79. NUT_PERFMON_INC(nif->if_out_ucast_pkts);
  80. } else {
  81. memset(eh->ether_dhost, 0xff, 6);
  82. NUT_PERFMON_INC(nif->if_out_n_ucast_pkts);
  83. }
  84. eh->ether_type = htons(type);
  85. /*
  86. * Call the network device output routine.
  87. */
  88. if((*nif->if_send) (dev, nb)) {
  89. NutNetBufFree(nb);
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. /*@}*/