arpout.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/arpout.c
  38. * \brief ARP output functions.
  39. *
  40. * \verbatim
  41. * $Id: arpout.c 3686 2011-12-04 14:20:38Z haraldkipp $
  42. * \endverbatim
  43. */
  44. #include <string.h>
  45. #include <sys/heap.h>
  46. #include <sys/thread.h>
  47. #include <sys/types.h>
  48. #include <net/if_var.h>
  49. #include <netinet/if_ether.h>
  50. #include <net/ether.h>
  51. #include <netinet/in.h>
  52. /*!
  53. * \addtogroup xgARP
  54. */
  55. /*@{*/
  56. /*!
  57. * \brief Allocate an ARP network buffer structure.
  58. *
  59. * \param type Type of ARP packet.
  60. * \param ip Target IP address.
  61. * \param mac Target MAC address, null pointer for broadcast.
  62. *
  63. * \return Pointer to the allocated network buffer structure
  64. * or 0 on failure.
  65. */
  66. NETBUF *NutArpAllocNetBuf(uint16_t type, uint32_t ip, uint8_t * mac)
  67. {
  68. NETBUF *nb;
  69. ETHERARP *ea;
  70. ARPHDR *ah;
  71. if ((nb = NutNetBufAlloc(0, NBAF_NETWORK, sizeof(ETHERARP))) == 0)
  72. return 0;
  73. ea = nb->nb_nw.vp;
  74. ah = (ARPHDR *) & ea->ea_hdr;
  75. /*
  76. * Set ARP header.
  77. */
  78. ah->ar_hrd = htons(ARPHRD_ETHER);
  79. ah->ar_pro = htons(ETHERTYPE_IP);
  80. ah->ar_hln = 6;
  81. ah->ar_pln = 4;
  82. ah->ar_op = htons(type);
  83. /*
  84. * Set ARP destination data.
  85. */
  86. if (mac)
  87. memcpy(ea->arp_tha, mac, 6);
  88. else
  89. memset(ea->arp_tha, 0xff, 6);
  90. ea->arp_tpa = ip;
  91. return nb;
  92. }
  93. /*!
  94. * \brief Send an ARP packet.
  95. *
  96. * \note Applications typically do not call this function.
  97. *
  98. * \param dev Identifies the device to use.
  99. * \param nb Network buffer structure containing the packet to be sent.
  100. * The structure must have been allocated by a previous
  101. * call NutNetBufAlloc().
  102. *
  103. * \return 0 on success, -1 in case of any errors.
  104. */
  105. int NutArpOutput(NUTDEVICE * dev, NETBUF * nb)
  106. {
  107. ETHERARP *ea;
  108. IFNET *nif;
  109. ea = nb->nb_nw.vp;
  110. /*
  111. * Set ARP source data.
  112. */
  113. nif = dev->dev_icb;
  114. memcpy(ea->arp_sha, nif->if_mac, 6);
  115. ea->arp_spa = nif->if_local_ip;
  116. return (*nif->if_output)(dev, ETHERTYPE_ARP, ea->arp_tha, nb);
  117. }
  118. /*@}*/