channel.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * channel.h - raw packet channel definitions and declarations;
  44. *
  45. * the channel structure contains information needed to perform
  46. * ISO Layer 2, raw packet I/O in a variety of environments;
  47. *
  48. *. Qualcomm Atheros HomePlug AV Powerline Toolkit
  49. *: Copyright (c) 2009-2013 by Qualcomm Atheros Inc. ALL RIGHTS RESERVED;
  50. *; For demonstration and evaluation only; Not for production use.
  51. *
  52. * Contributor(s):
  53. * Charles Maier
  54. * Nathaniel Houghton
  55. *
  56. *--------------------------------------------------------------------*/
  57. #ifndef CHANNEL_HEADER
  58. #define CHANNEL_HEADER
  59. /*====================================================================*
  60. * system header files;
  61. *--------------------------------------------------------------------*/
  62. #include <unistd.h>
  63. /*====================================================================*
  64. * custom header files;
  65. *--------------------------------------------------------------------*/
  66. #include "../ether/ether.h"
  67. #include "../tools/types.h"
  68. /*====================================================================*
  69. * sort out the raw socket mess;
  70. *--------------------------------------------------------------------*/
  71. #if defined (__linux__) || defined (__APPLE__) || defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  72. # ifdef WINPCAP
  73. # error "Don't enable winpcap on Linux. It won't work."
  74. # endif
  75. # ifdef LIBPCAP
  76. # error "Don't enable libpcap on Linux. You don't need it."
  77. # endif
  78. #elif defined (WIN32)
  79. # ifndef WINPCAP
  80. # error "Define preprocessor constant WINPCAP on Windows."
  81. # endif
  82. # ifdef LIBPCAP
  83. # error "Don't enable libpcap on Windows. It won't work."
  84. # endif
  85. #elif defined (__CYGWIN__)
  86. # error "cygwin is unsupported!"
  87. #else
  88. # error "unknown environment"
  89. #endif
  90. /*====================================================================*
  91. * channel flagword bitmasks;
  92. *--------------------------------------------------------------------*/
  93. #define CHANNEL_VERBOSE (1 << 0)
  94. #define CHANNEL_SILENCE (1 << 1)
  95. #define CHANNEL_WARNING (1 << 2)
  96. #define CHANNEL_SUCCESS (1 << 3)
  97. #define CHANNEL_FAILURE (1 << 4)
  98. #define CHANNEL_MONITOR (1 << 5)
  99. #define CHANNEL_UPDATE_TARGET (1 << 5) /* used by efsu only */
  100. #define CHANNEL_UPDATE_SOURCE (1 << 6) /* used by efsu only */
  101. #define CHANNEL_LISTEN (1 << 7) /* used by efsu only */
  102. #define CHANNEL_ETHNUMBER 2
  103. #if defined (__linux__)
  104. # define CHANNEL_ETHDEVICE "eth1"
  105. #elif defined (__APPLE__)
  106. # define CHANNEL_ETHDEVICE "en0"
  107. # define CHANNEL_BPFDEVICE "/dev/bpf%d"
  108. #elif defined (__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
  109. # define CHANNEL_ETHDEVICE "bce0"
  110. # define CHANNEL_BPFDEVICE "/dev/bpf%d"
  111. #else
  112. # define CHANNEL_ETHDEVICE "nic1"
  113. #endif
  114. #define CHANNEL_FOREVER (unsigned)(-1)
  115. #define CHANNEL_BAILOUT 0
  116. #define CHANNEL_FLAGS 0
  117. #if defined (WIN32)
  118. #define CHANNEL_CAPTURE 50
  119. #define CHANNEL_TIMEOUT 200
  120. #else
  121. #define CHANNEL_CAPTURE 15
  122. #define CHANNEL_TIMEOUT 50
  123. #endif
  124. /*====================================================================*
  125. * common channel error messages;
  126. *--------------------------------------------------------------------*/
  127. #define CHANNEL_CANTSEND "%s: Send timeout or network error", __func__
  128. #define CHANNEL_CANTREAD "%s: Read timeout or network error", __func__
  129. /*====================================================================*
  130. * channel ethertype definitions;
  131. *--------------------------------------------------------------------*/
  132. #if !defined (__linux__)
  133. # define ETH_P_802_2 1
  134. #endif
  135. /*====================================================================*
  136. * communication channel structure;
  137. *--------------------------------------------------------------------*/
  138. typedef struct channel
  139. {
  140. signed fd;
  141. signed ifstate;
  142. signed ifindex;
  143. char const * ifname;
  144. uint8_t peer [ETHER_ADDR_LEN];
  145. uint8_t host [ETHER_ADDR_LEN];
  146. uint16_t type;
  147. #if defined (__linux__)
  148. #elif defined (__APPLE__) || defined (__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
  149. struct bpf
  150. {
  151. unsigned bpf_length;
  152. uint8_t * bpf_buffer;
  153. uint8_t * bpf_bp;
  154. signed bpf_bufused;
  155. }
  156. * bpf;
  157. #elif defined (WINPCAP) || defined (LIBPCAP)
  158. pcap_t * socket;
  159. char errbuf [PCAP_ERRBUF_SIZE];
  160. #else
  161. #error "Unknown Environment"
  162. #endif
  163. signed capture;
  164. signed timeout;
  165. flag_t flags;
  166. }
  167. CHANNEL;
  168. /*====================================================================*
  169. * channel functions;
  170. *--------------------------------------------------------------------*/
  171. signed openchannel (struct channel *);
  172. signed closechannel (struct channel const *);
  173. ssize_t sendpacket (struct channel const *, void * memory, ssize_t extent);
  174. ssize_t readpacket (struct channel const *, void * memory, ssize_t extent);
  175. /*====================================================================*
  176. *
  177. *--------------------------------------------------------------------*/
  178. #endif