channel.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * channel.h - raw packet channel definitions and declarations;
  11. *
  12. * the channel structure contains information needed to perform
  13. * ISO Layer 2, raw packet I/O in a variety of environments;
  14. *
  15. * Contributor(s):
  16. * Charles Maier <cmaier@qca.qualcomm.com>
  17. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  18. *
  19. *--------------------------------------------------------------------*/
  20. #ifndef CHANNEL_HEADER
  21. #define CHANNEL_HEADER
  22. /*====================================================================*
  23. * system header files;
  24. *--------------------------------------------------------------------*/
  25. #include <unistd.h>
  26. /*====================================================================*
  27. * custom header files;
  28. *--------------------------------------------------------------------*/
  29. #include "../ether/ether.h"
  30. #include "../tools/types.h"
  31. /*====================================================================*
  32. * sort out the raw socket mess;
  33. *--------------------------------------------------------------------*/
  34. #if defined (__linux__) || defined (__APPLE__) || defined (__OpenBSD__)
  35. # ifdef WINPCAP
  36. # error "Don't enable winpcap on Linux. It won't work."
  37. # endif
  38. # ifdef LIBPCAP
  39. # error "Don't enable libpcap on Linux. You don't need it."
  40. # endif
  41. #elif defined (WIN32)
  42. # ifndef WINPCAP
  43. # error "Define preprocessor constant WINPCAP on Windows."
  44. # endif
  45. # ifdef LIBPCAP
  46. # error "Don't enable libpcap on Windows. It won't work."
  47. # endif
  48. #elif defined (__CYGWIN)
  49. # error "Cygwin is unsupported!"
  50. #else
  51. # error "Unknown environment!"
  52. #endif
  53. /*====================================================================*
  54. * channel flagword bitmasks;
  55. *--------------------------------------------------------------------*/
  56. #define CHANNEL_VERBOSE (1 << 0)
  57. #define CHANNEL_SILENCE (1 << 1)
  58. #define CHANNEL_WARNING (1 << 2)
  59. #define CHANNEL_SUCCESS (1 << 3)
  60. #define CHANNEL_FAILURE (1 << 4)
  61. #define CHANNEL_MONITOR (1 << 5)
  62. #define CHANNEL_UPDATE_TARGET (1 << 5) /* used by efsu only */
  63. #define CHANNEL_UPDATE_SOURCE (1 << 6) /* used by efsu only */
  64. #define CHANNEL_LISTEN (1 << 7) /* used by efsu only */
  65. #define CHANNEL_ETHNUMBER 2
  66. #if defined (__linux__)
  67. #define CHANNEL_ETHDEVICE "eth1"
  68. #elif defined (__APPLE__)
  69. #define CHANNEL_ETHDEVICE "en0"
  70. #define CHANNEL_BPFDEVICE "/dev/bpf%d"
  71. #elif defined (__OpenBSD__)
  72. #define CHANNEL_ETHDEVICE "bce0"
  73. #define CHANNEL_BPFDEVICE "/dev/bpf%d"
  74. #else
  75. #define CHANNEL_ETHDEVICE "nic1"
  76. #endif
  77. #define CHANNEL1_ETHNUMBER 3
  78. #if defined (__linux__)
  79. #define CHANNEL1_ETHDEVICE "eth2"
  80. #elif defined (__APPLE__)
  81. #define CHANNEL1_ETHDEVICE "en1"
  82. #define CHANNEL1_BPFDEVICE "/dev/bpf%d"
  83. #elif defined (__OpenBSD__)
  84. #define CHANNEL1_ETHDEVICE "bce1"
  85. #define CHANNEL1_BPFDEVICE "/dev/bpf%d"
  86. #else
  87. #define CHANNEL1_ETHDEVICE "nic2"
  88. #endif
  89. #define CHANNEL_FOREVER (unsigned)(-1)
  90. #if defined (WIN32)
  91. #define CHANNEL_CAPTURE 50
  92. #define CHANNEL_TIMEOUT 200
  93. #define CHANNEL_FLAGS 0
  94. #else
  95. #define CHANNEL_CAPTURE 15
  96. #define CHANNEL_TIMEOUT 50
  97. #define CHANNEL_FLAGS 0
  98. #endif
  99. /*====================================================================*
  100. * common channel error messages;
  101. *--------------------------------------------------------------------*/
  102. #define CHANNEL_CANTSEND "%s: Send timeout or network error", __func__
  103. #define CHANNEL_CANTREAD "%s: Read timeout or network error", __func__
  104. /*====================================================================*
  105. * communication channel structure;
  106. *--------------------------------------------------------------------*/
  107. typedef struct channel
  108. {
  109. signed fd;
  110. signed ifstate;
  111. signed ifindex;
  112. char const * ifname;
  113. uint8_t peer [ETHER_ADDR_LEN];
  114. uint8_t host [ETHER_ADDR_LEN];
  115. uint16_t type;
  116. #if defined (__linux__)
  117. #elif defined (__APPLE__) || defined (__OpenBSD__)
  118. struct bpf
  119. {
  120. signed bpf_length;
  121. uint8_t * bpf_buffer;
  122. uint8_t * bpf_bp;
  123. signed bpf_bufused;
  124. }
  125. * bpf;
  126. #elif defined (WINPCAP) || defined (LIBPCAP)
  127. pcap_t * socket;
  128. char errbuf [PCAP_ERRBUF_SIZE];
  129. #else
  130. #error "Unknown Environment"
  131. #endif
  132. signed capture;
  133. signed timeout;
  134. flag_t flags;
  135. }
  136. CHANNEL;
  137. /*====================================================================*
  138. * channel functions;
  139. *--------------------------------------------------------------------*/
  140. signed openchannel (struct channel *);
  141. signed closechannel (struct channel const *);
  142. ssize_t sendpacket (struct channel const *, void * memory, ssize_t extent);
  143. ssize_t readpacket (struct channel const *, void * memory, ssize_t extent);
  144. /*====================================================================*
  145. *
  146. *--------------------------------------------------------------------*/
  147. #endif